From e85346da255a7fbb75e32aec95208fad662e38ea Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 1 Apr 2022 15:47:50 +0200 Subject: [PATCH] More refactor --- src/app.rb | 4 ++-- src/db_models.rb | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/app.rb b/src/app.rb index 77bc80f..b2c03d0 100755 --- a/src/app.rb +++ b/src/app.rb @@ -297,7 +297,7 @@ end post "/admin/roles/give" do user = get_current_user - auth_denied if user.permitted? :roleman + auth_denied unless user.permitted?(:roleman) user_id = params[:user_id].to_i role_id = params[:role_id].to_i @@ -317,7 +317,7 @@ end post "/admin/roles" do user = get_current_user - auth_denied if user.permitted? :roleman + auth_denied unless user.permitted? :roleman name = params[:name] color = params[:color] diff --git a/src/db_models.rb b/src/db_models.rb index d7ab2bc..d4755a2 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -18,7 +18,9 @@ class User < EntityModel end def role - user_roles = roles + return Role.find_by_id(1).name if self.admin? + + user_roles = self.roles if user_roles.length > 0 then role = user_roles.max_by { |role| role.flags } return role.name @@ -149,9 +151,15 @@ class User < EntityModel return flags end + def admin? + return self.flags[1] == 1 + end + # Check if user has flags # Returns: true or false depending whether the user has those flags def permitted?(flag, *other_flags) + return true if self.admin? + flag_mask = PERM_LEVELS[flag] if other_flags then other_flags.each {|f| flag_mask |= PERM_LEVELS[f]} @@ -160,12 +168,8 @@ class User < EntityModel return self.flags & flag_mask == flag_mask end - def admin? - return self.permitted? :admin - end - def banned? - return self.permitted? :banned + return self.flags[ PERM_LEVELS.keys.index(:banned) ] == 1 end end