decidim-verifications 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +101 -0
  3. data/Rakefile +3 -0
  4. data/app/commands/decidim/verifications/authorize_user.rb +61 -0
  5. data/app/commands/decidim/verifications/confirm_user_authorization.rb +49 -0
  6. data/app/commands/decidim/verifications/perform_authorization_step.rb +48 -0
  7. data/app/controllers/decidim/verifications/authorizations_controller.rb +91 -0
  8. data/app/controllers/decidim/verifications/id_documents/admin/confirmations_controller.rb +48 -0
  9. data/app/controllers/decidim/verifications/id_documents/admin/pending_authorizations_controller.rb +28 -0
  10. data/app/controllers/decidim/verifications/id_documents/admin/rejections_controller.rb +37 -0
  11. data/app/controllers/decidim/verifications/id_documents/authorizations_controller.rb +82 -0
  12. data/app/controllers/decidim/verifications/postal_letter/admin/pending_authorizations_controller.rb +27 -0
  13. data/app/controllers/decidim/verifications/postal_letter/admin/postages_controller.rb +41 -0
  14. data/app/controllers/decidim/verifications/postal_letter/authorizations_controller.rb +74 -0
  15. data/app/forms/decidim/verifications/id_documents/information_form.rb +50 -0
  16. data/app/forms/decidim/verifications/id_documents/information_rejection_form.rb +15 -0
  17. data/app/forms/decidim/verifications/id_documents/upload_form.rb +20 -0
  18. data/app/forms/decidim/verifications/postal_letter/address_form.rb +28 -0
  19. data/app/forms/decidim/verifications/postal_letter/confirmation_form.rb +19 -0
  20. data/app/forms/decidim/verifications/postal_letter/postage_form.rb +34 -0
  21. data/app/presenters/decidim/verifications/id_documents/authorization_presenter.rb +20 -0
  22. data/app/presenters/decidim/verifications/postal_letter/authorization_presenter.rb +59 -0
  23. data/app/queries/decidim/verifications/authorizations.rb +41 -0
  24. data/app/services/decidim/authorization_handler.rb +112 -0
  25. data/app/services/decidim/dummy_authorization_handler.rb +27 -0
  26. data/app/uploaders/decidim/verifications/attachment_uploader.rb +16 -0
  27. data/app/views/decidim/verifications/authorizations/first_login.html.erb +22 -0
  28. data/app/views/decidim/verifications/authorizations/index.html.erb +78 -0
  29. data/app/views/decidim/verifications/authorizations/new.html.erb +33 -0
  30. data/app/views/decidim/verifications/id_documents/admin/confirmations/new.html.erb +26 -0
  31. data/app/views/decidim/verifications/id_documents/admin/pending_authorizations/index.html.erb +32 -0
  32. data/app/views/decidim/verifications/id_documents/authorizations/edit.html.erb +53 -0
  33. data/app/views/decidim/verifications/id_documents/authorizations/new.html.erb +35 -0
  34. data/app/views/decidim/verifications/postal_letter/admin/pending_authorizations/index.html.erb +50 -0
  35. data/app/views/decidim/verifications/postal_letter/authorizations/edit.html.erb +37 -0
  36. data/app/views/decidim/verifications/postal_letter/authorizations/new.html.erb +27 -0
  37. data/config/locales/ca.yml +126 -0
  38. data/config/locales/en.yml +126 -0
  39. data/config/locales/es.yml +126 -0
  40. data/config/locales/eu.yml +125 -0
  41. data/config/locales/fi.yml +125 -0
  42. data/config/locales/fr.yml +125 -0
  43. data/config/locales/it.yml +125 -0
  44. data/config/locales/nl.yml +125 -0
  45. data/config/locales/pl.yml +125 -0
  46. data/config/locales/pt.yml +125 -0
  47. data/config/locales/ru.yml +5 -0
  48. data/config/locales/uk.yml +41 -0
  49. data/db/migrate/20171030133426_move_authorizations_to_new_api.rb +66 -0
  50. data/lib/decidim/verifications.rb +26 -0
  51. data/lib/decidim/verifications/adapter.rb +88 -0
  52. data/lib/decidim/verifications/dummy.rb +5 -0
  53. data/lib/decidim/verifications/engine.rb +24 -0
  54. data/lib/decidim/verifications/id_documents.rb +6 -0
  55. data/lib/decidim/verifications/id_documents/admin.rb +12 -0
  56. data/lib/decidim/verifications/id_documents/admin_engine.rb +21 -0
  57. data/lib/decidim/verifications/id_documents/engine.rb +18 -0
  58. data/lib/decidim/verifications/id_documents/workflow.rb +6 -0
  59. data/lib/decidim/verifications/postal_letter.rb +6 -0
  60. data/lib/decidim/verifications/postal_letter/admin.rb +12 -0
  61. data/lib/decidim/verifications/postal_letter/admin_engine.rb +21 -0
  62. data/lib/decidim/verifications/postal_letter/engine.rb +18 -0
  63. data/lib/decidim/verifications/postal_letter/workflow.rb +6 -0
  64. data/lib/decidim/verifications/registry.rb +39 -0
  65. data/lib/decidim/verifications/version.rb +10 -0
  66. data/lib/decidim/verifications/workflow_manifest.rb +56 -0
  67. data/lib/decidim/verifications/workflows.rb +58 -0
  68. metadata +152 -0
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module IdDocuments
6
+ module Admin
7
+ #
8
+ # Handles rejections for verification by identity document upload.
9
+ #
10
+ class RejectionsController < Decidim::Admin::ApplicationController
11
+ layout "decidim/admin/users"
12
+
13
+ before_action :load_pending_authorization
14
+
15
+ def create
16
+ authorize! :update, @pending_authorization
17
+
18
+ @form = InformationRejectionForm.from_model(@pending_authorization)
19
+
20
+ PerformAuthorizationStep.call(@pending_authorization, @form) do
21
+ on(:ok) do
22
+ flash[:notice] = t("rejections.create.success", scope: "decidim.verifications.id_documents.admin")
23
+ redirect_to root_path
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def load_pending_authorization
31
+ @pending_authorization = Authorization.find(params[:pending_authorization_id])
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module IdDocuments
6
+ #
7
+ # Handles verification by identity document upload
8
+ #
9
+ class AuthorizationsController < Decidim::ApplicationController
10
+ helper_method :authorization
11
+
12
+ before_action :load_authorization
13
+
14
+ def new
15
+ authorize! :create, @authorization
16
+
17
+ @form = UploadForm.new
18
+ end
19
+
20
+ def create
21
+ authorize! :create, @authorization
22
+
23
+ @form = UploadForm.from_params(params.merge(user: current_user))
24
+
25
+ PerformAuthorizationStep.call(@authorization, @form) do
26
+ on(:ok) do
27
+ flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications.id_documents")
28
+ redirect_to decidim_verifications.authorizations_path
29
+ end
30
+
31
+ on(:invalid) do
32
+ flash[:alert] = t("authorizations.create.error", scope: "decidim.verifications.id_documents")
33
+ render action: :new
34
+ end
35
+ end
36
+ end
37
+
38
+ def edit
39
+ authorize! :update, @authorization
40
+
41
+ @form = UploadForm.from_model(@authorization)
42
+ end
43
+
44
+ def update
45
+ authorize! :update, @authorization
46
+
47
+ @form = UploadForm.from_params(
48
+ params.merge(
49
+ user: current_user,
50
+ verification_attachment: @authorization.verification_attachment
51
+ )
52
+ )
53
+
54
+ PerformAuthorizationStep.call(@authorization, @form) do
55
+ on(:ok) do
56
+ flash[:notice] = t("authorizations.update.success", scope: "decidim.verifications.id_documents")
57
+ redirect_to decidim_verifications.authorizations_path
58
+ end
59
+
60
+ on(:invalid) do
61
+ flash[:alert] = t("authorizations.update.error", scope: "decidim.verifications.id_documents")
62
+ render action: :edit
63
+ end
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def authorization
70
+ @authorization_presenter ||= AuthorizationPresenter.new(@authorization)
71
+ end
72
+
73
+ def load_authorization
74
+ @authorization = Decidim::Authorization.find_or_initialize_by(
75
+ user: current_user,
76
+ name: "id_documents"
77
+ )
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module PostalLetter
6
+ module Admin
7
+ class PendingAuthorizationsController < Decidim::Admin::ApplicationController
8
+ layout "decidim/admin/users"
9
+
10
+ def index
11
+ authorize! :index, Authorization
12
+
13
+ @pending_authorizations = AuthorizationPresenter.for_collection(
14
+ pending_authorizations
15
+ )
16
+ end
17
+
18
+ private
19
+
20
+ def pending_authorizations
21
+ Authorizations.new(name: "postal_letter", granted: false)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module PostalLetter
6
+ module Admin
7
+ #
8
+ # Handles postages for verification letters.
9
+ #
10
+ class PostagesController < Decidim::Admin::ApplicationController
11
+ layout "decidim/admin/users"
12
+
13
+ before_action :load_pending_authorization
14
+
15
+ def create
16
+ authorize! :update, @pending_authorization
17
+
18
+ @form = PostageForm.from_model(@pending_authorization)
19
+
20
+ PerformAuthorizationStep.call(@pending_authorization, @form) do
21
+ on(:ok) do
22
+ flash[:notice] = t("postages.create.success", scope: "decidim.verifications.postal_letter.admin")
23
+ redirect_to pending_authorizations_path
24
+ end
25
+
26
+ on(:invalid) do
27
+ render json: { error: I18n.t("postages.create.error", scope: "decidim.verifications.postal_letter.admin") }, status: 422
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def load_pending_authorization
35
+ @pending_authorization = Authorization.find(params[:pending_authorization_id])
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module PostalLetter
6
+ class AuthorizationsController < Decidim::ApplicationController
7
+ helper_method :authorization
8
+
9
+ before_action :load_authorization
10
+
11
+ def new
12
+ authorize! :create, @authorization
13
+
14
+ @form = AddressForm.new
15
+ end
16
+
17
+ def create
18
+ authorize! :create, @authorization
19
+
20
+ @form = AddressForm.from_params(params.merge(user: current_user))
21
+
22
+ PerformAuthorizationStep.call(@authorization, @form) do
23
+ on(:ok) do
24
+ flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications.postal_letter")
25
+ redirect_to decidim_verifications.authorizations_path
26
+ end
27
+
28
+ on(:invalid) do
29
+ flash.now[:alert] = t("authorizations.create.error", scope: "decidim.verifications.postal_letter")
30
+ render :new
31
+ end
32
+ end
33
+ end
34
+
35
+ def edit
36
+ authorize! :update, @authorization
37
+
38
+ @form = ConfirmationForm.from_params(params)
39
+ end
40
+
41
+ def update
42
+ authorize! :update, @authorization
43
+
44
+ @form = ConfirmationForm.from_params(params)
45
+
46
+ ConfirmUserAuthorization.call(@authorization, @form) do
47
+ on(:ok) do
48
+ flash[:notice] = t("authorizations.update.success", scope: "decidim.verifications.postal_letter")
49
+ redirect_to decidim_verifications.authorizations_path
50
+ end
51
+
52
+ on(:invalid) do
53
+ flash.now[:alert] = t("authorizations.update.error", scope: "decidim.verifications.postal_letter")
54
+ render :edit
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def authorization
62
+ @authorization_presenter ||= AuthorizationPresenter.new(@authorization)
63
+ end
64
+
65
+ def load_authorization
66
+ @authorization = Decidim::Authorization.find_or_initialize_by(
67
+ user: current_user,
68
+ name: "postal_letter"
69
+ )
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module IdDocuments
6
+ # A form object to be used as the base for identity document verification
7
+ class InformationForm < AuthorizationHandler
8
+ mimic :id_document_information
9
+
10
+ DOCUMENT_TYPES = %w(DNI NIE passport).freeze
11
+
12
+ attribute :document_number, String
13
+ attribute :document_type, String
14
+
15
+ validates :document_type,
16
+ inclusion: { in: DOCUMENT_TYPES },
17
+ presence: true
18
+
19
+ validates :document_number,
20
+ format: { with: /\A[A-Z0-9]*\z/, message: I18n.t("errors.messages.uppercase_only_letters_numbers") },
21
+ presence: true
22
+
23
+ def handler_name
24
+ "id_documents"
25
+ end
26
+
27
+ def map_model(model)
28
+ self.document_type = model.verification_metadata["document_type"]
29
+ self.document_number = model.verification_metadata["document_number"]
30
+ end
31
+
32
+ def verification_metadata
33
+ {
34
+ "document_type" => document_type,
35
+ "document_number" => document_number
36
+ }
37
+ end
38
+
39
+ def document_types_for_select
40
+ DOCUMENT_TYPES.map do |type|
41
+ [
42
+ I18n.t(type.downcase, scope: "decidim.verifications.id_documents"),
43
+ type
44
+ ]
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module IdDocuments
6
+ # A form object to be used for reject a verification request by identity
7
+ # document upload.
8
+ class InformationRejectionForm < InformationForm
9
+ def verification_metadata
10
+ super.merge("rejected" => true)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module IdDocuments
6
+ # A form object to be used when public users want to get verified by
7
+ # uploading their identity documents.
8
+ class UploadForm < InformationForm
9
+ mimic :id_document_upload
10
+
11
+ attribute :verification_attachment, String
12
+
13
+ validates :verification_attachment,
14
+ file_size: { less_than_or_equal_to: ->(_record) { Decidim.maximum_attachment_size } },
15
+ file_content_type: { allow: ["image/jpeg", "image/png"] },
16
+ presence: true
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Decidim
6
+ module Verifications
7
+ module PostalLetter
8
+ # A form object to be used when public users want to get verified by
9
+ # uploading their identity documents.
10
+ class AddressForm < AuthorizationHandler
11
+ attribute :full_address, String
12
+
13
+ validates :full_address, presence: true
14
+
15
+ def handler_name
16
+ "postal_letter"
17
+ end
18
+
19
+ def verification_metadata
20
+ {
21
+ address: full_address,
22
+ pending_verification_code: SecureRandom.random_number(1_000_000).to_s
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module PostalLetter
6
+ # A form object that just holds a verification code that the user will
7
+ # have received by postal letter.
8
+ class ConfirmationForm < AuthorizationHandler
9
+ attribute :verification_code, String
10
+
11
+ validates :verification_code, presence: true
12
+
13
+ def verification_metadata
14
+ { "verification_code" => verification_code }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module PostalLetter
6
+ # A form object to be used when admins want to mark a verification letter
7
+ # as sent.
8
+ class PostageForm < AuthorizationHandler
9
+ attribute :full_address, String
10
+ attribute :verification_code, String
11
+
12
+ validates :full_address, presence: true
13
+ validates :verification_code, presence: true
14
+
15
+ def handler_name
16
+ "postal_letter"
17
+ end
18
+
19
+ def map_model(model)
20
+ self.verification_code = model.verification_metadata["pending_verification_code"]
21
+ self.full_address = model.verification_metadata["address"]
22
+ end
23
+
24
+ def verification_metadata
25
+ {
26
+ address: full_address,
27
+ verification_code: verification_code,
28
+ letter_sent_at: Time.zone.now
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Verifications
5
+ module IdDocuments
6
+ #
7
+ # Decorator for id document authorizations
8
+ #
9
+ class AuthorizationPresenter < SimpleDelegator
10
+ #
11
+ # Whether the verification has been rejected or not and thus, whether
12
+ # the user should be prompted again to reupload documents
13
+ #
14
+ def rejected?
15
+ verification_metadata["rejected"] == true
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end