Class: Foreman::Plugin::RoleLock

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman/plugin/role_lock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_id) ⇒ RoleLock

Returns a new instance of RoleLock.


6
7
8
# File 'app/services/foreman/plugin/role_lock.rb', line 6

def initialize(plugin_id)
  @plugin_id = plugin_id
end

Instance Attribute Details

#plugin_idObject (readonly)

Returns the value of attribute plugin_id


4
5
6
# File 'app/services/foreman/plugin/role_lock.rb', line 4

def plugin_id
  @plugin_id
end

Instance Method Details

#create_plugin_role(name, permissions, description = '') ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/foreman/plugin/role_lock.rb', line 39

def create_plugin_role(name, permissions, description = '')
  Role.ignore_locking do
    begin
      role = Role.create! :name => name, :origin => @plugin_id, :description => description
      role.add_permissions!(permissions)
    rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
      role = Role.find_by_name(name)
    end
    role
  end
end

#generate_name(prefix, original_name, num = nil) ⇒ Object


71
72
73
74
# File 'app/services/foreman/plugin/role_lock.rb', line 71

def generate_name(prefix, original_name, num = nil)
  new_name = "#{prefix} #{original_name}"
  num ? new_name << " #{num}" : new_name
end

#last_role_num(role, role_name) ⇒ Object


76
77
78
# File 'app/services/foreman/plugin/role_lock.rb', line 76

def last_role_num(role, role_name)
  Integer(role.name.split("#{role_name} ").last) rescue false
end

#process_role(name, permissions, description = '') ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/foreman/plugin/role_lock.rb', line 17

def process_role(name, permissions, description = '')
  role = Role.find_by :name => name
  if role
    role.update_column(:description, description) if role.description != description

    if role&.origin && role.permission_diff(permissions).present?
      return update_plugin_role_permissions role, permissions
    end

    if role&.permission_diff(permissions)&.empty?
      role.update_attribute :origin, @plugin_id if role.origin.empty?
      return role
    end

  end

  Role.without_auditing do
    rename_existing role, name if role
    create_plugin_role name, permissions, description
  end
end

#register_role(name, permissions, role_registry, description = '') ⇒ Object


10
11
12
13
14
15
# File 'app/services/foreman/plugin/role_lock.rb', line 10

def register_role(name, permissions, role_registry, description = '')
  User.as_anonymous_admin do
    role = process_role name, permissions, description
    role_registry.role_ids << role.id
  end
end

#rename_existing(role, original_name) ⇒ Object


61
62
63
64
65
66
67
68
69
# File 'app/services/foreman/plugin/role_lock.rb', line 61

def rename_existing(role, original_name)
  prefix = "Customized"
  role_name = generate_name prefix, original_name
  candidate_roles = Role.where('name like ?', "#{role_name}%").order(:name => :desc)
  return role.update_attribute :name, role_name if candidate_roles.empty?
  last_role = candidate_roles.detect { |candidate_role| last_role_num candidate_role, role_name }
  last_num = last_role ? last_role_num(last_role, role_name) : 0
  role.update_attribute :name, generate_name(prefix, original_name, last_num + 1)
end

#update_plugin_role_permissions(role, permissions) ⇒ Object


51
52
53
54
55
56
57
58
59
# File 'app/services/foreman/plugin/role_lock.rb', line 51

def update_plugin_role_permissions(role, permissions)
  Role.ignore_locking do
    missing_permissions = role.missing_permissions(permissions)
    role.add_permissions!(missing_permissions) unless missing_permissions.empty?
    extra_permissions = role.extra_permissions(permissions)
    role.remove_permissions!(*extra_permissions) unless extra_permissions.empty?
    role
  end
end