-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from TechAndCheck/299-add-user-roles
Add user roles
- Loading branch information
Showing
27 changed files
with
248 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Role < ApplicationRecord | ||
has_and_belongs_to_many :users, join_table: :users_roles | ||
|
||
belongs_to :resource, | ||
polymorphic: true, | ||
optional: true | ||
|
||
validates :resource_type, | ||
inclusion: { in: Rolify.resource_types }, | ||
allow_nil: true | ||
|
||
scopify | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Rolify.configure do |config| | ||
# By default ORM adapter is ActiveRecord. uncomment to use mongoid | ||
# config.use_mongoid | ||
|
||
# Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false | ||
# | ||
# Enabled because these are convenient methods, and according to the Rolify documentation they | ||
# are generated at boot time (and when `add_role` is run), so shouldn't hurt performance. | ||
config.use_dynamic_shortcuts | ||
|
||
# Configuration to remove roles from database once the last resource is removed. Default is: true | ||
# | ||
# Toggled to false because we have well-defined user roles that we don't want removed, even if | ||
# the last user using them is deleted. | ||
config.remove_role_if_empty = false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class RolifyCreateRoles < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table "roles", id: :uuid do |t| | ||
t.string :name | ||
t.references :resource, polymorphic: true | ||
|
||
t.timestamps | ||
end | ||
|
||
create_table "users_roles", id: false do |t| | ||
t.references :user, type: :uuid | ||
t.references :role, type: :uuid | ||
end | ||
|
||
add_index :roles, [ :name, :resource_type, :resource_id ] | ||
add_index :users_roles, [ :user_id, :role_id ] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class ConvertAdminsToRolify < ActiveRecord::Migration[7.0] | ||
def up | ||
User.where(super_admin: true).each { |admin| admin.add_role :admin } | ||
|
||
remove_column :users, :super_admin | ||
end | ||
def down | ||
add_column :users, :super_admin, :boolean, default: false | ||
|
||
User.with_role(:admin).each do |admin| | ||
admin.update super_admin: true | ||
admin.remove_role :admin | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,22 @@ | |
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) | ||
# Character.create(name: 'Luke', movie: movies.first) | ||
|
||
Role.create!([ | ||
{ name: "new_user" }, | ||
{ name: "insights_user" }, | ||
{ name: "media_vault_user" }, | ||
{ name: "admin" }, | ||
]) | ||
|
||
easy_password = "password123" | ||
|
||
# Super-admin account; no applicant necessary. | ||
User.create!({ | ||
admin = User.create!({ | ||
email: "[email protected]", | ||
password: easy_password, | ||
super_admin: true, | ||
confirmed_at: Time.now, | ||
}) | ||
admin.add_role :admin | ||
|
||
Applicant.create!([ | ||
# This applicant is a fresh, unconfirmed applicant. | ||
|
@@ -84,8 +91,6 @@ | |
# Override the randomized initial password. | ||
password: easy_password, | ||
password_confirmation: easy_password, | ||
# Make sure they don't look fresh. | ||
sign_in_count: 1, | ||
}) | ||
|
||
# Create the restricted user | ||
|
@@ -95,8 +100,6 @@ | |
# Override the randomized initial password. | ||
password: easy_password, | ||
password_confirmation: easy_password, | ||
# Make sure they don't look fresh. | ||
sign_in_count: 1, | ||
}) | ||
|
||
Sources::Tweet.create_from_url "https://twitter.com/kairyssdal/status/1415029747826905090" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.