Password change API for any user. Related to xo-web issue#362

This commit is contained in:
Fabrice Marsaud 2015-09-14 11:20:40 +02:00
parent c1e9061568
commit a1a7c5e4bb
2 changed files with 24 additions and 1 deletions

View File

@ -72,3 +72,15 @@ set.params = {
password: { type: 'string', optional: true },
permission: { type: 'string', optional: true }
}
export async function changePassword ({id, oldPassword, newPassword}) {
await this.changePassword(id, oldPassword, newPassword)
}
changePassword.description = 'change password after checking old password (user function)'
changePassword.params = {
id: {type: 'string'},
oldPassword: {type: 'string'},
newPassword: {type: 'string'}
}

View File

@ -28,7 +28,7 @@ import {autobind} from './decorators'
import {generateToken} from './utils'
import {Groups} from './models/group'
import {Jobs} from './models/job'
import {JsonRpcError, NoSuchObject} from './api-errors'
import {InvalidCredential, JsonRpcError, NoSuchObject} from './api-errors'
import {ModelAlreadyExists} from './collection'
import {Remotes} from './models/remote'
import {Schedules} from './models/schedule'
@ -334,6 +334,17 @@ export default class Xo extends EventEmitter {
})
}
async changePassword (id, oldPassword, newPassword) {
const user = await this._getUser(id)
const auth = await user.checkPassword(oldPassword)
if (!auth) {
throw new InvalidCredential()
}
await user.setPassword(newPassword)
return await this._users.save(user.properties)
}
// -----------------------------------------------------------------
async createGroup ({name}) {