forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
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 solidusio#5846 from MadelineCollier/admin-role-cre…
…ation-with-permissions-round-2 [Admin] Allow assignment of permission sets when creating/editing admin roles
- Loading branch information
Showing
56 changed files
with
927 additions
and
6 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
29 changes: 29 additions & 0 deletions
29
admin/app/components/solidus_admin/ui/checkbox_row/component.html.erb
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,29 @@ | ||
<% if @layout == :default && @options.present? %> | ||
<span class="p-2 flex hover:bg-gray-50 rounded-md"> | ||
<span class="w-1/2"><%= @row_title %></span> | ||
<span class="w-1/2 text-right"> | ||
<% @options.each do |option| %> | ||
<label class="px-2 cursor-pointer"> | ||
<%= @form.check_box(@method.to_s, { checked: @form.object.try(@method.to_sym)&.include?(option[:id]), multiple: true }, option[:id], nil)%> | ||
<span class="capitalize"><%= option[:label] %></span> | ||
</label> | ||
<% end %> | ||
</span> | ||
</span> | ||
<% end %> | ||
<% if @layout == :subsection && @options.present? %> | ||
<div class="hover:bg-gray-50 rounded-md"> | ||
<div class="font-semibold px-2 py-2 mt-2 mb-1"><%= @row_title %></div> | ||
<div class="px-4"> | ||
<% @options.each do |option| %> | ||
<div class="py-1"> | ||
<label class="cursor-pointer"> | ||
<%= @form.check_box(@method.to_s, { checked: @form.object.try(@method.to_sym)&.include?(option[:id]), multiple: true }, option[:id], nil)%> | ||
<span class="capitalize"><%= option[:label] %></span> | ||
</label> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
<% end %> |
11 changes: 11 additions & 0 deletions
11
admin/app/components/solidus_admin/ui/checkbox_row/component.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::UI::CheckboxRow::Component < SolidusAdmin::BaseComponent | ||
def initialize(options:, row_title:, form:, method:, layout: :default) | ||
@options = options | ||
@row_title = row_title | ||
@form = form | ||
@method = method | ||
@layout = layout | ||
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
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
module PermissionSetsHelper | ||
# @param permission_sets [Array<Spree::PermissionSet>] an array of | ||
# PermissionSet objects to be organized into categories based on their | ||
# names. | ||
# @param view_label [String] A string of your choice associated with "View" | ||
# or "Display" level permissions. Used when rendering the checkbox. | ||
# @param edit_label [String] A string of your choice associated with "Edit" | ||
# or "Management" level permissions. Used when rendering the checkbox. | ||
def organize_permissions(permission_sets:, view_label:, edit_label:) | ||
return {} if permission_sets.blank? | ||
|
||
permission_sets.each_with_object({}) do |permission, grouped_permissions| | ||
group_key = permission.category.to_sym | ||
|
||
case permission.privilege | ||
when "display" | ||
grouped_permissions[group_key] ||= [] | ||
grouped_permissions[group_key] << { label: view_label, id: permission.id } | ||
when "management" | ||
grouped_permissions[group_key] ||= [] | ||
grouped_permissions[group_key] << { label: edit_label, id: permission.id } | ||
else | ||
grouped_permissions[:other] ||= [] | ||
grouped_permissions[:other] << { label: permission.name, id: permission.id } | ||
end | ||
end | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
admin/spec/components/previews/solidus_admin/ui/checkbox_row/component_preview.rb
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,10 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "ui/checkbox_row" | ||
class SolidusAdmin::UI::CheckboxRow::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
def overview | ||
render_with_template | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
...pec/components/previews/solidus_admin/ui/checkbox_row/component_preview/overview.html.erb
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 @@ | ||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Default Layout | ||
</h6> | ||
|
||
<%= render current_component.new(options: [{ label: "true", id: 1 }, { label: "false", id: 0 }], row_title: "I have flossed recently", form: ActionView::Helpers::FormBuilder.new(:object, Object.new, self, {}), method: "attribute", layout: :default) %> | ||
</div> | ||
|
||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Subsection Layout | ||
</h6> | ||
|
||
<%= render current_component.new(options: [{ label: "Pizza", id: 1 }, { label: "Pasta", id: 0 }, { label: "Soup", id: 3 }], row_title: "Favourite foods", form: ActionView::Helpers::FormBuilder.new(:object, Object.new, self, {}), method: "attribute", layout: :subsection) %> | ||
</div> |
9 changes: 9 additions & 0 deletions
9
admin/spec/components/solidus_admin/ui/checkbox_row/component_spec.rb
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::UI::CheckboxRow::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
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 |
---|---|---|
|
@@ -3,7 +3,26 @@ | |
require 'spec_helper' | ||
|
||
describe "Roles", :js, type: :feature do | ||
before { sign_in create(:admin_user, email: '[email protected]') } | ||
before do | ||
sign_in create(:admin_user, email: '[email protected]') | ||
end | ||
|
||
let!(:settings_edit_permission) { | ||
Spree::PermissionSet.find_or_create_by!( | ||
name: "ConfigurationManagement", | ||
set: "Spree::PermissionSets::ConfigurationManagement", | ||
privilege: "management", | ||
category: "configuration" | ||
) | ||
} | ||
let!(:settings_view_permission) { | ||
Spree::PermissionSet.find_or_create_by!( | ||
name: "ConfigurationDisplay", | ||
set: "Spree::PermissionSets::ConfigurationDisplay", | ||
privilege: "display", | ||
category: "configuration" | ||
) | ||
} | ||
|
||
it "lists roles and allows deleting them" do | ||
create(:role, name: "Customer Role" ) | ||
|
@@ -51,10 +70,20 @@ | |
fill_in "Name", with: "Purchaser" | ||
fill_in "Description", with: "A person who buys stuff" | ||
|
||
within("form.new_role") do | ||
expect(page).to have_content("Choose permissions") | ||
expect(page).to have_content("Settings") | ||
expect(page).to have_content("Edit") | ||
expect(page).to have_content("View") | ||
find('label', text: 'View').find('input[type=checkbox]').click | ||
end | ||
|
||
click_on "Add Role" | ||
|
||
expect(page).to have_content("Role was successfully created.") | ||
expect(Spree::Role.find_by(name: "Purchaser")).to be_present | ||
expect(Spree::Role.find_by(name: "Purchaser").permission_set_ids) | ||
.to contain_exactly(settings_view_permission.id) | ||
expect(page.current_url).to include(query) | ||
end | ||
end | ||
|
@@ -89,11 +118,13 @@ | |
let(:query) { "?page=1&q%5Bname_cont%5D=er" } | ||
|
||
before do | ||
Spree::Role.create(name: "Reviewer") | ||
Spree::Role.create(name: "Reviewer", permission_sets: [settings_edit_permission]) | ||
visit "/admin/roles#{query}" | ||
find_row("Reviewer").click | ||
expect(page).to have_content("Edit Role") | ||
expect(page).to be_axe_clean | ||
expect(Spree::Role.find_by(name: "Reviewer").permission_set_ids) | ||
.to contain_exactly(settings_edit_permission.id) | ||
end | ||
|
||
it "opens a modal" do | ||
|
@@ -107,12 +138,26 @@ | |
fill_in "Name", with: "Publisher" | ||
fill_in "Description", with: "A person who publishes stuff" | ||
|
||
within("form.edit_role") do | ||
expect(page).to have_content("Choose permissions") | ||
expect(page).to have_content("Settings") | ||
expect(page).to have_content("Edit") | ||
expect(page).to have_content("View") | ||
expect(find('label', text: 'Edit').find('input[type=checkbox]').checked?).to eq(true) | ||
find('label', text: 'Edit').find('input[type=checkbox]').uncheck | ||
find('label', text: 'View').find('input[type=checkbox]').check | ||
end | ||
|
||
click_on "Update Role" | ||
expect(page).to have_content("Role was successfully updated.") | ||
expect(page).to have_content("Publisher") | ||
expect(page).to have_content("A person who publishes stuff") | ||
expect(page).not_to have_content("Reviewer") | ||
expect(Spree::Role.find_by(name: "Publisher")).to be_present | ||
expect(Spree::Role.find_by(name: "Publisher").permission_set_ids) | ||
.to contain_exactly( | ||
settings_view_permission.id, | ||
) | ||
expect(page.current_url).to include(query) | ||
end | ||
end | ||
|
Oops, something went wrong.