From 0639632bc78cfc65b26624c20e6ce4cb81d8be8f Mon Sep 17 00:00:00 2001 From: Laurence Maultsby Date: Wed, 22 Mar 2017 22:49:54 -0400 Subject: [PATCH] Platform Role Assignments take 2 Signed-off-by: Laurence Maultsby --- .../core/modules/api/projectManagementAPI.js | 84 +++++++++ .../core/modules/routes/projectManagement.js | 15 ++ skyquake/plugins/user_management/config.json | 2 +- .../src/dashboard/dashboard.jsx | 7 +- .../platformRoleManagement.jsx | 172 ++++-------------- .../platformRoleManagementActions.js | 3 +- .../platformRoleManagementSource.js | 14 +- .../platformRoleManagementStore.js | 79 ++++---- 8 files changed, 198 insertions(+), 178 deletions(-) diff --git a/skyquake/framework/core/modules/api/projectManagementAPI.js b/skyquake/framework/core/modules/api/projectManagementAPI.js index d73eb215c..44d2ac5f2 100644 --- a/skyquake/framework/core/modules/api/projectManagementAPI.js +++ b/skyquake/framework/core/modules/api/projectManagementAPI.js @@ -62,6 +62,7 @@ ProjectManagement.get = function(req) { }); }); }; + ProjectManagement.create = function(req) { var self = this; var api_server = req.query['api_server']; @@ -175,4 +176,87 @@ ProjectManagement.delete = function(req) { }); }) } + + +ProjectManagement.getPlatform = function(req) { + var self = this; + var api_server = req.query['api_server']; + + return new Promise(function(resolve, reject) { + Promise.all([ + rp({ + uri: utils.confdPort(api_server) + '/api/operational/rbac-platform-config', + method: 'GET', + headers: _.extend({}, constants.HTTP_HEADERS.accept.data, { + 'Authorization': req.get('Authorization') + }), + forever: constants.FOREVER_ON, + rejectUnauthorized: false, + resolveWithFullResponse: true + }) + ]).then(function(result) { + var response = {}; + response['data'] = {}; + if (result[0].body) { + response['data']['platform'] = JSON.parse(result[0].body)['rw-rbac-platform:rbac-platform-config']; + } + response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK + + resolve(response); + }).catch(function(error) { + var response = {}; + console.log('Problem with ProjectManagement.getPlatform', error); + response.statusCode = error.statusCode || 500; + response.errorMessage = { + error: 'Failed to get ProjectManagement.getPlatform' + error + }; + reject(response); + }); + }); +}; + +ProjectManagement.updatePlatform = function(req) { + var self = this; + var api_server = req.query['api_server']; + var bodyData = req.body; + data = bodyData; + var updateTasks = []; + + var updateUser = rp({ + uri: utils.confdPort(api_server) + '/api/config/rbac-platform-config', + method: 'PUT', + headers: _.extend({}, constants.HTTP_HEADERS.accept.data, { + 'Authorization': req.get('Authorization') + }), + forever: constants.FOREVER_ON, + json: data, + rejectUnauthorized: false, + resolveWithFullResponse: true + }); + updateTasks.push(updateUser) + return new Promise(function(resolve, reject) { + Promise.all([ + updateTasks + ]).then(function(result) { + var response = {}; + response['data'] = {}; + if (result[0].body) { + response['data'] = result[0].body; + } + response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK + + resolve(response); + }).catch(function(error) { + var response = {}; + console.log('Problem with ProjectManagement.updatePlatform', error); + response.statusCode = error.statusCode || 500; + response.errorMessage = { + error: 'Failed to passwordChange user' + error + }; + reject(response); + }); + }); +}; + + module.exports = ProjectManagement; diff --git a/skyquake/framework/core/modules/routes/projectManagement.js b/skyquake/framework/core/modules/routes/projectManagement.js index a8f9a4d11..ef52ba305 100644 --- a/skyquake/framework/core/modules/routes/projectManagement.js +++ b/skyquake/framework/core/modules/routes/projectManagement.js @@ -65,6 +65,21 @@ Router.delete('/project/:projectname', cors(), function(req, res) { }); }); +Router.put('/platform', cors(), function(req, res) { + ProjectManagementAPI.updatePlatform(req).then(function(response) { + utils.sendSuccessResponse(response, res); + }, function(error) { + utils.sendErrorResponse(error, res); + }); +}); + +Router.get('/platform', cors(), function(req, res) { + ProjectManagementAPI.getPlatform(req).then(function(response) { + utils.sendSuccessResponse(response, res); + }, function(error) { + utils.sendErrorResponse(error, res); + }); +}); module.exports = Router; diff --git a/skyquake/plugins/user_management/config.json b/skyquake/plugins/user_management/config.json index 0e66f6f2b..4ee344e1a 100644 --- a/skyquake/plugins/user_management/config.json +++ b/skyquake/plugins/user_management/config.json @@ -14,6 +14,6 @@ "label": "Platform Role Management", "route": "platform", "component": "./platformRoleManagement/platformRoleManagement.jsx", - "type": "internal" + "type": "external" }] } diff --git a/skyquake/plugins/user_management/src/dashboard/dashboard.jsx b/skyquake/plugins/user_management/src/dashboard/dashboard.jsx index c86d6b1cf..601205059 100644 --- a/skyquake/plugins/user_management/src/dashboard/dashboard.jsx +++ b/skyquake/plugins/user_management/src/dashboard/dashboard.jsx @@ -24,9 +24,9 @@ class UserManagementDashboard extends React.Component { constructor(props) { super(props); this.Store = this.props.flux.stores.hasOwnProperty('UserManagementStore') ? this.props.flux.stores.UserManagementStore : this.props.flux.createStore(UserManagementStore); - this.Store.getUsers(); - this.state = this.Store.getState(); - this.actions = this.state.actions; + this.state = this.Store.getState(); + this.actions = this.state.actions; + } componentDidUpdate() { let self = this; @@ -38,6 +38,7 @@ class UserManagementDashboard extends React.Component { } componentWillMount() { this.Store.listen(this.updateState); + this.Store.getUsers(); } componentWillUnmount() { this.Store.unlisten(this.updateState); diff --git a/skyquake/plugins/user_management/src/platformRoleManagement/platformRoleManagement.jsx b/skyquake/plugins/user_management/src/platformRoleManagement/platformRoleManagement.jsx index 49efb8217..39ca9efa8 100644 --- a/skyquake/plugins/user_management/src/platformRoleManagement/platformRoleManagement.jsx +++ b/skyquake/plugins/user_management/src/platformRoleManagement/platformRoleManagement.jsx @@ -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); @@ -86,65 +85,41 @@ class PlatformRoleManagement extends React.Component { '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; - } - }) - }) - this.Store.createProject({ - 'name': self.state['name'], - 'description': self.state.description, - 'project-config' : { - 'user': projectUsers + let platformUsers = self.state.platformUsers; + let cleanUsers = this.cleanUsers(platformUsers); + + + this.Store.updatePlatform({ + 'user': platformUsers } - }); + ); } - updateProject = (e) => { - let self = this; - e.preventDefault(); - e.stopPropagation(); - let projectUsers = self.state.projectUsers; - + cleanUsers(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 - } - })); + let cleanRoles = []; + u.role && u.role.map((r,i) => { + let role = {}; + if(r.role){ + //removing key for rbac-platform + delete r.keys; + cleanRoles.push(r) + } + }); + u.role = cleanRoles; + 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 +184,9 @@ class PlatformRoleManagement extends React.Component {