decidim-initiatives 0.22.0 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/decidim/initiatives/admin/initiatives_types.js.es6 +18 -0
  3. data/app/assets/stylesheet/decidim/initiatives/initiatives.scss +6 -0
  4. data/app/cells/decidim/initiatives/initiative_m_cell.rb +14 -0
  5. data/app/cells/decidim/initiatives_votes/vote/show.erb +12 -9
  6. data/app/cells/decidim/initiatives_votes/vote_cell.rb +7 -0
  7. data/app/commands/decidim/initiatives/admin/create_initiative_type.rb +3 -1
  8. data/app/commands/decidim/initiatives/admin/update_initiative_type.rb +3 -1
  9. data/app/commands/decidim/initiatives/attachment_methods.rb +9 -5
  10. data/app/commands/decidim/initiatives/create_initiative.rb +2 -2
  11. data/app/commands/decidim/initiatives/unvote_initiative.rb +4 -10
  12. data/app/commands/decidim/initiatives/vote_initiative.rb +36 -36
  13. data/app/controllers/decidim/initiatives/admin/answers_controller.rb +2 -3
  14. data/app/controllers/decidim/initiatives/admin/initiatives_controller.rb +2 -2
  15. data/app/controllers/decidim/initiatives/create_initiative_controller.rb +1 -1
  16. data/app/controllers/decidim/initiatives/initiative_signatures_controller.rb +19 -23
  17. data/app/controllers/decidim/initiatives/initiative_votes_controller.rb +11 -5
  18. data/app/controllers/decidim/initiatives/initiatives_controller.rb +11 -10
  19. data/app/controllers/decidim/initiatives/initiatives_type_scopes_controller.rb +9 -1
  20. data/app/controllers/decidim/initiatives/{initiative_widgets_controller.rb → widgets_controller.rb} +2 -2
  21. data/app/forms/decidim/initiatives/admin/initiative_form.rb +19 -8
  22. data/app/forms/decidim/initiatives/admin/initiative_type_form.rb +3 -0
  23. data/app/forms/decidim/initiatives/initiative_form.rb +23 -2
  24. data/app/forms/decidim/initiatives/vote_form.rb +133 -78
  25. data/app/models/decidim/initiative.rb +123 -52
  26. data/app/models/decidim/initiatives_type.rb +5 -2
  27. data/app/models/decidim/initiatives_type_scope.rb +5 -1
  28. data/app/models/decidim/initiatives_vote.rb +19 -23
  29. data/app/permissions/decidim/initiatives/admin/permissions.rb +2 -2
  30. data/app/permissions/decidim/initiatives/permissions.rb +2 -4
  31. data/app/presenters/decidim/initiatives/initiative_stats_presenter.rb +1 -5
  32. data/app/views/decidim/initiatives/admin/answers/_info_initiative.html.erb +1 -1
  33. data/app/views/decidim/initiatives/admin/initiatives/_form.html.erb +39 -17
  34. data/app/views/decidim/initiatives/admin/initiatives/export_pdf_signatures.pdf.erb +12 -9
  35. data/app/views/decidim/initiatives/admin/initiatives_types/_form.html.erb +35 -10
  36. data/app/views/decidim/initiatives/admin/initiatives_types/_initiative_type_scopes.html.erb +28 -25
  37. data/app/views/decidim/initiatives/create_initiative/fill_data.html.erb +18 -16
  38. data/app/views/decidim/initiatives/initiative_signatures/fill_personal_data.html.erb +0 -1
  39. data/app/views/decidim/initiatives/initiative_signatures/update_buttons_and_counters.js.erb +1 -1
  40. data/app/views/decidim/initiatives/initiative_votes/update_buttons_and_counters.js.erb +1 -1
  41. data/app/views/decidim/initiatives/initiatives/_index_header.html.erb +36 -2
  42. data/app/views/decidim/initiatives/initiatives/_initiatives.html.erb +10 -0
  43. data/app/views/decidim/initiatives/initiatives/_progress_bar.html.erb +24 -9
  44. data/app/views/decidim/initiatives/initiatives/_vote_cabin.html.erb +1 -13
  45. data/app/views/decidim/initiatives/initiatives/show.html.erb +1 -3
  46. data/app/views/layouts/decidim/initiative.html.erb +1 -0
  47. data/config/locales/am-ET.yml +1 -0
  48. data/config/locales/ar.yml +0 -4
  49. data/config/locales/bg.yml +13 -0
  50. data/config/locales/ca.yml +22 -8
  51. data/config/locales/cs.yml +18 -4
  52. data/config/locales/da.yml +1 -0
  53. data/config/locales/de.yml +7 -4
  54. data/config/locales/el.yml +0 -4
  55. data/config/locales/en.yml +18 -4
  56. data/config/locales/eo.yml +1 -0
  57. data/config/locales/es-MX.yml +18 -4
  58. data/config/locales/es-PY.yml +18 -4
  59. data/config/locales/es.yml +31 -17
  60. data/config/locales/et.yml +1 -0
  61. data/config/locales/eu.yml +0 -14
  62. data/config/locales/fi-plain.yml +18 -4
  63. data/config/locales/fi.yml +18 -4
  64. data/config/locales/fr-CA.yml +32 -4
  65. data/config/locales/fr.yml +36 -8
  66. data/config/locales/gl.yml +0 -14
  67. data/config/locales/hr.yml +1 -0
  68. data/config/locales/hu.yml +5 -4
  69. data/config/locales/id-ID.yml +0 -14
  70. data/config/locales/is-IS.yml +0 -10
  71. data/config/locales/is.yml +251 -0
  72. data/config/locales/it.yml +9 -4
  73. data/config/locales/ja-JP.yml +43 -49
  74. data/config/locales/ja.yml +549 -0
  75. data/config/locales/ko-KR.yml +1 -0
  76. data/config/locales/ko.yml +1 -0
  77. data/config/locales/lt.yml +1 -0
  78. data/config/locales/{lv-LV.yml → lv.yml} +0 -4
  79. data/config/locales/mt.yml +1 -0
  80. data/config/locales/nl.yml +9 -4
  81. data/config/locales/no.yml +48 -4
  82. data/config/locales/om-ET.yml +1 -0
  83. data/config/locales/pl.yml +28 -22
  84. data/config/locales/pt-BR.yml +0 -14
  85. data/config/locales/pt.yml +5 -4
  86. data/config/locales/ro-RO.yml +5 -4
  87. data/config/locales/ru.yml +0 -14
  88. data/config/locales/sk.yml +0 -4
  89. data/config/locales/sl.yml +6 -0
  90. data/config/locales/so-SO.yml +1 -0
  91. data/config/locales/sv.yml +25 -5
  92. data/config/locales/ti-ER.yml +1 -0
  93. data/config/locales/tr-TR.yml +0 -14
  94. data/config/locales/uk.yml +0 -14
  95. data/config/locales/vi-VN.yml +1 -0
  96. data/config/locales/vi.yml +1 -0
  97. data/config/locales/zh-CN.yml +549 -0
  98. data/config/locales/zh-TW.yml +1 -0
  99. data/db/migrate/20191106144259_add_settings_to_initiatives_types.rb +8 -0
  100. data/db/migrate/20191107134847_add_scopes_to_initiatives_votes.rb +28 -0
  101. data/db/migrate/20191116170841_allow_multiple_initiative_votes_counter_caches.rb +32 -0
  102. data/db/migrate/20191118105634_allow_multiple_offline_votes.rb +34 -0
  103. data/db/migrate/20200528151456_remove_user_groups_from_initiative_votes.rb +7 -0
  104. data/db/migrate/20200827154214_add_commentable_counter_cache_to_initiatives.rb +9 -0
  105. data/db/seeds/city.jpeg +0 -0
  106. data/lib/decidim/initiatives/admin_engine.rb +1 -0
  107. data/lib/decidim/initiatives/engine.rb +2 -2
  108. data/lib/decidim/initiatives/participatory_space.rb +7 -0
  109. data/lib/decidim/initiatives/test/factories.rb +21 -3
  110. data/lib/decidim/initiatives/version.rb +1 -1
  111. data/lib/tasks/decidim_initiatives.rake +1 -3
  112. metadata +42 -18
  113. data/app/views/decidim/initiatives/initiatives/_supports.html.erb +0 -22
  114. data/app/views/decidim/initiatives/initiatives/signature_identities.html.erb +0 -42
