Changeset - 39bac9410169
[Not reviewed]
kallithea/controllers/admin/my_account.py
Show inline comments
 
@@ -34,12 +34,13 @@ from formencode import htmlfill
 
from pylons import request, tmpl_context as c, url
 
from pylons.controllers.util import redirect
 
from pylons.i18n.translation import _
 

	
 
from kallithea import EXTERN_TYPE_INTERNAL
 
from kallithea.lib import helpers as h
 
from kallithea.lib import auth_modules
 
from kallithea.lib.auth import LoginRequired, NotAnonymous, AuthUser
 
from kallithea.lib.base import BaseController, render
 
from kallithea.lib.utils2 import generate_api_key, safe_int
 
from kallithea.lib.compat import json
 
from kallithea.model.db import Repository, \
 
    UserEmailMap, UserApiKeys, User, UserFollowing
 
@@ -97,12 +98,14 @@ class MyAccountController(BaseController
 
        """
 
        # 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)
 
        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,
 
@@ -112,18 +115,14 @@ class MyAccountController(BaseController
 
                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']
 
                #TODO: plugin should define if username can be updated
 
                if c.user.extern_type != EXTERN_TYPE_INTERNAL:
 
                    # forbid updating username for external accounts
 
                    # TODO: also skip username (and email etc) if self registration not enabled
 
                    skip_attrs.append('username')
 
                              '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()
kallithea/controllers/admin/users.py
Show inline comments
 
@@ -39,13 +39,12 @@ from webob.exc import HTTPNotFound
 
import kallithea
 
from kallithea.lib.exceptions import DefaultUserException, \
 
    UserOwnsReposException, UserCreationError
 
from kallithea.lib import helpers as h
 
from kallithea.lib.auth import LoginRequired, HasPermissionAllDecorator, \
 
    AuthUser
 
import kallithea.lib.auth_modules.auth_internal
 
from kallithea.lib import auth_modules
 
from kallithea.lib.base import BaseController, render
 
from kallithea.model.api_key import ApiKeyModel
 

	
 
from kallithea.model.db import User, UserEmailMap, UserIpMap, UserToPerm
 
from kallithea.model.forms import UserForm, CustomDefaultPermissionsForm
 
@@ -172,17 +171,14 @@ class UsersController(BaseController):
 
        c.ip_addr = self.ip_addr
 
        _form = UserForm(edit=True, old_data={'user_id': id,
 
                                              'email': c.user.email})()
 
        form_result = {}
 
        try:
 
            form_result = _form.to_python(dict(request.POST))
 
            skip_attrs = ['extern_type', 'extern_name']
 
            #TODO: plugin should define if username can be updated
 
            if c.user.extern_type != kallithea.EXTERN_TYPE_INTERNAL:
 
                # forbid updating username for external accounts
 
                skip_attrs.append('username')
 
            skip_attrs = ['extern_type', 'extern_name',
 
                         ] + auth_modules.get_managed_fields(c.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')
 
@@ -246,12 +242,14 @@ class UsersController(BaseController):
 
        """GET /users/id/edit: Form to edit an existing item"""
 
        # url('edit_user', id=ID)
 
        c.user = self._get_user_or_raise_if_default(id)
 
        c.active = 'profile'
 
        c.perm_user = AuthUser(user_id=id)
 
        c.ip_addr = self.ip_addr
 
        managed_fields = auth_modules.get_managed_fields(c.user)
 
        c.readonly = lambda n: 'readonly' if n in managed_fields else None
 

	
 
        defaults = c.user.get_dict()
 
        return htmlfill.render(
 
            render('admin/users/user_edit.html'),
 
            defaults=defaults,
 
            encoding="UTF-8",
kallithea/lib/auth_modules/__init__.py
Show inline comments
 
@@ -413,6 +413,19 @@ def authenticate(username, password, env
 

	
 
        # we failed to Auth because .auth() method didn't return the user
 
        if username:
 
            log.warning("User `%s` failed to authenticate against %s"
 
                        % (username, plugin.__module__))
 
    return None
 

	
 
def get_managed_fields(user):
 
    """return list of fields that are managed by the user's auth source, usually some of
 
    'username', 'firstname', 'lastname', 'email', 'active', 'password'
 
    """
 
    auth_plugins = Setting.get_auth_plugins()
 
    for module in auth_plugins:
 
        log.debug('testing %s (%s) with auth plugin %s', user, user.extern_type, module)
 
        plugin = loadplugin(module)
 
        if plugin.name == user.extern_type:
 
            return plugin.get_managed_fields()
 
    log.error('no auth plugin %s found for %s', user.extern_type, user)
 
    return [] # TODO: Fail badly instead of allowing everything to be edited?
kallithea/lib/auth_modules/auth_container.py
Show inline comments
 
@@ -187,6 +187,9 @@ class KallitheaAuthPlugin(auth_modules.K
 
            'active_from_extern': True,
 
            'extern_name': username,
 
        }
 

	
 
        log.info('user `%s` authenticated correctly' % user_data['username'])
 
        return user_data
 

	
 
    def get_managed_fields(self):
 
        return ['username', 'password']
kallithea/lib/auth_modules/auth_crowd.py
Show inline comments
 
@@ -226,17 +226,20 @@ class KallitheaAuthPlugin(auth_modules.K
 
            'firstname': crowd_user["first-name"] or firstname,
 
            'lastname': crowd_user["last-name"] or lastname,
 
            'groups': crowd_user["groups"],
 
            'email': crowd_user["email"] or email,
 
            'admin': admin,
 
            'active': active,
 
            'active_from_extern': crowd_user.get('active'),
 
            'active_from_extern': crowd_user.get('active'), # ???
 
            'extern_name': crowd_user["name"],
 
        }
 

	
 
        # set an admin if we're in admin_groups of crowd
 
        for group in settings["admin_groups"].split(","):
 
            if group in user_data["groups"]:
 
                user_data["admin"] = True
 
        log.debug("Final crowd user object: \n%s" % (formatted_json(user_data)))
 
        log.info('user %s authenticated correctly' % user_data['username'])
 
        return user_data
 

	
 
    def get_managed_fields(self):
 
        return ['username', 'firstname', 'lastname', 'email', 'password']
kallithea/lib/auth_modules/auth_internal.py
Show inline comments
 
@@ -94,6 +94,10 @@ class KallitheaAuthPlugin(auth_modules.K
 
                return user_data
 
            log.error("user %s had a bad password" % username)
 
            return None
 
        else:
 
            log.warning('user %s tried auth but is disabled' % username)
 
            return None
 

	
 
    def get_managed_fields(self):
 
        # Note: 'username' should only be editable (at least for user) if self registration is enabled
 
        return []
kallithea/lib/auth_modules/auth_ldap.py
Show inline comments
 
@@ -356,6 +356,9 @@ class KallitheaAuthPlugin(auth_modules.K
 
        except (LdapUsernameError, LdapPasswordError, LdapImportError):
 
            log.error(traceback.format_exc())
 
            return None
 
        except (Exception,):
 
            log.error(traceback.format_exc())
 
            return None
 

	
 
    def get_managed_fields(self):
 
        return ['username', 'firstname', 'lastname', 'email', 'password']
kallithea/lib/auth_modules/auth_pam.py
Show inline comments
 
@@ -133,6 +133,9 @@ class KallitheaAuthPlugin(auth_modules.K
 
            log.warning("Cannot extract additional info for PAM user %s", username)
 
            pass
 

	
 
        log.debug("pamuser: \n%s" % formatted_json(user_data))
 
        log.info('user %s authenticated correctly' % user_data['username'])
 
        return user_data
 

	
 
    def get_managed_fields(self):
 
        return ['username', 'password']
kallithea/templates/admin/my_account/my_account_profile.html
Show inline comments
 
@@ -16,52 +16,49 @@ ${h.form(url('my_account'), method='post
 
                    [${_('Current IP')}: ${c.ip_addr}]
 
                %endif
 
               </p>
 
           </div>
 
         </div>
 

	
 
        <% readonly = None %>
 
        <div class="fields">
 
            %if c.user.extern_type != c.EXTERN_TYPE_INTERNAL:
 
                <% readonly = "readonly" %>
 
                <strong>${_('Your user is in an external Source of Record; some details cannot be managed here')}.</strong>
 
            %endif
 
             <div class="field">
 
                <div class="label">
 
                    <label for="username">${_('Username')}:</label>
 
                </div>
 
                <div class="input">
 
                  ${h.text('username',class_='medium', readonly=readonly)}
 
                  ${h.text('username',class_='medium', readonly=c.readonly('username'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="name">${_('First Name')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('firstname',class_="medium")}
 
                    ${h.text('firstname',class_="medium", readonly=c.readonly('firstname'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="lastname">${_('Last Name')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('lastname',class_="medium")}
 
                    ${h.text('lastname',class_="medium", readonly=c.readonly('lastname'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="email">${_('Email')}:</label>
 
                </div>
 
                <div class="input">
 
                    ## we should be able to edit email !
 
                    ${h.text('email',class_="medium")}
 
                    ${h.text('email',class_="medium", readonly=c.readonly('email'))}
 
                </div>
 
             </div>
 

	
 
            <div class="buttons">
 
              ${h.submit('save',_('Save'),class_="btn")}
 
              ${h.reset('reset',_('Reset'),class_="btn")}
kallithea/templates/admin/users/user_edit_profile.html
Show inline comments
 
@@ -14,36 +14,34 @@ ${h.form(url('update_user', id=c.user.us
 
                        %if c.authuser.username == c.user.username:
 
                            [${_('Current IP')}: ${c.ip_addr}]
 
                        %endif
 
                %endif
 
           </div>
 
        </div>
 
        <% readonly = None %>
 
        <div class="fields">
 
            %if c.user.extern_type != c.EXTERN_TYPE_INTERNAL:
 
             <div class="field">
 
               <% readonly = "readonly" %>
 
               <strong>${_('This user is in an external Source of Record (%s); some details cannot be managed here.' % c.user.extern_type)}.</strong>
 
             </div>
 
            %endif
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="username">${_('Username')}:</label>
 
                </div>
 
                <div class="input">
 
                  ${h.text('username',class_='medium', readonly=readonly)}
 
                  ${h.text('username',class_='medium', readonly=c.readonly('username'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="email">${_('Email')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('email',class_='medium')}
 
                    ${h.text('email',class_='medium', readonly=c.readonly('email'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="extern_type">${_('Source of Record')}:</label>
 
@@ -64,58 +62,58 @@ ${h.form(url('update_user', id=c.user.us
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="new_password">${_('New password')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.password('new_password',class_='medium',readonly=readonly)}
 
                    ${h.password('new_password',class_='medium',readonly=c.readonly('password'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="password_confirmation">${_('New password confirmation')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.password('password_confirmation',class_="medium",readonly=readonly)}
 
                    ${h.password('password_confirmation',class_="medium",readonly=c.readonly('password'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="firstname">${_('First Name')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('firstname',class_='medium')}
 
                    ${h.text('firstname',class_='medium', readonly=c.readonly('firstname'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label">
 
                    <label for="lastname">${_('Last Name')}:</label>
 
                </div>
 
                <div class="input">
 
                    ${h.text('lastname',class_='medium')}
 
                    ${h.text('lastname',class_='medium', readonly=c.readonly('lastname'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label label-checkbox">
 
                    <label for="active">${_('Active')}:</label>
 
                </div>
 
                <div class="checkboxes">
 
                    ${h.checkbox('active',value=True)}
 
                    ${h.checkbox('active',value=True, readonly=c.readonly('active'))}
 
                </div>
 
             </div>
 

	
 
             <div class="field">
 
                <div class="label label-checkbox">
 
                    <label for="admin">${_('Admin')}:</label>
 
                </div>
 
                <div class="checkboxes">
 
                    ${h.checkbox('admin',value=True)}
 
                    ${h.checkbox('admin',value=True, readonly=c.readonly('admin'))}
 
                </div>
 
             </div>
 

	
 
            <div class="buttons">
 
              ${h.submit('save',_('Save'),class_="btn")}
 
              ${h.reset('reset',_('Reset'),class_="btn")}
0 comments (0 inline, 0 general)