Class: Casbin::Model::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/casbin-ruby/model/policy.rb

Direct Known Subclasses

Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePolicy

Returns a new instance of Policy.



10
11
12
# File 'lib/casbin-ruby/model/policy.rb', line 10

def initialize
  @model = {}
end

Instance Attribute Details

#modelObject (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.



58
59
60
61
62
63
64
65
66
# File 'lib/casbin-ruby/model/policy.rb', line 58

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.



49
50
51
52
53
54
55
# File 'lib/casbin-ruby/model/policy.rb', line 49

def add_policy(sec, ptype, rule)
  return false if has_policy(sec, ptype, rule)

  model[sec][ptype].policy << rule

  true
end

initializes the roles in RBAC.



15
16
17
18
19
20
21
22
# File 'lib/casbin-ruby/model/policy.rb', line 15

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_policyObject

clears all current policy.



38
39
40
41
42
43
44
45
46
# File 'lib/casbin-ruby/model/policy.rb', line 38

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.



115
116
117
118
119
# File 'lib/casbin-ruby/model/policy.rb', line 115

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.



85
86
87
# File 'lib/casbin-ruby/model/policy.rb', line 85

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.



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/casbin-ruby/model/policy.rb', line 136

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 unless values.include?(value)
  end

  values
end

#has_policy(sec, ptype, rule) ⇒ Object

determines whether a model has the specified policy rule.



90
91
92
# File 'lib/casbin-ruby/model/policy.rb', line 90

def has_policy(sec, ptype, rule)
  model.key?(sec) && model[sec].key?(ptype) && model[sec][ptype].policy.include?(rule)
end

Log using info



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/casbin-ruby/model/policy.rb', line 25

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.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/casbin-ruby/model/policy.rb', line 122

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.



104
105
106
107
108
109
110
111
112
# File 'lib/casbin-ruby/model/policy.rb', line 104

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.



95
96
97
98
99
100
101
# File 'lib/casbin-ruby/model/policy.rb', line 95

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.



76
77
78
79
80
81
82
# File 'lib/casbin-ruby/model/policy.rb', line 76

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.



69
70
71
72
73
# File 'lib/casbin-ruby/model/policy.rb', line 69

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