@@ -13,9 +13,14 @@ module Decidim
13
13
 
14
14
  # POST /initiatives/:initiative_id/initiative_vote
15
15
  def create
16
- enforce_permission_to :vote, :initiative, initiative: current_initiative, group_id: params[:group_id]
17
- @form = form(Decidim::Initiatives::VoteForm).from_params(initiative_id: current_initiative.id, author_id: current_user.id, group_id: params[:group_id])
18
- VoteInitiative.call(@form, current_user) do
16
+ enforce_permission_to :vote, :initiative, initiative: current_initiative
17
+
18
+ @form = form(Decidim::Initiatives::VoteForm).from_params(
19
+ initiative: current_initiative,
20
+ signer: current_user
21
+ )
22
+
23
+ VoteInitiative.call(@form) do
19
24
  on(:ok) do
20
25
  current_initiative.reload
21
26
  render :update_buttons_and_counters
@@ -31,8 +36,9 @@ module Decidim
31
36
 
32
37
  # DELETE /initiatives/:initiative_id/initiative_vote
33
38
  def destroy
34
- enforce_permission_to :unvote, :initiative, initiative: current_initiative, group_id: params[:group_id]
35
- UnvoteInitiative.call(current_initiative, current_user, params[:group_id]) do
39
+ enforce_permission_to :unvote, :initiative, initiative: current_initiative
40
+
41
+ UnvoteInitiative.call(current_initiative, current_user) do
36
42
  on(:ok) do
