Exemple #1
0
    def my_account_password(self):
        c.active = 'password'
        self.__load_data()

        managed_fields = auth_modules.get_managed_fields(c.user)
        c.can_change_password = '******' not in managed_fields

        if request.POST and c.can_change_password:
            _form = PasswordChangeForm(self.authuser.username)()
            try:
                form_result = _form.to_python(request.POST)
                UserModel().update(self.authuser.user_id, form_result)
                Session().commit()
                h.flash(_("Successfully updated password"), category='success')
            except formencode.Invalid as errors:
                return htmlfill.render(
                    render('admin/my_account/my_account.html'),
                    defaults=errors.value,
                    errors=errors.error_dict or {},
                    prefix_error=False,
                    encoding="UTF-8",
                    force_defaults=False)
            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during update of user password'),
                        category='error')
        return render('admin/my_account/my_account.html')
Exemple #2
0
 def _render_edit_profile(self, user):
     c.user = user
     c.active = 'profile'
     c.perm_user = AuthUser(dbuser=user)
     managed_fields = auth_modules.get_managed_fields(user)
     c.readonly = lambda n: 'readonly' if n in managed_fields else None
     return render('admin/users/user_edit.html')
Exemple #3
0
    def my_account(self):
        """
        GET /_admin/my_account Displays info about my account
        """
        # url('my_account')
        c.active = 'profile'
        self.__load_data()
        c.perm_user = AuthUser(user_id=self.authuser.user_id)
        c.ip_addr = self.ip_addr
        managed_fields = auth_modules.get_managed_fields(c.user)
        def_user_perms = User.get_default_user().AuthUser.permissions['global']
        if 'hg.register.none' in def_user_perms:
            managed_fields.extend(['username', 'firstname', 'lastname', 'email'])

        c.readonly = lambda n: 'readonly' if n in managed_fields else None

        defaults = c.user.get_dict()
        update = False
        if request.POST:
            _form = UserForm(edit=True,
                             old_data={'user_id': self.authuser.user_id,
                                       'email': self.authuser.email})()
            form_result = {}
            try:
                post_data = dict(request.POST)
                post_data['new_password'] = ''
                post_data['password_confirmation'] = ''
                form_result = _form.to_python(post_data)
                # skip updating those attrs for my account
                skip_attrs = ['admin', 'active', 'extern_type', 'extern_name',
                              'new_password', 'password_confirmation',
                             ] + managed_fields

                UserModel().update(self.authuser.user_id, form_result,
                                   skip_attrs=skip_attrs)
                h.flash(_('Your account was updated successfully'),
                        category='success')
                Session().commit()
                update = True

            except formencode.Invalid as errors:
                return htmlfill.render(
                    render('admin/my_account/my_account.html'),
                    defaults=errors.value,
                    errors=errors.error_dict or {},
                    prefix_error=False,
                    encoding="UTF-8",
                    force_defaults=False)
            except Exception:
                log.error(traceback.format_exc())
                h.flash(_('Error occurred during update of user %s') \
                        % form_result.get('username'), category='error')
        if update:
            raise HTTPFound(location='my_account')
        return htmlfill.render(
            render('admin/my_account/my_account.html'),
            defaults=defaults,
            encoding="UTF-8",
            force_defaults=False)
Exemple #4
0
    def update(self, id):
        """PUT /users/id: Update an existing item"""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="PUT" />
        # Or using helpers:
        #    h.form(url('update_user', id=ID),
        #           method='put')
        # url('user', id=ID)
        user_model = UserModel()
        user = user_model.get(id)
        _form = UserForm(edit=True, old_data={'user_id': id,
                                              'email': user.email})()
        form_result = {}
        try:
            form_result = _form.to_python(dict(request.POST))
            skip_attrs = ['extern_type', 'extern_name',
                         ] + auth_modules.get_managed_fields(user)

            user_model.update(id, form_result, skip_attrs=skip_attrs)
            usr = form_result['username']
            action_logger(self.authuser, 'admin_updated_user:%s' % usr,
                          None, self.ip_addr, self.sa)
            h.flash(_('User updated successfully'), category='success')
            Session().commit()
        except formencode.Invalid as errors:
            defaults = errors.value
            e = errors.error_dict or {}
            defaults.update({
                'create_repo_perm': user_model.has_perm(id,
                                                        'hg.create.repository'),
                'fork_repo_perm': user_model.has_perm(id, 'hg.fork.repository'),
                '_method': 'put'
            })
            return htmlfill.render(
                self._render_edit_profile(user),
                defaults=defaults,
                errors=e,
                prefix_error=False,
                encoding="UTF-8",
                force_defaults=False)
        except Exception:
            log.error(traceback.format_exc())
            h.flash(_('Error occurred during update of user %s') \
                    % form_result.get('username'), category='error')
        raise HTTPFound(location=url('edit_user', id=id))
Exemple #5
0
    def update(self, id):
        user_model = UserModel()
        user = user_model.get(id)
        _form = UserForm(edit=True, old_data={'user_id': id,
                                              'email': user.email})()
        form_result = {}
        try:
            form_result = _form.to_python(dict(request.POST))
            skip_attrs = ['extern_type', 'extern_name',
                         ] + auth_modules.get_managed_fields(user)

            user_model.update(id, form_result, skip_attrs=skip_attrs)
            usr = form_result['username']
            action_logger(request.authuser, 'admin_updated_user:%s' % usr,
                          None, request.ip_addr)
            h.flash(_('User updated successfully'), category='success')
            Session().commit()
        except formencode.Invalid as errors:
            defaults = errors.value
            e = errors.error_dict or {}
            defaults.update({
                'create_repo_perm': user_model.has_perm(id,
                                                        'hg.create.repository'),
                'fork_repo_perm': user_model.has_perm(id, 'hg.fork.repository'),
            })
            return htmlfill.render(
                self._render_edit_profile(user),
                defaults=defaults,
                errors=e,
                prefix_error=False,
                encoding="UTF-8",
                force_defaults=False)
        except Exception:
            log.error(traceback.format_exc())
            h.flash(_('Error occurred during update of user %s') \
                    % form_result.get('username'), category='error')
        raise HTTPFound(location=url('edit_user', id=id))
Exemple #6
0
 def can_change_password(self, user):
     from kallithea.lib import auth_modules
     managed_fields = auth_modules.get_managed_fields(user)
     return 'password' not in managed_fields