Class: Spree::Role
- Inherits:
-
Object
- Object
- Spree::Role
- Defined in:
- app/models/spree/role.rb
Constant Summary collapse
- ADMIN_ROLE =
'admin'
Class Method Summary collapse
-
.default_admin_permissions_for(resource) ⇒ Array<String>
The keys a resource's own super-role is born with.
-
.default_admin_role(resource = nil) ⇒ Spree::Role
Class Methods.
Instance Method Summary collapse
-
#admin? ⇒ Boolean
The protected super-role: everything in this store.
-
#audience ⇒ Symbol?
The permission catalog's name for the panel this role belongs to — derived from the owning resource, never stored, so the two cannot drift.
-
#can_be_deleted? ⇒ Boolean
A role can be deleted only when it is mutable and nothing references it: staff assignments and pending invitations must be reassigned first.
-
#mutable? ⇒ Boolean
Whether the dashboard may edit or delete this role.
-
#permissions ⇒ Object
Rows predating the column (or raw inserts) hold NULL — the array contract holds regardless.
- #permissions=(value) ⇒ Object
-
#staff? ⇒ Boolean
Whether this role governs a store's own back office, as opposed to another panel's.
Class Method Details
.default_admin_permissions_for(resource) ⇒ Array<String>
Returns the keys a resource's own super-role is born with.
96 97 98 99 100 101 |
# File 'app/models/spree/role.rb', line 96 def self.(resource) audience = resource.try(:role_audience) return [] if audience.blank? Spree..grantable_keys(audience) end |
.default_admin_role(resource = nil) ⇒ Spree::Role
Class Methods
The resource's own super-role, created on first ask. Each owns one: "admin" means everything in this store, so it cannot be shared.
Permissions come from the audience the resource grants to, so a role is
born holding them whichever caller asks first — an invitation filling in
its default role, or the resource seeding its own team. A store's stays
empty by design: its super-role is short-circuited by
Ability#activate_full_access and needs no keys, while a seller's is an
ordinary role that would otherwise be refused by every endpoint.
86 87 88 89 90 91 92 93 |
# File 'app/models/spree/role.rb', line 86 def self.default_admin_role(resource = nil) resource ||= Spree::Current.store || Spree::Store.current for_resource(resource).find_or_create_by(name: ADMIN_ROLE) do |role| role.mutable = false role. = (resource) end end |
Instance Method Details
#admin? ⇒ Boolean
The protected super-role: everything in this store. Guarded by the owning
resource as well as the name — names are unique per resource, so a seller
may legitimately call a role "admin" and it must never be mistaken for
this one — and by the mutable flag, so a stale flag cannot expose it.
109 110 111 |
# File 'app/models/spree/role.rb', line 109 def admin? staff? && (name == ADMIN_ROLE || name_was == ADMIN_ROLE) end |
#audience ⇒ Symbol?
The permission catalog's name for the panel this role belongs to —
derived from the owning resource, never stored, so the two cannot drift.
Spree::Seller reads as :seller.
126 127 128 129 130 |
# File 'app/models/spree/role.rb', line 126 def audience return if resource_type.blank? resource_type.demodulize.underscore.to_sym end |
#can_be_deleted? ⇒ Boolean
A role can be deleted only when it is mutable and nothing references it: staff assignments and pending invitations must be reassigned first. Accepted or expired invitations are history and cascade on destroy.
145 146 147 |
# File 'app/models/spree/role.rb', line 145 def can_be_deleted? mutable? && !role_users.exists? && !invitations.pending.exists? end |
#mutable? ⇒ Boolean
Whether the dashboard may edit or delete this role. False for the admin
role and for host-locked rows (mutable: false via seeds/console).
136 137 138 |
# File 'app/models/spree/role.rb', line 136 def mutable? super && !admin? end |
#permissions ⇒ Object
Rows predating the column (or raw inserts) hold NULL — the array contract holds regardless.
20 21 22 |
# File 'app/models/spree/role.rb', line 20 def super || [] end |
#permissions=(value) ⇒ Object
24 25 26 |
# File 'app/models/spree/role.rb', line 24 def (value) super(Array(value).map(&:to_s).reject(&:blank?).uniq) end |