37
43
  current_initiative.reload
38
44
  render :update_buttons_and_counters
@@ -19,7 +19,6 @@ module Decidim
19
19
  helper PaginateHelper
20
20
  helper InitiativeHelper
21
21
  include InitiativeSlug
22
-
23
22
  include FilterResource
24
23
  include Paginable
25
24
  include Decidim::Initiatives::Orderable
@@ -32,6 +31,17 @@ module Decidim
32
31
  # GET /initiatives
33
32
  def index
34
33
  enforce_permission_to :list, :initiative
34
+
35
+ return unless search.results.blank? && params.dig("filter", "state") != %w(closed)
36
+
37
+ @closed_initiatives = search_klass.new(search_params.merge(state: %w(closed)))
38
+
39
+ if @closed_initiatives.results.present?
40
+ params[:filter] ||= {}
41
+ params[:filter][:date] = %w(closed)
42
+ @forced_closed_initiatives = true
43
+ @search = @closed_initiatives
44
+ end
35
45
  end
36
46
 
37
47
  # GET /initiatives/:id
@@ -39,15 +49,6 @@ module Decidim
39
49
  enforce_permission_to :read, :initiative, initiative: current_initiative
40
50
  end
41
51
 
42
- # GET /initiatives/:id/signature_identities
43
- def signature_identities
44
- @voted_groups = InitiativesVote
45
- .supports
46
- .where(initiative: current_initiative, author: current_user)
47
- .pluck(:decidim_user_group_id)
48
- render layout: false
49
- end
50
-
51
52
  private
52
53
 
53
54
  alias current_initiative current_participatory_space
@@ -15,7 +15,15 @@ module Decidim
15
15
  private
16
16
 
17
17
  def scoped_types
18
- @scoped_types ||= InitiativesType.find(params[:type_id]).scopes
18
+ @scoped_types ||= if initiative_type.only_global_scope_enabled?
19
+ initiative_type.scopes.where(scope: nil)
20
+ else
21
+ initiative_type.scopes
22
+ end
23
+ end
24
+
25
+ def initiative_type
26
+ @initiative_type ||= InitiativesType.find(params[:type_id])
19
27
  end
20
28
  end
21
29
  end
@@ -3,7 +3,7 @@
3
3
  module Decidim
4
4
  module Initiatives
5
5
  # This controller provides a widget that allows embedding the initiative
6
- class InitiativeWidgetsController < Decidim::WidgetsController
6
+ class WidgetsController < Decidim::WidgetsController
7
7
  helper InitiativesHelper
