Beispiel #1
0
 def update_user_permission(self, repository, user, permission):
     permission = Permission.get_by_key(permission)
     current = self.get_user_permission(repository, user)
     if current:
         if not current.permission is permission:
             current.permission = permission
     else:
         p = RepoToPerm()
         p.user = user
         p.repository = repository
         p.permission = permission
         Session.add(p)
     Session.commit()
Beispiel #2
0
    def update(self, repo_name, form_data):
        try:
            cur_repo = self.get_by_repo_name(repo_name, cache=False)

            # update permissions
            for member, perm, member_type in form_data["perms_updates"]:
                if member_type == "user":
                    r2p = (
                        self.sa.query(RepoToPerm)
                        .filter(RepoToPerm.user == User.by_username(member))
                        .filter(RepoToPerm.repository == cur_repo)
                        .one()
                    )

                    r2p.permission = self.sa.query(Permission).filter(Permission.permission_name == perm).scalar()
                    self.sa.add(r2p)
                else:
                    g2p = (
                        self.sa.query(UsersGroupRepoToPerm)
                        .filter(UsersGroupRepoToPerm.users_group == UsersGroup.get_by_group_name(member))
                        .filter(UsersGroupRepoToPerm.repository == cur_repo)
                        .one()
                    )

                    g2p.permission = self.sa.query(Permission).filter(Permission.permission_name == perm).scalar()
                    self.sa.add(g2p)

            # set new permissions
            for member, perm, member_type in form_data["perms_new"]:
                if member_type == "user":
                    r2p = RepoToPerm()
                    r2p.repository = cur_repo
                    r2p.user = User.by_username(member)

                    r2p.permission = self.sa.query(Permission).filter(Permission.permission_name == perm).scalar()
                    self.sa.add(r2p)
                else:
                    g2p = UsersGroupRepoToPerm()
                    g2p.repository = cur_repo
                    g2p.users_group = UsersGroup.get_by_group_name(member)
                    g2p.permission = self.sa.query(Permission).filter(Permission.permission_name == perm).scalar()
                    self.sa.add(g2p)

            # update current repo
            for k, v in form_data.items():
                if k == "user":
                    cur_repo.user = User.by_username(v)
                elif k == "repo_name":
                    cur_repo.repo_name = form_data["repo_name_full"]
                elif k == "repo_group":
                    cur_repo.group_id = v

                else:
                    setattr(cur_repo, k, v)

            self.sa.add(cur_repo)

            if repo_name != form_data["repo_name_full"]:
                # rename repository
                self.__rename_repo(old=repo_name, new=form_data["repo_name_full"])

            self.sa.commit()
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise
Beispiel #3
0
    def create(self, form_data, cur_user, just_db=False, fork=False):

        try:
            if fork:
                repo_name = form_data["fork_name"]
                org_name = form_data["repo_name"]
                org_full_name = org_name

            else:
                org_name = repo_name = form_data["repo_name"]
                repo_name_full = form_data["repo_name_full"]

            new_repo = Repository()
            new_repo.enable_statistics = False
            for k, v in form_data.items():
                if k == "repo_name":
                    if fork:
                        v = repo_name
                    else:
                        v = repo_name_full
                if k == "repo_group":
                    k = "group_id"

                setattr(new_repo, k, v)

            if fork:
                parent_repo = self.sa.query(Repository).filter(Repository.repo_name == org_full_name).one()
                new_repo.fork = parent_repo

            new_repo.user_id = cur_user.user_id
            self.sa.add(new_repo)

            # create default permission
            repo_to_perm = RepoToPerm()
            default = "repository.read"
            for p in UserModel(self.sa).get_by_username("default", cache=False).user_perms:
                if p.permission.permission_name.startswith("repository."):
                    default = p.permission.permission_name
                    break

            default_perm = "repository.none" if form_data["private"] else default

            repo_to_perm.permission_id = (
                self.sa.query(Permission).filter(Permission.permission_name == default_perm).one().permission_id
            )

            repo_to_perm.repository = new_repo
            repo_to_perm.user_id = UserModel(self.sa).get_by_username("default", cache=False).user_id

            self.sa.add(repo_to_perm)

            if not just_db:
                self.__create_repo(repo_name, form_data["repo_type"], form_data["repo_group"], form_data["clone_uri"])

            self.sa.commit()

            # now automatically start following this repository as owner
            from rhodecode.model.scm import ScmModel

            ScmModel(self.sa).toggle_following_repo(new_repo.repo_id, cur_user.user_id)

        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise
Beispiel #4
0
 def get_user_permission(self, repository, user):
     return RepoToPerm.query().filter(RepoToPerm.user == user).filter(RepoToPerm.repository == repository).scalar()
Beispiel #5
0
    def update(self, repo_name, form_data):
        try:
            cur_repo = self.get_by_repo_name(repo_name, cache=False)

            # update permissions
            for member, perm, member_type in form_data['perms_updates']:
                if member_type == 'user':
                    r2p = self.sa.query(RepoToPerm)\
                            .filter(RepoToPerm.user == User.get_by_username(member))\
                            .filter(RepoToPerm.repository == cur_repo)\
                            .one()

                    r2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.permission_name ==
                                                perm).scalar()
                    self.sa.add(r2p)
                else:
                    g2p = self.sa.query(UsersGroupRepoToPerm)\
                            .filter(UsersGroupRepoToPerm.users_group ==
                                    UsersGroup.get_by_group_name(member))\
                            .filter(UsersGroupRepoToPerm.repository ==
                                    cur_repo).one()

                    g2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.permission_name ==
                                                perm).scalar()
                    self.sa.add(g2p)

            # set new permissions
            for member, perm, member_type in form_data['perms_new']:
                if member_type == 'user':
                    r2p = RepoToPerm()
                    r2p.repository = cur_repo
                    r2p.user = User.get_by_username(member)

                    r2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.
                                                permission_name == perm)\
                                                .scalar()
                    self.sa.add(r2p)
                else:
                    g2p = UsersGroupRepoToPerm()
                    g2p.repository = cur_repo
                    g2p.users_group = UsersGroup.get_by_group_name(member)
                    g2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.
                                                permission_name == perm)\
                                                .scalar()
                    self.sa.add(g2p)

            # update current repo
            for k, v in form_data.items():
                if k == 'user':
                    cur_repo.user = User.get_by_username(v)
                elif k == 'repo_name':
                    pass
                elif k == 'repo_group':
                    cur_repo.group_id = v

                else:
                    setattr(cur_repo, k, v)

            new_name = cur_repo.get_new_name(form_data['repo_name'])
            cur_repo.repo_name = new_name

            self.sa.add(cur_repo)

            if repo_name != new_name:
                # rename repository
                self.__rename_repo(old=repo_name, new=new_name)

            self.sa.commit()
            return cur_repo
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise
Beispiel #6
0
    def create(self, form_data, cur_user, just_db=False, fork=False):

        try:
            if fork:
                repo_name = form_data['fork_name']
                org_name = form_data['repo_name']
                org_full_name = org_name

            else:
                org_name = repo_name = form_data['repo_name']
                repo_name_full = form_data['repo_name_full']

            new_repo = Repository()
            new_repo.enable_statistics = False
            for k, v in form_data.items():
                if k == 'repo_name':
                    if fork:
                        v = repo_name
                    else:
                        v = repo_name_full
                if k == 'repo_group':
                    k = 'group_id'

                if k == 'description':
                    v = v or repo_name

                setattr(new_repo, k, v)

            if fork:
                parent_repo = self.sa.query(Repository)\
                        .filter(Repository.repo_name == org_full_name).one()
                new_repo.fork = parent_repo

            new_repo.user_id = cur_user.user_id
            self.sa.add(new_repo)

            #create default permission
            repo_to_perm = RepoToPerm()
            default = 'repository.read'
            for p in User.get_by_username('default').user_perms:
                if p.permission.permission_name.startswith('repository.'):
                    default = p.permission.permission_name
                    break

            default_perm = 'repository.none' if form_data['private'] else default

            repo_to_perm.permission_id = self.sa.query(Permission)\
                    .filter(Permission.permission_name == default_perm)\
                    .one().permission_id

            repo_to_perm.repository = new_repo
            repo_to_perm.user_id = User.get_by_username('default').user_id

            self.sa.add(repo_to_perm)

            if not just_db:
                self.__create_repo(repo_name, form_data['repo_type'],
                                   form_data['repo_group'],
                                   form_data['clone_uri'])

            self.sa.commit()

            #now automatically start following this repository as owner
            from rhodecode.model.scm import ScmModel
            ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
                                             cur_user.user_id)
            return new_repo
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise