lesli_shield 1.1.6 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -0
  3. data/app/assets/stylesheets/lesli_shield/application.css +1222 -1
  4. data/app/assets/stylesheets/lesli_shield/confirmations.css +917 -18
  5. data/app/assets/stylesheets/lesli_shield/confirmations.tailwind.css +918 -0
  6. data/app/assets/stylesheets/lesli_shield/invitations.css +1008 -0
  7. data/app/assets/stylesheets/lesli_shield/passwords.css +872 -18
  8. data/app/assets/stylesheets/lesli_shield/passwords.tailwind.css +873 -0
  9. data/app/assets/stylesheets/lesli_shield/registrations.css +904 -832
  10. data/app/assets/stylesheets/lesli_shield/registrations.tailwind.css +908 -0
  11. data/app/assets/stylesheets/lesli_shield/sessions.css +984 -32
  12. data/app/assets/stylesheets/lesli_shield/sessions.tailwind.css +983 -1
  13. data/app/controllers/lesli_shield/invites_controller.rb +100 -49
  14. data/app/controllers/lesli_shield/profile/passwords_controller.rb +119 -0
  15. data/app/controllers/lesli_shield/profiles_controller.rb +69 -0
  16. data/app/controllers/lesli_shield/role/actions_controller.rb +134 -38
  17. data/app/controllers/lesli_shield/roles_controller.rb +114 -141
  18. data/app/controllers/lesli_shield/sessions_controller.rb +1 -2
  19. data/app/controllers/lesli_shield/users_controller.rb +11 -4
  20. data/app/controllers/users/confirmations_controller.rb +162 -49
  21. data/app/controllers/users/invitations_controller.rb +101 -0
  22. data/app/controllers/users/passwords_controller.rb +211 -28
  23. data/app/controllers/users/registrations_controller.rb +108 -83
  24. data/app/controllers/users/sessions_controller.rb +124 -84
  25. data/app/interfaces/lesli_shield/authorization_interface.rb +2 -2
  26. data/app/jobs/lesli_shield/invitation_delivery_job.rb +123 -0
  27. data/app/mailers/lesli_shield/devise_mailer.rb +73 -17
  28. data/app/mailers/lesli_shield/invitation_mailer.rb +58 -0
  29. data/app/models/concerns/lesli_shield/role_management.rb +70 -0
  30. data/app/models/concerns/lesli_shield/user_authentication.rb +54 -0
  31. data/app/models/concerns/lesli_shield/user_security.rb +22 -19
  32. data/app/models/lesli_shield/account.rb +4 -1
  33. data/app/models/lesli_shield/invite.rb +183 -12
  34. data/app/models/lesli_shield/user/role.rb +1 -1
  35. data/app/models/lesli_shield/user.rb +5 -2
  36. data/app/services/lesli_shield/invitation_audit.rb +34 -0
  37. data/app/services/lesli_shield/invitation_email_policy.rb +64 -0
  38. data/app/services/lesli_shield/invitation_service.rb +405 -0
  39. data/app/services/lesli_shield/invite_service.rb +34 -5
  40. data/app/services/lesli_shield/profile/password_service.rb +47 -0
  41. data/app/services/lesli_shield/profile_service.rb +25 -0
  42. data/app/services/lesli_shield/role_action_service.rb +37 -92
  43. data/app/services/lesli_shield/role_audit.rb +42 -0
  44. data/app/services/lesli_shield/role_permission_service.rb +347 -0
  45. data/app/services/lesli_shield/role_privilege_deployment_service.rb +79 -0
  46. data/app/services/lesli_shield/role_privilege_service.rb +69 -105
  47. data/app/services/lesli_shield/role_service.rb +431 -0
  48. data/app/services/lesli_shield/user_registration_service.rb +41 -74
  49. data/app/services/lesli_shield/user_session_service.rb +1 -1
  50. data/app/services/lesli_shield/user_validator_service.rb +37 -39
  51. data/app/views/devise/confirmations/new.html.erb +58 -12
  52. data/app/views/devise/confirmations/show.html.erb +27 -60
  53. data/app/views/devise/passwords/edit.html.erb +59 -25
  54. data/app/views/devise/passwords/new.html.erb +61 -26
  55. data/app/views/devise/registrations/new.html.erb +83 -56
  56. data/app/views/devise/sessions/new.html.erb +58 -41
  57. data/app/views/devise/shared/_application-devise-simple.erb +18 -19
  58. data/app/views/devise/shared/_demo.html.erb +8 -6
  59. data/app/views/devise/shared/_error_messages.html.erb +45 -10
  60. data/app/views/devise/shared/_links.html.erb +10 -20
  61. data/app/views/lesli_shield/invites/_form.html.erb +64 -7
  62. data/app/views/lesli_shield/invites/_invite.html.erb +55 -2
  63. data/app/views/lesli_shield/invites/_invite_actions.html.erb +34 -0
  64. data/app/views/lesli_shield/invites/_status_badge.html.erb +13 -0
  65. data/app/views/lesli_shield/invites/index.html.erb +119 -62
  66. data/app/views/lesli_shield/invites/new.html.erb +11 -36
  67. data/app/views/lesli_shield/invites/show.html.erb +13 -52
  68. data/app/views/lesli_shield/profiles/_information-form.html.erb +11 -0
  69. data/app/views/lesli_shield/profiles/_password-form.html.erb +68 -0
  70. data/app/views/lesli_shield/profiles/show.html.erb +26 -0
  71. data/app/views/lesli_shield/role/actions/_custom_actions.html.erb +24 -0
  72. data/app/views/lesli_shield/role/actions/_deployment_status.html.erb +14 -0
  73. data/app/views/lesli_shield/role/actions/_matrix.html.erb +69 -0
  74. data/app/views/lesli_shield/role/actions/_permissions.html.erb +95 -0
  75. data/app/views/lesli_shield/role/actions/_toggle.html.erb +34 -0
  76. data/app/views/lesli_shield/role/actions/index.html.erb +14 -10
  77. data/app/views/lesli_shield/roles/_deployment_badge.html.erb +15 -0
  78. data/app/views/lesli_shield/roles/_empty_state.html.erb +20 -0
  79. data/app/views/lesli_shield/roles/_field_errors.html.erb +5 -0
  80. data/app/views/lesli_shield/roles/_filters.html.erb +66 -0
  81. data/app/views/lesli_shield/roles/_form.html.erb +149 -18
  82. data/app/views/lesli_shield/roles/_management_menu.html.erb +37 -0
  83. data/app/views/lesli_shield/roles/_pagination.html.erb +43 -0
  84. data/app/views/lesli_shield/roles/_role_list.html.erb +116 -0
  85. data/app/views/lesli_shield/roles/_status_badge.html.erb +11 -0
  86. data/app/views/lesli_shield/roles/_type_badge.html.erb +9 -0
  87. data/app/views/lesli_shield/roles/index.html.erb +15 -38
  88. data/app/views/lesli_shield/roles/new.html.erb +11 -0
  89. data/app/views/lesli_shield/roles/show.html.erb +79 -7
  90. data/app/views/lesli_shield/users/_information-form.html.erb +15 -1
  91. data/app/views/users/invitations/show.html.erb +142 -0
  92. data/app/views/users/invitations/unavailable.html.erb +19 -0
  93. data/config/initializers/devise.rb +52 -328
  94. data/config/locales/devise.en.yml +8 -0
  95. data/config/locales/devise.es.yml +8 -0
  96. data/config/locales/translations.en.yml +314 -16
  97. data/config/locales/translations.es.yml +314 -16
  98. data/config/routes.rb +14 -3
  99. data/db/migrate/v1/0801120110_create_lesli_shield_invites.rb +40 -6
  100. data/lib/lesli_shield/engine.rb +42 -4
  101. data/lib/lesli_shield/router.rb +62 -18
  102. data/lib/lesli_shield/version.rb +2 -2
  103. data/lib/tasks/lesli_shield_tasks.rake +1 -1
  104. data/readme.md +17 -3
  105. metadata +63 -8
  106. data/app/assets/config/lesli_shield_manifest.js +0 -48
  107. data/app/assets/stylesheets/lesli_shield/application.tailwind.css +0 -2
  108. data/app/mailers/lesli_shield/invitation.html.erb +0 -23
  109. data/app/views/lesli_shield/invites/edit.html.erb +0 -12
  110. data/app/views/lesli_shield/role/actions/_form.html.erb +0 -75
