Class: ConcentricPolicy
- Inherits:
-
Object
- Object
- ConcentricPolicy
- Defined in:
- app/policies/concentric_policy.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_filters(aResult) ⇒ Object
-
#create? ⇒ Boolean
kojac methods.
- #destroy? ⇒ Boolean
- #edit? ⇒ Boolean
-
#index? ⇒ Boolean
rails methods.
-
#initialize(user, record) ⇒ ConcentricPolicy
constructor
A new instance of ConcentricPolicy.
- #inner_query_ability(aAbility) ⇒ Object
- #inner_query_fields(aAbility = nil) ⇒ Object
- #new? ⇒ Boolean
- #permitted_associations(aAbility = nil) ⇒ Object
- #permitted_attributes(aAbility = nil) ⇒ Object
- #permitted_fields(aAbility = nil) ⇒ Object
- #read? ⇒ Boolean
- #scope ⇒ Object
- #show? ⇒ Boolean
- #unauthorized!(aMessage = nil) ⇒ Object
- #update? ⇒ Boolean
-
#user_ring ⇒ Object
this could use an alternative field or method in future.
- #write? ⇒ Boolean
Constructor Details
#initialize(user, record) ⇒ ConcentricPolicy
Returns a new instance of ConcentricPolicy.
7 8 9 10 11 |
# File 'app/policies/concentric_policy.rb', line 7 def initialize(user, record) raise Pundit::NotAuthorizedError, "must be logged in" unless user @user = user @record = record end |
Instance Attribute Details
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
5 6 7 |
# File 'app/policies/concentric_policy.rb', line 5 def ability @ability end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
5 6 7 |
# File 'app/policies/concentric_policy.rb', line 5 def record @record end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
5 6 7 |
# File 'app/policies/concentric_policy.rb', line 5 def user @user end |
Class Method Details
.allow_filter(aOptions = nil, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/policies/concentric_policy.rb', line 17 def self.allow_filter(aOptions=nil,&block) aOptions = {all: true} if !aOptions if rings = aOptions[:ring] rings = [rings] unless rings.is_a? Array aOptions[:ring] = rings.map {|r| Concentric.lookup_ring(r) } end if abilities = aOptions[:ability] aOptions[:ability] = [abilities] unless abilities.is_a? Array end if block self.filters ||= [] self.filters += [[aOptions,block]] # double brackets necessary to add an array into the array end end |
Instance Method Details
#apply_filters(aResult) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/policies/concentric_policy.rb', line 37 def apply_filters(aResult) if self.class.filters self.class.filters.each do |f| , handler = f unless [:all] if rings = [:ring] next unless rings.include? user_ring end if abilities = [:ability] next unless abilities.include? @ability end end aResult = handler.call(self, aResult.clone) # ring not necessary, use aPolicy.user.ring instead. aAbility not necessary, use aPolicy.ability end aResult.uniq! aResult.sort! end aResult end |
#create? ⇒ Boolean
kojac methods
90 91 92 |
# File 'app/policies/concentric_policy.rb', line 90 def create? inner_query_ability(:create) end |
#destroy? ⇒ Boolean
102 103 104 |
# File 'app/policies/concentric_policy.rb', line 102 def destroy? inner_query_ability(:destroy) end |
#edit? ⇒ Boolean
123 124 125 |
# File 'app/policies/concentric_policy.rb', line 123 def edit? inner_query_ability(:write) end |
#index? ⇒ Boolean
rails methods
107 108 109 |
# File 'app/policies/concentric_policy.rb', line 107 def index? inner_query_ability(:read) end |
#inner_query_ability(aAbility) ⇒ Object
84 85 86 87 |
# File 'app/policies/concentric_policy.rb', line 84 def inner_query_ability(aAbility) @ability = aAbility inner_query_fields.length > 0 end |
#inner_query_fields(aAbility = nil) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'app/policies/concentric_policy.rb', line 57 def inner_query_fields(aAbility=nil) aAbility = @ability = (aAbility || @ability) raise "Ability must be set or given" unless aAbility cls = record.is_a?(Class) ? record : record.class result = cls.permitted(user_ring,aAbility) result = apply_filters(result) result end |
#new? ⇒ Boolean
115 116 117 |
# File 'app/policies/concentric_policy.rb', line 115 def new? inner_query_ability(:create) end |
#permitted_associations(aAbility = nil) ⇒ Object
77 78 79 80 81 82 |
# File 'app/policies/concentric_policy.rb', line 77 def permitted_associations(aAbility=nil) result = inner_query_fields(aAbility) cls = record.is_a?(Class) ? record : record.class result.delete_if { |f| !cls.reflections.has_key? f } result end |
#permitted_attributes(aAbility = nil) ⇒ Object
66 67 68 |
# File 'app/policies/concentric_policy.rb', line 66 def permitted_attributes(aAbility=nil) inner_query_fields(aAbility) end |
#permitted_fields(aAbility = nil) ⇒ Object
70 71 72 73 74 75 |
# File 'app/policies/concentric_policy.rb', line 70 def permitted_fields(aAbility=nil) result = inner_query_fields(aAbility) cls = record.is_a?(Class) ? record : record.class result.delete_if { |f| cls.reflections.has_key? f } result end |
#read? ⇒ Boolean
94 95 96 |
# File 'app/policies/concentric_policy.rb', line 94 def read? inner_query_ability(:read) end |
#scope ⇒ Object
127 128 129 |
# File 'app/policies/concentric_policy.rb', line 127 def scope Pundit.policy_scope!(user, record.class) end |
#show? ⇒ Boolean
111 112 113 |
# File 'app/policies/concentric_policy.rb', line 111 def show? inner_query_ability(:read) end |
#unauthorized!(aMessage = nil) ⇒ Object
13 14 15 |
# File 'app/policies/concentric_policy.rb', line 13 def (aMessage=nil) raise Pundit::NotAuthorizedError, aMessage||"You are not authorized to perform this action" end |
#update? ⇒ Boolean
119 120 121 |
# File 'app/policies/concentric_policy.rb', line 119 def update? inner_query_ability(:write) end |
#user_ring ⇒ Object
this could use an alternative field or method in future
33 34 35 |
# File 'app/policies/concentric_policy.rb', line 33 def user_ring user.ring end |
#write? ⇒ Boolean
98 99 100 |
# File 'app/policies/concentric_policy.rb', line 98 def write? inner_query_ability(:write) end |