Ejemplo n.º 1
0
    def dispatch_request(self, link=None):
        user = user_service.first(password_link=link)
        if not user:
            abort(404)

        if user.password_link_expired():
            params = dict()
            params.update(self.expired_redirect_params)
            return redirect(url_for(self.expired_redirect, **params))

        form = self.form(schema=self.schema())
        if form.validate_on_submit():
            new_password = form.password.data
            user_service.change_password(user, new_password)
            params = dict()
            params.update(self.ok_redirect_params)
            if self.flash:
                flash(self.ok_message, 'success')
            return redirect(url_for(self.ok_redirect, **params))

        elif form.is_submitted():
            if self.flash:
                flash(self.invalid_message, 'danger')

        params = dict(form=form)
        if self.params:
            additional = deepcopy(self.params)
            params = params.update(additional)

        return render_template(self.template, **params)
Ejemplo n.º 2
0
 def test_password_change(self):
     """ Password change possible """
     u = self.create_user()
     with events.events.disconnect_receivers():
         with self.app.test_request_context():
             user_service.force_login(u)
             self.assertTrue(current_user.is_authenticated)
             user_service.change_password(u, '0987654')
             self.assertFalse(current_user.is_authenticated)
             self.assertTrue(u.verify_password('0987654'))
Ejemplo n.º 3
0
 def test_password_change_emits_event(self):
     """ Password change  """
     u = self.create_user()
     with events.events.disconnect_receivers():
         spy = mock.Mock()
         events.password_changed_event.connect(spy, weak=False)
         with self.app.test_request_context():
             user_service.force_login(u)
             self.assertTrue(current_user.is_authenticated)
             user_service.change_password(u, '0987654')
             self.assertFalse(current_user.is_authenticated)
             self.assertTrue(u.verify_password('0987654'))
             spy.assert_called_with(u)
Ejemplo n.º 4
0
 def test_password_change_returns_validation_errors(self):
     """ Password change returns validation errors on bad data """
     u = self.create_user()
     u.email = '1'  # trigger error
     with events.events.disconnect_receivers():
         res = user_service.change_password(u, '0987654')
         self.assertIsInstance(res, Result)
Ejemplo n.º 5
0
    def dispatch_request(self, id=None):
        user = user_service.get_or_404(id)
        myself = self.is_myself(id)
        form = self.form(schema=self.schema(), context=user)
        if form.validate_on_submit():
            ok = user_service.change_password(user, form.password.data)
            if ok:
                if self.flash:
                    flash(self.ok_message, 'success')
                return redirect(url_for(self.ok_redirect))
            else:
                if self.flash:
                    flash(self.exception_message, 'danger')

        params = dict(form=form, user=user, myself=myself)
        return render_template(self.template, **params)
Ejemplo n.º 6
0
def change_password(*_, user_id=None, password=None):
    """ Change user password """
    click.echo(green('\nChange password:'******'-' * 40))

    with get_app().app_context():
        user = find_user(dict(id=user_id))
        if not user:
            click.echo(red('User not found\n'))
            return

        result = user_service.change_password(user, password)
        if isinstance(result, User):
            msg = 'Changed password for user {} \n'.format(user.email)
            click.echo(green(msg))
            return

        print_validation_errors(result)
        return