lesli_shield 1.1.7 → 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 (86) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/lesli_shield/application.css +1082 -0
  3. data/app/assets/stylesheets/lesli_shield/invitations.css +1008 -0
  4. data/app/controllers/lesli_shield/invites_controller.rb +100 -49
  5. data/app/controllers/lesli_shield/profiles_controller.rb +9 -3
  6. data/app/controllers/lesli_shield/role/actions_controller.rb +134 -38
  7. data/app/controllers/lesli_shield/roles_controller.rb +114 -141
  8. data/app/controllers/lesli_shield/users_controller.rb +11 -4
  9. data/app/controllers/users/confirmations_controller.rb +2 -2
  10. data/app/controllers/users/invitations_controller.rb +101 -0
  11. data/app/controllers/users/passwords_controller.rb +1 -1
  12. data/app/controllers/users/registrations_controller.rb +2 -2
  13. data/app/controllers/users/sessions_controller.rb +3 -1
  14. data/app/jobs/lesli_shield/invitation_delivery_job.rb +123 -0
  15. data/app/mailers/lesli_shield/devise_mailer.rb +3 -3
  16. data/app/mailers/lesli_shield/invitation_mailer.rb +58 -0
  17. data/app/models/concerns/lesli_shield/role_management.rb +70 -0
  18. data/app/models/concerns/lesli_shield/user_security.rb +22 -19
  19. data/app/models/lesli_shield/invite.rb +183 -12
  20. data/app/services/lesli_shield/invitation_audit.rb +34 -0
  21. data/app/services/lesli_shield/invitation_email_policy.rb +64 -0
  22. data/app/services/lesli_shield/invitation_service.rb +405 -0
  23. data/app/services/lesli_shield/invite_service.rb +34 -5
  24. data/app/services/lesli_shield/profile_service.rb +1 -0
  25. data/app/services/lesli_shield/role_action_service.rb +37 -92
  26. data/app/services/lesli_shield/role_audit.rb +42 -0
  27. data/app/services/lesli_shield/role_permission_service.rb +347 -0
  28. data/app/services/lesli_shield/role_privilege_deployment_service.rb +79 -0
  29. data/app/services/lesli_shield/role_privilege_service.rb +69 -105
  30. data/app/services/lesli_shield/role_service.rb +431 -0
  31. data/app/services/lesli_shield/user_registration_service.rb +1 -1
  32. data/app/services/lesli_shield/user_validator_service.rb +9 -9
  33. data/app/views/devise/confirmations/new.html.erb +5 -5
  34. data/app/views/devise/confirmations/show.html.erb +3 -3
  35. data/app/views/devise/passwords/edit.html.erb +7 -7
  36. data/app/views/devise/passwords/new.html.erb +7 -7
  37. data/app/views/devise/registrations/new.html.erb +13 -13
  38. data/app/views/devise/sessions/new.html.erb +8 -8
  39. data/app/views/devise/shared/_demo.html.erb +3 -3
  40. data/app/views/devise/shared/_links.html.erb +8 -11
  41. data/app/views/lesli_shield/invites/_form.html.erb +64 -7
  42. data/app/views/lesli_shield/invites/_invite.html.erb +55 -2
  43. data/app/views/lesli_shield/invites/_invite_actions.html.erb +34 -0
  44. data/app/views/lesli_shield/invites/_status_badge.html.erb +13 -0
  45. data/app/views/lesli_shield/invites/index.html.erb +119 -63
  46. data/app/views/lesli_shield/invites/new.html.erb +11 -36
  47. data/app/views/lesli_shield/invites/show.html.erb +13 -52
  48. data/app/views/lesli_shield/profiles/_information-form.html.erb +11 -0
  49. data/app/views/lesli_shield/profiles/show.html.erb +1 -4
  50. data/app/views/lesli_shield/role/actions/_custom_actions.html.erb +24 -0
  51. data/app/views/lesli_shield/role/actions/_deployment_status.html.erb +14 -0
  52. data/app/views/lesli_shield/role/actions/_matrix.html.erb +69 -0
  53. data/app/views/lesli_shield/role/actions/_permissions.html.erb +95 -0
  54. data/app/views/lesli_shield/role/actions/_toggle.html.erb +34 -0
  55. data/app/views/lesli_shield/role/actions/index.html.erb +14 -10
  56. data/app/views/lesli_shield/roles/_deployment_badge.html.erb +15 -0
  57. data/app/views/lesli_shield/roles/_empty_state.html.erb +20 -0
  58. data/app/views/lesli_shield/roles/_field_errors.html.erb +5 -0
  59. data/app/views/lesli_shield/roles/_filters.html.erb +66 -0
  60. data/app/views/lesli_shield/roles/_form.html.erb +149 -18
  61. data/app/views/lesli_shield/roles/_management_menu.html.erb +37 -0
  62. data/app/views/lesli_shield/roles/_pagination.html.erb +43 -0
  63. data/app/views/lesli_shield/roles/_role_list.html.erb +116 -0
  64. data/app/views/lesli_shield/roles/_status_badge.html.erb +11 -0
  65. data/app/views/lesli_shield/roles/_type_badge.html.erb +9 -0
  66. data/app/views/lesli_shield/roles/index.html.erb +15 -38
  67. data/app/views/lesli_shield/roles/new.html.erb +11 -0
  68. data/app/views/lesli_shield/roles/show.html.erb +79 -7
  69. data/app/views/lesli_shield/users/_information-form.html.erb +6 -0
  70. data/app/views/users/invitations/show.html.erb +142 -0
  71. data/app/views/users/invitations/unavailable.html.erb +19 -0
  72. data/config/locales/devise.en.yml +8 -0
  73. data/config/locales/devise.es.yml +8 -0
  74. data/config/locales/translations.en.yml +313 -78
  75. data/config/locales/translations.es.yml +313 -78
  76. data/config/routes.rb +10 -3
  77. data/db/migrate/v1/0801120110_create_lesli_shield_invites.rb +40 -6
  78. data/lib/lesli_shield/engine.rb +4 -0
  79. data/lib/lesli_shield/router.rb +10 -0
  80. data/lib/lesli_shield/version.rb +2 -2
  81. data/lib/tasks/lesli_shield_tasks.rake +1 -1
  82. data/readme.md +13 -1
  83. metadata +35 -5
  84. data/app/mailers/lesli_shield/invitation.html.erb +0 -23
  85. data/app/views/lesli_shield/invites/edit.html.erb +0 -12
  86. 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
