diff --git a/app/models/perm.rb b/app/models/perm.rb index 114c0f0..e53909e 100644 --- a/app/models/perm.rb +++ b/app/models/perm.rb @@ -7,6 +7,15 @@ # Possibly needed for active_admin # -relies on protected_attributes gem as syntax depricated in rails 4.2 attr_accessible :name, :as => [:default, :admin] - + validates :name, presence: true, uniqueness: true + + ADD_ORGS = Perm.where(name: 'add_organisations').first.freeze + CHANGE_AFFILIATION = Perm.where(name: 'change_org_affiliation').first.freeze + GRANT_PERMISSIONS = Perm.where(name: 'grant_permissions').first.freeze + MODIFY_TEMPLATES = Perm.where(name: 'modify_templates').first.freeze + MODIFY_GUIDANCE = Perm.where(name: 'modify_guidance').first.freeze + USE_API = Perm.where(name: 'use_api').first.freeze + CHANGE_ORG_DETAILS = Perm.where(name: 'change_org_details').first.freeze + GRANT_API = Perm.where(name: 'grant_api_to_orgs').first.freeze end diff --git a/app/models/user.rb b/app/models/user.rb index 4cf42a2..15d6112 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -151,7 +151,7 @@ # # @return [Boolean] true if the user can add new organisations def can_add_orgs? - perms.include? Perm.find_by(name: constant("roles.add_organisations")) + perms.include? Perm::ADD_ORGS end ## @@ -159,7 +159,7 @@ # # @return [Boolean] true if the user can change their organisation affiliations def can_change_org? - perms.include? Perm.find_by(name: constant("roles.change_org_affiliation")) + perms.include? Perm::CHANGE_AFFILIATION end ## @@ -167,7 +167,7 @@ # # @return [Boolean] true if the user can grant their permissions to others def can_grant_permissions? - perms.include? Perm.find_by(name: constant("roles.grant_permissions")) + perms.include? Perm::GRANT_PERMISSIONS end ## @@ -175,7 +175,7 @@ # # @return [Boolean] true if the user can modify organisation templates def can_modify_templates? - perms.include? Perm.find_by(name: constant("roles.modify_templates")) + perms.include? Perm::MODIFY_TEMPLATES end ## @@ -183,7 +183,7 @@ # # @return [Boolean] true if the user can modify organistion guidance def can_modify_guidance? - perms.include? Perm.find_by(name: constant("roles.modify_guidance")) + perms.include? Perm::MODIFY_GUIDANCE end ## @@ -191,7 +191,7 @@ # # @return [Boolean] true if the user can use the api def can_use_api? - perms.include? Perm.find_by(name: constant("roles.use_api")) + perms.include? Perm::USE_API end ## @@ -199,7 +199,7 @@ # # @return [Boolean] true if the user can modify the org's details def can_modify_org_details? - perms.include? Perm.find_by(name: constant("roles.change_org_details")) + perms.include? Perm::CHANGE_ORG_DETAILS end @@ -208,7 +208,7 @@ # # @return [Boolean] true if the user can grant api permissions to organisations def can_grant_api_to_orgs? - perms.include? Perm.find_by(name: constant('roles.grant_api_to_orgs')) + perms.include? Perm::GRANT_API end ##