Class: KojacBasePolicy

Inherits:
Object
  • Object
show all
Defined in:
app/policies/kojac_base_policy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, record, op = nil) ⇒ KojacBasePolicy

Returns a new instance of KojacBasePolicy.

Raises:

  • (Pundit::NotAuthorizedError)


7
8
9
10
11
12
# File 'app/policies/kojac_base_policy.rb', line 7

def initialize(user, record, op=nil)
 raise Pundit::NotAuthorizedError, "must be logged in" unless user
  @user = user
  @record = record
 @op = op
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



5
6
7
# File 'app/policies/kojac_base_policy.rb', line 5

def op
  @op
end

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'app/policies/kojac_base_policy.rb', line 5

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'app/policies/kojac_base_policy.rb', line 5

def user
  @user
end

Class Method Details

.ability_from_op(aOp) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/policies/kojac_base_policy.rb', line 18

def self.ability_from_op(aOp)
 return nil unless aOp
 case aOp[:verb]
	when 'CREATE'
	when 'UPDATE'
		:write
	when 'READ'
		:read
	when 'ADD'
		:add
	when 'REMOVE'
		:remove
	when 'CREATE_ON'
		:create_on
end
end

.allow_filter(aOptions = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/policies/kojac_base_policy.rb', line 35

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, aAbility) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/policies/kojac_base_policy.rb', line 54

def apply_filters(aResult, aAbility)
	if self.class.filters
		self.class.filters.each do |f|
			options, handler = f
			unless options[:all]
				if rings = options[:ring]
					next unless rings.include? query_ring
				end
				if abilities = options[:ability]
					next unless abilities.include? aAbility
				end
			end
			aResult = handler.call(self, aResult.clone, query_ring, aAbility)
		end
		aResult.uniq!
		aResult.sort!
	end
	aResult
end

#create?Boolean

kojac methods

Returns:

  • (Boolean)


112
113
114
# File 'app/policies/kojac_base_policy.rb', line 112

def create?
 inner_query_record(:create)
end

#destroy?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/policies/kojac_base_policy.rb', line 124

def destroy?
 inner_query_record(:destroy)
end

#edit?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'app/policies/kojac_base_policy.rb', line 145

def edit?
 inner_query_record(:write)
end

#index?Boolean

rails methods

Returns:

  • (Boolean)


129
130
131
# File 'app/policies/kojac_base_policy.rb', line 129

def index?
 inner_query_record(:read)
end

#inner_query_fields(aAbility) ⇒ Object



74
75
76
77
78
79
# File 'app/policies/kojac_base_policy.rb', line 74

def inner_query_fields(aAbility)
 cls = record.is_a?(Class) ? record : record.class
 result = cls.permitted(query_ring,aAbility)
 result = apply_filters(result, aAbility)
 result
end

#inner_query_record(aAbility) ⇒ Object



81
82
83
# File 'app/policies/kojac_base_policy.rb', line 81

def inner_query_record(aAbility)
	inner_query_fields(aAbility).length > 0
end

#new?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/policies/kojac_base_policy.rb', line 137

def new?
 inner_query_record(:create)
end

#permitted_associations(aAbility = nil) ⇒ Object



104
105
106
107
108
109
# File 'app/policies/kojac_base_policy.rb', line 104

def permitted_associations(aAbility=nil)
  result = permitted_attributes(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



85
86
87
88
89
90
91
92
93
94
95
# File 'app/policies/kojac_base_policy.rb', line 85

def permitted_attributes(aAbility=nil)
	#raise "Ability from op no longer supported" if !aAbility && @op && @op[:verb]
	aAbility ||= self.class.ability_from_op(@op)
	raise "ability not given" unless aAbility
	fields = inner_query_fields(aAbility)

	#cls = record.is_a?(Class) ? record : record.class
	#fields = cls.permitted(query_ring,aAbility)
	#result = apply_filters(fields,aAbility)
	fields
end

#permitted_fields(aAbility = nil) ⇒ Object



97
98
99
100
101
102
# File 'app/policies/kojac_base_policy.rb', line 97

def permitted_fields(aAbility=nil)
  result = permitted_attributes(aAbility)
  cls = record.is_a?(Class) ? record : record.class
	result.delete_if { |f| cls.reflections.has_key? f }
	result
end

#query_ringObject



50
51
52
# File 'app/policies/kojac_base_policy.rb', line 50

def query_ring
 user.ring
end

#read?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/policies/kojac_base_policy.rb', line 116

def read?
 inner_query_record(:read)
end

#scopeObject



149
150
151
# File 'app/policies/kojac_base_policy.rb', line 149

def scope
  Pundit.policy_scope!(user, record.class)
end

#show?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/policies/kojac_base_policy.rb', line 133

def show?
 inner_query_record(:read)
end

#unauthorized!(aMessage = nil) ⇒ Object

Raises:

  • (Pundit::NotAuthorizedError)


14
15
16
# File 'app/policies/kojac_base_policy.rb', line 14

def unauthorized!(aMessage=nil)
 raise Pundit::NotAuthorizedError, aMessage||"You are not authorized to perform this action"
end

#update?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/policies/kojac_base_policy.rb', line 141

def update?
 inner_query_record(:write)
end

#write?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/policies/kojac_base_policy.rb', line 120

def write?
 inner_query_record(:write)
end