diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -37,7 +37,7 @@ from rhodecode.model import meta from rhodecode.model.user import UserModel from rhodecode.model.repo import RepoModel from rhodecode.model.users_group import UsersGroupModel -from rhodecode.model.db import User +from rhodecode.model.db import User, UsersGroup from rhodecode import BACKENDS from webhelpers.pylonslib.secure_form import authentication_token @@ -238,7 +238,8 @@ def ValidForkType(old_data): return _ValidForkType class ValidPerms(formencode.validators.FancyValidator): - messages = {'perm_new_user_name':_('This username is not valid')} + messages = {'perm_new_member_name':_('This username or users group name' + ' is not valid')} def to_python(self, value, state): perms_update = [] @@ -246,32 +247,42 @@ class ValidPerms(formencode.validators.F #build a list of permission to update and new permission to create for k, v in value.items(): if k.startswith('perm_'): - if k.startswith('perm_new_user'): - new_perm = value.get('perm_new_user', False) - new_user = value.get('perm_new_user_name', False) - if new_user and new_perm: - if (new_user, new_perm) not in perms_new: - perms_new.append((new_user, new_perm)) + if k.startswith('perm_new_member'): + #means new added member to permissions + new_perm = value.get('perm_new_member', False) + new_member = value.get('perm_new_member_name', False) + new_type = value.get('perm_new_member_type') + + if new_member and new_perm: + if (new_member, new_perm) not in perms_new: + perms_new.append((new_member, new_perm, new_type)) else: usr = k[5:] + t = 'user' if usr == 'default': if value['private']: #set none for default when updating to private repo v = 'repository.none' - perms_update.append((usr, v)) + perms_update.append((usr, v, t)) value['perms_updates'] = perms_update value['perms_new'] = perms_new sa = meta.Session - for k, v in perms_new: + for k, v, t in perms_new: try: - self.user_db = sa.query(User)\ - .filter(User.active == True)\ - .filter(User.username == k).one() + if t is 'user': + self.user_db = sa.query(User)\ + .filter(User.active == True)\ + .filter(User.username == k).one() + if t is 'users_group': + self.user_db = sa.query(UsersGroup)\ + .filter(UsersGroup.users_group_active == True)\ + .filter(UsersGroup.users_group_name == k).one() + except Exception: - msg = self.message('perm_new_user_name', + msg = self.message('perm_new_member_name', state=State_obj) raise formencode.Invalid(msg, value, state, - error_dict={'perm_new_user_name':msg}) + error_dict={'perm_new_member_name':msg}) return value class ValidSettings(formencode.validators.FancyValidator): diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -30,12 +30,10 @@ import logging import traceback from datetime import datetime -from pylons import app_globals as g - from rhodecode.model import BaseModel from rhodecode.model.caching_query import FromCache from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \ - Statistics + Statistics, UsersGroup from rhodecode.model.user import UserModel from vcs.backends import get_backend @@ -44,6 +42,24 @@ log = logging.getLogger(__name__) class RepoModel(BaseModel): + def __init__(self, sa=None): + try: + from pylons import app_globals + self._base_path = app_globals.base_path + except: + self._base_path = None + + @property + def base_path(): + if self._base_path is None: + raise Exception('Base Path is empty, try set this after' + 'class initialization when not having ' + 'app_globals available') + return self._base_path + + super(RepoModel, self).__init__() + + def get(self, repo_id, cache=False): repo = self.sa.query(Repository)\ .filter(Repository.repo_id == repo_id) @@ -67,19 +83,31 @@ class RepoModel(BaseModel): users = self.sa.query(User).filter(User.active == True).all() u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},''' - users_array = '[%s];' % '\n'.join([u_tmpl % (u.user_id, u.name, + users_array = '[%s]' % '\n'.join([u_tmpl % (u.user_id, u.name, u.lastname, u.username) for u in users]) return users_array + def get_users_groups_js(self): + users_groups = self.sa.query(UsersGroup)\ + .filter(UsersGroup.users_group_active == True).all() + + g_tmpl = '''{id:%s, grname:"%s",grmembers:"%s"},''' + + users_groups_array = '[%s]' % '\n'.join([g_tmpl % \ + (gr.users_group_id, gr.users_group_name, + len(gr.members)) + for gr in users_groups]) + return users_groups_array + def update(self, repo_name, form_data): try: cur_repo = self.get_by_repo_name(repo_name, cache=False) user_model = UserModel(self.sa) #update permissions - for username, perm in form_data['perms_updates']: + for username, perm, member_type in form_data['perms_updates']: r2p = self.sa.query(RepoToPerm)\ .filter(RepoToPerm.user == user_model.get_by_username(username))\ .filter(RepoToPerm.repository == cur_repo)\ @@ -91,7 +119,7 @@ class RepoModel(BaseModel): self.sa.add(r2p) #set new permissions - for username, perm in form_data['perms_new']: + for username, perm, member_type in form_data['perms_new']: r2p = RepoToPerm() r2p.repository = cur_repo r2p.user = user_model.get_by_username(username, cache=False) @@ -223,8 +251,8 @@ class RepoModel(BaseModel): :param alias: """ from rhodecode.lib.utils import check_repo - repo_path = os.path.join(g.base_path, repo_name) - if check_repo(repo_name, g.base_path): + repo_path = os.path.join(self.base_path, repo_name) + if check_repo(repo_name, self.base_path): log.info('creating repo %s in %s', repo_name, repo_path) backend = get_backend(alias) backend(repo_path, create=True) @@ -237,8 +265,8 @@ class RepoModel(BaseModel): """ log.info('renaming repo from %s to %s', old, new) - old_path = os.path.join(g.base_path, old) - new_path = os.path.join(g.base_path, new) + old_path = os.path.join(self.base_path, old) + new_path = os.path.join(self.base_path, new) if os.path.isdir(new_path): raise Exception('Was trying to rename to already existing dir %s', new_path) @@ -252,12 +280,12 @@ class RepoModel(BaseModel): by reverting the renames on this repository :param repo: repo object """ - rm_path = os.path.join(g.base_path, repo.repo_name) + rm_path = os.path.join(self.base_path, repo.repo_name) log.info("Removing %s", rm_path) #disable hg/git alias = repo.repo_type shutil.move(os.path.join(rm_path, '.%s' % alias), os.path.join(rm_path, 'rm__.%s' % alias)) #disable repo - shutil.move(rm_path, os.path.join(g.base_path, 'rm__%s__%s' \ + shutil.move(rm_path, os.path.join(self.base_path, 'rm__%s__%s' \ % (datetime.today(), repo.repo_name)))