GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AddCustomRoles   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 113
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generate_scoped_role() 0 12 3
A down() 0 10 1
A assign_new_users() 0 11 1
B up() 0 75 1
1
# frozen_string_literal: true
2
3
class AddCustomRoles < ActiveRecord::Migration[5.2]
4
  def up
5
    super_admin_id = -1
6
    user_id = -1
7
    admin_id = -1
8
    denied_id = -1
9
    pending_id = -1
10
11
    old_roles = ActiveRecord::Base.connection.execute("select * from roles")
12
13
    # Determine what ids corresponded to what roles in the old table
14
    old_roles.each do |role|
15
      if role["name"] == "super_admin"
16
        super_admin_id = role["id"]
17
      elsif role["name"] == "user"
18
        user_id = role["id"]
19
      elsif role["name"] == "admin"
20
        admin_id = role["id"]
21
      elsif role["name"] == "denied"
22
        denied_id = role["id"]
23
      elsif role["name"] == "pending"
24
        pending_id = role["id"]
25
      end
26
    end
27
28
    # Replace Rolify's table with our own
29
    drop_table :roles
30
31
    create_table(:roles) do |t|
32
      t.string :name
33
      t.integer :priority, default: 9999
34
      t.boolean :can_create_rooms, default: false
35
      t.boolean :send_promoted_email, default: false
36
      t.boolean :send_demoted_email, default: false
37
      t.boolean :can_edit_site_settings, default: false
38
      t.boolean :can_edit_roles, default: false
39
      t.boolean :can_manage_users, default: false
40
      t.string  :colour
41
      t.string :provider
42
43
      t.timestamps
44
    end
45
46
    add_index(:roles, :name)
47
    add_index(:roles, [:name, :provider], unique: true)
48
49
    # Look at all the old role assignments and and for each role create a new role
50
    # that is scoped to the provider
51
    old_assignments = ActiveRecord::Base.connection.execute("select * from users_roles")
52
    new_assignments = []
53
54
    old_assignments.each do |assignment|
55
      begin
56
        user = User.find(assignment["user_id"])
57
      rescue
58
        next
59
      end
60
61
      new_assignment = { "user_id" => assignment["user_id"] }
62
      if assignment["role_id"] == super_admin_id
63
        new_assignment["new_role_id"] = generate_scoped_role(user, "super_admin")
64
      elsif assignment["role_id"] == user_id
65
        new_assignment["new_role_id"] = generate_scoped_role(user, "user")
66
      elsif assignment["role_id"] == admin_id
67
        new_assignment["new_role_id"] = generate_scoped_role(user, "admin")
68
      elsif assignment["role_id"] == denied_id
69
        new_assignment["new_role_id"] = generate_scoped_role(user, "denied")
70
      elsif assignment["role_id"] == pending_id
71
        new_assignment["new_role_id"] = generate_scoped_role(user, "pending")
72
      end
73
74
      new_assignments << new_assignment
75
    end
76
77
    assign_new_users(new_assignments)
78
  end
79
80
  def generate_scoped_role(user, role_name)
81
    provider = Rails.configuration.loadbalanced_configuration ? user.provider : 'greenlight'
82
    new_role = Role.find_by(name: role_name, provider: provider)
83
84
    if new_role.nil?
85
      Role.create_default_roles(provider)
86
87
      new_role = Role.find_by(name: role_name, provider: provider)
88
    end
89
90
    new_role.id
91
  end
92
93
  def assign_new_users(new_assignments)
94
    # Delete the old assignments
95
    ActiveRecord::Base.connection.execute("DELETE FROM users_roles")
96
    # Add the role assignments to the new roles
97
    new_assignments.each do |assignment|
98
      if assignment['new_role_id']
99
        ActiveRecord::Base.connection.execute("INSERT INTO users_roles (user_id, role_id)" \
100
          " VALUES (#{assignment['user_id']}, #{assignment['new_role_id']})")
101
      end
102
    end
103
  end
104
105
  def down
106
    drop_table :roles
107
108
    create_table(:roles) do |t|
109
      t.string :name
110
      t.references :resource, polymorphic: true
111
112
      t.timestamps
113
    end
114
  end
115
end
116