Class: Decidim::DecidimAwesome::UserGrantedAuthorizationsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organization, user) ⇒ UserGrantedAuthorizationsService

Returns a new instance of UserGrantedAuthorizationsService.



6
7
8
9
10
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 6

def initialize(organization, user)
  @organization = organization
  @user = user
  @available_handlers = organization.available_authorizations & Decidim.authorization_workflows.map(&:name)
end

Instance Attribute Details

#available_handlersObject (readonly)

Returns the value of attribute available_handlers.



12
13
14
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 12

def available_handlers
  @available_handlers
end

#organizationObject (readonly)

Returns the value of attribute organization.



12
13
14
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 12

def organization
  @organization
end

#userObject (readonly)

Returns the value of attribute user.



12
13
14
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 12

def user
  @user
end

Instance Method Details

#all_handlersObject

Returns a hash with all the authorization handlers enabled in the organization defined in the force_authorizations config var and their status for the user { “key” => { “handler_name” => { “options” => …} } }



42
43
44
45
46
47
48
49
50
51
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 42

def all_handlers
  @all_handlers ||= AwesomeConfig.find_by(organization:, var: "force_authorizations")&.value&.filter_map do |key, group|
    handlers = group&.fetch("authorization_handlers", {})&.filter do |handler, _options|
      available_handlers.include?(handler)
    end
    next if handlers.blank?

    [key, handlers]
  end.to_h
end

#component_with_invisible_resourcesObject

Returns a list of components ids which resources are invisible because authorization is not granted in that context



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 54

def component_with_invisible_resources
  @components_with_invisible_resources ||= constraints_with_invisible_resources.filter_map do |constraint|
    component_manifest = constraint.settings["component_manifest"]
    component_id = constraint.settings["component_id"]

    where = {}
    where[:manifest_name] = component_manifest if component_manifest.present?
    where[:id] = component_id if component_id.present?

    space_manifest = constraint.settings["participatory_space_manifest"]
    space_slug = constraint.settings["participatory_space_slug"]

    space_query = space_query_for(space_manifest, space_slug)
    where[:participatory_space] = space_query if space_query

    organization.published_components.where(where)
  end
end

#granted_handlersObject

Returns a hash with only the authorization handlers enabled in the organization defined in the force_authorizations config var and granted for the user { “key” => { “handler_name” => { “options” => …} } }



21
22
23
24
25
26
27
28
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 21

def granted_handlers
  @granted_handlers ||= all_handlers.filter_map do |key, group|
    # all handlers in the group must be granted
    next if (group.keys - user_authorizations).any?

    [key, group]
  end.to_h
end

#non_granted_handlersObject



30
31
32
33
34
35
36
37
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 30

def non_granted_handlers
  @non_granted_handlers ||= all_handlers.filter_map do |key, group|
    # at least one handler in the group must not be granted
    next if (group.keys - user_authorizations).empty?

    [key, group]
  end.to_h
end

#spaces_with_invisible_componentsObject

Returns a list of components that are invisible because authorization is not granted in that context



74
75
76
77
78
79
80
81
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 74

def spaces_with_invisible_components
  @spaces_with_invisible_components ||= constraints_with_invisible_components.filter_map do |constraint|
    space_manifest = constraint.settings["participatory_space_manifest"]
    space_slug = constraint.settings["participatory_space_slug"]

    space_query_for(space_manifest, space_slug)
  end
end

#user_authorizationsObject



14
15
16
# File 'app/services/decidim/decidim_awesome/user_granted_authorizations_service.rb', line 14

def user_authorizations
  @user_authorizations ||= Decidim::Authorization.where(user:, name: available_handlers).where.not(granted_at: nil).pluck(:name)
end