-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Integrate Role and Permission Management from solidus_user_roles
(solidus_core step)
#5129
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
Open
rainerdema
wants to merge
4
commits into
solidusio:main
Choose a base branch
from
nebulab:rainerd/incorporate-roles-management-code-into-the-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ddd26a
Add Migrations for Role and Permission Set management
rainerdema d13296f
Enhance Role model for better role management
rainerdema cc9eb18
Add task to import current permissions
the-krg f4e0fd2
Assign roles from database on app configuration
the-krg 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
class PermissionSet < Spree::Base | ||
has_many :role_permissions | ||
has_many :roles, through: :role_permissions | ||
|
||
validates :name, :group, presence: true | ||
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
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
class RolePermission < Spree::Base | ||
belongs_to :role | ||
belongs_to :permission_set | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
core/db/migrate/20230607100048_create_spree_permission_sets.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateSpreePermissionSets < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :spree_permission_sets do |t| | ||
t.string :name | ||
t.string :group | ||
t.timestamps | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
core/db/migrate/20230607100109_create_spree_roles_permissions.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateSpreeRolesPermissions < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :spree_role_permissions do |t| | ||
t.references :role | ||
t.references :permission_set | ||
t.timestamps | ||
end | ||
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
30 changes: 30 additions & 0 deletions
30
core/lib/tasks/solidus/import_existing_permission_sets.rake
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :solidus do | ||
desc "Import existing permission sets to role permissions table" | ||
task import_existing_permission_sets: :environment do | ||
Zeitwerk::Loader.eager_load_all unless Rails.env.test? | ||
|
||
ActiveRecord::Base.transaction do | ||
Spree::PermissionSets::Base.descendants.each do |permission| | ||
Spree::PermissionSet.find_or_create_by(name: permission.to_s, group: permission.to_s.split("PermissionSets::").last.gsub(/Display|Management/i, "")) | ||
end | ||
|
||
Spree::AppConfiguration.new.roles.roles.each do |role_name, role_config| | ||
role_config.permission_sets.each do |set| | ||
role = Spree::Role.find_or_create_by(name: role_name) | ||
permission_set = Spree::PermissionSet.find_by(name: set.name) | ||
|
||
if permission_set | ||
Spree::RolePermission.find_or_create_by!( | ||
role: role, | ||
permission_set: permission_set | ||
) | ||
else | ||
puts "#{set} was not found." | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
56 changes: 56 additions & 0 deletions
56
core/spec/lib/tasks/solidus/import_existing_permission_sets_spec.rb
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,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
path = Spree::Core::Engine.root.join('lib/tasks/solidus/import_existing_permission_sets.rake') | ||
|
||
RSpec.describe 'solidus' do | ||
describe 'import_existing_permission_sets' do | ||
include_context( | ||
'rake', | ||
task_path: path, | ||
task_name: 'solidus:import_existing_permission_sets' | ||
) | ||
|
||
it 'creates permission sets' do | ||
expect(Spree::PermissionSet.pluck(:name)).to eq([]) | ||
|
||
task.invoke | ||
|
||
expect(Spree::PermissionSet.pluck(:name)).to eq(Spree::PermissionSets::Base.subclasses.map(&:to_s)) | ||
end | ||
|
||
context 'when there is a custom role' do | ||
let(:role_name) { :customer_service } | ||
let(:permissions) { ['Spree::PermissionSets::OrderDisplay', 'Spree::PermissionSets::UserDisplay', 'Spree::PermissionSets::ProductDisplay'] } | ||
|
||
before do | ||
roles = Spree::RoleConfiguration.new.tap do |role| | ||
role.assign_permissions :default, ['Spree::PermissionSets::DefaultCustomer'] | ||
role.assign_permissions :admin, ['Spree::PermissionSets::SuperUser'] | ||
role.assign_permissions role_name, permissions | ||
end | ||
|
||
allow_any_instance_of(Spree::AppConfiguration).to receive(:roles).and_return(roles) | ||
end | ||
|
||
it 'creates the new role with permissions' do | ||
expect(Spree::Role.find_by(name: role_name.to_s)).not_to be_present | ||
|
||
task.invoke | ||
|
||
role = Spree::Role.find_by(name: role_name.to_s) | ||
expect(role).to be_present | ||
expect(role.permission_sets.pluck(:name)).to match_array(permissions) | ||
end | ||
end | ||
|
||
context 'when permission set is not found' do | ||
it 'prints out the missing permission set' do | ||
allow(Spree::PermissionSet).to receive(:find_by).and_return(nil) | ||
|
||
expect { task.invoke }.to output(a_string_including('Spree::PermissionSets::DefaultCustomer')).to_stdout | ||
end | ||
end | ||
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Spree::Role, type: :model do | ||
describe '.non_base_roles' do | ||
subject do | ||
Spree::Role.non_base_roles | ||
end | ||
|
||
context 'when there is a custom role' do | ||
let(:role) { create(:role, name: 'custom role') } | ||
let(:admin_role) { create(:admin_role) } | ||
let(:default_role) { create(:role, name: 'default') } | ||
|
||
it { is_expected.to include(role) } | ||
it { is_expected.not_to include(admin_role, default_role) } | ||
end | ||
|
||
context 'when there is no custom roles' do | ||
it { is_expected.to be_empty } | ||
end | ||
end | ||
end |
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.