Module: Permissable::InstanceMethods
- Defined in:
- lib/permissable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #allows?(user, action, relevant_scopes = nil) ⇒ Boolean
- #cache_key(prefix = nil) ⇒ Object
- #clear_cached(prefix) ⇒ Object
- #get_cached(prefix) ⇒ Object
- #permissions_for(user, relevant_scopes = nil) ⇒ Object
- #set_cached(prefix, data, expires = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/permissable.rb', line 135 def self.included(base) base.define_singleton_method(:included) do |klass| klass.cattr_accessor :permissions_lookup klass.cattr_accessor :allow_cached_permissions klass.cattr_accessor :default_permission_scopes klass. = ['full'] klass. = [] end end |
Instance Method Details
#allows?(user, action, relevant_scopes = nil) ⇒ Boolean
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/permissable.rb', line 71 def allows?(user, action, relevant_scopes=nil) relevant_scopes ||= user. if user && user.respond_to?(:permission_scopes) relevant_scopes ||= self.class. relevant_scopes += ['*'] unless relevant_scopes == ['none'] if self.class. # check for an existing result keyed off the record's id and updated_at = (user, relevant_scopes) action.instance_variable_set('@scope_rejected', [action] == false) return [action] == true end scope_rejected = false self.class..each do |actions, block, allowed_scopes| next unless actions.include?(action.to_s) next if block.arity == 1 && !user res = instance_exec(user, &block) if res == true if (allowed_scopes & relevant_scopes).length > 0 return true else scope_rejected = true end end end action.instance_variable_set('@scope_rejected', !!scope_rejected) return false end |
#cache_key(prefix = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/permissable.rb', line 43 def cache_key(prefix=nil) id = self.id || 'nil' updated = (self.updated_at || Time.now).to_f key = "#{self.class.to_s}#{id}-#{updated}:#{Permissable.cache_token}" if prefix key = prefix + "/" + key end key end |
#clear_cached(prefix) ⇒ Object
67 68 69 |
# File 'lib/permissable.rb', line 67 def clear_cached(prefix) Permissable..del(self.cache_key(prefix)) end |
#get_cached(prefix) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/permissable.rb', line 58 def get_cached(prefix) cache_string = Permissable..get(self.cache_key(prefix)) cache = nil if cache_string cache = JSON.parse(cache_string) rescue nil end cache end |
#permissions_for(user, relevant_scopes = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/permissable.rb', line 100 def (user, relevant_scopes=nil) relevant_scopes ||= user. if user && user.respond_to?(:permission_scopes) relevant_scopes ||= self.class. relevant_scopes += ['*'] unless relevant_scopes == ['none'] if self.class. cache_key = (user && user.cache_key) || "nobody" cache_key += "/scopes_#{relevant_scopes.join(',')}" = get_cached("permissions-for/#{cache_key}") return if end = { 'user_id' => (user && user.global_id) } .with_indifferent_access if .respond_to?(:with_indifferent_access) self.class..each do |actions, block, allowed_scopes| already_granted = .select{|k, v| v == true }.map(&:first) next if block.arity == 1 && !user next if actions - already_granted == [] if instance_exec(user, &block) actions.each do |action| if (allowed_scopes & relevant_scopes).length > 0 [action] = true else [action] ||= false end end end end # cache the result with a 30-minute expiration keyed off the id and updated_at set_cached("permissions-for/#{cache_key}", ) if self.class. end |
#set_cached(prefix, data, expires = nil) ⇒ Object
53 54 55 56 |
# File 'lib/permissable.rb', line 53 def set_cached(prefix, data, expires=nil) expires ||= 1800 # 30 minutes Permissable..setex(self.cache_key(prefix), expires, data.to_json) end |