PlatformRoleMgmt: Appropriate user is now deleted
[osm/UI.git] / skyquake / plugins / user_management / src / platformRoleManagement / platformRoleManagement.jsx
index 49efb82..4278d74 100644 (file)
@@ -5,7 +5,7 @@
 import React from 'react';
 import ReactDOM from 'react-dom';
 import AppHeader from 'widgets/header/header.jsx';
-import ProjectManagementStore from './platformRoleManagementStore.js';
+import PlatformRoleManagementStore from './platformRoleManagementStore.js';
 import SkyquakeComponent from 'widgets/skyquake_container/skyquakeComponent.jsx';
 import 'style/layout.scss';
 import './platformRoleManagement.scss';
@@ -23,15 +23,14 @@ import imgRemove from '../../node_modules/open-iconic/svg/trash.svg'
 class PlatformRoleManagement extends React.Component {
     constructor(props) {
         super(props);
-        this.Store = this.props.flux.stores.hasOwnProperty('ProjectManagementStore') ? this.props.flux.stores.ProjectManagementStore : this.props.flux.createStore(ProjectManagementStore);
-        this.Store.getProjects();
-        this.Store.getUsers();
+        this.Store = this.props.flux.stores.hasOwnProperty('PlatformRoleManagementStore') ? this.props.flux.stores.PlatformRoleManagementStore : this.props.flux.createStore(PlatformRoleManagementStore);
         this.state = this.Store.getState();
         this.actions = this.state.actions;
+        this.Store.getPlatform();
+        this.Store.getUsers();
     }
     componentDidUpdate() {
-        let self = this;
-        ReactDOM.findDOMNode(this.projectList).addEventListener('transitionend', this.onTransitionEnd, false);
+
     }
     componentWillMount() {
         this.Store.listen(this.updateState);
@@ -54,97 +53,52 @@ class PlatformRoleManagement extends React.Component {
     platformChange = (platformRole, e) => {
         this.actions.handlePlatformRoleUpdate(platformRole, e.currentTarget.checked);
     }
-    addProjectRole = (e) => {
-        this.actions.handleAddProjectItem();
-    }
-    removeProjectRole = (i, e) => {
-        this.actions.handleRemoveProjectItem(i);
-    }
-    updateProjectRole = (i, e) => {
-        this.actions.handleUpdateProjectRole(i, e)
-    }
-    addProject = () => {
-        this.actions.handleAddProject();
-    }
-    viewProject = (un, index) => {
-        this.actions.viewProject(un, index);
-    }
     editProject = () => {
-        this.actions.editProject(false);
+        this.actions.editPlatform(false);
     }
-    cancelEditProject = () => {
-        this.actions.editProject(true)
+    cancelEditPlatform = () => {
+        this.actions.editPlatform(true)
     }
     closePanel = () => {
         this.actions.handleCloseProjectPanel();
     }
-
-    deleteProject = (e) => {
-        e.preventDefault();
-        e.stopPropagation();
-        this.Store.deleteProject({
-                'name': this.state['name']
-            });
-    }
-    createProject = (e) => {
+    updatePlatform = (e) => {
         let self = this;
         e.preventDefault();
         e.stopPropagation();
-        let projectUsers = self.state.projectUsers;
-        let selectedUsers = [];
-        //Remove null values from role
-        projectUsers.map((u) => {
-           u.role && u.role.map((r,i) => {
-             let role = {};
-             //you may add a user without a role or a keys, but if one is present then the other must be as well.
-            if(!r || ((r.role || r['keys']) && (!r.role || !r['keys']))) {
-                projectUsers.splice(i, 1);
-            } else {
-                return u;
+        let platformUsers = self.state.platformUsers;
+        let cleanUsers = this.cleanUsers(platformUsers);
+        this.Store.updatePlatform({
+                'user': JSON.stringify(cleanUsers)
             }
-           })
-        })
-        this.Store.createProject({
-            'name': self.state['name'],
-            'description': self.state.description,
-            'project-config' : {
-                'user': projectUsers
-            }
-        });
+        );
     }
-    updateProject = (e) => {
+     cleanUsers(platformUsers) {
         let self = this;
-        e.preventDefault();
-        e.stopPropagation();
-        let projectUsers = self.state.projectUsers;
-
+        let cleanUsers = [];
         //Remove null values from role
-        projectUsers.map((u) => {
-           u.role && u.role.map((r,i) => {
-             let role = {};
-             //you may add a user without a role or a keys, but if one is present then the other must be as well.
-            if(!r || ((r.role || r['keys']) && (!r.role || !r['keys']))) {
-                projectUsers.splice(i, 1);
-            } else {
-                return u;
-            }
-           })
-        })
-
-        this.Store.updateProject(_.merge({
-            'name': self.state['name'],
-            'description': self.state.description,
-            'project-config' : {
-                'user': projectUsers
-            }
-        }));
+        platformUsers.map((u) => {
+            let cleanRoles = [];
+            u.role && u.role.map((r,i) => {
+                let role = {};
+                //Platform user can not change role of itself.
+                if(r.role){
+                    //removing key for rbac-platform
+                    delete r.keys;
+                    cleanRoles.push(r)
+                }
+            });
+           u.role = cleanRoles;
+           if (u['user-name'] != self.context.userProfile.userId) {
+                cleanUsers.push(u);
+           }
+        });
+        return cleanUsers;
     }
      evaluateSubmit = (e) => {
         if (e.keyCode == 13) {
             if (this.props.isEdit) {
-                this.updateProject(e);
-            } else {
-                this.createProject(e);
+                this.updatePlatform(e);
             }
             e.preventDefault();
             e.stopPropagation();
@@ -209,9 +163,9 @@ class PlatformRoleManagement extends React.Component {
                 <Button label="EDIT" type="submit" onClick={this.editProject} />
             </ButtonGroup>
         );
-        let projectUsers = [];
-        self.state.projectUsers.map((u) => {
-            projectUsers.push(u['user-name']);
+        let platformUsers = [];
+        self.state.platformUsers.map((u) => {
+            platformUsers.push(u['user-name']);
         });
 
         if(!this.state.isReadOnly) {
@@ -219,176 +173,102 @@ class PlatformRoleManagement extends React.Component {
                                 state.isEdit ?
                                 (
                                     <ButtonGroup className="buttonGroup">
-                                        <Button label="Update" type="submit" onClick={this.updateProject} />
-                                        <Button label="Delete" onClick={this.deleteProject} />
-                                        <Button label="Cancel" onClick={this.cancelEditProject} />
+                                        <Button label="Update" type="submit" onClick={this.updatePlatform} />
+                                        <Button label="Cancel" onClick={this.cancelEditPlatform} />
                                     </ButtonGroup>
                                 )
                                 : (
                                     <ButtonGroup className="buttonGroup">
-                                        <Button label="Create" type="submit" onClick={this.createProject}  />
+                                        <Button label="Edit" type="submit" onClick={this.updatePlatform}  />
                                     </ButtonGroup>
                                 )
                             )
         }
 
         html = (
-            <PanelWrapper className={`row projectManagement ${false ? 'projectList-open' : ''}`} style={{'alignContent': 'center', 'flexDirection': 'row'}} >
-                <PanelWrapper onKeyUp={this.evaluateSubmit}
-                    className={`ProjectAdmin column`}>
-                    <Panel
-                        title={state.isEdit ? state['name'] : 'Create Project'}
-                        style={{marginBottom: 0}}
-                        hasCloseButton={this.closePanel}
-                        no-corners>
-                        <FormSection title="USER ROLES">
-
-                        <table>
-                            <thead>
-                                <tr>
-                                    {!state.isReadOnly ? <td></td> : null}
-                                    <td>User Name</td>
-                                    {
-                                        state.roles.map((r,i) => {
-                                            return <td key={i}>{r}</td>
-                                        })
-                                    }
-                                </tr>
-                            </thead>
-                            <tbody>
-                                {
-                            state.projectUsers.map((u,i)=> {
-                                let userRoles = u.role.map((r) => {
-                                    return r.role;
-                                })
-                                return (
-                                    <tr key={i}>
-                                        {!state.isReadOnly ? <td><span
-                                                                    className="removeInput"
-                                                                    onClick={self.removeUserFromProject.bind(self, u)}
-                                                                >
-                                                                    <img src={imgRemove} />
+            <PanelWrapper column>
+                <AppHeader nav={[{name: 'USER MANAGEMENT', onClick: this.context.router.push.bind(this, {pathname: '/'})}]}/>
+                <PanelWrapper className={`row projectManagement ${false ? 'projectList-open' : ''}`} style={{'alignContent': 'center', 'flexDirection': 'row'}} >
+                    <PanelWrapper onKeyUp={this.evaluateSubmit}
+                        className={`ProjectAdmin column`}>
+                        <Panel
+                            title="Manage Roles"
+                            style={{marginBottom: 0}}
+                            no-corners>
+                            <FormSection title="USER ROLES">
 
-                                                                </span></td> : null}
-                                        <td>
-                                            {u['user-name']}
-                                        </td>
+                            <table>
+                                <thead>
+                                    <tr>
+                                        {!state.isReadOnly ? <td></td> : null}
+                                        <td>User Name</td>
                                         {
-                                            state.roles.map((r,j) => {
-                                                return <td key={j}><Input type="checkbox" onChange={self.toggleUserRoleInProject.bind(self, i, j)} checked={(userRoles.indexOf(r) > -1)} /></td>
+                                            state.roles.map((r,i) => {
+                                                return <td key={i}>{r}</td>
                                             })
                                         }
                                     </tr>
-                                )
-                            })
-                        }
-                            </tbody>
-                        </table>
-
-
-                        { false ?
-                            <div>
-                                <div className="tableRow tableRow--header">
-                                    <div className="projectName">
-                                        User Name
-                                    </div>
-                                    <div>
-                                        Role
-                                    </div>
-                                </div>
-                                {
-                                    state.projectUsers && state.projectUsers.map((u, k) => {
-                                        return (
-                                            <div ref={(el) => this[`project-ref-${k}`] = el} className={`tableRow tableRow--data projectUsers`} key={k}>
-                                                <div className="userName" style={state.isReadOnly ? {paddingTop: '0.25rem'} : {} }>{u['user-name']}</div>
-                                                <div>
-                                                    {
-                                                        u.role && u.role.map((r, l) => {
-                                                            return (
-                                                                <div key={l}>
-                                                                    <div style={{display: 'flex'}} className="selectRole">
-                                                                        <SelectOption
-                                                                            readonly={state.isReadOnly}
-                                                                            defaultValue={r.role}
-                                                                            options={state.roles}
-                                                                            onChange={self.updateUserRoleInProject.bind(self, k, l)}
-                                                                        />
-                                                                        {!state.isReadOnly ?
-                                                                            <span
-                                                                            className="removeInput"
-                                                                            onClick={self.removeRoleFromUserInProject.bind(self, k, l)}
-                                                                            >
-                                                                                <img src={imgRemove} />
-                                                                                Remove Role
-                                                                            </span>
-                                                                            : null
-                                                                        }
-
-                                                                    </div>
-                                                                </div>
-                                                            )
-                                                        })
-                                                    }
-                                                    {!state.isReadOnly ?
-                                                        <div className="buttonGroup">
-                                                            <span className="addInput addRole" onClick={self.addRoleToUserInProject.bind(self, k)}><img src={imgAdd} />
-                                                                Add Role
-                                                            </span>
-                                                            {
-                                                                (!(u.role && u.role.length)) ?
-                                                                    <span
+                                </thead>
+                                <tbody>
+                                    {
+                                state.platformUsers.map((u,i)=> {
+                                    let userRoles = u.role && u.role.map((r) => {
+                                        return r.role;
+                                    }) || [];
+                                    return (
+                                        <tr key={i}>
+                                            {!state.isReadOnly ? <td><span
                                                                         className="removeInput"
-                                                                        onClick={self.removeUserFromProject.bind(self, k)}
+                                                                        onClick={self.removeUserFromProject.bind(self, i)}
                                                                     >
                                                                         <img src={imgRemove} />
-                                                                        Remove User
-                                                                    </span> : null
+
+                                                                    </span></td> : null}
+                                            <td>
+                                                {u['user-name']}
+                                            </td>
+                                            {
+                                                state.roles.map((r,j) => {
+                                                    return <td key={j}><Input readonly={state.isReadOnly} type="checkbox" onChange={self.toggleUserRoleInProject.bind(self, i, j)} checked={(userRoles.indexOf(r) > -1)} /></td>
+                                                })
+                                            }
+                                        </tr>
+                                    )
+                                })
+                            }
+                                </tbody>
+                            </table>
+                                {
+                                    !state.isReadOnly ?
+                                        <div className="tableRow tableRow--header">
+                                            <div>
+                                                <div className="addUser">
+                                                    <SelectOption
+                                                        onChange={this.actions.handleSelectedUser}
+                                                        defaultValue={state.selectedUser}
+                                                        initial={true}
+                                                        options={state.users && state.users.filter((u) => {
+                                                            return platformUsers.indexOf(u['user-name']) == -1
+                                                        }).map((u) => {
+                                                            return {
+                                                                label: u['user-name'],
+                                                                value: u
                                                             }
-                                                        </div>
-                                                        : null
-                                                    }
+                                                        })}
+                                                    />
+                                                    <span className="addInput" onClick={this.addUserToProject}><img src={imgAdd} />
+                                                        Add User
+                                                    </span>
                                                 </div>
                                             </div>
-                                        )
-                                    })
+                                        </div> : null
                                 }
-                                </div>
-                                 : null
-                             }
-                            {
-                                !state.isReadOnly ?
-                                    <div className="tableRow tableRow--header">
-                                        <div>
-                                            <div className="addUser">
-                                                <SelectOption
-                                                    onChange={this.actions.handleSelectedUser}
-                                                    defaultValue={state.selectedUser}
-                                                    initial={true}
-                                                    options={state.users && state.users.filter((u) => {
-                                                        return projectUsers.indexOf(u['user-name']) == -1
-                                                    }).map((u) => {
-                                                        return {
-                                                            label: u['user-name'],
-                                                            value: u
-                                                        }
-                                                    })}
-                                                />
-                                                <span className="addInput" onClick={this.addUserToProject}><img src={imgAdd} />
-                                                    Add User
-                                                </span>
-                                            </div>
-                                        </div>
-                                    </div> : null
-                            }
-
-                        </FormSection>
 
-                    </Panel>
+                            </FormSection>
+                        </Panel>
                         {formButtonsHTML}
-
+                    </PanelWrapper>
                 </PanelWrapper>
-
-
             </PanelWrapper>
         );
         return html;
@@ -396,7 +276,8 @@ class PlatformRoleManagement extends React.Component {
 }
 // onClick={this.Store.update.bind(null, Account)}
 PlatformRoleManagement.contextTypes = {
-    router: React.PropTypes.object
+    router: React.PropTypes.object,
+    userProfile: React.PropTypes.object
 };
 
 PlatformRoleManagement.defaultProps = {