@@ -1,184 +1,157 @@
1
- =begin
2
-
3
- Lesli
4
-
5
- Copyright (c) 2025, Lesli Technologies, S. A.
6
-
7
- This program is free software: you can redistribute it and/or modify
8
- it under the terms of the GNU General Public License as published by
9
- the Free Software Foundation, either version 3 of the License, or
10
- (at your option) any later version.
11
-
12
- This program is distributed in the hope that it will be useful,
13
- but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- GNU General Public License for more details.
16
-
17
- You should have received a copy of the GNU General Public License
18
- along with this program. If not, see http://www.gnu.org/licenses/.
19
-
20
- Lesli · Ruby on Rails SaaS Development Framework.
21
-
22
- Made with ♥ by LesliTech
23
- Building a better future, one line of code at a time.
24
-
25
- @contact hello@lesli.tech
26
- @website https://www.lesli.tech
27
- @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
-
29
- // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
- // ·
31
- =end
1
+ # frozen_string_literal: true
32
2
 
33
3
  module LesliShield
34
4
  class RolesController < ApplicationController
35
- before_action :set_role, only: %i[ show update destroy deploy ]
36
-
37
- def deploy
38
- RolePrivilegeService.new.synchronize(@role.result)
39
- respond_with_lesli(
40
- :turbo => stream_notification_success("success")
41
- )
42
- end
5
+ before_action :set_visible_role, only: %i[show duplicate]
6
+ before_action :set_manageable_role, only: %i[edit update destroy deploy]
43
7
 