@@ -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
@@ -128,7 +128,7 @@ class Users::ConfirmationsController < Devise::ConfirmationsController
128
128
  failed_resource.errors.add(
129
129
  :base,
130
130
  I18n.t(
131
- "lesli_shield.devise/confirmations.message_confirmation_failed"
131
+ "lesli_shield.devise_confirmations.message_confirmation_failed"
132
132
  )
133
133
  )
134
134
  failed_resource
@@ -179,7 +179,7 @@ class Users::ConfirmationsController < Devise::ConfirmationsController
179
179
 
180
180
  def respond_to_confirmation_rate_limit
181
181
  message = I18n.t(
182
- "lesli_shield.devise/confirmations.message_too_many_attempts"
182
+ "lesli_shield.devise_confirmations.message_too_many_attempts"
183
183
  )
184
184
 
185
185
  response.set_header(
@@ -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
@@ -188,7 +188,7 @@ class Users::PasswordsController < Devise::PasswordsController
188
188
 
189
189
  def respond_to_password_reset_rate_limit
190
190
  message = I18n.t(
191
- "lesli_shield.devise/passwords.message_too_many_attempts"
191
+ "lesli_shield.devise_passwords.message_too_many_attempts"
192
192
  )
193
193
 
194
194
  response.set_header(
@@ -80,7 +80,7 @@ class Users::RegistrationsController < Devise::RegistrationsController
80
80
  redirect_to(
81
81
  new_user_session_path,
82
82
  alert: I18n.t(
83
- "lesli_shield.devise/registrations.message_registration_not_allowed"
83
+ "lesli_shield.devise_registrations.message_registration_not_allowed"
84
84
  ),
85
85
  status: request.post? ? :see_other : :found
86
86
  )
@@ -105,7 +105,7 @@ class Users::RegistrationsController < Devise::RegistrationsController
105
105
 
106
106
  def respond_to_registration_rate_limit
107
107
  message = I18n.t(
108
- "lesli_shield.devise/registrations.message_too_many_attempts"
108
+ "lesli_shield.devise_registrations.message_too_many_attempts"
109
109
  )
110
110
 
111
111
  response.set_header(
@@ -61,6 +61,8 @@ class Users::SessionsController < Devise::SessionsController
61
61
  super do |user|
62
62
  persist_application_session!(user)
63
63
  record_successful_login_analytics
64
+ # Devise sets the successful-login notice before yielding.
65
+ flash.delete(:notice)
64
66
  end
65
67
  end
66
68
 
@@ -153,7 +155,7 @@ class Users::SessionsController < Devise::SessionsController
153
155
  end
154
156
 
155
157
  def respond_to_login_rate_limit
156
- message = I18n.t("lesli_shield.devise/sessions.message_too_many_attempts")
158
+ message = I18n.t("lesli_shield.devise_sessions.message_too_many_attempts")
157
159
  response.set_header("Retry-After", LOGIN_RATE_LIMIT_WINDOW.to_i.to_s)
158
160
 
159
161
  respond_to do |format|
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Delivers invitations outside the web request and turns delivery failures
5
+ # into durable audit evidence plus an error-reporter notification.
6
+ class InvitationDeliveryJob < ApplicationJob
7
+ queue_as :mailers
8
+
9
+ class << self
10
+ def report_enqueue_failure(invitation_id, actor_id, exception)
11
+ new.send(
12
+ :record_delivery_failure,
13
+ invitation_id,
14
+ actor_id,
15
+ exception,
16
+ stage: "enqueue"
17
+ )
18
+ end
19
+ end
20
+
21
+ def perform(invitation_id, acceptance_url, operation, actor_id)
22
+ begin
23
+ invitation = Invite.find(invitation_id)
24
+ actor = Lesli::User.find(actor_id)
25
+ deliver_invitation(invitation, acceptance_url)
26
+ rescue StandardError => exception
27
+ record_delivery_failure(
28
+ invitation_id,
29
+ actor_id,
30
+ exception,
31
+ stage: "delivery"
32
+ )
33
+ raise
34
+ end
35
+
36
+ record_delivery_success(
37
+ operation,
38
+ invitation: invitation,
39
+ actor: actor
40
+ )
41
+ end
42
+
43
+ private
44
+
45
+ def record_delivery_success(operation, invitation:, actor:)
46
+ InvitationAudit.record!(
47
+ operation,
48
+ invitation: invitation,
49
+ actor: actor,
50
+ source: self.class
51
+ )
52
+ rescue StandardError => exception
53
+ # Retrying after the SMTP server accepted the message could send a
54
+ # duplicate. Report an audit failure without retrying the delivery.
55
+ Rails.error.report(
56
+ exception,
57
+ handled: true,
58
+ context: error_context(
59
+ invitation.id,
60
+ stage: "audit_delivery_success",
61
+ operation: operation
62
+ )
63
+ )
64
+ end
65
+
66
+ def deliver_invitation(invitation, acceptance_url)
67
+ InvitationMailer
68
+ .account_invitation(invitation, acceptance_url)
69
+ .deliver_now
70
+ end
71
+
72
+ def record_delivery_failure(
73
+ invitation_id,
74
+ actor_id,
75
+ delivery_exception,
76
+ stage:
77
+ )
78
+ invitation = Invite.find_by(id: invitation_id)
79
+ actor = Lesli::User.find_by(id: actor_id) || invitation&.invited_by
80
+
81
+ if invitation && actor
82
+ record_delivery_failure_event(invitation, actor)
83
+ end
84
+ rescue StandardError => audit_exception
85
+ Rails.error.report(
86
+ audit_exception,
87
+ handled: true,
88
+ context: error_context(
89
+ invitation_id,
90
+ stage: "audit_delivery_failure"
91
+ )
92
+ )
93
+ ensure
94
+ Rails.error.report(
95
+ delivery_exception,
96
+ handled: stage == "enqueue",
97
+ context: error_context(invitation_id, stage: stage)
98
+ )
99
+ end
100
+
101
+ def record_delivery_failure_event(invitation, actor)
102
+ InvitationAudit.record!(
103
+ :invitation_delivery_failed,
104
+ invitation: invitation,
105
+ actor: actor,
106
+ source: self.class
107
+ )
108
+ end
109
+
110
+ def error_context(
111
+ invitation_id,
112
+ stage:,
113
+ operation: "invitation_delivery_failed"
114
+ )
115
+ {
116
+ job: self.class.name,
117
+ operation: operation,
118
+ invitation_id: invitation_id,
119
+ stage: stage
120
+ }
121
+ end
122
+ end
123
+ end
@@ -40,7 +40,7 @@ module LesliShield
40
40
  # defaults for new accounts/users
41
41
  email_template = "devise/reset_password_instructions"
42
42
  email_subject = I18n.t(
43
- "lesli_shield.devise/passwords.mailer_subject"
43
+ "lesli_shield.devise_passwords.mailer_subject"
44
44
  )
45
45
 
46
46
  # email parameters
@@ -65,7 +65,7 @@ module LesliShield
65
65
  # defaults for new accounts/users
66
66
  email_template = "devise/confirmation_instructions"
67
67
  email_subject = I18n.t(
68
- "lesli_shield.devise/confirmations.mailer_confirmation_subject"
68
+ "lesli_shield.devise_confirmations.mailer_confirmation_subject"
69
69
  )
70
70
 
71
71
  # # custom email and subject if user is changin his email address
@@ -100,7 +100,7 @@ module LesliShield
100
100
  options: opts,
101
101
  to: email_recipient,
102
102
  subject: I18n.t(
103
- "lesli_shield.devise/confirmations.mailer_welcome_subject"
103
+ "lesli_shield.devise_confirmations.mailer_welcome_subject"
104
104
  ),
105
105
  template_name: "lesli/welcome"
106
106
  )