Class: Casbin::Model::Policy
- Inherits:
-
Object
- Object
- Casbin::Model::Policy
- Defined in:
- lib/casbin-ruby/model/policy.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#add_policies(sec, ptype, rules) ⇒ Object
adds policy rules to the model.
-
#add_policy(sec, ptype, rule) ⇒ Object
adds a policy rule to the model.
-
#build_role_links(rm_map) ⇒ Object
initializes the roles in RBAC.
-
#clear_policy ⇒ Object
clears all current policy.
-
#get_filtered_policy(sec, ptype, field_index, *field_values) ⇒ Object
gets rules based on field filters from a policy.
-
#get_policy(sec, ptype) ⇒ Object
gets all rules in a policy.
-
#get_values_for_field_in_policy(sec, ptype, field_index) ⇒ Object
gets all values for a field for all rules in a policy, duplicated values are removed.
-
#has_policy(sec, ptype, rule) ⇒ Object
determines whether a model has the specified policy rule.
-
#initialize(logger: Logger.new($stdout)) ⇒ Policy
constructor
A new instance of Policy.
-
#print_policy ⇒ Object
Log using info.
-
#remove_filtered_policy(sec, ptype, field_index, *field_values) ⇒ Object
removes policy rules based on field filters from the model.
-
#remove_policies(sec, ptype, rules) ⇒ Object
removes policy rules from the model.
-
#remove_policy(sec, ptype, rule) ⇒ Object
removes a policy rule from the model.
-
#update_policies(sec, ptype, old_rules, new_rules) ⇒ Object
update policy rules from the model.
-
#update_policy(sec, ptype, old_rule, new_rule) ⇒ Object
update a policy rule from the model.
Constructor Details
#initialize(logger: Logger.new($stdout)) ⇒ Policy
Returns a new instance of Policy.
10 11 12 13 |
# File 'lib/casbin-ruby/model/policy.rb', line 10 def initialize(logger: Logger.new($stdout)) @model = {} @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/casbin-ruby/model/policy.rb', line 8 def logger @logger end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
8 9 10 |
# File 'lib/casbin-ruby/model/policy.rb', line 8 def model @model end |
Instance Method Details
#add_policies(sec, ptype, rules) ⇒ Object
adds policy rules to the model.
59 60 61 62 63 64 65 66 67 |
# File 'lib/casbin-ruby/model/policy.rb', line 59 def add_policies(sec, ptype, rules) rules.each do |rule| return false if has_policy(sec, ptype, rule) end model[sec][ptype].policy += rules true end |
#add_policy(sec, ptype, rule) ⇒ Object
adds a policy rule to the model.
50 51 52 53 54 55 56 |
# File 'lib/casbin-ruby/model/policy.rb', line 50 def add_policy(sec, ptype, rule) return false if has_policy(sec, ptype, rule) model[sec][ptype].policy << rule true end |
#build_role_links(rm_map) ⇒ Object
initializes the roles in RBAC.
16 17 18 19 20 21 22 23 |
# File 'lib/casbin-ruby/model/policy.rb', line 16 def build_role_links(rm_map) return unless model.key? 'g' model['g'].each do |ptype, ast| rm = rm_map[ptype] ast.build_role_links(rm) end end |
#clear_policy ⇒ Object
clears all current policy.
39 40 41 42 43 44 45 46 47 |
# File 'lib/casbin-ruby/model/policy.rb', line 39 def clear_policy %w[p g].each do |sec| next unless model.key? sec model[sec].each do |key, _ast| model[sec][key].policy = [] end end end |
#get_filtered_policy(sec, ptype, field_index, *field_values) ⇒ Object
gets rules based on field filters from a policy.
116 117 118 119 120 |
# File 'lib/casbin-ruby/model/policy.rb', line 116 def get_filtered_policy(sec, ptype, field_index, *field_values) model[sec][ptype].policy.select do |rule| field_values.select { |value| rule[field_index] == value }.any? end.compact end |
#get_policy(sec, ptype) ⇒ Object
gets all rules in a policy.
86 87 88 |
# File 'lib/casbin-ruby/model/policy.rb', line 86 def get_policy(sec, ptype) model[sec][ptype].policy end |
#get_values_for_field_in_policy(sec, ptype, field_index) ⇒ Object
gets all values for a field for all rules in a policy, duplicated values are removed.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/casbin-ruby/model/policy.rb', line 137 def get_values_for_field_in_policy(sec, ptype, field_index) values = [] return values unless model.keys.include?(sec) return values unless model[sec].include?(ptype) model[sec][ptype].policy.each do |rule| value = rule[field_index] values << value if values.include?(value) end values end |
#has_policy(sec, ptype, rule) ⇒ Object
determines whether a model has the specified policy rule.
91 92 93 |
# File 'lib/casbin-ruby/model/policy.rb', line 91 def has_policy(sec, ptype, rule) model.key?(sec) && model[sec].key?(ptype) && model[sec][ptype].policy.include?(rule) end |
#print_policy ⇒ Object
Log using info
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/casbin-ruby/model/policy.rb', line 26 def print_policy logger.info 'Policy:' %w[p g].each do |sec| next unless model.key? sec model[sec].each do |key, ast| logger.info "#{key} : #{ast.value} : #{ast.policy}" end end end |
#remove_filtered_policy(sec, ptype, field_index, *field_values) ⇒ Object
removes policy rules based on field filters from the model.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/casbin-ruby/model/policy.rb', line 123 def remove_filtered_policy(sec, ptype, field_index, *field_values) return false unless model.key?(sec) return false unless model[sec].include?(ptype) state = { tmp: [], res: false } model[sec][ptype].policy.each do |rule| state = filtered_rule(state, rule, field_values, field_index) end model[sec][ptype].policy = state[:tmp] state[:res] end |
#remove_policies(sec, ptype, rules) ⇒ Object
removes policy rules from the model.
105 106 107 108 109 110 111 112 113 |
# File 'lib/casbin-ruby/model/policy.rb', line 105 def remove_policies(sec, ptype, rules) rules.each do |rule| return false unless has_policy(sec, ptype, rule) end model[sec][ptype].policy.reject! { |rule| rules.include? rule } true end |
#remove_policy(sec, ptype, rule) ⇒ Object
removes a policy rule from the model.
96 97 98 99 100 101 102 |
# File 'lib/casbin-ruby/model/policy.rb', line 96 def remove_policy(sec, ptype, rule) return false unless has_policy(sec, ptype, rule) model[sec][ptype].policy.delete(rule) true end |
#update_policies(sec, ptype, old_rules, new_rules) ⇒ Object
update policy rules from the model.
77 78 79 80 81 82 83 |
# File 'lib/casbin-ruby/model/policy.rb', line 77 def update_policies(sec, ptype, old_rules, new_rules) old_rules.each do |rule| return false unless has_policy(sec, ptype, rule) end remove_policies(sec, ptype, old_rules) && add_policies(sec, ptype, new_rules) end |
#update_policy(sec, ptype, old_rule, new_rule) ⇒ Object
update a policy rule from the model.
70 71 72 73 74 |
# File 'lib/casbin-ruby/model/policy.rb', line 70 def update_policy(sec, ptype, old_rule, new_rule) return false unless has_policy(sec, ptype, old_rule) remove_policy(sec, ptype, old_rule) && add_policy(sec, ptype, new_rule) end |