44
8
  def index
45
- @roles = respond_with_pagination(Lesli::RoleService.new(current_user, query).index)
9
+ @filters = {
10
+ search: params[:search].to_s.strip,
11
+ status: allowed_filter(params[:status], RoleService::STATUS_FILTERS),
12
+ type: allowed_filter(params[:type], RoleService::TYPE_FILTERS)
13
+ }
14
+ @roles = respond_with_pagination(
15
+ role_service.list(@filters)
16
+ )
46
17
  end
47
18
 
48
19
  def show
49
- @role = @role.show
20
+ prepare_role_page
50
21
  end
51
22
 
52
- # @return [HTML] HTML view for creating a new role
53
- # @description returns an HTML view with a form so users can create a new role
54
- # @example
55
- # # Executing this controller's action from javascript's frontend
56
- # this.url.go('/roles/new')
57
23
  def new
24
+ @role = role_service.build
58
25
  end
59
26
 
60
- # @return [HTML] HTML view for editing the role
61
- # @description returns an HTML view with a form so users edit an existing role
62
- # @example
63
- # # Executing this controller's action from javascript's frontend
64
- # let role_id = 3;
65
- # this.url.go(`/roles/${role_id}/edit`)
66
27
  def edit
28
+ prepare_role_page
29
+ render :show
67
30
  end
68
31
 
69
- # @return [Json] Json that contains wheter the creation of the role was successful or not.
70
- # If it is not successful, it returns an error message
71
- # @description Creates a new role associated to the *current_user*'s *account*.
72
- # @example
73
- # # Executing this controller's action from javascript's frontend
74
- # let data = {
75
- # role: {
76
- # name: "Change Request"
77
- # }
78
- # };
79
- # this.http.post('127.0.0.1/house/roles', data);
80
32
  def create
81
-
82
- role = RoleService.new(current_user).create(role_params)
83
-
84
- if role.successful?
85
- respond_with_successful(role.result)
86
- else
87
- respond_with_error(role.errors_as_sentence)
88
- end
33
+ @role = role_service.create!(role_attributes)
34
+ success(I18n.t("lesli_shield.roles.created_successfully"))
35
+ respond_with_lesli(
36
+ json: @role,
37
+ redirect: role_path(@role)
38
+ )
39
+ rescue RoleService::Error => exception
40
+ respond_with_role_error(exception, template: "new")
89
41
  end
