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, '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 platformChange = (platformRole, e) => {
59 this.actions.handlePlatformRoleUpdate(platformRole, e.currentTarget.checked);
61 addProjectRole = (e) => {
62 this.actions.handleAddProjectItem();
64 removeProjectRole = (i, e) => {
65 this.actions.handleRemoveProjectItem(i);
67 updateProjectRole = (i, e) => {
68 this.actions.handleUpdateProjectRole(i, e)
71 this.actions.handleAddUser();
73 viewUser = (un, index) => {
74 this.actions.viewUser(un, index);
77 this.actions.editUser(false);
79 cancelEditUser = () => {
80 this.actions.editUser(true)
83 this.actions.handleCloseUserPanel();
85 // updateUser = (e) => {
86 // e.preventDefault();
87 // e.stopPropagation();
89 // this.Store.updateUser();
94 if (confirm('Are you sure you want to delete this user?')) {
95 this.Store.deleteUser({
96 'user-name': this.state['user-name'],
97 'user-domain': this.state['user-domain']
102 createUser = (e) => {
105 if (!this.state['user-name'] || (this.state['user-name'].trim() == "")) {
106 this.props.actions.showNotification('Please enter a valid username');
110 if((this.state['user-domain'].toLowerCase() == 'system') && !this.state['new-password'] || (this.state['new-password'].trim == "")) {
111 this.props.actions.showNotification('You must enter a password');
114 if((this.state['user-domain'].toLowerCase() == 'system') && this.state['new-password'] != this.state['confirm-password']) {
115 this.props.actions.showNotification('Passwords do not match');
119 if (this.state.disabled == "TRUE") {
124 this.Store.createUser(_.merge({
125 'user-name': this.state['user-name'],
126 'user-domain': this.state['user-domain'],
127 'password': this.state['new-password'],
128 // 'confirm-password': this.state['confirm-password']
132 updateUser = (e) => {
136 let validatedPasswords = validatePasswordFields(this.state);
137 if (validatedPasswords) {
140 if (self.state.disabled == "TRUE") {
145 if (this.state['new-password'] != '') {
146 password = { 'password': this.state['new-password'] }
149 'password': this.state.currentPassword
152 this.Store.updateUser(_.merge({
153 'user-name': this.state['user-name'],
154 'user-domain': this.state['user-domain'],
155 'ui-state': this.context.userProfile.data['ui-state']
156 }, _.merge(isDisabled, password)));
158 function validatePasswordFields(state) {
159 let newOne = state['new-password'];
160 let confirmOne = state['confirm-password'];
161 if (!newOne && !confirmOne) {
162 // self.props.actions.showNotification('Please fill in all fields.');
165 if (newOne != confirmOne) {
166 self.props.actions.showNotification('Passwords do not match');
170 'new-password': newOne,
171 'confirm-password': confirmOne
175 evaluateSubmit = (e) => {
176 if (e.keyCode == 13) {
177 if (this.state.isEdit) {
186 onTransitionEnd = (e) => {
187 this.actions.handleHideColumns(e);
188 console.log('transition end')
190 disableChange = (e) => {
191 let value = e.target.value;
192 value = value.toUpperCase();
193 this.actions.handleDisabledChange(value);
198 let props = this.props;
199 let state = this.state;
200 let passwordSectionHTML = null;
201 let formButtonsHTML = (
202 <ButtonGroup className="buttonGroup">
203 <Button label="EDIT" type="submit" onClick={this.editUser} />
206 if(!this.state.isReadOnly) {
207 passwordSectionHTML = (state['user-domain'].toLowerCase() == 'system') ?
208 ( this.state.isEdit ?
210 <FormSection title="PASSWORD CHANGE">
211 <Input label="NEW PASSWORD" type="password" value={state['new-password']} onChange={this.updateInput.bind(null, 'new-password')}/>
212 <Input label="REPEAT NEW PASSWORD" type="password" value={state['confirm-password']} onChange={this.updateInput.bind(null, 'confirm-password')}/>
216 <FormSection title="CREATE PASSWORD">
217 <Input label="CREATE PASSWORD" type="password" value={state.newPassword} onChange={this.updateInput.bind(null, 'new-password')}/>
218 <Input label="REPEAT PASSWORD" type="password" value={state.repeatNewPassword} onChange={this.updateInput.bind(null, 'confirm-password')}/>
226 <ButtonGroup className="buttonGroup">
227 <Button label="Delete" onClick={this.deleteUser} />
228 <Button label="Cancel" onClick={this.cancelEditUser} />
229 <Button label="Update" type="submit" onClick={this.updateUser} />
233 <ButtonGroup className="buttonGroup">
234 <Button label="Cancel" onClick={this.closePanel} />
235 <Button label="Create" type="submit" onClick={this.createUser} />
241 <PanelWrapper column>
242 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} >
243 <AppHeader nav={[{ name: 'USER MANAGEMENT' }, { name: 'PLATFORM ROLE MANAGEMENT', onClick: this.context.router.push.bind(this, { pathname: '/platform' }) }]} />
245 <PanelWrapper className={`row userManagement ${!this.state.userOpen ? 'userList-open' : ''}`} style={{ 'flexDirection': 'row' }} >
246 <PanelWrapper ref={(div) => { this.UserList = div }} className={`column userList expanded ${this.state.userOpen ? 'collapsed ' : ' '} ${this.state.hideColumns ? 'hideColumns ' : ' '}`}>
247 <Panel title="User List" style={{ marginBottom: 0 }} no-corners>
248 <div className="tableRow tableRow--header">
249 <div className="userName">
259 {state.users && state.users.map((u, k) => {
260 let platformRoles = [];
261 for (let role in u.platformRoles) {
262 platformRoles.push(<div>{`${role}: ${u.platformRoles[role]}`}</div>)
265 <div ref={(el) => this[`user-ref-${k}`] = el} className={`tableRow tableRow--data ${((self.state.activeIndex == k) && self.state.userOpen) ? 'tableRow--data-active' : ''}`}
267 onClick={self.viewUser.bind(null, u, k)}>
269 className={`userName userName-header ${((self.state.activeIndex == k) && self.state.userOpen) ? 'activeUser' : ''}`}
277 {u['disabled'] ? "DISABLED" : "ENABLED"}
283 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
284 <ButtonGroup className="buttonGroup">
285 <Button label="Add User" onClick={this.addUser} />
289 <PanelWrapper onKeyUp={this.evaluateSubmit}
290 className={`userAdmin column`}>
292 title={state.isEdit ? state['user-name'] : 'Create User'}
293 style={{ marginBottom: 0 }}
294 hasCloseButton={this.closePanel}
296 <FormSection title="USER INFO" className="userInfo">
298 (!state.isEditUser || state.isReadOnly) ?
299 <Input className="userInfo-section" readonly={state.isReadOnly || this.state.isEdit} label="Username" value={state['user-name']} onChange={this.updateInput.bind(null, 'user-name')} />
302 <Input className="userInfo-section" readonly={state.isReadOnly || this.state.isEdit} label="Domain" value={state['user-domain']} onChange={this.updateInput.bind(null, 'user-domain')}></Input>
303 <Input className="userInfo-section"
305 onChange={this.disableChange}
307 value={this.state.disabled}
308 options={[{ label: "DISABLED", value: "TRUE" }, { label: "ENABLED", value: "FALSE" }]}
309 readonly={state.isReadOnly}
310 readonlydisplay={this.state.disabled == "TRUE" ? "DISABLED" : "ENABLED"}
313 <FormSection title="PLATFORM ROLES" style={{ display: 'none' }}>
314 <Input label="Super Admin" onChange={this.platformChange.bind(null, 'super_admin')} checked={state.platformRoles.super_admin} type="checkbox" />
315 <Input label="Platform Admin" onChange={this.platformChange.bind(null, 'platform_admin')} checked={state.platformRoles.platform_admin} type="checkbox" />
316 <Input label="Platform Oper" onChange={this.platformChange.bind(null, 'platform_oper')} checked={state.platformRoles.platform_oper} type="checkbox" />
320 <FormSection title="PROJECT ROLES">
321 <table className="userProfile-table">
330 this.state.projects && this.state.projects.ids && this.state.projects.ids.map((p, i) => {
331 let project = self.state.projects.data[p];
340 project.map(function (k) {
341 return <div>{k}</div>
355 {passwordSectionHTML}
358 <SkyquakeRBAC allow={[PLATFORM.SUPER, PLATFORM.ADMIN]} className="rbacButtonGroup">
368 // onClick={this.Store.update.bind(null, Account)}
369 UserManagementDashboard.contextTypes = {
370 router: React.PropTypes.object,
371 userProfile: React.PropTypes.object
374 UserManagementDashboard.defaultProps = {
379 export default SkyquakeComponent(UserManagementDashboard);
382 function isElementInView(el) {
383 var rect = el && el.getBoundingClientRect() || {};
388 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
389 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
394 // isReadOnly={state.isReadOnly} disabled={state.disabled} onChange={this.disableChange}
396 class isDisabled extends React.Component {
401 let props = this.props;
409 class InputCollection extends React.Component {
412 this.collection = props.collection;
414 buildTextInput(onChange, v, i) {
417 readonly={this.props.readonly}
418 style={{ flex: '1 1' }}
421 onChange={onChange.bind(null, i)}
425 buildSelectOption(initial, options, onChange, v, i) {
428 readonly={this.props.readonly}
429 key={`${i}-${v.replace(' ', '_')}`}
433 onChange={onChange.bind(null, i)}
441 const props = this.props;
443 let className = "InputCollection";
444 if (props.className) {
445 className = `${className} ${props.className}`;
447 if (props.type == 'select') {
448 inputType = this.buildSelectOption.bind(this, props.initial, props.options, props.onChange);
450 inputType = this.buildTextInput.bind(this, props.onChange)
453 <div className="InputCollection-wrapper">
454 {props.collection.map((v, i) => {
456 <div key={i} className={className} >
459 props.readonly ? null : <span onClick={props.RemoveItemFn.bind(null, i)} className="removeInput"><img src={imgRemove} />Remove</span>}
463 {props.readonly ? null : <span onClick={props.AddItemFn} className="addInput"><img src={imgAdd} />Add</span>}
470 InputCollection.defaultProps = {
473 onChange: function (i, e) {
475 Updating with: ${e.target.value}
479 AddItemFn: function (e) {
480 console.log(`Adding a new item to collection`)
482 RemoveItemFn: function (i, e) {
483 console.log(`Removing item from collection at index of: ${i}`)
487 class FormSection extends React.Component {
489 let className = 'FormSection ' + this.props.className;
492 style={this.props.style}
493 className={className}
495 <div className="FormSection-title">
498 <div className="FormSection-body">
499 {this.props.children}
507 FormSection.defaultProps = {