From 0a8e792efc33db434e93656935c5443cdee0230e Mon Sep 17 00:00:00 2001 From: Marcin Kuzminski Date: Wed, 27 Mar 2019 12:29:31 +0100 Subject: [PATCH] auth: reduced usage of raw auth calls inside templates --- rhodecode/apps/_base/__init__.py | 22 ++++++++++++++++ rhodecode/apps/admin/views/main_views.py | 13 ++++++---- rhodecode/lib/auth.py | 12 +++------ rhodecode/lib/base.py | 1 - rhodecode/templates/admin/gists/index.mako | 2 +- rhodecode/templates/admin/gists/show.mako | 4 +-- rhodecode/templates/admin/main.mako | 10 +------ .../repo_group_edit_permissions.mako | 2 +- .../admin/repo_groups/repo_groups.mako | 2 +- .../admin/repos/repo_edit_permissions.mako | 2 +- rhodecode/templates/admin/repos/repos.mako | 2 +- .../user_groups/user_group_edit_perms.mako | 2 +- .../admin/user_groups/user_groups.mako | 2 +- rhodecode/templates/base/base.mako | 26 +++++++++---------- .../changeset/changeset_file_comment.mako | 2 +- rhodecode/templates/index_base.mako | 11 ++------ rhodecode/templates/summary/components.mako | 4 +-- rhodecode/templates/user_group/profile.mako | 2 +- rhodecode/templates/users/user_profile.mako | 2 +- 19 files changed, 64 insertions(+), 59 deletions(-) diff --git a/rhodecode/apps/_base/__init__.py b/rhodecode/apps/_base/__init__.py index 9b2435d7..199a25f7 100644 --- a/rhodecode/apps/_base/__init__.py +++ b/rhodecode/apps/_base/__init__.py @@ -168,6 +168,28 @@ class BaseAppView(object): from rhodecode.lib.base import attach_context_attributes attach_context_attributes(c, self.request, self.request.user.user_id) + c.is_super_admin = c.auth_user.is_admin + + c.can_create_repo = c.is_super_admin + c.can_create_repo_group = c.is_super_admin + c.can_create_user_group = c.is_super_admin + + c.is_delegated_admin = False + + if not c.auth_user.is_default: + c.can_create_repo = h.HasPermissionAny('hg.create.repository')( + user=self.request.user) + repositories = c.auth_user.repositories_admin or c.can_create_repo + + c.can_create_repo_group = h.HasPermissionAny('hg.repogroup.create.true')( + user=self.request.user) + repository_groups = c.auth_user.repository_groups_admin or c.can_create_repo_group + + c.can_create_user_group = h.HasPermissionAny('hg.usergroup.create.true')( + user=self.request.user) + user_groups = c.auth_user.user_groups_admin or c.can_create_user_group + # delegated admin can create, or manage some objects + c.is_delegated_admin = repositories or repository_groups or user_groups return c def _get_template_context(self, tmpl_args, **kwargs): diff --git a/rhodecode/apps/admin/views/main_views.py b/rhodecode/apps/admin/views/main_views.py index 6566fb2f..752b305c 100644 --- a/rhodecode/apps/admin/views/main_views.py +++ b/rhodecode/apps/admin/views/main_views.py @@ -20,12 +20,12 @@ import logging -from pyramid.httpexceptions import HTTPFound +from pyramid.httpexceptions import HTTPFound, HTTPNotFound from pyramid.view import view_config from rhodecode.apps._base import BaseAppView from rhodecode.lib import helpers as h -from rhodecode.lib.auth import (LoginRequired, HasPermissionAllDecorator) +from rhodecode.lib.auth import (LoginRequired, NotAnonymous) from rhodecode.model.db import PullRequest @@ -38,13 +38,17 @@ class AdminMainView(BaseAppView): return c @LoginRequired() - @HasPermissionAllDecorator('hg.admin') + @NotAnonymous() @view_config( route_name='admin_home', request_method='GET', renderer='rhodecode:templates/admin/main.mako') def admin_main(self): c = self.load_default_context() c.active = 'admin' + + if not (c.is_super_admin or c.is_delegated_admin): + raise HTTPNotFound() + return self._get_template_context(c) @LoginRequired() @@ -54,8 +58,7 @@ class AdminMainView(BaseAppView): def pull_requests(self): """ Global redirect for Pull Requests - - :param pull_request_id: id of pull requests in the system + pull_request_id: id of pull requests in the system """ pull_request = PullRequest.get_or_404( diff --git a/rhodecode/lib/auth.py b/rhodecode/lib/auth.py index c15e7890..4e6e4844 100644 --- a/rhodecode/lib/auth.py +++ b/rhodecode/lib/auth.py @@ -2078,8 +2078,7 @@ class HasRepoPermissionAny(PermsFunction): class HasRepoGroupPermissionAny(PermsFunction): def __call__(self, group_name=None, check_location='', user=None): self.repo_group_name = group_name - return super(HasRepoGroupPermissionAny, self).__call__( - check_location, user) + return super(HasRepoGroupPermissionAny, self).__call__(check_location, user) def check_permissions(self, user): perms = user.permissions @@ -2095,8 +2094,7 @@ class HasRepoGroupPermissionAny(PermsFunction): class HasRepoGroupPermissionAll(PermsFunction): def __call__(self, group_name=None, check_location='', user=None): self.repo_group_name = group_name - return super(HasRepoGroupPermissionAll, self).__call__( - check_location, user) + return super(HasRepoGroupPermissionAll, self).__call__(check_location, user) def check_permissions(self, user): perms = user.permissions @@ -2112,8 +2110,7 @@ class HasRepoGroupPermissionAll(PermsFunction): class HasUserGroupPermissionAny(PermsFunction): def __call__(self, user_group_name=None, check_location='', user=None): self.user_group_name = user_group_name - return super(HasUserGroupPermissionAny, self).__call__( - check_location, user) + return super(HasUserGroupPermissionAny, self).__call__(check_location, user) def check_permissions(self, user): perms = user.permissions @@ -2129,8 +2126,7 @@ class HasUserGroupPermissionAny(PermsFunction): class HasUserGroupPermissionAll(PermsFunction): def __call__(self, user_group_name=None, check_location='', user=None): self.user_group_name = user_group_name - return super(HasUserGroupPermissionAll, self).__call__( - check_location, user) + return super(HasUserGroupPermissionAll, self).__call__(check_location, user) def check_permissions(self, user): perms = user.permissions diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py index 2f3c4e8c..827a1bac 100644 --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -288,7 +288,6 @@ def attach_context_attributes(context, request, user_id=None): """ config = request.registry.settings - rc_config = SettingsModel().get_all_settings(cache=True) context.rhodecode_version = rhodecode.__version__ diff --git a/rhodecode/templates/admin/gists/index.mako b/rhodecode/templates/admin/gists/index.mako index e417225e..480e45c6 100644 --- a/rhodecode/templates/admin/gists/index.mako +++ b/rhodecode/templates/admin/gists/index.mako @@ -52,7 +52,7 @@ ##main
- %if h.HasPermissionAny('hg.admin')() or c.gist.gist_owner == c.rhodecode_user.user_id: + %if c.is_super_admin or c.gist.gist_owner == c.rhodecode_user.user_id:
${h.secure_form(h.route_path('gist_delete', gist_id=c.gist.gist_access_id), request=request)} ${h.submit('remove_gist', _('Delete'),class_="btn btn-mini btn-danger",onclick="return confirm('"+_('Confirm to delete this Gist')+"');")} @@ -59,7 +59,7 @@ ## only owner should see that ${_('Copy content')} - %if h.HasPermissionAny('hg.admin')() or c.gist.gist_owner == c.rhodecode_user.user_id: + %if c.is_super_admin or c.gist.gist_owner == c.rhodecode_user.user_id: ${h.link_to(_('Edit'), h.route_path('gist_edit', gist_id=c.gist.gist_access_id), class_="btn btn-mini")} %endif ${h.link_to(_('Show as Raw'), h.route_path('gist_show_formatted', gist_id=c.gist.gist_access_id, revision='tip', format='raw'), class_="btn btn-mini")} diff --git a/rhodecode/templates/admin/main.mako b/rhodecode/templates/admin/main.mako index 108bce38..9a57210e 100644 --- a/rhodecode/templates/admin/main.mako +++ b/rhodecode/templates/admin/main.mako @@ -34,16 +34,8 @@
##main - diff --git a/rhodecode/templates/admin/repo_groups/repo_group_edit_permissions.mako b/rhodecode/templates/admin/repo_groups/repo_group_edit_permissions.mako index 5f8eb175..0a6f183c 100644 --- a/rhodecode/templates/admin/repo_groups/repo_group_edit_permissions.mako +++ b/rhodecode/templates/admin/repo_groups/repo_group_edit_permissions.mako @@ -141,7 +141,7 @@ ${h.radio('g_perm_%s' % _user_group.users_group_id,'group.admin', checked=_user_group.permission=='group.admin')} - %if h.HasPermissionAny('hg.admin')(): + %if c.is_super_admin: ${_user_group.users_group_name} diff --git a/rhodecode/templates/admin/repo_groups/repo_groups.mako b/rhodecode/templates/admin/repo_groups/repo_groups.mako index 176fc39b..cdcb6bb1 100644 --- a/rhodecode/templates/admin/repo_groups/repo_groups.mako +++ b/rhodecode/templates/admin/repo_groups/repo_groups.mako @@ -26,7 +26,7 @@