Class: Decidim::Assemblies::Abilities::Admin::AssemblyRoleAbility

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb

Overview

Defines the abilities for an assembly admin user, whatever they role. Intended to be used with ‘cancancan`.

Instance Method Summary collapse

Constructor Details

#initialize(user, context) ⇒ AssemblyRoleAbility

Returns a new instance of AssemblyRoleAbility.



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 12

def initialize(user, context)
  @user = user
  @context = context

  # Define abilities if the user is not an admin and has at least one assembly to manage.
  if not_admin? && has_manageable_assemblies?
    define_abilities

    define_assembly_abilities if current_assembly && can_manage_assembly?(current_assembly)
  end
end

Instance Method Details

#assemblies_with_role_privilegesObject

Returns a collection of assemblies where the given user has the specific role privilege.



44
45
46
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 44

def assemblies_with_role_privileges
  @assemblies ||= Decidim::Assemblies::AssembliesWithUserRole.for(@user, role)
end

#can_manage_assembly?(assembly) ⇒ Boolean

Whether the user can manage the given assembly or not.

Returns:

  • (Boolean)


54
55
56
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 54

def can_manage_assembly?(assembly)
  assemblies_with_role_privileges.include? assembly
end

#current_assemblyObject



58
59
60
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 58

def current_assembly
  @current_assembly ||= @context[:current_participatory_space]
end

#define_abilitiesObject

Grant access to admin panel by default.



25
26
27
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 25

def define_abilities
  can :read, :admin_dashboard
end

#define_assembly_abilitiesObject



29
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 29

def define_assembly_abilities; end

#has_manageable_assemblies?Boolean

Whether the user has at least one assembly to manage or not.

Returns:

  • (Boolean)


49
50
51
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 49

def has_manageable_assemblies?
  assemblies_with_role_privileges.any?
end

#not_admin?Boolean

Whether the user is an admin or not.

Returns:

  • (Boolean)


38
39
40
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 38

def not_admin?
  @user && !@user.admin?
end

#roleObject

Abstract: A subclass must define this method returning a valid role. See ParticipatoryProcessUserRoles::ROLES for more information.



33
34
35
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 33

def role
  raise "Needs implementation"
end