Module: Spree::UserManagement
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/spree/user_management.rb
Overview
Included into whatever a role can govern — a Store today, a marketplace seller later. Assignments hang off the resource's own roles, so a user is "in" a resource exactly when they hold one of its roles.
Instance Method Summary collapse
-
#add_user(user, role = nil) ⇒ Spree::RoleUser
Adds a user to the resource with the default user role If no role is provided, the default user role will be used If a role is provided, it will be used instead of the default user role.
-
#default_user_role ⇒ Object
this can be overridden in the base model to use a different user role, eg.
-
#remove_user(user) ⇒ void
Revokes a user's access to the resource, including any invitation still pending for them here, so they cannot rejoin by accepting it.
Instance Method Details
#add_user(user, role = nil) ⇒ Spree::RoleUser
Adds a user to the resource with the default user role If no role is provided, the default user role will be used If a role is provided, it will be used instead of the default user role
The role must be one this resource owns: a role names what it governs, so granting another's here would add the user somewhere else entirely.
26 27 28 29 30 31 |
# File 'app/models/concerns/spree/user_management.rb', line 26 def add_user(user, role = nil) role ||= default_user_role raise ArgumentError, "#{role.name} does not belong to this #{self.class.name}" unless role.resource == self role.role_users.find_or_create_by!(user: user) end |
#default_user_role ⇒ Object
this can be overridden in the base model to use a different user role, eg. 'seller'
50 51 52 |
# File 'app/models/concerns/spree/user_management.rb', line 50 def default_user_role Spree::Role.default_admin_role(self) end |
#remove_user(user) ⇒ void
This method returns an undefined value.
Revokes a user's access to the resource, including any invitation still pending for them here, so they cannot rejoin by accepting it.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/concerns/spree/user_management.rb', line 37 def remove_user(user) # Invitations first: an acceptance locks its invitation row, so this # waits for one in flight and then removes the role it granted. transaction do revoke_pending_invitations_for(user) # One DELETE, evaluated against the latest committed rows. A per-row # destroy first SELECTs from MySQL's repeatable-read snapshot, which # misses a role an acceptance committed while this waited on its lock. Spree::RoleUser.where(user: user, role: roles).delete_all end end |