90
42
 
91
- # @controller_action_param :name [String] The name of the role
92
- # @return [Json] Json that contains wheter the role was successfully updated or not.
93
- # If it it not successful, it returns an error message
94
- # @description Updates an existing role associated to the *current_user*'s *account*.
95
43
  def update
96
- # Respond with 404 if role was not found
97
- return respond_with_not_found unless @role.found?
98
-
99
- # check if current user can work with role
100
- # unless current_user.can_work_with_role?(@role.resource)
101
- # return respond_with_error(I18n.t("core.roles.messages_danger_updating_role_object_level_permission_too_high"))
102
- # end
44
+ @role = role_service.update!(@role, role_attributes)
45
+ success(I18n.t("lesli_shield.roles.updated_successfully"))
46
+ respond_with_lesli(
47
+ json: @role,
48
+ redirect: role_path(@role)
49
+ )
50
+ rescue RoleService::Error => exception
51
+ respond_with_role_error(exception, template: "show")
52
+ end
103
53
 
104
- # Update role information
105
- @role.update(role_params)
54
+ def destroy
55
+ role_service.destroy!(@role)
56
+ success(I18n.t("lesli_shield.roles.destroyed_successfully"))
57
+ respond_with_lesli(redirect: roles_path)
58
+ rescue RoleService::Error => exception
59
+ respond_with_role_error(exception, template: "show")
60
+ end
106
61
 
107
- # check if the update went OK
108
- if @role.successful?
109
- respond_with_lesli(:turbo => stream_notification_success("Role updated successfully!"))
110
- else
111
- respond_with_lesli(:turbo => stream_notification_danger(@role.errors_as_sentence))
112
- end
62
+ def duplicate
63
+ duplicate = role_service.duplicate!(@role)
64
+ success(
65
+ I18n.t(
66
+ "lesli_shield.roles.duplicated_successfully",
67
+ role: duplicate.name
68
+ )
69
+ )
70
+ respond_with_lesli(
71
+ json: duplicate,
72
+ redirect: role_path(duplicate)
73
+ )
74
+ rescue RoleService::Error => exception
75
+ danger(exception.message)
76
+ respond_with_lesli(
77
+ json: { message: exception.message },
78
+ redirect: role_path(@role),
79
+ status: :unprocessable_content
80
+ )
113
81
  end
114
82
 
115
- # @return [Json] Json that contains wheter the role was successfully deleted or not.
116
- # If it it not successful, it returns an error message
117
- # @description Deletes an existing *role* associated to the *current_user*'s *account*.
118
- def destroy
119
- return respond_with_not_found unless @role.found?
83
+ def deploy
84
+ RolePrivilegeDeploymentService.new(current_user).deploy!(@role)
85
+ success(I18n.t("lesli_shield.roles.permissions_deployed"))
86
+ respond_with_lesli(
87
+ json: { role_id: @role.id },
88
+ redirect: permission_index_path
89
+ )
90
+ end
120
91
 
121
- # Validation: check if the role has still associated users
122
- if @role.has_users?
123
- return respond_with_error(I18n.t("core.roles.messages_danger_users_assigned_validation"))
124
- end
92
+ private
125
93
 
126
- @role.destroy
94
+ def role_service
95
+ @role_service ||= RoleService.new(current_user, query)
96
+ end
127
97
 
128
- # Check if the deletion went ok
129
- unless @role.successful?
130
- return respond_with_error(@role.errors)
98
+ def permission_index_path
99
+ options = { search: params[:search].presence }
100
+ if params[:engine_slug].present?
101
+ engine_role_actions_path(@role, engine_slug: params[:engine_slug], **options)
102
+ else
103
+ role_actions_path(@role, **options)
131
104
  end
132
-
133
- respond_with_successful
134
105
  end
135
106
 
