2 * STANDARD_RIFT_IO_COPYRIGHT
5 import React from 'react';
6 import ReactDOM from 'react-dom';
7 import AppHeader from 'widgets/header/header.jsx';
8 import UserManagementStore from './userMgmtStore.js';
9 import SkyquakeComponent from 'widgets/skyquake_container/skyquakeComponent.jsx';
10 import 'style/layout.scss';
11 import './userMgmt.scss';
12 import {Panel, PanelWrapper} from 'widgets/panel/panel';
13 import SkyquakeRBAC from 'widgets/skyquake_rbac/skyquakeRBAC.jsx';
15 import TextInput from 'widgets/form_controls/textInput.jsx';
16 import Input from 'widgets/form_controls/input.jsx';
17 import Button, {ButtonGroup} from 'widgets/button/sq-button.jsx';
18 import SelectOption from 'widgets/form_controls/selectOption.jsx';
19 import 'widgets/form_controls/formControls.scss';
20 import imgAdd from '../../node_modules/open-iconic/svg/plus.svg'
21 import imgRemove from '../../node_modules/open-iconic/svg/trash.svg';
22 import {merge} from 'lodash';
24 import ROLES from 'utils/roleConstants.js';
25 const PLATFORM = ROLES.PLATFORM;
27 class UserManagementDashboard extends React.Component {
30 this.Store = this.props.flux.stores.hasOwnProperty('UserManagementStore') ? this.props.flux.stores.UserManagementStore : this.props.flux.createStore(UserManagementStore);
31 this.state = this.Store.getState();
32 this.actions = this.state.actions;
34 componentDidUpdate() {
36 ReactDOM.findDOMNode(this.UserList).addEventListener('transitionend', this.onTransitionEnd, false);
37 setTimeout(function() {
38 let element = self[`user-ref-${self.state.activeIndex}`]
39 element && !isElementInView(element) && element.scrollIntoView({block: 'end', behavior: 'smooth'});
42 componentWillMount() {
43 this.Store.listen(this.updateState);
44 this.Store.getUsers();
46 componentWillUnmount() {
47 this.Store.unlisten(this.updateState);
49 updateState = (state) => {
52 updateInput = (key, e) => {
54 this.actions.handleUpdateInput({
55 [property]:e.target.value
58 disabledChange = (e) => {
59 this.actions.handleDisabledChange(e.target.checked);
61 platformChange = (platformRole, e) => {
62 this.actions.handlePlatformRoleUpdate(platformRole, e.currentTarget.checked);
64 addProjectRole = (e) => {
65 this.actions.handleAddProjectItem();
67 removeProjectRole = (i, e) => {
68 this.actions.handleRemoveProjectItem(i);
70 updateProjectRole = (i, e) => {
71 this.actions.handleUpdateProjectRole(i, e)
74 this.actions.handleAddUser();
76 viewUser = (un, index) => {
77 this.actions.viewUser(un, index);
80 this.actions.editUser(false);
82 cancelEditUser = () => {
83 this.actions.editUser(true)
86 this.actions.handleCloseUserPanel();
88 // updateUser = (e) => {
89 // e.preventDefault();
90 // e.stopPropagation();
92 // this.Store.updateUser();
97 if (confirm('Are you sure you want to delete this user?')) {
98 this.Store.deleteUser({
99 'user-name': this.state['user-name'],
100 'user-domain': this.state['user-domain']
105 createUser = (e) => {
108 if(this.state['new-password'] != this.state['confirm-password']) {
109 this.props.actions.showNotification('Passwords do not match')
112 if (this.state.disabled == "TRUE") {
117 this.Store.createUser({
118 'user-name': this.state['user-name'],
119 'user-domain': this.state['user-domain'],
120 'password': this.state['new-password']
121 // 'confirm-password': this.state['confirm-password']
125 updateUser = (e) => {
129 let validatedPasswords = validatePasswordFields(this.state);
130 if(validatedPasswords) {
133 if (self.state.disabled == "TRUE") {
138 if (this.state['new-password'] != '') {
139 password = {'password': this.state['new-password']}
142 'password': this.state.currentPassword
145 this.Store.updateUser(_.merge({
146 'user-name': this.state['user-name'],
147 'user-domain': this.state['user-domain']
148 }, _.merge(isDisabled, password)));
150 function validatePasswordFields(state) {
151 let oldOne = state['old-password'];
152 let newOne = state['new-password'];
153 let confirmOne = state['confirm-password'];
155 // if(!oldOne || !newOne) {
156 // self.props.actions.showNotification('Please fill in all fields.');
159 if((oldOne || newOne) && (oldOne == newOne)) {
160 self.props.actions.showNotification('Your new password must not match your old one');
163 if(newOne != confirmOne) {
164 self.props.actions.showNotification('Passwords do not match');
168 // 'old-password': oldOne,
169 'new-password': newOne,
170 'confirm-password': confirmOne
177 evaluateSubmit = (e) => {
178 if (e.keyCode == 13) {
179 if (this.props.isEdit) {
188 onTransitionEnd = (e) => {
189 this.actions.handleHideColumns(e);
190 console.log('transition end')
192 disableChange = (e) => {
193 let value = e.target.value;
194 value = value.toUpperCase();
195 this.actions.handleDisabledChange(value);
200 let props = this.props;
201 let state = this.state;
202 let passwordSectionHTML = null;
203 let formButtonsHTML = (
204 <ButtonGroup className="buttonGroup">
205 <Button label="EDIT" type="submit" onClick={this.editUser} />
208 if(!this.state.isReadOnly) {
209 passwordSectionHTML = ( this.state.isEdit ?
211 <FormSection title="PASSWORD CHANGE">
212 <Input label="NEW PASSWORD" type="password" value={state['new-password']} onChange={this.updateInput.bind(null, 'new-password')}/>
213 <Input label="REPEAT NEW PASSWORD" type="password" value={state['confirm-password']} onChange={this.updateInput.bind(null, 'confirm-password')}/>
217 <FormSection title="CREATE PASSWORD">
218 <Input label="CREATE PASSWORD" type="password" value={state.newPassword} onChange={this.updateInput.bind(null, 'new-password')}/>
219 <Input label="REPEAT PASSWORD" type="password" value={state.repeatNewPassword} onChange={this.updateInput.bind(null, 'confirm-password')}/>
226 <ButtonGroup className="buttonGroup">
227 <Button label="Update" type="submit" onClick={this.updateUser} />
228 <Button label="Delete" onClick={this.deleteUser} />
229 <Button label="Cancel" onClick={this.cancelEditUser} />
233 <ButtonGroup className="buttonGroup">
234 <Button label="Create" type="submit" onClick={this.createUser} />
240 <PanelWrapper column>
241 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} >
242 <AppHeader nav={[{name: 'PLATFORM ROLE MANAGEMENT', onClick: this.context.router.push.bind(this, {pathname: '/platform'})}]}/>
244 <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'flexDirection': 'row'}} >
245 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
246 <Panel title="User List" style={{marginBottom: 0}} no-corners>
247 <div className="tableRow tableRow--header">
248 <div className="userName">
255 {state.users && state.users.map((u, k) => {
256 let platformRoles = [];
257 for(let role in u.platformRoles) {
258 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
261 <div ref={(el) => this[`user-ref-${k}`] = el} className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`}
263 onClick={self.viewUser.bind(null, u, k)}>
265 className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
278 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
279 <ButtonGroup className="buttonGroup">
280 <Button label="Add User" onClick={this.addUser} />
284 <PanelWrapper onKeyUp={this.evaluateSubmit}
285 className={`userAdmin column`}>
287 title={state.isEdit ? state['user-name'] : 'Create User'}
288 style={{marginBottom: 0}}
289 hasCloseButton={this.closePanel}
291 <FormSection title="USER INFO">
295 : <Input readonly={state.isReadOnly} label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
297 <Input readonly={true} label="Domain" value={state['user-domain']} onChange={this.updateInput.bind(null, 'user-domain')}></Input>
300 onChange={this.disableChange}
302 value={this.state.disabled}
303 options={["TRUE","FALSE"]}
304 readonly={state.isReadOnly} />
306 <FormSection title="PLATFORM ROLES" style={{display:'none'}}>
307 <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
308 <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')} checked={state.platformRoles.platform_admin} type="checkbox" />
309 <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')} checked={state.platformRoles.platform_oper} type="checkbox" />
311 <FormSection title="PROJECT ROLES" style={{display:'none'}}>
315 readonly={state.isReadOnly}
316 options={state.projectRolesOptions}
317 collection={state.projectRoles}
318 onChange={this.updateProjectRole}
319 AddItemFn={this.addProjectRole}
320 RemoveItemFn={this.removeProjectRole}
323 {passwordSectionHTML}
326 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
336 // onClick={this.Store.update.bind(null, Account)}
337 UserManagementDashboard.contextTypes = {
338 router: React.PropTypes.object,
339 userProfile: React.PropTypes.object
342 UserManagementDashboard.defaultProps = {
347 export default SkyquakeComponent(UserManagementDashboard);
350 function isElementInView(el) {
351 var rect = el && el.getBoundingClientRect() || {};
356 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
357 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
362 // isReadOnly={state.isReadOnly} disabled={state.disabled} onChange={this.disableChange}
364 class isDisabled extends React.Component {
369 let props = this.props;
377 class InputCollection extends React.Component {
380 this.collection = props.collection;
382 buildTextInput(onChange, v, i) {
385 readonly={this.props.readonly}
386 style={{flex: '1 1'}}
389 onChange= {onChange.bind(null, i)}
393 buildSelectOption(initial, options, onChange, v, i) {
396 readonly={this.props.readonly}
397 key={`${i}-${v.replace(' ', '_')}`}
401 onChange={onChange.bind(null, i)}
409 const props = this.props;
411 let className = "InputCollection";
412 if (props.className) {
413 className = `${className} ${props.className}`;
415 if (props.type == 'select') {
416 inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
418 inputType = this.buildTextInput.bind(this, props.onChange)
421 <div className="InputCollection-wrapper">
422 {props.collection.map((v,i) => {
424 <div key={i} className={className} >
427 props.readonly ? null : <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>}
431 { props.readonly ? null : <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>}
438 InputCollection.defaultProps = {
441 onChange: function(i, e) {
443 Updating with: ${e.target.value}
447 AddItemFn: function(e) {
448 console.log(`Adding a new item to collection`)
450 RemoveItemFn: function(i, e) {
451 console.log(`Removing item from collection at index of: ${i}`)
455 class FormSection extends React.Component {
457 let className = 'FormSection ' + this.props.className;
460 style={this.props.style}
461 className={className}
463 <div className="FormSection-title">
466 <div className="FormSection-body">
467 {this.props.children}
475 FormSection.defaultProps = {