From 5f3f20879814eeb42f3046ff24b630694bb3ed7d Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 11 Jun 2023 21:27:52 +0200 Subject: [PATCH] Permissions: code simplification. --- src/requests/permissions.cr | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/requests/permissions.cr b/src/requests/permissions.cr index bf211e9..a3ba9a9 100644 --- a/src/requests/permissions.cr +++ b/src/requests/permissions.cr @@ -16,20 +16,16 @@ class AuthD::Request user = authd.user? @user return Response::Error.new "no such user" if user.nil? - service = @service - service_permissions = user.permissions[service]? - - if service_permissions.nil? - return Response::PermissionCheck.new service, @resource, user.uid, User::PermissionLevel::None + service_permissions = user.permissions[@service]? + resource_permissions = if service_permissions.nil? + User::PermissionLevel::None + elsif p = service_permissions[@resource]? + p + else + User::PermissionLevel::None end - resource_permissions = service_permissions[@resource]? - - if resource_permissions.nil? - return Response::PermissionCheck.new service, @resource, user.uid, User::PermissionLevel::None - end - - return Response::PermissionCheck.new service, @resource, user.uid, resource_permissions + return Response::PermissionCheck.new @service, @resource, user.uid, resource_permissions end end AuthD.requests << CheckPermission @@ -52,12 +48,11 @@ class AuthD::Request user = authd.user? @user return Response::Error.new "no such user" if user.nil? - service = @service - service_permissions = user.permissions[service]? + service_permissions = user.permissions[@service]? if service_permissions.nil? service_permissions = Hash(String, User::PermissionLevel).new - user.permissions[service] = service_permissions + user.permissions[@service] = service_permissions end if @permission.none? @@ -68,7 +63,7 @@ class AuthD::Request authd.users_per_uid.update user.uid.to_s, user - Response::PermissionSet.new user.uid, service, @resource, @permission + Response::PermissionSet.new user.uid, @service, @resource, @permission end end AuthD.requests << SetPermission