8
8
  helper PaginateHelper
9
9
  helper InitiativeHelper
@@ -23,7 +23,7 @@ module Decidim
23
23
  end
24
24
 
25
25
  def iframe_url
26
- @iframe_url ||= initiative_initiative_widget_url(model)
26
+ @iframe_url ||= initiative_widget_url(model)
27
27
  end
28
28
  end
29
29
  end
@@ -19,7 +19,7 @@ module Decidim
19
19
  attribute :signature_start_date, Decidim::Attributes::LocalizedDate
20
20
  attribute :signature_end_date, Decidim::Attributes::LocalizedDate
21
21
  attribute :hashtag, String
22
- attribute :offline_votes, Integer
22
+ attribute :offline_votes, Hash[String => Integer]
23
23
  attribute :state, String
24
24
  attribute :attachment, AttachmentForm
25
25
 
@@ -35,18 +35,29 @@ module Decidim
35
35
  form.signature_start_date.blank? && form.signature_end_date.present?
36
36
  }
37
37
 
38
- validates :offline_votes,
39
- numericality: {
40
- only_integer: true,
41
- greater_than: 0
42
- }, allow_blank: true
43
-
44
38
  validate :notify_missing_attachment_if_errored
45
39
  validate :area_is_not_removed
46
40
 
47
41
  def map_model(model)
48
42
  self.type_id = model.type.id
49
43
  self.decidim_scope_id = model.scope&.id
44
+ self.offline_votes = model.offline_votes
45
+
46
+ if offline_votes.empty?
47
+ self.offline_votes = model.votable_initiative_type_scopes.each_with_object({}) do |initiative_scope_type, all_votes|
48
+ all_votes[initiative_scope_type.decidim_scopes_id || "global"] = [0, initiative_scope_type.scope_name]
49
+ end
50
+ else
51
+ offline_votes.delete("total")
52
+ self.offline_votes = offline_votes.each_with_object({}) do |(decidim_scope_id, votes), all_votes|
53
+ scope_name = model.votable_initiative_type_scopes.find do |initiative_scope_type|
54
+ initiative_scope_type.global_scope? && decidim_scope_id == "global" ||
55
+ initiative_scope_type.decidim_scopes_id == decidim_scope_id.to_i
56
+ end.scope_name
57
+
58
+ all_votes[decidim_scope_id || "global"] = [votes, scope_name]
59
+ end
60
+ end
50
61
  end
51
62
 
52
63
  def signature_type_updatable?
@@ -67,7 +78,7 @@ module Decidim
67
78
  def scoped_type_id
68
79
  return unless type && decidim_scope_id
69
80
 
70
- type.scopes.find_by!(decidim_scopes_id: decidim_scope_id).id
81
+ type.scopes.find_by(decidim_scopes_id: decidim_scope_id.presence).id
71
82
  end
72
83
 
73
84
  def area
@@ -17,6 +17,8 @@ module Decidim
17
17
  attribute :attachments_enabled, Boolean
18
18
  attribute :custom_signature_end_date_enabled, Boolean
19
19
  attribute :area_enabled, Boolean
20
+ attribute :child_scope_threshold_enabled, Boolean
21
+ attribute :only_global_scope_enabled, Boolean
20
22
  attribute :promoting_committee_enabled, Boolean
21
23
  attribute :minimum_committee_members, Integer
22
24
  attribute :collect_user_extra_fields, Boolean
@@ -29,6 +31,7 @@ module Decidim
29
31
  :area_enabled, :promoting_committee_enabled, inclusion: { in: [true, false] }
30
32
  validates :minimum_committee_members, numericality: { only_integer: true }, allow_nil: true
31
33
  validates :banner_image, presence: true, if: ->(form) { form.context.initiative_type.nil? }
34
+ validates :document_number_authorization_handler, presence: true, if: ->(form) { form.collect_user_extra_fields? }
32
35
 
33
36
  def minimum_committee_members=(value)
34
37
  super(value.presence)
@@ -41,6 +41,8 @@ module Decidim
41
41
  end
42
42
 
