'Allows to change a user password.'

@expose_api
def set_password(self, trans, id, payload={}, **kwd):

    password = payload.get('password')
    confirm = payload.get('confirm')
    current = payload.get('current')
    token = payload.get('token')
    token_result = None
    if token:
        token_result = trans.sa_session.query(trans.app.model.PasswordResetToken).get(token)
        if ((not token_result) or (not (token_result.expiration_time > datetime.utcnow()))):
            raise MessageException('Invalid    or    expired    password    reset    token,    please    request    a    new    one.')
        user = token_result.user
    else:
        user = self._get_user(trans, id)
        (ok, message) = trans.app.auth_manager.check_change_password(user, current)
        if (not ok):
            raise MessageException(message)
    if user:
        message = validate_password(trans, password, confirm)
        if message:
            raise MessageException(message)
        else:
            user.set_password_cleartext(password)
            if token_result:
                trans.handle_user_login(token_result.user)
                token_result.expiration_time = datetime.utcnow()
                trans.sa_session.add(token_result)
            for other_galaxy_session in trans.sa_session.query(trans.app.model.GalaxySession).filter(and_((trans.app.model.GalaxySession.table.c.user_id == user.id), (trans.app.model.GalaxySession.table.c.is_valid == true()), (trans.app.model.GalaxySession.table.c.id != trans.galaxy_session.id))):
                other_galaxy_session.is_valid = False
                trans.sa_session.add(other_galaxy_session)
            trans.sa_session.add(user)
            trans.sa_session.flush()
            trans.log_event('User    change    password')
            return {'message': 'Password    has    been    saved.'}
    raise MessageException('Failed    to    determine    user,    access    denied.')
