Class: Roleback::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/roleback/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, resource:, scope:, action:, outcome:) ⇒ Rule

Returns a new instance of Rule.



9
10
11
12
13
14
15
# File 'lib/roleback/rule.rb', line 9

def initialize(role:, resource:, scope:, action:, outcome:)
	@role = role
	@resource = resource
	@scope = scope
	@action = action
	@outcome = outcome
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/roleback/rule.rb', line 6

def action
  @action
end

#outcomeObject (readonly)

Returns the value of attribute outcome.



7
8
9
# File 'lib/roleback/rule.rb', line 7

def outcome
  @outcome
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/roleback/rule.rb', line 4

def resource
  @resource
end

#roleObject (readonly)

Returns the value of attribute role.



3
4
5
# File 'lib/roleback/rule.rb', line 3

def role
  @role
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/roleback/rule.rb', line 5

def scope
  @scope
end

Instance Method Details

#conflicts_with?(rule) ⇒ Boolean

two rules are conflicting, when the have the same scope, resource and action, but different outcomes

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'lib/roleback/rule.rb', line 36

def conflicts_with?(rule)
	# if the rules are the same, they don't conflict
	return false if self == rule

	# if the scope, resource and action are the same, but the outcome is different, they conflict
	return true if @scope.name == rule.scope.name && @resource.name == rule.resource.name && @action == rule.action && @outcome.outcome != rule.outcome.outcome

	# otherwise, they don't conflict
	false
end

#keyObject



17
18
19
# File 'lib/roleback/rule.rb', line 17

def key
	"#{@scope.name}:/#{@resource.name}/#{@action}"
end

#match(resource:, scope:, action:) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/roleback/rule.rb', line 25

def match(resource:, scope:, action:)
	if @resource.match(resource) && @scope.match(scope)
		if @action == ::Roleback.any || @action.to_s == action.to_s
			return self
		end
	end

	nil
end

#numerical_valueObject

calculate a numerical value for this rule to be used for sorting the value is the sum of the following: (scope_value * scope_weight) + (resource_value * resource_weight) * (outcome_value * outcome_weight) scope_weight = 100 resource_weight = 10 outcome_weight = 1 scope_value = 1 if scope == ANY, 2 otherwise resource_value = 1 if resource == ANY, 2 otherwise outcome_value = 1 if outcome == ALLOW, 2 otherwise



56
57
58
59
60
61
62
# File 'lib/roleback/rule.rb', line 56

def numerical_value
	scope_value = @scope == ::Roleback::ANY ? 1 : 2
	resource_value = @resource == ::Roleback::ANY ? 1 : 2
	outcome_value = @outcome == ::Roleback::ALLOW ? 1 : 2

	(scope_value * 100) + (resource_value * 10) + (outcome_value * 1)
end

#to_sObject



21
22
23
# File 'lib/roleback/rule.rb', line 21

def to_s
	"#{key}->#{@outcome}"
end