Changeset - 87c2cd07166a
[Not reviewed]
default
0 5 0
Mads Kiilerich (mads) - 5 years ago 2020-11-01 23:04:25
mads@kiilerich.com
Grafted from: 90dd5831e497
lib: move get_all_user_repos to AuthUser
5 files changed with 13 insertions and 17 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/api/api.py
Show inline comments
 
@@ -1094,13 +1094,13 @@ class ApiController(JSONRPCController):
 
                      },
 
 
                    ]
 
            error:  null
 
        """
 
        if not HasPermissionAny('hg.admin')():
 
            repos = RepoModel().get_all_user_repos(user=request.authuser.user_id)
 
            repos = request.authuser.get_all_user_repos()
 
        else:
 
            repos = db.Repository.query()
 

	
 
        return [
 
            repo.get_api_data()
 
            for repo in repos
kallithea/lib/auth.py
Show inline comments
 
@@ -464,12 +464,22 @@ class AuthUser(object):
 
            except ObjectDeletedError:
 
                # since we use heavy caching sometimes it happens that we get
 
                # deleted objects here, we just skip them
 
                pass
 
        return _set or set(['0.0.0.0/0', '::/0'])
 

	
 
    def get_all_user_repos(self):
 
        """
 
        Gets all repositories that user have at least read access
 
        """
 
        repos = [repo_name
 
            for repo_name, perm in self.repository_permissions.items()
 
            if perm in ['repository.read', 'repository.write', 'repository.admin']
 
            ]
 
        return db.Repository.query().filter(db.Repository.repo_name.in_(repos))
 

	
 

	
 
#==============================================================================
 
# CHECK DECORATORS
 
#==============================================================================
 

	
 
def _redirect_to_login(message=None):
kallithea/lib/auth_modules/__init__.py
Show inline comments
 
@@ -19,13 +19,13 @@ import importlib
 
import logging
 
import traceback
 
from inspect import isfunction
 

	
 
from kallithea.lib.auth import AuthUser
 
from kallithea.lib.compat import hybrid_property
 
from kallithea.lib.utils2 import asbool, PasswordGenerator
 
from kallithea.lib.utils2 import PasswordGenerator, asbool
 
from kallithea.model import db, meta, validators
 
from kallithea.model.user import UserModel
 
from kallithea.model.user_group import UserGroupModel
 

	
 

	
 
log = logging.getLogger(__name__)
kallithea/model/repo.py
Show inline comments
 
@@ -86,26 +86,12 @@ class RepoModel(object):
 

	
 
    def get_by_repo_name(self, repo_name):
 
        repo = db.Repository.query() \
 
            .filter(db.Repository.repo_name == repo_name)
 
        return repo.scalar()
 

	
 
    def get_all_user_repos(self, user):
 
        """
 
        Gets all repositories that user have at least read access
 

	
 
        :param user:
 
        """
 
        from kallithea.lib.auth import AuthUser
 
        auth_user = AuthUser(dbuser=db.User.guess_instance(user))
 
        repos = [repo_name
 
            for repo_name, perm in auth_user.repository_permissions.items()
 
            if perm in ['repository.read', 'repository.write', 'repository.admin']
 
            ]
 
        return db.Repository.query().filter(db.Repository.repo_name.in_(repos))
 

	
 
    @classmethod
 
    def _render_datatable(cls, tmpl, *args, **kwargs):
 
        from tg import app_globals, request
 
        from tg import tmpl_context as c
 
        from tg.i18n import ugettext as _
 

	
kallithea/tests/api/api_base.py
Show inline comments
 
@@ -685,13 +685,13 @@ class _BaseTestApi(object):
 
    def test_api_get_repos_non_admin(self):
 
        id_, params = _build_data(self.apikey_regular, 'get_repos')
 
        response = api_call(self, params)
 

	
 
        expected = jsonify([
 
            repo.get_api_data()
 
            for repo in RepoModel().get_all_user_repos(self.TEST_USER_LOGIN)
 
            for repo in AuthUser(dbuser=db.User.get_by_username(self.TEST_USER_LOGIN)).get_all_user_repos()
 
        ])
 

	
 
        self._compare_ok(id_, expected, given=response.body)
 

	
 
    @base.parametrize('name,ret_type', [
 
        ('all', 'all'),
0 comments (0 inline, 0 general)