Class: Spree::RoleConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/core/role_configuration.rb

Overview

A class responsible for associating Role with a list of permission sets.

Examples:

Adding order, product, and user display to customer service users.

Spree::RoleConfiguration.configure do |config|
  config.assign_permissions :customer_service, [
    Spree::PermissionSets::OrderDisplay,
    Spree::PermissionSets::UserDisplay,
    Spree::PermissionSets::ProductDisplay
  ]
end

See Also:

Defined Under Namespace

Classes: Role

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rolesObject

Returns the value of attribute roles



32
33
34
# File 'lib/spree/core/role_configuration.rb', line 32

def roles
  @roles
end

Class Method Details

.configure {|instance| ... } ⇒ Object

Yields the instance of the singleton, used for configuration

Yield Parameters:



42
43
44
45
# File 'lib/spree/core/role_configuration.rb', line 42

def configure
  Spree::Deprecation.warn "Spree::RoleConfiguration.configure is deprecated. Call Spree::Config.roles.assign_permissions instead"
  yield(Spree::Config.roles)
end

.instanceObject



35
36
37
38
# File 'lib/spree/core/role_configuration.rb', line 35

def instance
  Spree::Deprecation.warn "Spree::RoleConfiguration.instance is DEPRECATED use Spree::Config.roles instead"
  Spree::Config.roles
end

Instance Method Details

#activate_permissions!(ability, user) ⇒ Object

Given a CanCan::Ability, and a user, determine what permissions sets can be activated on the ability, then activate them.

This performs can/cannot declarations on the ability, and can modify its internal permissions.

Parameters:

  • ability (CanCan::Ability)

    the ability to invoke declarations on

  • user (#spree_roles)

    the user that holds the spree_roles association.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spree/core/role_configuration.rb', line 56

def activate_permissions!(ability, user)
  spree_roles = ['default'] | user.spree_roles.map(&:name)
  applicable_permissions = Set.new

  spree_roles.each do |role_name|
    applicable_permissions |= roles[role_name].permission_sets
  end

  applicable_permissions.each do |permission_set|
    permission_set.new(ability).activate!
  end
end

#assign_permissions(role_name, permission_sets) ⇒ Object

Assign permission sets for a Spree::Role that has the name of role_name

Parameters:



81
82
83
84
85
86
# File 'lib/spree/core/role_configuration.rb', line 81

def assign_permissions(role_name, permission_sets)
  name = role_name.to_s

  roles[name].permission_sets.concat permission_sets
  roles[name]
end