-
Notifications
You must be signed in to change notification settings - Fork 1
Add user roles #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add user roles #349
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b5fa0d
Add the Rolify gem
reefdog 9acb2dd
Rolify the User model
reefdog 60cec4d
Use role instead of attribute to define admins
reefdog df97cee
Assign default roles to new users
reefdog 27df82f
Remove `new_user` role during account creation
reefdog baf8952
Add tests for Insights and MediaVault roles
reefdog df4b8d4
Use `new_user` role for "already-setup check
reefdog f9a9cfd
Clean up users fixtures and update tests
reefdog d3a4f3e
Add Rubocop exclusions for Rolify stuff
reefdog d27e0a8
Seed database with Rolify roles and tweak config
reefdog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| 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 | ||
| config.use_dynamic_shortcuts | ||
|
|
||
| # Configuration to remove roles from database once the last resource is removed. Default is: true | ||
| # config.remove_role_if_empty = false | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -11,12 +11,12 @@ | |
| 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 +84,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 +93,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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.