Browse Source

WIP. change password almost working

pull/1/head
Jonathan Cobb 4 years ago
parent
commit
194dbc0054
3 changed files with 80 additions and 5 deletions
  1. +10
    -0
      src/_services/user.service.js
  2. +35
    -3
      src/_store/users.module.js
  3. +35
    -2
      src/account/profile/ChangePasswordPage.vue

+ 10
- 0
src/_services/user.service.js View File

@@ -19,6 +19,8 @@ export const userService = {
createUser, createUser,
updateUser, updateUser,
deleteUser, deleteUser,
changePassword,
adminChangePassword,
approveAction, approveAction,
denyAction, denyAction,
sendAuthenticatorCode, sendAuthenticatorCode,
@@ -160,6 +162,14 @@ function deleteUser(userId, messages, errors) {
return fetch(`${config.apiUrl}/users/${userId}`, util.deleteWithAuth()).then(util.handleCrudResponse(messages, errors)); return fetch(`${config.apiUrl}/users/${userId}`, util.deleteWithAuth()).then(util.handleCrudResponse(messages, errors));
} }


function changePassword(request, messages, errors) {
return fetch(`${config.apiUrl}/me/changePassword`, util.postWithAuth(request)).then(util.handleCrudResponse(messages, errors));
}

function adminChangePassword(userId, request, messages, errors) {
return fetch(`${config.apiUrl}/users/${userId}/changePassword`, util.postWithAuth(request)).then(util.handleCrudResponse(messages, errors));
}

function handleAuthResponse(messages, errors) { function handleAuthResponse(messages, errors) {
return function (response) { return function (response) {
return response.text().then(text => { return response.text().then(text => {


+ 35
- 3
src/_store/users.module.js View File

@@ -4,7 +4,7 @@ import { util } from '../_helpers';


const state = { const state = {
loading: { loading: {
users: false, user: false, creating: false, updating: false, deleting: false,
users: false, user: false, creating: false, updating: false, deleting: false, changingPassword: false,
policy: false, updatingPolicy: false, addPolicyContact: false, removePolicyContact: false, policy: false, updatingPolicy: false, addPolicyContact: false, removePolicyContact: false,
listSshKeys: false, addSshKey: false, removeSshKey: false listSshKeys: false, addSshKey: false, removeSshKey: false
}, },
@@ -15,7 +15,8 @@ const state = {
contact: null, contact: null,
authenticator: {}, authenticator: {},
sshKey: null, sshKey: null,
sshKeys: []
sshKeys: [],
changePasswordResponse: null
}; };


export const CONTACT_TYPE_AUTHENTICATOR = 'authenticator'; export const CONTACT_TYPE_AUTHENTICATOR = 'authenticator';
@@ -166,9 +167,28 @@ const actions = {
userService.deleteUser(userId, messages, errors) userService.deleteUser(userId, messages, errors)
.then( .then(
id => commit('deleteSuccess', id), id => commit('deleteSuccess', id),
error => commit('deleteFailure', { id, error: error.toString() })
error => commit('deleteFailure', { userId, error: error.toString() })
);
},

changePassword({ commit }, {request, messages, errors}) {
commit('changePasswordRequest', request);
userService.changePasswordUser(request, messages, errors)
.then(
response => commit('changePasswordSuccess', response),
error => commit('changePasswordFailure', { error: error.toString() })
);
},

adminChangePassword({ commit }, {userId, request, messages, errors}) {
commit('changePasswordRequest', userId);
userService.adminChangePassword(userId, request, messages, errors)
.then(
id => commit('changePasswordSuccess', id),
error => commit('changePasswordFailure', { error: error.toString() })
); );
} }

}; };


const mutations = { const mutations = {
@@ -339,6 +359,18 @@ const mutations = {
deleteFailure(state, { id, error }) { deleteFailure(state, { id, error }) {
state.loading.deleting = false; state.loading.deleting = false;
state.errors.deleteUser = error; state.errors.deleteUser = error;
},

changePasswordRequest(state, id) {
state.loading.changingPassword = true;
},
changePasswordSuccess(state, response) {
state.loading.changingPassword = false;
state.changePasswordResponse = response;
},
changePasswordFailure(state, { error }) {
state.loading.deleting = false;
state.errors.changePassword = error;
} }
}; };




+ 35
- 2
src/account/profile/ChangePasswordPage.vue View File

@@ -66,7 +66,7 @@
} }
}, },
computed: { computed: {
...mapState('users', ['user', 'policy']),
...mapState('users', ['user', 'policy', 'changePasswordResponse']),
...mapState('system', ['messages']), ...mapState('system', ['messages']),
requiredExternalAuthContacts () { requiredExternalAuthContacts () {
const contacts = []; const contacts = [];
@@ -88,7 +88,7 @@
} }
}, },
methods: { methods: {
...mapActions('users', ['getPolicyByUserId', 'changePassword']),
...mapActions('users', ['getUserById', 'getPolicyByUserId', 'changePassword', 'adminChangePassword']),
...mapGetters('users', ['loading']), ...mapGetters('users', ['loading']),
authenticatorRequired (p) { authenticatorRequired (p) {
if (p && p.accountContacts && p.accountContacts.length > 0) { if (p && p.accountContacts && p.accountContacts.length > 0) {
@@ -104,6 +104,32 @@
changePass (e) { changePass (e) {
this.submitted = true; this.submitted = true;
this.errors.clear(); this.errors.clear();
// todo: validate that newPassword and newPasswordConfirm match
if (this.me) {
this.changePassword({
request: {
oldPassword: this.currentPassword,
newPassword: this.newPassword,
totpToken: this.totpToken
},
messages: this.messages,
errors: this.errors
});
} else if (this.currentUser.admin) {
this.adminChangePassword({
userId: this.userId,
request: {
oldPassword: this.currentPassword,
newPassword: this.newPassword,
totpToken: this.totpToken
},
messages: this.messages,
errors: this.errors
});
} else {
// should never happen
console.warn('Not current user and not admin, API call would fail anyway, not sending');
}
console.log('changePass called'); console.log('changePass called');
} }
}, },
@@ -140,6 +166,13 @@
this.getUserById({userId: this.userId, messages: this.messages, errors: this.errors}); this.getUserById({userId: this.userId, messages: this.messages, errors: this.errors});
this.getPolicyByUserId({userId: this.userId, messages: this.messages, errors: this.errors}); this.getPolicyByUserId({userId: this.userId, messages: this.messages, errors: this.errors});
} }
},
watch: {
changePasswordResponse (r) {
if (r) {
console.log('watch.changePasswordResponse: received '+JSON.stringify(r));
}
}
} }
}; };
</script> </script>

Loading…
Cancel
Save