Created
May 1, 2013 18:43
-
-
Save imoss/5497322 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Gatekeeper | |
| def self.has_access?(user: user, context: context) | |
| perms_1 = self.find_by_sql(" | |
| select p.* | |
| from user_permissions p | |
| where p.context_id = #{context} | |
| and p.user_id = #{user.id} | |
| union | |
| select p.* | |
| from group_permissions g | |
| where g.context_id = (#{context}) | |
| and g.group_id in #{user.groups.map(&:id).join(',')} | |
| limit 1 | |
| ").first | |
| return perms_1.present? | |
| end | |
| def self.any_access?(user: user, contexts: contexts) | |
| perms_1 = self.find_by_sql(" | |
| select p.* | |
| from user_permissions p | |
| where p.context_id IN #{contexts.map { |c| c.id }.join(',')} | |
| and p.user_id = #{user.id} | |
| union | |
| select p.* | |
| from group_permissions g | |
| where g.context_id IN #{contexts.map { |c| c.id }.join(',')} | |
| and g.group_id in #{user.groups.map { |g| g.id }.join(',')} | |
| limit 1 | |
| ").first | |
| return perms_1.present? | |
| end | |
| end | |
| class UserPermission < ActiveRecord::Base | |
| belongs_to :user | |
| belongs_to :context | |
| end | |
| class GroupPermission < ActiveRecord::Base | |
| belongs_to :group | |
| belongs_to :context | |
| end | |
| class User < ActiveRecord::Base | |
| has_many :user_permissions | |
| has_and_belongs_to_many :groups | |
| end | |
| class Group < ActiveRecord::Base | |
| has_many :group_permissions | |
| has_and_belongs_to_many :users | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment