diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -1,8 +1,6 @@ syntax: glob *.pyc *.swp -*.ini -Paste* syntax: regexp ^build @@ -16,4 +14,3 @@ syntax: regexp ^test\.db$ ^repositories\.config$ ^RhodeCode\.egg-info$ -^env$ diff --git a/rhodecode/controllers/api/api.py b/rhodecode/controllers/api/api.py --- a/rhodecode/controllers/api/api.py +++ b/rhodecode/controllers/api/api.py @@ -11,6 +11,8 @@ from rhodecode.model.user import UserMod from rhodecode.model.repo_permission import RepositoryPermissionModel from rhodecode.model.users_group import UsersGroupModel from rhodecode.model import users_group +from rhodecode.model.repos_group import ReposGroupModel +from sqlalchemy.orm.exc import NoResultFound log = logging.getLogger( __name__ ) @@ -59,8 +61,9 @@ class ApiController( JSONRPCController ) :param username """ - user = User.by_username( username ) - if not user: + try: + user = User.by_username( username ) + except NoResultFound: return None return dict( id = user.user_id, @@ -109,6 +112,9 @@ class ApiController( JSONRPCController ) :param ldap_dn: """ + if self.get_user( apiuser, username ): + raise JSONRPCError( "user %s already exist" % username ) + try: form_data = dict( username = username, password = password, @@ -191,6 +197,9 @@ class ApiController( JSONRPCController ) :param active: """ + if self.get_users_group( apiuser, name ): + raise JSONRPCError( "users group %s already exist" % name ) + try: form_data = dict( users_group_name = name, users_group_active = active ) @@ -216,8 +225,9 @@ class ApiController( JSONRPCController ) if not users_group: raise JSONRPCError( 'unknown users group %s' % group_name ) - user = User.by_username( user_name ) - if not user: + try: + user = User.by_username( user_name ) + except NoResultFound: raise JSONRPCError( 'unknown user %s' % user_name ) ugm = UsersGroupModel().add_user_to_group( users_group, user ) @@ -237,8 +247,9 @@ class ApiController( JSONRPCController ) :param repo_name """ - repo = Repository.by_repo_name( repo_name ) - if not repo: + try: + repo = Repository.by_repo_name( repo_name ) + except NoResultFound: return None members = [] @@ -287,8 +298,8 @@ class ApiController( JSONRPCController ) return result @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' ) - def create_repo( self, apiuser, name, owner_name, description = None, repo_type = 'hg', \ - private = False, group_name = None ): + def create_repo( self, apiuser, name, owner_name, description = '', repo_type = 'hg', \ + private = False ): """ Create a repository @@ -298,29 +309,36 @@ class ApiController( JSONRPCController ) :param type :param private :param owner_name - :param group_name - :param clone """ try: - if group_name: - group = Group.get_by_group_name( group_name ) - if group is None: - raise JSONRPCError( 'unknown group %s' % group_name ) - else: - group = None - - owner = User.by_username( owner_name ) - if owner is None: + try: + owner = User.by_username( owner_name ) + except NoResultFound: raise JSONRPCError( 'unknown user %s' % owner ) - RepoModel().create( { "repo_name" : name, - "repo_name_full" : name, - "description" : description, - "private" : private, - "repo_type" : repo_type, - "repo_group" : group, - "clone_uri" : None }, owner ) + if self.get_repo( apiuser, name ): + raise JSONRPCError( "repo %s already exist" % name ) + + groups = name.split( '/' ) + real_name = groups[-1] + groups = groups[:-1] + parent_id = None + for g in groups: + group = Group.get_by_group_name( g ) + if not group: + group = ReposGroupModel().create( dict( group_name = g, + group_description = '', + group_parent_id = parent_id ) ) + parent_id = group.group_id + + RepoModel().create( dict( repo_name = real_name, + repo_name_full = name, + description = description, + private = private, + repo_type = repo_type, + repo_group = parent_id, + clone_uri = None ), owner ) except Exception: log.error( traceback.format_exc() ) raise JSONRPCError( 'failed to create repository %s' % name ) @@ -337,12 +355,14 @@ class ApiController( JSONRPCController ) """ try: - repo = Repository.by_repo_name( repo_name ) - if not repo: + try: + repo = Repository.by_repo_name( repo_name ) + except NoResultFound: raise JSONRPCError( 'unknown repository %s' % repo ) - user = User.by_username( user_name ) - if not user: + try: + user = User.by_username( user_name ) + except NoResultFound: raise JSONRPCError( 'unknown user %s' % user ) RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm ) diff --git a/rhodecode/model/repo_permission.py b/rhodecode/model/repo_permission.py --- a/rhodecode/model/repo_permission.py +++ b/rhodecode/model/repo_permission.py @@ -27,18 +27,18 @@ import logging from rhodecode.model.db import BaseModel, RepoToPerm, Permission from rhodecode.model.meta import Session -log = logging.getLogger(__name__) +log = logging.getLogger( __name__ ) -class RepositoryPermissionModel(BaseModel): - def getUserPermission(self, repository, user): +class RepositoryPermissionModel( BaseModel ): + def getUserPermission( self, repository, user ): return RepoToPerm.query() \ - .filter(RepoToPerm.user == user) \ - .filter(RepoToPerm.repository == repository) \ + .filter( RepoToPerm.user == user ) \ + .filter( RepoToPerm.repository == repository ) \ .scalar() - def updateUserPermission(self, repository, user, permission): - permission = Permission.get_by_key(permission) - current = self.getUserPermission(repository, user) + def updateUserPermission( self, repository, user, permission ): + permission = Permission.get_by_key( permission ) + current = self.getUserPermission( repository, user ) if current: if not current.permission is permission: current.permission = permission @@ -47,16 +47,17 @@ class RepositoryPermissionModel(BaseMode p.user = user p.repository = repository p.permission = permission - Session.add(p) + Session.add( p ) Session.commit() - def deleteUserPermission(self, repository, user): - current = self.getUserPermission(repository, user) - Session.delete(current) - Session.commit() + def deleteUserPermission( self, repository, user ): + current = self.getUserPermission( repository, user ) + if current: + Session.delete( current ) + Session.commit() - def updateOrDeleteUserPermission(self, repository, user, permission): + def updateOrDeleteUserPermission( self, repository, user, permission ): if permission: - self.updateUserPermission(repository, user, permission) + self.updateUserPermission( repository, user, permission ) else: - self.deleteUserPermission(repository, user) \ No newline at end of file + self.deleteUserPermission( repository, user ) diff --git a/rhodecode/model/repos_group.py b/rhodecode/model/repos_group.py --- a/rhodecode/model/repos_group.py +++ b/rhodecode/model/repos_group.py @@ -36,21 +36,21 @@ from rhodecode.model import BaseModel from rhodecode.model.caching_query import FromCache from rhodecode.model.db import Group, RhodeCodeUi -log = logging.getLogger(__name__) +log = logging.getLogger( __name__ ) -class ReposGroupModel(BaseModel): +class ReposGroupModel( BaseModel ): @LazyProperty - def repos_path(self): + def repos_path( self ): """ Get's the repositories root path from database """ - q = RhodeCodeUi.get_by_key('/').one() + q = RhodeCodeUi.get_by_key( '/' ).one() return q.ui_value - def __create_group(self, group_name, parent_id): + def __create_group( self, group_name, parent_id ): """ makes repositories group on filesystem @@ -59,64 +59,64 @@ class ReposGroupModel(BaseModel): """ if parent_id: - paths = Group.get(parent_id).full_path.split(Group.url_sep()) - parent_path = os.sep.join(paths) + paths = Group.get( parent_id ).full_path.split( Group.url_sep() ) + parent_path = os.sep.join( paths ) else: parent_path = '' - create_path = os.path.join(self.repos_path, parent_path, group_name) - log.debug('creating new group in %s', create_path) + create_path = os.path.join( self.repos_path, parent_path, group_name ) + log.debug( 'creating new group in %s', create_path ) - if os.path.isdir(create_path): - raise Exception('That directory already exists !') + if os.path.isdir( create_path ): + raise Exception( 'That directory already exists !' ) - os.makedirs(create_path) + os.makedirs( create_path ) - def __rename_group(self, old, old_parent_id, new, new_parent_id): + def __rename_group( self, old, old_parent_id, new, new_parent_id ): """ Renames a group on filesystem :param group_name: """ - log.debug('renaming repos group from %s to %s', old, new) + log.debug( 'renaming repos group from %s to %s', old, new ) if new_parent_id: - paths = Group.get(new_parent_id).full_path.split(Group.url_sep()) - new_parent_path = os.sep.join(paths) + paths = Group.get( new_parent_id ).full_path.split( Group.url_sep() ) + new_parent_path = os.sep.join( paths ) else: new_parent_path = '' if old_parent_id: - paths = Group.get(old_parent_id).full_path.split(Group.url_sep()) - old_parent_path = os.sep.join(paths) + paths = Group.get( old_parent_id ).full_path.split( Group.url_sep() ) + old_parent_path = os.sep.join( paths ) else: old_parent_path = '' - old_path = os.path.join(self.repos_path, old_parent_path, old) - new_path = os.path.join(self.repos_path, new_parent_path, new) + old_path = os.path.join( self.repos_path, old_parent_path, old ) + new_path = os.path.join( self.repos_path, new_parent_path, new ) - log.debug('renaming repos paths from %s to %s', old_path, new_path) + log.debug( 'renaming repos paths from %s to %s', old_path, new_path ) - if os.path.isdir(new_path): - raise Exception('Was trying to rename to already ' - 'existing dir %s' % new_path) - shutil.move(old_path, new_path) + if os.path.isdir( new_path ): + raise Exception( 'Was trying to rename to already ' + 'existing dir %s' % new_path ) + shutil.move( old_path, new_path ) - def __delete_group(self, group): + def __delete_group( self, group ): """ Deletes a group from a filesystem :param group: instance of group from database """ - paths = group.full_path.split(Group.url_sep()) - paths = os.sep.join(paths) + paths = group.full_path.split( Group.url_sep() ) + paths = os.sep.join( paths ) - rm_path = os.path.join(self.repos_path, paths) - os.rmdir(rm_path) + rm_path = os.path.join( self.repos_path, paths ) + os.rmdir( rm_path ) - def create(self, form_data): + def create( self, form_data ): try: new_repos_group = Group() new_repos_group.group_name = form_data['group_name'] @@ -124,21 +124,22 @@ class ReposGroupModel(BaseModel): form_data['group_description'] new_repos_group.group_parent_id = form_data['group_parent_id'] - self.sa.add(new_repos_group) + self.sa.add( new_repos_group ) - self.__create_group(form_data['group_name'], - form_data['group_parent_id']) + self.__create_group( form_data['group_name'], + form_data['group_parent_id'] ) self.sa.commit() + return new_repos_group except: - log.error(traceback.format_exc()) + log.error( traceback.format_exc() ) self.sa.rollback() raise - def update(self, repos_group_id, form_data): + def update( self, repos_group_id, form_data ): try: - repos_group = Group.get(repos_group_id) + repos_group = Group.get( repos_group_id ) old_name = repos_group.group_name old_parent_id = repos_group.group_parent_id @@ -147,27 +148,27 @@ class ReposGroupModel(BaseModel): form_data['group_description'] repos_group.group_parent_id = form_data['group_parent_id'] - self.sa.add(repos_group) + self.sa.add( repos_group ) - if old_name != form_data['group_name'] or (old_parent_id != - form_data['group_parent_id']): - self.__rename_group(old=old_name, old_parent_id=old_parent_id, - new=form_data['group_name'], - new_parent_id=form_data['group_parent_id']) + if old_name != form_data['group_name'] or ( old_parent_id != + form_data['group_parent_id'] ): + self.__rename_group( old = old_name, old_parent_id = old_parent_id, + new = form_data['group_name'], + new_parent_id = form_data['group_parent_id'] ) self.sa.commit() except: - log.error(traceback.format_exc()) + log.error( traceback.format_exc() ) self.sa.rollback() raise - def delete(self, users_group_id): + def delete( self, users_group_id ): try: - users_group = Group.get(users_group_id) - self.sa.delete(users_group) - self.__delete_group(users_group) + users_group = Group.get( users_group_id ) + self.sa.delete( users_group ) + self.__delete_group( users_group ) self.sa.commit() except: - log.error(traceback.format_exc()) + log.error( traceback.format_exc() ) self.sa.rollback() raise diff --git a/rhodecode/model/users_group.py b/rhodecode/model/users_group.py --- a/rhodecode/model/users_group.py +++ b/rhodecode/model/users_group.py @@ -67,6 +67,11 @@ class UsersGroupModel( BaseModel ): raise def add_user_to_group( self, users_group, user ): + for m in users_group.members: + u = m.user + if u.user_id == user.user_id: + return m + try: users_group_member = UsersGroupMember() users_group_member.user = user