Class: Decidim::Assemblies::Abilities::Admin::AssemblyRoleAbility
- Inherits:
-
Object
- Object
- Decidim::Assemblies::Abilities::Admin::AssemblyRoleAbility
- 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`.
Direct Known Subclasses
AssemblyAdminAbility, AssemblyCollaboratorAbility, AssemblyModeratorAbility
Instance Method Summary collapse
-
#assemblies_with_role_privileges ⇒ Object
Returns a collection of assemblies where the given user has the specific role privilege.
-
#can_manage_assembly?(assembly) ⇒ Boolean
Whether the user can manage the given assembly or not.
- #current_assembly ⇒ Object
-
#define_abilities ⇒ Object
Grant access to admin panel by default.
- #define_assembly_abilities ⇒ Object
-
#has_manageable_assemblies? ⇒ Boolean
Whether the user has at least one assembly to manage or not.
-
#initialize(user, context) ⇒ AssemblyRoleAbility
constructor
A new instance of AssemblyRoleAbility.
-
#not_admin? ⇒ Boolean
Whether the user is an admin or not.
-
#role ⇒ Object
Abstract: A subclass must define this method returning a valid role.
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_privileges ⇒ Object
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.
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_assembly ⇒ Object
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_abilities ⇒ Object
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_abilities ⇒ Object
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.
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.
38 39 40 |
# File 'app/models/decidim/assemblies/abilities/admin/assembly_role_ability.rb', line 38 def not_admin? @user && !@user.admin? end |
#role ⇒ Object
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 |