136
- # @return [JSON]
137
- # @description Gets all the specific options for roles CRUD
138
- def options
139
- respond_with_successful(RoleService.new(current_user).options)
107
+ def set_visible_role
108
+ @role = role_service.find!(params[:id])
109
+ @role_editable = @role.manageable_by?(current_user)
140
110
  end
141
111
 
142
- private
112
+ def set_manageable_role
113
+ @role = role_service.find!(params[:id], manageable: true)
114
+ # Keep the form editable after validation fails. Submitted values
115
+ # must not redefine whether the persisted role can be administered.
116
+ @role_editable = true
117
+ end
143
118
 
144
- # @return [void]
145
- # @description Sets the requested user based on the current_users's account
146
- # @example
147
- # # Executing this method from a controller action:
148
- # set_role
149
- # puts @role
150
- # # This will either display nil or an instance of Role
151
- def set_role
152
- @role = Lesli::RoleService.new(current_user, @query).find(params[:id])
153
- return respond_with_not_found unless @role.found?
154
- end
155
-
156
- # @return [Parameters] Allowed parameters for the role
157
- # @description Sanitizes the parameters received from an HTTP call to only allow the specified ones.
158
- # Allowed params are detail_attributes: [:name, :active, :object_level_permission]
159
- # @example
160
- # # suppose params contains {
161
- # # "role": {
162
- # # "name": "Admin",
163
- # # "word": Hello
164
- # # }
165
- # #}
166
- # filtered_params = role_params
167
- # puts filtered_params
168
- # # will remove all unpermitted attributes and only print {
169
- # # "name": "Admin",
170
- # #}
171
- def role_params
119
+ def role_attributes
172
120
  params.require(:role).permit(
173
121
  :name,
174
122
  :active,
175
123
  :description,
176
124
  :path_default,
177
- :path_limited,
178
- :isolated,
179
125
  :permission_level
180
- )
126
+ ).to_h.symbolize_keys
181
127
  end
182
128
 
129
+ def allowed_filter(value, options)
130
+ value if options.include?(value)
131
+ end
132
+
133
+ def prepare_role_page
134
+ @role_summary = role_service.summary(@role)
135
+ end
136
+
137
+ def respond_with_role_error(exception, template:)
138
+ @role = exception.record || @role
139
+ prepare_role_page if @role.persisted?
140
+ respond_with_lesli(
141
+ turbo: [
142
+ turbo_stream.replace(
143
+ "role-form",
144
+ partial: "lesli_shield/roles/form"
145
+ ),
146
+ stream_notification_danger(exception.message)
147
+ ],
148
+ json: {
149
+ message: exception.message,
150
+ errors: @role&.errors&.to_hash
151
+ },
152
+ html: template,
153
+ status: :unprocessable_content
154
+ )
155
+ end
183
156
  end
184
157
  end
@@ -6,8 +6,7 @@ module LesliShield
6
6
  def index
7
7
  @sessions = respond_with_pagination(UserSessionService.new(current_user, query).index())
8
8
  respond_with_lesli(
9
- :html => @sessions,
10
- :json => @sessions
9
+ json: @sessions
11
10
  )
12
11
  end
13
12
 
@@ -36,15 +36,21 @@ module LesliShield
36
36
 
37
37
  # GET /users
38
38
  def index
39
- @users = respond_with_pagination(Lesli::UserService.new(current_user, query).index(params))
39
+ @users = respond_with_pagination(Lesli::UserService.new(current_user, query).index)
40
40
  end
41
41
 
42
42
  # GET /users/1
43
43
  def show
44
+ @user = @user.show
44
45
  @user_roles = @user.roles
45
- @activities = @user.logs
46
46
  @sessions = @user.sessions
47
- @user = @user.show
47
+ @activities = if defined?(LesliAudit)
48
+ @user.logs.order(id: :desc)
49
+ .select(:id, :operation, :description, Lesli::Date2.new.db_column("created_at", as: "date"))
50
+ .limit(7).as_json
51
+ else
52
+ []
53
+ end
48
54
  end
49
55
 
50
56
  # GET /users/new