43
43
  def scope_id
44
+ return nil if initiative_type.only_global_scope_enabled?
45
+
44
46
  super.presence
45
47
  end
46
48
 
@@ -48,12 +50,28 @@ module Decidim
48
50
  @area ||= current_organization.areas.find_by(id: area_id)
49
51
  end
50
52
 
53
+ def initiative_type
54
+ @initiative_type ||= InitiativesType.find(type_id)
55
+ end
56
+
57
+ def available_scopes
58
+ @available_scopes ||= if initiative_type.only_global_scope_enabled?
59
+ initiative_type.scopes.where(scope: nil)
60
+ else
61
+ initiative_type.scopes
62
+ end
63
+ end
64
+
65
+ def scope
66
+ @scope ||= Scope.find(scope_id) if scope_id.present?
67
+ end
68
+
51
69
  private
52
70
 
53
71
  def scope_exists
54
72
  return if scope_id.blank?
55
73
 
56
- errors.add(:scope_id, :invalid) unless InitiativesTypeScope.where(decidim_initiatives_types_id: type_id, decidim_scopes_id: scope_id).exists?
74
+ errors.add(:scope_id, :invalid) unless InitiativesTypeScope.where(type: initiative_type, scope: scope).exists?
57
75
  end
58
76
 
59
77
  # This method will add an error to the `attachment` field only if there's
@@ -72,7 +90,10 @@ module Decidim
72
90
 
73
91
  attachment.errors.each { |error| errors.add(:attachment, error) }
74
92
 
75
- attachment = Attachment.new(file: attachment.try(:file))
93
+ attachment = Attachment.new(
94
+ attached_to: attachment.try(:attached_to),
95
+ file: attachment.try(:file)
96
+ )
76
97
 
77
98
  errors.add(:attachment, :file) if !attachment.save && attachment.errors.has_key?(:file)
78
99
  end
@@ -19,112 +19,163 @@ module Decidim
19
19
  attribute :encrypted_metadata, String
20
20
  attribute :hash_id, String
21
21
 
22
- attribute :initiative_id, Integer
23
- attribute :author_id, Integer
24
- attribute :group_id, Integer
22
+ attribute :initiative, Decidim::Initiative
23
+ attribute :signer, Decidim::User
25
24
 
26
- validates :name_and_surname, :document_number, :date_of_birth, :postal_code, presence: true, if: :required_personal_data?
27
- validates :encrypted_metadata, presence: true, if: :required_personal_data?
28
- validates :initiative_id, presence: true
29
- validates :author_id, presence: true
25
+ validates :initiative, :signer, presence: true
30
26
 
31
- validate :document_number_authorized, if: :required_personal_data?
32
- validate :document_number_uniqueness, if: :required_personal_data?
33
- validate :personal_data_consistent_with_metadata, if: :required_personal_data?
34
-
35
- def initiative
36
- @initiative ||= Decidim::Initiative.find_by(id: initiative_id)
27
+ with_options if: :required_personal_data? do
28
+ validates :name_and_surname, :document_number, :date_of_birth, :postal_code, :encrypted_metadata, :hash_id, presence: true
29
+ validate :document_number_authorized?
30
+ validate :already_voted?
37
31
  end
38
32
 
39
33
  delegate :scope, to: :initiative
40
34
 
41
- def metadata
42
- { name_and_surname: name_and_surname,
43
- document_number: document_number,
44
- date_of_birth: date_of_birth,
45
- postal_code: postal_code }
46
- end
47
-
48
35
  def encrypted_metadata
49
- @encrypted_metadata ||= encrypt_metadata
36
+ return unless required_personal_data?
37
+
38
+ @encrypted_metadata ||= encryptor.encrypt(metadata)
50
39
  end
51
40
 
41
+ # Public: The hash to uniquely identify an initiative vote. It uses the
42
+ # initiative scope as a default.
43
+ #
44
+ # Returns a String.
52
45
  def hash_id
