diff --git a/rhodecode/controllers/admin/users.py b/rhodecode/controllers/admin/users.py --- a/rhodecode/controllers/admin/users.py +++ b/rhodecode/controllers/admin/users.py @@ -41,8 +41,8 @@ from rhodecode.lib.auth import LoginRequ AuthUser from rhodecode.lib.base import BaseController, render -from rhodecode.model.db import User, UserEmailMap, UserIpMap -from rhodecode.model.forms import UserForm +from rhodecode.model.db import User, UserEmailMap, UserIpMap, UserToPerm +from rhodecode.model.forms import UserForm, CustomDefaultPermissionsForm from rhodecode.model.user import UserModel from rhodecode.model.meta import Session from rhodecode.lib.utils import action_logger @@ -240,12 +240,13 @@ class UsersController(BaseController): .filter(UserEmailMap.user == c.user).all() c.user_ip_map = UserIpMap.query()\ .filter(UserIpMap.user == c.user).all() - user_model = UserModel() + umodel = UserModel() c.ldap_dn = c.user.ldap_dn defaults = c.user.get_dict() defaults.update({ - 'create_repo_perm': user_model.has_perm(id, 'hg.create.repository'), - 'fork_repo_perm': user_model.has_perm(id, 'hg.fork.repository'), + 'create_repo_perm': umodel.has_perm(c.user, 'hg.create.repository'), + 'create_user_group_perm': umodel.has_perm(c.user, 'hg.usergroup.create.true'), + 'fork_repo_perm': umodel.has_perm(c.user, 'hg.fork.repository'), }) return htmlfill.render( @@ -258,39 +259,36 @@ class UsersController(BaseController): def update_perm(self, id): """PUT /users_perm/id: Update an existing item""" # url('user_perm', id=ID, method='put') - usr = User.get_or_404(id) - grant_create_perm = str2bool(request.POST.get('create_repo_perm')) - grant_fork_perm = str2bool(request.POST.get('fork_repo_perm')) - inherit_perms = str2bool(request.POST.get('inherit_default_permissions')) - - user_model = UserModel() + user = User.get_or_404(id) try: - usr.inherit_default_permissions = inherit_perms - Session().add(usr) + form = CustomDefaultPermissionsForm()() + form_result = form.to_python(request.POST) + + inherit_perms = form_result['inherit_default_permissions'] + user.inherit_default_permissions = inherit_perms + Session().add(user) + user_model = UserModel() - if grant_create_perm: - user_model.revoke_perm(usr, 'hg.create.none') - user_model.grant_perm(usr, 'hg.create.repository') - h.flash(_("Granted 'repository create' permission to user"), - category='success') + defs = UserToPerm.query()\ + .filter(UserToPerm.user == user)\ + .all() + for ug in defs: + Session().delete(ug) + + if form_result['create_repo_perm']: + user_model.grant_perm(id, 'hg.create.repository') else: - user_model.revoke_perm(usr, 'hg.create.repository') - user_model.grant_perm(usr, 'hg.create.none') - h.flash(_("Revoked 'repository create' permission to user"), - category='success') - - if grant_fork_perm: - user_model.revoke_perm(usr, 'hg.fork.none') - user_model.grant_perm(usr, 'hg.fork.repository') - h.flash(_("Granted 'repository fork' permission to user"), - category='success') + user_model.grant_perm(id, 'hg.create.none') + if form_result['create_user_group_perm']: + user_model.grant_perm(id, 'hg.usergroup.create.true') else: - user_model.revoke_perm(usr, 'hg.fork.repository') - user_model.grant_perm(usr, 'hg.fork.none') - h.flash(_("Revoked 'repository fork' permission to user"), - category='success') - + user_model.grant_perm(id, 'hg.usergroup.create.false') + if form_result['fork_repo_perm']: + user_model.grant_perm(id, 'hg.fork.repository') + else: + user_model.grant_perm(id, 'hg.fork.none') + h.flash(_("Updated permissions"), category='success') Session().commit() except Exception: log.error(traceback.format_exc()) diff --git a/rhodecode/controllers/admin/users_groups.py b/rhodecode/controllers/admin/users_groups.py --- a/rhodecode/controllers/admin/users_groups.py +++ b/rhodecode/controllers/admin/users_groups.py @@ -43,7 +43,8 @@ from rhodecode.model.users_group import from rhodecode.model.repo import RepoModel from rhodecode.model.db import User, UserGroup, UserGroupToPerm,\ UserGroupRepoToPerm, UserGroupRepoGroupToPerm -from rhodecode.model.forms import UserGroupForm, UserGroupPermsForm +from rhodecode.model.forms import UserGroupForm, UserGroupPermsForm,\ + CustomDefaultPermissionsForm from rhodecode.model.meta import Session from rhodecode.lib.utils import action_logger from sqlalchemy.orm import joinedload @@ -113,6 +114,8 @@ class UsersGroupsController(BaseControll data.update({ 'create_repo_perm': ug_model.has_perm(user_group, 'hg.create.repository'), + 'create_user_group_perm': ug_model.has_perm(user_group, + 'hg.usergroup.create.true'), 'fork_repo_perm': ug_model.has_perm(user_group, 'hg.fork.repository'), }) @@ -326,38 +329,36 @@ class UsersGroupsController(BaseControll # url('users_group_perm', id=ID, method='put') users_group = UserGroup.get_or_404(id) - grant_create_perm = str2bool(request.POST.get('create_repo_perm')) - grant_fork_perm = str2bool(request.POST.get('fork_repo_perm')) - inherit_perms = str2bool(request.POST.get('inherit_default_permissions')) - - usergroup_model = UserGroupModel() try: + form = CustomDefaultPermissionsForm()() + form_result = form.to_python(request.POST) + + inherit_perms = form_result['inherit_default_permissions'] users_group.inherit_default_permissions = inherit_perms Session().add(users_group) + usergroup_model = UserGroupModel() - if grant_create_perm: - usergroup_model.revoke_perm(id, 'hg.create.none') - usergroup_model.grant_perm(id, 'hg.create.repository') - h.flash(_("Granted 'repository create' permission to user group"), - category='success') - else: - usergroup_model.revoke_perm(id, 'hg.create.repository') - usergroup_model.grant_perm(id, 'hg.create.none') - h.flash(_("Revoked 'repository create' permission to user group"), - category='success') + defs = UserGroupToPerm.query()\ + .filter(UserGroupToPerm.users_group == users_group)\ + .all() + for ug in defs: + Session().delete(ug) - if grant_fork_perm: - usergroup_model.revoke_perm(id, 'hg.fork.none') - usergroup_model.grant_perm(id, 'hg.fork.repository') - h.flash(_("Granted 'repository fork' permission to user group"), - category='success') + if form_result['create_repo_perm']: + usergroup_model.grant_perm(id, 'hg.create.repository') + else: + usergroup_model.grant_perm(id, 'hg.create.none') + if form_result['create_user_group_perm']: + usergroup_model.grant_perm(id, 'hg.usergroup.create.true') else: - usergroup_model.revoke_perm(id, 'hg.fork.repository') + usergroup_model.grant_perm(id, 'hg.usergroup.create.false') + if form_result['fork_repo_perm']: + usergroup_model.grant_perm(id, 'hg.fork.repository') + else: usergroup_model.grant_perm(id, 'hg.fork.none') - h.flash(_("Revoked 'repository fork' permission to user group"), - category='success') + h.flash(_("Updated permissions"), category='success') Session().commit() except Exception: log.error(traceback.format_exc()) diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -334,6 +334,21 @@ def DefaultPermissionsForm(repo_perms_ch return _DefaultPermissionsForm +def CustomDefaultPermissionsForm(): + class _CustomDefaultPermissionsForm(formencode.Schema): + filter_extra_fields = True + allow_extra_fields = True + inherit_default_permissions = v.StringBoolean(if_missing=False) + + create_repo_perm = v.StringBoolean(if_missing=False) + create_user_group_perm = v.StringBoolean(if_missing=False) + #create_repo_group_perm Impl. later + + fork_repo_perm = v.StringBoolean(if_missing=False) + + return _CustomDefaultPermissionsForm + + def DefaultsForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()): class _DefaultsForm(formencode.Schema): allow_extra_fields = True diff --git a/rhodecode/model/user.py b/rhodecode/model/user.py --- a/rhodecode/model/user.py +++ b/rhodecode/model/user.py @@ -524,8 +524,11 @@ class UserModel(BaseModel): # !! OVERRIDE GLOBALS !! with user permissions if any found #====================================================================== # those can be configured from groups or users explicitly - _configurable = set(['hg.fork.none', 'hg.fork.repository', - 'hg.create.none', 'hg.create.repository']) + _configurable = set([ + 'hg.fork.none', 'hg.fork.repository', + 'hg.create.none', 'hg.create.repository', + 'hg.usergroup.create.false', 'hg.usergroup.create.true' + ]) # USER GROUPS comes first # user group global permissions @@ -565,6 +568,8 @@ class UserModel(BaseModel): for perm in user_perms: user.permissions[GLOBAL].add(perm.permission.permission_name) + ## END GLOBAL PERMISSIONS + #====================================================================== # !! PERMISSIONS FOR REPOSITORIES !! diff --git a/rhodecode/templates/admin/users/user_edit.html b/rhodecode/templates/admin/users/user_edit.html --- a/rhodecode/templates/admin/users/user_edit.html +++ b/rhodecode/templates/admin/users/user_edit.html @@ -149,45 +149,8 @@