@@ -116,7 +122,8 @@ module LesliShield
116
122
  :roles_id,
117
123
  :first_name,
118
124
  :last_name,
119
- :telephone
125
+ :telephone,
126
+ :time_zone
120
127
  )
121
128
  end
122
129
  end
@@ -29,73 +29,186 @@ Building a better future, one line of code at a time.
29
29
  @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
30
30
 
31
31
  // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
32
- // ·
32
+ // ·
33
33
  =end
34
- class Users::ConfirmationsController < Devise::ConfirmationsController
35
34
 
35
+ class Users::ConfirmationsController < Devise::ConfirmationsController
36
+ CONFIRMATION_RATE_LIMIT_WINDOW = 1.hour
37
+
38
+ rate_limit(
39
+ to: 10,
40
+ within: CONFIRMATION_RATE_LIMIT_WINDOW,
41
+ by: -> { request.remote_ip },
42
+ with: :respond_to_confirmation_rate_limit,
43
+ only: :create,
44
+ name: "confirmation-request-ip"
45
+ )
46
+
47
+ rate_limit(
48
+ to: 3,
49
+ within: CONFIRMATION_RATE_LIMIT_WINDOW,
50
+ by: :confirmation_email_rate_limit_key,
51
+ with: :respond_to_confirmation_rate_limit,
52
+ only: :create,
53
+ name: "confirmation-request-email"
54
+ )
55
+
56
+ before_action :protect_confirmation_token, only: :show
57
+
58
+ # Devise validates and consumes the raw token. Account provisioning and the
59
+ # successful audit event share the same transaction so a partial account can
60
+ # never be left behind and a provisioning failure leaves the token reusable.
36
61
  def show
62
+ self.resource = confirm_user_and_provision_account
63
+
64
+ if resource.errors.empty?
65
+ send_welcome_email_safely(resource)
66
+ set_flash_message!(:notice, :confirmed)
67
+ respond_with_navigational(resource) do
68
+ redirect_to after_confirmation_path_for(resource_name, resource)
69
+ end
70
+ else
71
+ respond_with_navigational(
72
+ resource.errors,
73
+ status: :unprocessable_content
74
+ ) do
75
+ render :new, status: :unprocessable_content
76
+ end
77
+ end
78
+ end
79
+
80
+ protected
81
+
82
+ # Keep the user on the resend page so Devise's generic paranoid-mode notice
83
+ # is visible without disclosing whether the email exists or is confirmed.
84
+ def after_resending_confirmation_instructions_path_for(_resource_name)
85
+ new_user_confirmation_path
86
+ end
87
+
88
+ private
89
+
90
+ def confirm_user_and_provision_account
91
+ confirmed_user = nil
37
92
 
38
- # delete all previus messages
39
- flash.clear
93
+ resource_class.transaction do
94
+ confirmed_user = resource_class.confirm_by_token(
95
+ params[:confirmation_token]
96
+ )
40
97
 
41
- # get the confirmation token sent through get params
42
- token = params[:confirmation_token]
98
+ if confirmed_user.errors.empty?
99
+ LesliShield::UserRegistrationService
100
+ .new(confirmed_user)
101
+ .provision_account!
43
102
 
44
- # validate that token were sent
45
- if token.blank?
46
- return flash[:danger] = I18n.t("core.users/confirmations.messages_warning_invalid_token")
103
+ confirmed_user.update!(confirmation_token: nil)
104
+ record_successful_confirmation!(confirmed_user)
105
+ end
47
106
  end
48
107
 
49
- # check if token belongs to a unconfirmed user
50
- user = Lesli::User.find_by(:confirmation_token => token, :confirmed_at => nil)
108
+ confirmed_user
109
+ rescue StandardError => exception
110
+ Rails.error.report(
111
+ exception,
112
+ handled: true,
113
+ context: {
114
+ controller: self.class.name,
115
+ operation: "user_confirmation_succeeded"
116
+ }
117
+ )
118
+
119
+ confirmation_failure_resource(confirmed_user)
120
+ end
51
121
 
