diff --git a/src/_services/user.service.js b/src/_services/user.service.js index 23de84b..191c076 100644 --- a/src/_services/user.service.js +++ b/src/_services/user.service.js @@ -19,6 +19,8 @@ export const userService = { createUser, updateUser, deleteUser, + changePassword, + adminChangePassword, approveAction, denyAction, sendAuthenticatorCode, @@ -160,6 +162,14 @@ function deleteUser(userId, 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) { return function (response) { return response.text().then(text => { diff --git a/src/_store/users.module.js b/src/_store/users.module.js index 18fcd31..a6b1d7e 100644 --- a/src/_store/users.module.js +++ b/src/_store/users.module.js @@ -4,7 +4,7 @@ import { util } from '../_helpers'; const state = { 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, listSshKeys: false, addSshKey: false, removeSshKey: false }, @@ -15,7 +15,8 @@ const state = { contact: null, authenticator: {}, sshKey: null, - sshKeys: [] + sshKeys: [], + changePasswordResponse: null }; export const CONTACT_TYPE_AUTHENTICATOR = 'authenticator'; @@ -166,9 +167,28 @@ const actions = { userService.deleteUser(userId, messages, errors) .then( 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 = { @@ -339,6 +359,18 @@ const mutations = { deleteFailure(state, { id, error }) { state.loading.deleting = false; 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; } }; diff --git a/src/account/profile/ChangePasswordPage.vue b/src/account/profile/ChangePasswordPage.vue index 394760f..a81694b 100644 --- a/src/account/profile/ChangePasswordPage.vue +++ b/src/account/profile/ChangePasswordPage.vue @@ -66,7 +66,7 @@ } }, computed: { - ...mapState('users', ['user', 'policy']), + ...mapState('users', ['user', 'policy', 'changePasswordResponse']), ...mapState('system', ['messages']), requiredExternalAuthContacts () { const contacts = []; @@ -88,7 +88,7 @@ } }, methods: { - ...mapActions('users', ['getPolicyByUserId', 'changePassword']), + ...mapActions('users', ['getUserById', 'getPolicyByUserId', 'changePassword', 'adminChangePassword']), ...mapGetters('users', ['loading']), authenticatorRequired (p) { if (p && p.accountContacts && p.accountContacts.length > 0) { @@ -104,6 +104,32 @@ changePass (e) { this.submitted = true; 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'); } }, @@ -140,6 +166,13 @@ this.getUserById({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)); + } + } } }; \ No newline at end of file