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 this.Store.deleteUser({
97 'user-name': this.state['user-name'],
98 'user-domain': this.state['user-domain']
101 createUser = (e) => {
104 if(this.state['new-password'] != this.state['confirm-password']) {
105 this.props.actions.showNotification('Passwords do not match')
107 this.Store.createUser({
108 'user-name': this.state['user-name'],
109 'user-domain': this.state['user-domain'],
110 'password': this.state['new-password']
111 // 'confirm-password': this.state['confirm-password']
115 updateUser = (e) => {
119 let validatedPasswords = validatePasswordFields(this.state);
120 if(validatedPasswords) {
121 this.Store.updateUser(_.merge({
122 'user-name': this.state['user-name'],
123 'user-domain': this.state['user-domain'],
124 'password': this.state['new-password']
127 function validatePasswordFields(state) {
128 let oldOne = state['old-password'];
129 let newOne = state['new-password'];
130 let confirmOne = state['confirm-password'];
132 if(oldOne == newOne) {
133 self.props.actions.showNotification('Your new password must not match your old one');
136 if(newOne != confirmOne) {
137 self.props.actions.showNotification('Passwords do not match');
141 // 'old-password': oldOne,
142 'new-password': newOne,
143 'confirm-password': confirmOne
150 evaluateSubmit = (e) => {
151 if (e.keyCode == 13) {
152 if (this.props.isEdit) {
161 onTransitionEnd = (e) => {
162 this.actions.handleHideColumns(e);
163 console.log('transition end')
165 disableChange = (e) => {
166 let value = e.target.value;
167 value = value.toUpperCase();
178 let props = this.props;
179 let state = this.state;
180 let passwordSectionHTML = null;
181 let formButtonsHTML = (
182 <ButtonGroup className="buttonGroup">
183 <Button label="EDIT" type="submit" onClick={this.editUser} />
186 if(!this.state.isReadOnly) {
187 passwordSectionHTML = ( this.state.isEdit ?
189 <FormSection title="PASSWORD CHANGE">
190 <Input label="NEW PASSWORD" type="password" value={state['new-password']} onChange={this.updateInput.bind(null, 'new-password')}/>
191 <Input label="REPEAT NEW PASSWORD" type="password" value={state['confirm-password']} onChange={this.updateInput.bind(null, 'confirm-password')}/>
195 <FormSection title="CREATE PASSWORD">
196 <Input label="CREATE PASSWORD" type="password" value={state.newPassword} onChange={this.updateInput.bind(null, 'new-password')}/>
197 <Input label="REPEAT PASSWORD" type="password" value={state.repeatNewPassword} onChange={this.updateInput.bind(null, 'confirm-password')}/>
204 <ButtonGroup className="buttonGroup">
205 <Button label="Update" type="submit" onClick={this.updateUser} />
206 <Button label="Delete" onClick={this.deleteUser} />
207 <Button label="Cancel" onClick={this.cancelEditUser} />
211 <ButtonGroup className="buttonGroup">
212 <Button label="Create" type="submit" onClick={this.createUser} />
218 <PanelWrapper column>
219 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} >
220 <AppHeader nav={[{name: 'PLATFORM ROLE MANAGEMENT', onClick: this.context.router.push.bind(this, {pathname: '/platform'})}]}/>
222 <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{'flexDirection': 'row'}} >
223 <PanelWrapper ref={(div) => { this.UserList = div}} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
224 <Panel title="User List" style={{marginBottom: 0}} no-corners>
225 <div className="tableRow tableRow--header">
226 <div className="userName">
233 {state.users && state.users.map((u, k) => {
234 let platformRoles = [];
235 for(let role in u.platformRoles) {
236 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
239 <div ref={(el) => this[`user-ref-${k}`] = el} className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`}
241 onClick={self.viewUser.bind(null, u, k)}>
243 className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
256 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
257 <ButtonGroup className="buttonGroup">
258 <Button label="Add User" onClick={this.addUser} />
262 <PanelWrapper onKeyUp={this.evaluateSubmit}
263 className={`userAdmin column`}>
265 title={state.isEdit ? state['user-name'] : 'Create User'}
266 style={{marginBottom: 0}}
267 hasCloseButton={this.closePanel}
269 <FormSection title="USER INFO">
273 : <Input readonly={state.isReadOnly} label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
275 <Input readonly={true} label="Domain" value={state['user-domain']} onChange={this.updateInput.bind(null, 'user-domain')}></Input>
277 <FormSection title="PLATFORM ROLES" style={{display:'none'}}>
278 <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
279 <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')} checked={state.platformRoles.platform_admin} type="checkbox" />
280 <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')} checked={state.platformRoles.platform_oper} type="checkbox" />
282 <FormSection title="PROJECT ROLES" style={{display:'none'}}>
286 readonly={state.isReadOnly}
287 options={state.projectRolesOptions}
288 collection={state.projectRoles}
289 onChange={this.updateProjectRole}
290 AddItemFn={this.addProjectRole}
291 RemoveItemFn={this.removeProjectRole}
294 {passwordSectionHTML}
297 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
307 // onClick={this.Store.update.bind(null, Account)}
308 UserManagementDashboard.contextTypes = {
309 router: React.PropTypes.object,
310 userProfile: React.PropTypes.object
313 UserManagementDashboard.defaultProps = {
318 export default SkyquakeComponent(UserManagementDashboard);
321 function isElementInView(el) {
322 var rect = el && el.getBoundingClientRect() || {};
327 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
328 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
333 // isReadOnly={state.isReadOnly} disabled={state.disabled} onChange={this.disableChange}
335 class isDisabled extends React.Component {
340 let props = this.props;
348 class InputCollection extends React.Component {
351 this.collection = props.collection;
353 buildTextInput(onChange, v, i) {
356 readonly={this.props.readonly}
357 style={{flex: '1 1'}}
360 onChange= {onChange.bind(null, i)}
364 buildSelectOption(initial, options, onChange, v, i) {
367 readonly={this.props.readonly}
368 key={`${i}-${v.replace(' ', '_')}`}
372 onChange={onChange.bind(null, i)}
380 const props = this.props;
382 let className = "InputCollection";
383 if (props.className) {
384 className = `${className} ${props.className}`;
386 if (props.type == 'select') {
387 inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
389 inputType = this.buildTextInput.bind(this, props.onChange)
392 <div className="InputCollection-wrapper">
393 {props.collection.map((v,i) => {
395 <div key={i} className={className} >
398 props.readonly ? null : <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>}
402 { props.readonly ? null : <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>}
409 InputCollection.defaultProps = {
412 onChange: function(i, e) {
414 Updating with: ${e.target.value}
418 AddItemFn: function(e) {
419 console.log(`Adding a new item to collection`)
421 RemoveItemFn: function(i, e) {
422 console.log(`Removing item from collection at index of: ${i}`)
426 class FormSection extends React.Component {
428 let className = 'FormSection ' + this.props.className;
431 style={this.props.style}
432 className={className}
434 <div className="FormSection-title">
437 <div className="FormSection-body">
438 {this.props.children}
446 FormSection.defaultProps = {