52
- # validate that user were found
53
- if user.blank?
54
- return flash[:danger] = I18n.t("core.users/confirmations.messages_warning_invalid_token")
122
+ def confirmation_failure_resource(user)
123
+ failed_resource = if user&.persisted?
124
+ resource_class.find_by(id: user.id)
55
125
  end
56
126
 
57
- # register a log with a validation atempt for the user
58
- log = user.log(engine: LesliShield, source: self.class.name, action: action_name, operation: "user_confirmation", description: "Confirmation process started")
59
-
60
- # create a new instance of the registration service
61
- registration_service = LesliShield::UserRegistrationService.new(user)
127
+ failed_resource ||= resource_class.new
128
+ failed_resource.errors.add(
129
+ :base,
130
+ I18n.t(
131
+ "lesli_shield.devise_confirmations.message_confirmation_failed"
132
+ )
133
+ )
134
+ failed_resource
135
+ end
62
136
 
63
- # confirm the user
64
- registration_service.confirm
137
+ def record_successful_confirmation!(user)
138
+ user.log(
139
+ engine: LesliShield,
140
+ source: self.class.name,
141
+ action: action_name,
142
+ operation: "user_confirmation_succeeded",
143
+ description: "User confirmed successfully"
144
+ )
145
+ end
65
146
 
66
- # send a welcome email to user as is confirmed
67
- LesliShield::DeviseMailer.with(user: resource).welcome.deliver_later
147
+ def send_welcome_email_safely(user)
148
+ LesliShield::DeviseMailer.welcome(user).deliver_later
149
+ rescue StandardError => exception
150
+ Rails.error.report(
151
+ exception,
152
+ handled: true,
153
+ context: {
154
+ controller: self.class.name,
155
+ operation: "welcome_email_delivery"
156
+ }
157
+ )
158
+ end
68
159
 
69
- # let the user knows that the confirmation is done
70
- flash[:success] = I18n.t("core.users/confirmations.messages_success_email_updated")
71
-
72
- # setup the new account
73
- registration_service.create_account if user.account.blank?
160
+ def confirmation_email_rate_limit_key
161
+ normalized_email = confirmation_request_params[:email]
162
+ .to_s
163
+ .strip
164
+ .downcase
165
+ identity = normalized_email.presence || "blank:#{request.remote_ip}"
166
+
167
+ OpenSSL::HMAC.hexdigest(
168
+ "SHA256",
169
+ Rails.application.secret_key_base,
170
+ identity
171
+ )
172
+ end
74
173
 
75
- log.update(description: 'User confirmed successfully') if defined?(LesliAudit)
174
+ def confirmation_request_params
175
+ params
176
+ .fetch(resource_name, ActionController::Parameters.new)
177
+ .permit(:email)
76
178
  end
77
179
 
78
-
79
- # @controller_action_param :email [String] The registered user email
80
- # @return [Json] Json that contains wheter the email confirmation was sent or not.
81
- # If it is not successful, it returs an error message
82
- # @description Resends a email confirmation an already registered user
83
- # @example
84
- # # Executing this controller's action from javascript's frontend
85
- # let email = 'john.doe@email.com';
86
- # let data = {
87
- # user: {
88
- # email: email
89
- # }
90
- # };
91
- # this.http.post('127.0.0.1/conformation', data);
92
- def create
93
- super do |resource|
94
- if successfully_sent?(resource)
95
- return respond_with_successful
96
- else
97
- return respond_with_error(resource.errors.full_messages.to_sentence)
180
+ def respond_to_confirmation_rate_limit
181
+ message = I18n.t(
182
+ "lesli_shield.devise_confirmations.message_too_many_attempts"
183
+ )
184
+
185
+ response.set_header(
186
+ "Retry-After",
187
+ CONFIRMATION_RATE_LIMIT_WINDOW.to_i.to_s
188
+ )
189
+
190
+ respond_to do |format|
191
+ format.html { render_confirmation_rate_limit(message) }
192
+ format.json do
193
+ render json: { error: message }, status: :too_many_requests
98
194
  end
