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';
23 import ROLES from 'utils/roleConstants.js';
24 const PLATFORM = ROLES.PLATFORM;
26 class UserManagementDashboard extends React.Component {
29 this.Store = this.props.flux.stores.hasOwnProperty('UserManagementStore') ? this.props.flux.stores.UserManagementStore : this.props.flux.createStore(UserManagementStore);
30 this.state = this.Store.getState();
31 this.actions = this.state.actions;
33 componentDidUpdate() {
35 ReactDOM.findDOMNode(this.UserList).addEventListener('transitionend', this.onTransitionEnd, false);
36 setTimeout(function() {
37 let element = self[`user-ref-${self.state.activeIndex}`]
38 element && !isElementInView(element) && element.scrollIntoView({block: 'end', behavior: 'smooth'});
41 componentWillMount() {
42 this.Store.listen(this.updateState);
43 this.Store.getUsers();
45 componentWillUnmount() {
46 this.Store.unlisten(this.updateState);
48 updateState = (state) => {
51 updateInput = (key, e) => {
53 this.actions.handleUpdateInput({
54 [property]:e.target.value
57 disabledChange = (e) => {
58 this.actions.handleDisabledChange(e.target.checked);
60 platformChange = (platformRole, e) => {
61 this.actions.handlePlatformRoleUpdate(platformRole, e.currentTarget.checked);
63 addProjectRole = (e) => {
64 this.actions.handleAddProjectItem();
66 removeProjectRole = (i, e) => {
67 this.actions.handleRemoveProjectItem(i);
69 updateProjectRole = (i, e) => {
70 this.actions.handleUpdateProjectRole(i, e)
73 this.actions.handleAddUser();
75 viewUser = (un, index) => {
76 this.actions.viewUser(un, index);
79 this.actions.editUser(false);
81 cancelEditUser = () => {
82 this.actions.editUser(true)
85 this.actions.handleCloseUserPanel();
87 // updateUser = (e) => {
88 // e.preventDefault();
89 // e.stopPropagation();
91 // this.Store.updateUser();
96 if (confirm('Are you sure you want to delete this user?')) {
97 this.Store.deleteUser({
98 'user-name': this.state['user-name'],
99 'user-domain': this.state['user-domain']
104 createUser = (e) => {
107 if(this.state['new-password'] != this.state['confirm-password']) {
108 this.props.actions.showNotification('Passwords do not match')
110 this.Store.createUser({
111 'user-name': this.state['user-name'],
112 'user-domain': this.state['user-domain'],
113 'password': this.state['new-password']
114 // 'confirm-password': this.state['confirm-password']
118 updateUser = (e) => {
122 let validatedPasswords = validatePasswordFields(this.state);
123 if(validatedPasswords) {
124 this.Store.updateUser(_.merge({
125 'user-name': this.state['user-name'],
126 'user-domain': this.state['user-domain'],
127 'password': this.state['new-password']
130 function validatePasswordFields(state) {
131 let oldOne = state['old-password'];
132 let newOne = state['new-password'];
133 let confirmOne = state['confirm-password'];
135 if(oldOne == newOne) {
136 self.props.actions.showNotification('Your new password must not match your old one');
139 if(newOne != confirmOne) {
140 self.props.actions.showNotification('Passwords do not match');
144 // 'old-password': oldOne,
145 'new-password': newOne,
146 'confirm-password': confirmOne
153 evaluateSubmit = (e) => {
154 if (e.keyCode == 13) {
155 if (this.props.isEdit) {
164 onTransitionEnd = (e) => {
165 this.actions.handleHideColumns(e);
166 console.log('transition end')
168 disableChange = (e) => {
169 let value = e.target.value;
170 value = value.toUpperCase();
181 let props = this.props;
182 let state = this.state;
183 let passwordSectionHTML = null;
184 let formButtonsHTML = (
185 <ButtonGroup className="buttonGroup">
186 <Button label="EDIT" type="submit" onClick={this.editUser} />
189 if(!this.state.isReadOnly) {
190 passwordSectionHTML = ( this.state.isEdit ?
192 <FormSection title="PASSWORD CHANGE">
193 <Input label="NEW PASSWORD" type="password" value={state['new-password']} onChange={this.updateInput.bind(null, 'new-password')}/>
194 <Input label="REPEAT NEW PASSWORD" type="password" value={state['confirm-password']} onChange={this.updateInput.bind(null, 'confirm-password')}/>
198 <FormSection title="CREATE PASSWORD">
199 <Input label="CREATE PASSWORD" type="password" value={state.newPassword} onChange={this.updateInput.bind(null, 'new-password')}/>
200 <Input label="REPEAT PASSWORD" type="password" value={state.repeatNewPassword} onChange={this.updateInput.bind(null, 'confirm-password')}/>
207 <ButtonGroup className="buttonGroup">
208 <Button label="Update" type="submit" onClick={this.updateUser} />
209 <Button label="Delete" onClick={this.deleteUser} />
210 <Button label="Cancel" onClick={this.cancelEditUser} />
214 <ButtonGroup className="buttonGroup">
215 <Button label="Create" type="submit" onClick={this.createUser} />
221 <PanelWrapper column>
222 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} >
223 <AppHeader nav={[{name: 'PLATFORM ROLE MANAGEMENT', onClick: this.context.router.push.bind(this, {pathname: '/platform'})}]}/>
225 <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'flexDirection': 'row'}} >
226 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
227 <Panel title="User List" style={{marginBottom: 0}} no-corners>
228 <div className="tableRow tableRow--header">
229 <div className="userName">
236 {state.users && state.users.map((u, k) => {
237 let platformRoles = [];
238 for(let role in u.platformRoles) {
239 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
242 <div ref={(el) => this[`user-ref-${k}`] = el} className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`}
244 onClick={self.viewUser.bind(null, u, k)}>
246 className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
259 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
260 <ButtonGroup className="buttonGroup">
261 <Button label="Add User" onClick={this.addUser} />
265 <PanelWrapper onKeyUp={this.evaluateSubmit}
266 className={`userAdmin column`}>
268 title={state.isEdit ? state['user-name'] : 'Create User'}
269 style={{marginBottom: 0}}
270 hasCloseButton={this.closePanel}
272 <FormSection title="USER INFO">
276 : <Input readonly={state.isReadOnly} label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
278 <Input readonly={true} label="Domain" value={state['user-domain']} onChange={this.updateInput.bind(null, 'user-domain')}></Input>
280 <FormSection title="PLATFORM ROLES" style={{display:'none'}}>
281 <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
282 <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')} checked={state.platformRoles.platform_admin} type="checkbox" />
283 <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')} checked={state.platformRoles.platform_oper} type="checkbox" />
285 <FormSection title="PROJECT ROLES" style={{display:'none'}}>
289 readonly={state.isReadOnly}
290 options={state.projectRolesOptions}
291 collection={state.projectRoles}
292 onChange={this.updateProjectRole}
293 AddItemFn={this.addProjectRole}
294 RemoveItemFn={this.removeProjectRole}
297 {passwordSectionHTML}
300 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
310 // onClick={this.Store.update.bind(null, Account)}
311 UserManagementDashboard.contextTypes = {
312 router: React.PropTypes.object,
313 userProfile: React.PropTypes.object
316 UserManagementDashboard.defaultProps = {
321 export default SkyquakeComponent(UserManagementDashboard);
324 function isElementInView(el) {
325 var rect = el && el.getBoundingClientRect() || {};
330 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
331 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
336 // isReadOnly={state.isReadOnly} disabled={state.disabled} onChange={this.disableChange}
338 class isDisabled extends React.Component {
343 let props = this.props;
351 class InputCollection extends React.Component {
354 this.collection = props.collection;
356 buildTextInput(onChange, v, i) {
359 readonly={this.props.readonly}
360 style={{flex: '1 1'}}
363 onChange= {onChange.bind(null, i)}
367 buildSelectOption(initial, options, onChange, v, i) {
370 readonly={this.props.readonly}
371 key={`${i}-${v.replace(' ', '_')}`}
375 onChange={onChange.bind(null, i)}
383 const props = this.props;
385 let className = "InputCollection";
386 if (props.className) {
387 className = `${className} ${props.className}`;
389 if (props.type == 'select') {
390 inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
392 inputType = this.buildTextInput.bind(this, props.onChange)
395 <div className="InputCollection-wrapper">
396 {props.collection.map((v,i) => {
398 <div key={i} className={className} >
401 props.readonly ? null : <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>}
405 { props.readonly ? null : <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>}
412 InputCollection.defaultProps = {
415 onChange: function(i, e) {
417 Updating with: ${e.target.value}
421 AddItemFn: function(e) {
422 console.log(`Adding a new item to collection`)
424 RemoveItemFn: function(i, e) {
425 console.log(`Removing item from collection at index of: ${i}`)
429 class FormSection extends React.Component {
431 let className = 'FormSection ' + this.props.className;
434 style={this.props.style}
435 className={className}
437 <div className="FormSection-title">
440 <div className="FormSection-body">
441 {this.props.children}
449 FormSection.defaultProps = {