User Management: Change password initial support for user admin
authorLaurence Maultsby <laurence.maultsby@riftio.com>
Thu, 9 Mar 2017 20:57:49 +0000 (15:57 -0500)
committerLaurence Maultsby <laurence.maultsby@riftio.com>
Thu, 9 Mar 2017 20:57:49 +0000 (15:57 -0500)
Signed-off-by: Laurence Maultsby <laurence.maultsby@riftio.com>
skyquake/framework/core/modules/api/userManagementAPI.js
skyquake/framework/core/modules/routes/userManagement.js
skyquake/plugins/user-management/src/dashboard/dashboard.jsx
skyquake/plugins/user-management/src/dashboard/userMgmtSource.js
skyquake/plugins/user-management/src/dashboard/userMgmtStore.js

index cf33313..3972122 100644 (file)
@@ -102,6 +102,71 @@ UserManagement.create = function(req) {
         });
     });
 };
+UserManagement.update = function(req) {
+    var self = this;
+    var api_server = req.query['api_server'];
+    var bodyData = req.body;
+    data = {
+        "users":[bodyData]
+    }
+    var updateTasks = [];
+    if(bodyData.hasOwnProperty('old-password')) {
+        var changePW = rp({
+            uri: utils.confdPort(api_server) + '/api/operations/change-password',
+            method: 'POST',
+            headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
+                'Authorization': req.get('Authorization')
+            }),
+            forever: constants.FOREVER_ON,
+            json: {
+                "input": {
+                    'user-name' : bodyData['user-name'],
+                    'user-domain' : bodyData['user-domain'],
+                    'old-password' : bodyData['old-password'],
+                    'new-password' : bodyData['new-password'],
+                    'confirm-password' : bodyData['confirm-password'],
+                }
+            },
+            rejectUnauthorized: false,
+            resolveWithFullResponse: true
+        });
+        updateTasks.push(changePW);
+    };
+    var updateUser = rp({
+                uri: utils.confdPort(api_server) + '/api/config/user-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 UserManagement.passwordChange', error);
+            response.statusCode = error.statusCode || 500;
+            response.errorMessage = {
+                error: 'Failed to passwordChange user' + error
+            };
+            reject(response);
+        });
+    });
+};
 
 UserManagement.delete = function(req) {
     var self = this;
index 359d985..0552229 100644 (file)
@@ -50,6 +50,13 @@ Router.post('/user', cors(), function(req, res) {
         utils.sendErrorResponse(error, res);
     });
 });
+Router.put('/user', cors(), function(req, res) {
+    UserManagementAPI.update(req).then(function(response) {
+        utils.sendSuccessResponse(response, res);
+    }, function(error) {
+        utils.sendErrorResponse(error, res);
+    });
+});
 Router.delete('/user/:username/:domain', cors(), function(req, res) {
     UserManagementAPI.delete(req).then(function(response) {
         utils.sendSuccessResponse(response, res);
index df28d21..c997e15 100644 (file)
@@ -81,12 +81,12 @@ class UserManagementDashboard extends React.Component {
     closePanel = () => {
         this.actions.handleCloseUserPanel();
     }
-    updateUser = (e) => {
-        e.preventDefault();
-        e.stopPropagation();
+    // updateUser = (e) => {
+    //     e.preventDefault();
+    //     e.stopPropagation();
 
-        this.Store.updateUser();
-    }
+    //     this.Store.updateUser();
+    // }
     deleteUser = (e) => {
         e.preventDefault();
         e.stopPropagation();
@@ -108,6 +108,41 @@ class UserManagementDashboard extends React.Component {
                 // 'confirm-password': this.state['confirm-password']
             });
         }
+    }
+    updateUser = (e) => {
+        let self = this;
+        e.preventDefault();
+        e.stopPropagation();
+        let validatedPasswords = validatePasswordFields(this.state);
+        if(validatedPasswords) {
+            this.Store.updateUser(_.merge({
+                            'user-name': this.state['user-name'],
+                            'user-domain': this.state['user-domain'],
+                            'password': this.state['new-password']
+                        }));
+        }
+        function validatePasswordFields(state) {
+            let oldOne = state['old-password'];
+            let newOne = state['new-password'];
+            let confirmOne = state['confirm-password'];
+            if(true) {
+                if(oldOne == newOne) {
+                    self.props.actions.showNotification('Your new password must not match your old one');
+                    return false;
+                }
+                if(newOne != confirmOne) {
+                    self.props.actions.showNotification('Passwords do not match');
+                    return false;
+                }
+                return {
+                    // 'old-password': oldOne,
+                    'new-password': newOne,
+                    'confirm-password': confirmOne
+                }
+            } else {
+                return {};
+            }
+        }
     }
      evaluateSubmit = (e) => {
         if (e.keyCode == 13) {
@@ -149,7 +184,6 @@ class UserManagementDashboard extends React.Component {
             passwordSectionHTML = ( this.state.isEdit ?
                                         (
                                             <FormSection title="PASSWORD CHANGE">
-                                                <Input label="OLD PASSWORD" type="password" value={state['old-password']} onChange={this.updateInput.bind(null, 'old-password')} />
                                                 <Input label="NEW PASSWORD" type="password" value={state['new-password']}  onChange={this.updateInput.bind(null, 'new-password')}/>
                                                 <Input label="REPEAT NEW PASSWORD" type="password"  value={state['confirm-password']}  onChange={this.updateInput.bind(null, 'confirm-password')}/>
                                             </FormSection>
index 18cb83d..2f922a9 100644 (file)
@@ -47,9 +47,18 @@ module.exports = function(Alt) {
         updateUser: {
           remote: function(state, user) {
             return new Promise(function(resolve, reject) {
-              setTimeout(function() {
-                  resolve(true);
-              }, 1000)
+              $.ajax({
+                  url: `/user?api_server=${API_SERVER}`,
+                  type: 'PUT',
+                  data: user,
+                  beforeSend: Utils.addAuthorizationStub,
+                  success: function(data, textStatus, jqXHR) {
+                    resolve(data);
+                  }
+                }).fail(function(xhr){
+                  //Authentication and the handling of fail states should be wrapped up into a connection class.
+                  Utils.checkAuthentication(xhr.status);
+                });
             });
           },
           interceptResponse: interceptResponse({
index 5883995..19952fb 100644 (file)
@@ -187,7 +187,11 @@ export default class UserManagementStore {
             disabled: this.disabled,
             projectRoles: this.projectRoles
         }
-        this.setState({users})
+        this.setState({
+            users,
+            isEdit: true,
+            isReadOnly: true
+        })
     }
     deleteUserSuccess() {
         this.alt.actions.global.hideScreenLoader.defer();