195
+ format.any { head :too_many_requests }
99
196
  end
100
197
  end
198
+
199
+ def render_confirmation_rate_limit(message)
200
+ flash.now[:alert] = message
201
+ self.resource = resource_class.new(
202
+ email: confirmation_request_params[:email]
203
+ )
204
+ render :new, status: :too_many_requests
205
+ end
206
+
207
+ def protect_confirmation_token
208
+ # Prevent the token in the query string from being sent to third-party
209
+ # origins while keeping a valid Origin for the resend form rendered when
210
+ # Devise rejects an invalid or expired token.
211
+ response.set_header("Referrer-Policy", "same-origin")
212
+ response.set_header("Cache-Control", "no-store")
213
+ end
101
214
  end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Users::InvitationsController < Lesli::ApplicationDeviseController
4
+ before_action :protect_invitation_response
5
+ before_action :load_invitation
6
+
7
+ def show
8
+ @user = Lesli::User.new(email: @invitation.email)
9
+ set_minimum_password_length
10
+ end
11
+
12
+ def update
13
+ accepted_user = invitation_service.accept!(
14
+ token: @invitation_token,
15
+ user_attributes: user_params
16
+ )
17
+
18
+ redirect_to(
19
+ new_user_session_path,
20
+ notice: I18n.t(
21
+ "lesli_shield.invites.acceptance_success",
22
+ email: accepted_user.email
23
+ ),
24
+ status: :see_other
25
+ )
26
+ rescue ActiveRecord::RecordInvalid => exception
27
+ unless exception.record.is_a?(Lesli::User)
28
+ return render_invitation_unavailable(
29
+ invitation_failure_reason(exception.record),
30
+ :gone
31
+ )
32
+ end
33
+
34
+ @user = exception.record
35
+ set_minimum_password_length
36
+ flash.now[:alert] = @user.errors.full_messages.to_sentence
37
+ render :show, status: :unprocessable_content
38
+ end
39
+
40
+ private
41
+
42
+ def load_invitation
43
+ @invitation_token = params.expect(:invitation_token)
44
+ @invitation = invitation_service.invitation_from_token!(
45
+ @invitation_token
46
+ )
47
+ rescue ActiveRecord::RecordInvalid => exception
48
+ render_invitation_unavailable(
49
+ invitation_failure_reason(exception.record),
50
+ :gone
51
+ )
52
+ rescue ActiveSupport::MessageVerifier::InvalidSignature,
53
+ ActiveRecord::RecordNotFound,
54
+ ActionController::ParameterMissing
55
+ render_invitation_unavailable(:invalid, :not_found)
56
+ end
57
+
58
+ def invitation_service
59
+ @invitation_service ||= LesliShield::InvitationService.new(nil)
60
+ end
61
+
62
+ def user_params
63
+ params.require(:user).permit(
64
+ :first_name,
65
+ :last_name,
66
+ :telephone,
67
+ :password,
68
+ :password_confirmation
69
+ )
70
+ end
71
+
72
+ def set_minimum_password_length
73
+ @minimum_password_length = Devise.password_length.min
74
+ end
75
+
76
+ def protect_invitation_response
77
+ response.set_header("Cache-Control", "no-store")
78
+ response.set_header("Referrer-Policy", "same-origin")
79
+ end
80
+
81
+ def invitation_failure_reason(invitation)
82
+ return :used if invitation.accepted? || invitation.accepted_at.present?
83
+ return :cancelled if invitation.cancelled? ||
84
+ invitation.cancelled_at.present?
85
+ return :expired if invitation.expired? ||
86
+ invitation.expires_at&.past?
87
+
88
+ :unavailable
89
+ end
90
+
91
+ def render_invitation_unavailable(reason, status)
92
+ @invitation_error_title = I18n.t(
93
+ "lesli_shield.invites.acceptance_#{reason}_title"
94
+ )
95
+ @invitation_error_description = I18n.t(
96
+ "lesli_shield.invites.acceptance_#{reason}_description"
97
+ )
98
+
99
+ render :unavailable, status: status
100
+ end
101
+ end