Skip to content

Commit

Permalink
Rename required_permissions view attribute to permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrisan committed Jul 24, 2019
1 parent b06004a commit b9dad39
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions colibris/authorization/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def _require_permissions(and_set=None, or_set=None):

def decorator(handler):
# Combine any existing permissions with the new ones
required_permissions = get_required_permissions(handler) or Permissions()
handler.required_permissions = required_permissions.combine(new_permissions)
permissions = get_required_permissions(handler) or Permissions()
handler.permissions = permissions.combine(new_permissions)

return handler

Expand All @@ -74,7 +74,7 @@ def require_all_permissions(permissions):


def get_required_permissions(handler):
permissions = getattr(handler, 'required_permissions', None)
permissions = getattr(handler, 'permissions', None)
if permissions is None:
return

Expand Down
18 changes: 9 additions & 9 deletions colibris/middleware/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ async def handle_auth(request, handler):

# First look for auth info in the view handler itself (class or function)
authentication_required = authentication.get_authentication_required(original_handler)
required_permissions = authorization.get_required_permissions(original_handler)
permissions = authorization.get_required_permissions(original_handler)

# Then, if we've got a class-based view, look for required permissions in method function
if inspect.isclass(original_handler) and issubclass(original_handler, views.View):
method_func = getattr(original_handler, method.lower(), None)
if method_func:
method_func_authentication_required = authentication.get_authentication_required(method_func)
method_func_required_permissions = authorization.get_required_permissions(method_func)
method_func_permissions = authorization.get_required_permissions(method_func)

if method_func_required_permissions and required_permissions:
required_permissions = required_permissions.combine(method_func_required_permissions)
if method_func_permissions and permissions:
permissions = permissions.combine(method_func_permissions)

else:
required_permissions = required_permissions or method_func_required_permissions
permissions = permissions or method_func_permissions

if method_func_authentication_required is not None:
authentication_required = method_func_authentication_required

# A value of None for authentication_required indicates decision based on permissions
if authentication_required is None:
authentication_required = bool(required_permissions)
authentication_required = bool(permissions)

if not authentication_required and required_permissions:
if not authentication_required and permissions:
raise authorization.AuthorizationException('view requires permissions but does not require authentication')

if authentication_required:
Expand All @@ -56,9 +56,9 @@ async def handle_auth(request, handler):
except authentication.AuthenticationException:
raise api.UnauthenticatedException()

if required_permissions is not None:
if permissions is not None:
try:
authorization.authorize(account, method, path, original_handler, required_permissions)
authorization.authorize(account, method, path, original_handler, permissions)

except authorization.AuthorizationException:
raise api.ForbiddenException()
Expand Down
4 changes: 2 additions & 2 deletions colibris/skeleton/__packagename__/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def get(self):


class UsersView(ListCreateModelView):
required_permissions = {constants.ROLE_ADMIN}
permissions = {constants.ROLE_ADMIN}
body_schema_class = schemas.UserSchema
model = models.User

Expand All @@ -53,7 +53,7 @@ async def post(self):


class UserView(RetrieveUpdateDestroyModelView):
required_permissions = {constants.ROLE_ADMIN}
permissions = {constants.ROLE_ADMIN}
body_schema_class = schemas.UserSchema
model = models.User

Expand Down
2 changes: 1 addition & 1 deletion colibris/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def __init__(cls, name, bases, attrs):

class View(web.View, metaclass=ViewMeta):
authentication_required = None
required_permissions = None
permissions = None

0 comments on commit b9dad39

Please sign in to comment.