Module: Operations

Defined in:
lib/operations.rb,
lib/operations/utils.rb,
lib/operations/config.rb,
lib/operations/engine.rb,
lib/operations/errors.rb,
lib/operations/railtie.rb,
lib/operations/version.rb,
lib/operations/enforcer.rb,
lib/operations/operation.rb,
lib/operations/act_as_operationable.rb,
lib/operations/errors/base_exception.rb,
lib/operations/errors/invalid_field_error.rb,
lib/operations/errors/not_logged_in_error.rb,
lib/operations/errors/not_authorized_error.rb,
lib/operations/errors/not_implemented_error.rb,
lib/operations/errors/invalid_operation_error.rb,
app/controllers/operations/enforced_controller.rb

Defined Under Namespace

Modules: ActAsOperationable, Config, Enforcer, Errors, Utils Classes: EnforcedController, Engine, Operation, Railtie

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.allowed_int_roles_for(scope) ⇒ Object



61
62
63
64
# File 'lib/operations.rb', line 61

def allowed_int_roles_for(scope)
  allowed_named_roles_for(scope)
      .map{|named_scope| user_role_int_from(named_scope)}
end

.allowed_named_roles_for(scope) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/operations.rb', line 48

def allowed_named_roles_for(scope)
  current_value = user_role_value_from(scope)
  user_roles
      .select{|role| role[:name] == :all ||
          !role[:value].nil? && role[:value] <= current_value}
      .map{|role| role[:name]}
end

.allowed_value_roles_for(scope) ⇒ Object



66
67
68
69
# File 'lib/operations.rb', line 66

def allowed_value_roles_for(scope)
  allowed_named_roles_for(scope)
      .map{|named_scope| user_role_value_from(named_scope)}
end

.allowing(scope) ⇒ Object

Returns the operations that have scope scope



43
44
45
46
# File 'lib/operations.rb', line 43

def allowing(scope)
  return nil if scope.blank?
  operations_list.select{|operation| operation > scope}
end

.allows?(user, name) ⇒ Boolean

Determines if the user is allowed to execute the operation name

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/operations.rb', line 72

def allows?(user, name)
  operation = from_string(name)
  return false if operation.nil?
  operation.users.map{|u| u.id}.include?(user.id)
end

.from_string(name) ⇒ Object

Returns the operation associated with the passed in name



38
39
40
# File 'lib/operations.rb', line 38

def from_string(name)
  operations_list.select{|operation| operation.name.to_s == name.to_s}[0]
end

.from_user_type(scope) ⇒ Object



56
57
58
59
# File 'lib/operations.rb', line 56

def from_user_type(scope)
  value = allowed_named_roles_for(scope.to_sym)
  scopes = value.reject{|role| role.nil?}
end

.from_uuid(uuid) ⇒ Object

Returns the operation associated with the passed in UUID



33
34
35
# File 'lib/operations.rb', line 33

def from_uuid(uuid)
  operations_list.select{|operation| operation.uuid == uuid}[0]
end

.named_user_roles(all: false) ⇒ Object



22
23
24
25
26
# File 'lib/operations.rb', line 22

def named_user_roles(all: false)
  roles = Operations::Config.user_roles
  roles = roles.reject{|role| role[:db_value].nil?} unless all
  roles.map{|role| role[:name]}
end

.operations_for(scope) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/operations.rb', line 114

def operations_for(scope)
  scopes = scopes_as(scope)
  list = operations_list
  if %w{all nobody}.include?(scope.to_s)
    list.select{|op| op == scope}.uniq
  else
    list.select{|op| op >= scope}.uniq
  end
end

.operations_listObject



14
15
16
# File 'lib/operations.rb', line 14

def operations_list
  Operations::Config.get_operations_list
end

.operations_list_by_uuidObject



28
29
30
# File 'lib/operations.rb', line 28

def operations_list_by_uuid
  operations_list.map{|op| {operation: op, uuid: op.uuid}}
end

.scopes_as(scope) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/operations.rb', line 105

def scopes_as(scope)
  value = user_role_value_from(scope)
  return [] if value.nil?
  return [scope] if %w{all nobody}.include?(scope.to_s)
  user_roles
      .select{|role| role[:value] && role[:value] >= value}
      .map{|role| role[:name]}
end

.user_role_int_from(role_name) ⇒ Object



134
135
136
137
138
# File 'lib/operations.rb', line 134

def user_role_int_from(role_name)
  result = user_roles.select{|role| role[:name] == role_name}[0]
  return nil if result.nil?
  result[:db_value]
end

.user_role_name_from(role_int) ⇒ Object



146
147
148
149
150
# File 'lib/operations.rb', line 146

def user_role_name_from(role_int)
  result = user_roles.select{|role| role[:db_value] == role_int}[0]
  return nil if result.nil?
  result[:name]
end

.user_role_value_from(role_name) ⇒ Object



140
141
142
143
144
# File 'lib/operations.rb', line 140

def user_role_value_from(role_name)
  result = user_roles.select{|role| role[:name] == role_name}[0]
  return nil if result.nil?
  result[:value]
end

.user_rolesObject



18
19
20
# File 'lib/operations.rb', line 18

def user_roles
  Operations::Config.user_roles
end

.user_roles_from(role_name, select_only: false) ⇒ Object

Returns a list of users roles that qualify as role_name



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/operations.rb', line 84

def user_roles_from(role_name, select_only: false)
  value = user_role_value_from(role_name)
  return [] if value.nil?
  compare =-> (role, value) do
    select_only ? role[:value] == value : role[:value] <= value
  end
  user_roles
      .select{|role| role[:value] && compare.call(role, value)}
      .map{|role| role[:db_value]}
      .reject{|res| res.nil?}
end

.user_roles_namesObject

Returns a list of user roles names straight from the config file



79
80
81
# File 'lib/operations.rb', line 79

def user_roles_names
  user_roles.map{|ur| ur[:name]}
end

.user_roles_until(role_name) ⇒ Object

Returns a list of users roles that do not qualify as role_name



97
98
99
100
101
102
103
# File 'lib/operations.rb', line 97

def user_roles_until(role_name)
  value = user_role_value_from(role_name)
  return [] if value.nil?
  user_roles
      .select{|role| role[:value] && role[:value] > value}
      .map{|role| role[:db_value]}
end

.users_acting_as(role_name, select_only: false) ⇒ Object

Returns a list of users that have roles user_roles_from



125
126
127
# File 'lib/operations.rb', line 125

def users_acting_as(role_name, select_only: false)
  User.where(role: user_roles_from(role_name, select_only: select_only))
end

.users_denied_from(role_name) ⇒ Object

Returns a list of users that have roles user_roles_until



130
131
132
# File 'lib/operations.rb', line 130

def users_denied_from(role_name)
  User.where(role: user_roles_until(role_name))
end