Skip to content

Commit

Permalink
Enhance Role model for better role management
Browse files Browse the repository at this point in the history
This commit expands the functionality of the `Spree::Role` model to provide more
granular and efficient control over user roles and permissions.

  * Spree::RolePermission
  * Spree::Role enhancements
  * Spree::PermissionSet

These enhancements are a part of our broader initiative to improve the
flexibility and extensibility of role and permission management in Solidus,
adapting functionality from the `solidus_user_roles` gem.
  • Loading branch information
rainerdema authored and the-krg committed Aug 18, 2023
1 parent 82b8580 commit 3bbbf0c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/app/models/spree/permission_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Spree
class PermissionSet < Spree::Base
has_many :role_permissions
has_many :roles, through: :role_permissions

validates :name, :set, presence: true

scope :display_permissions, -> { where('name LIKE ?', '%Display') }
scope :management_permissions, -> { where('name LIKE ?', '%Management') }

scope :custom_permissions, -> {
where.not(id: display_permissions)
.where.not(id: management_permissions)
}
end
end
14 changes: 14 additions & 0 deletions core/app/models/spree/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
module Spree
class Role < Spree::Base
has_many :role_users, class_name: "Spree::RoleUser", dependent: :destroy
has_many :role_permissions, dependent: :destroy
has_many :permission_sets, through: :role_permissions
has_many :users, through: :role_users

scope :non_base_roles, -> { where.not(name: ['admin']) }

validates_uniqueness_of :name, case_sensitive: true
validates :name, uniqueness: true
after_save :assign_permissions

def admin?
name == "admin"
end

def permission_sets_constantized
permission_sets.map(&:set).map(&:constantize)
end

def assign_permissions
::Spree::Config.roles.assign_permissions name, permission_sets_constantized
end
end
end
8 changes: 8 additions & 0 deletions core/app/models/spree/role_permission.rb
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

0 comments on commit 3bbbf0c

Please sign in to comment.