53
- Digest::MD5.hexdigest(
54
- "#{initiative_id}-#{document_number || author_id}-#{Rails.application.secrets.secret_key_base}"
46
+ return unless initiative && (document_number || signer)
47
+
48
+ @hash_id ||= Digest::MD5.hexdigest(
49
+ [
50
+ initiative.id,
51
+ document_number || signer.id,
52
+ Rails.application.secrets.secret_key_base
53
+ ].compact.join("-")
55
54
  )
56
55
  end
57
56
 
58
- def decrypted_metadata
59
- return unless encrypted_metadata
57
+ # Public: Builds the list of scopes where the user is authorized to vote in. This is used when
58
+ # the initiative allows also voting on child scopes, not only the main scope.
59
+ #
60
+ # Instead of just listing the children of the main scope, we just want to select the ones that
61
+ # have been added to the InitiativeType with its voting settings.
62
+ #
63
+ def authorized_scopes
64
+ initiative.votable_initiative_type_scopes.select do |initiative_type_scope|
65
+ initiative_type_scope.global_scope? ||
66
+ initiative_type_scope.scope == user_authorized_scope ||
67
+ initiative_type_scope.scope.ancestor_of?(user_authorized_scope)
68
+ end.flat_map(&:scope)
69
+ end
70
+
71
+ # Public: Finds the scope the user has an authorization for, this way the user can vote
72
+ # on that scope and its parents.
73
+ #
74
+ # This is can be used to allow users that are authorized with a children
75
+ # scope to sign an initiative with a parent scope.
76
+ #
77
+ # As an example: A city (global scope) has many districts (scopes with
78
+ # parent nil), and each district has different neighbourhoods (with its
79
+ # parent as a district). If we setup the authorization handler to match
80
+ # a neighbourhood, the same authorization can be used to participate
81
+ # in district, neighbourhoods or city initiatives.
82
+ #
83
+ # Returns a Decidim::Scope.
84
+ def user_authorized_scope
85
+ return scope if handler_name.blank?
86
+ return unless authorized?
87
+
88
+ @user_authorized_scope ||= authorized_scope_candidates.find do |scope|
89
+ scope&.id == authorization.metadata.symbolize_keys[:scope_id]
90
+ end
91
+ end
92
+
93
+ # Public: Builds a list of Decidim::Scopes where the user could have a
94
+ # valid authorization.
95
+ #
96
+ # If the intiative is set with a global scope (meaning the scope is nil),
97
+ # all the scopes in the organizaton are valid.
98
+ #
99
+ # Returns an array of Decidim::Scopes.
100
+ def authorized_scope_candidates
101
+ authorized_scope_candidates = [initiative.scope]
102
+ authorized_scope_candidates += if initiative.scope.present?
103
+ initiative.scope.descendants
104
+ else
105
+ initiative.organization.scopes
106
+ end
107
+ authorized_scope_candidates.uniq
108
+ end
60
109
 
61
- encryptor.decrypt(encrypted_metadata)
110
+ def metadata
111
+ {
112
+ name_and_surname: name_and_surname,
113
+ document_number: document_number,
114
+ date_of_birth: date_of_birth,
115
+ postal_code: postal_code
116
+ }
62
117
  end
63
118
 
64
119
  protected
65
120
 
121
+ # Private: Whether the personal data given when signing the initiative should
122
+ # be stored together with the vote or not.
123
+ #
124
+ # Returns a Boolean.
66
125
  def required_personal_data?
67
- initiative_type&.collect_user_extra_fields?
126
+ @required_personal_data ||= initiative&.type&.collect_user_extra_fields?
68
127
  end
69
128
 
70
- def initiative_type
71
- @initiative_type ||= initiative&.scoped_type&.type
72
- end
73
-
74
- def encryptor
75
- @encryptor ||= DataEncryptor.new(secret: "personal user metadata")
76
- end
77
-
78
- def encrypt_metadata
79
- return unless required_personal_data?
80
-
81
- encryptor.encrypt(metadata)
82
- end
83
-
84
- def document_number_authorized
129
+ # Private: Checks that the unique hash computed from the authorization
130
+ # and the user provided data match.
131
+ #
132
+ # This prevents users that know partial data from another user to sign
133
+ # initiatives with someone elses identity.
134
+ def document_number_authorized?
85
135
  return if initiative.document_number_authorization_handler.blank?
86
136
 
87
137
  errors.add(:document_number, :invalid) unless authorized? && authorization_handler && authorization.unique_id == authorization_handler.unique_id
88
138
  end
89
139
 
90
- def document_number_uniqueness
91
- errors.add(:document_number, :taken) if initiative.votes.where(hash_id: hash_id).exists?
92
- end
93
-
94
- def personal_data_consistent_with_metadata
95
- return if initiative.document_number_authorization_handler.blank?
96
-
97
- errors.add(:base, :invalid) unless authorized? &&
98
- authorization_handler &&
99
- authorization_handler_metadata_variations.any? do |variation|
100
- authorization.metadata.symbolize_keys.except(:extras) == variation.symbolize_keys.except(:extras)
101
- end
140
+ # Private: Checks if there's any existing vote that matches the user's data.
141
+ def already_voted?
142
+ errors.add(:document_number, :taken) if initiative.votes.where(hash_id: hash_id, scope: scope).exists?
102
143
  end
103
144
 
104
145
  def author
105
146
  @author ||= current_organization.users.find_by(id: author_id)
106
147
  end
107
148
 
149
+ # Private: Finds an authorization for the user signing the initiative and
150
+ # the configured handler.
108
151
  def authorization
109
- return unless author && handler_name
110
-
111
- @authorization ||= Verifications::Authorizations.new(organization: author.organization, user: author, name: handler_name).first
112
- end
113
-
114
- def authorization_status
115
- return unless authorization
152
+ return unless signer && handler_name
116
153
 
117
- Decidim::Verifications::Adapter.from_element(handler_name).authorize(authorization, {}, nil, nil)
154
+ @authorization ||= Verifications::Authorizations.new(
155
+ organization: signer.organization,
156
+ user: signer,
157
+ name: handler_name
158
+ ).first
118
159
  end
119
160
 
161
+ # Private: Checks if the authorization hasn't expired or is invalid.
120
162
  def authorized?
121
163
  authorization_status&.first == :ok
122
164
  end
123
165
 
124
- def handler_name
125
- initiative.document_number_authorization_handler
126
- end
127
-
166
+ # Private: Builds an authorization handler with the data the user provided
167
+ # when signing the initiative.
168
+ #
169
+ # This is currently tied to authorization handlers that have, at least, these attributes:
170
+ # * document_number
171
+ # * name_and_surname
172
+ # * date_of_birth
173
+ # * postal_code
174
+ #
175
+ # Once we have the authorization handler we can use is to compute the
176
+ # unique_id and compare it to an existing authorization.
177
+ #
178
+ # Returns a Decidim::AuthorizationHandler.
128
179
  def authorization_handler
129
180
  return unless document_number && handler_name
130
181
 
@@ -132,21 +183,25 @@ module Decidim
132
183
  document_number: document_number,
133
184
  name_and_surname: name_and_surname,
134
185
  date_of_birth: date_of_birth,
135
- postal_code: postal_code,
136
- scope_id: scope&.id)
186
+ postal_code: postal_code)
137
187
  end
138
188
 
139
- def authorization_handler_metadata_variations
140
- return [] unless authorization_handler && scope.present?
189
+ # Private: The AuthorizationHandler name used to verify the user's
190
+ # document number.
191
+ #
192
+ # Returns a String.
193
+ def handler_name
194
+ initiative.document_number_authorization_handler
195
+ end
196
+
197
+ def authorization_status
198
+ return unless authorization
141
199
 
142
- scope.children.map do |child_scope|
143
- Decidim::AuthorizationHandler.handler_for(handler_name,
144
- document_number: document_number,
145
- name_and_surname: name_and_surname,
146
- date_of_birth: date_of_birth,
147
- postal_code: postal_code,
148
- scope_id: child_scope&.id)
149
- end.unshift(authorization_handler).map(&:metadata)
200
+ Decidim::Verifications::Adapter.from_element(handler_name).authorize(authorization, {}, nil, nil)
201
+ end
202
+
203
+ def encryptor
204
+ @encryptor ||= DataEncryptor.new(secret: "personal user metadata")
150
205
  end
151
206
  end
152
207
  end