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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Lesli
4
4
 
5
- Copyright (c) 2025, Lesli Technologies, S. A.
5
+ Copyright (c) 2026, Lesli Technologies, S. A.
6
6
 
7
7
  This program is free software: you can redistribute it and/or modify
8
8
  it under the terms of the GNU General Public License as published by
@@ -30,109 +30,149 @@ Building a better future, one line of code at a time.
30
30
  // ·
31
31
  =end
32
32
 
33
- class Users::SessionsController < Devise::SessionsController
33
+ require "openssl"
34
34
 
35
- # Creates a new session for the user and allows them access to the platform.
36
- #
37
- # Devise provides extension points such as Warden hooks and a custom FailureApp
38
- # to modify the authentication flow. However, in this case we need full and
39
- # explicit control over each step of the login process, including validation,
40
- # logging, session creation, and redirection.
41
- #
42
- # For that reason, the default Devise session logic is intentionally overridden
43
- # and reimplemented here. While this approach is less conventional and may
44
- # introduce compatibility risks with future Devise releases, it provides a
45
- # predictable and fully controlled authentication pipeline, which is important
46
- # for the needs of this framework.
47
- #
48
- # This trade-off is accepted as part of the framework’s lifecycle: any
49
- # incompatibilities introduced by Devise updates will be addressed and
50
- # maintained as needed.
35
+ class Users::SessionsController < Devise::SessionsController
36
+ LOGIN_RATE_LIMIT_WINDOW = 15.minutes
37
+
38
+ rate_limit(
39
+ to: 20,
40
+ within: LOGIN_RATE_LIMIT_WINDOW,
41
+ by: -> { request.remote_ip },
42
+ with: :respond_to_login_rate_limit,
43
+ only: :create,
44
+ name: "login-ip"
45
+ )
46
+
47
+ rate_limit(
48
+ to: 10,
49
+ within: LOGIN_RATE_LIMIT_WINDOW,
50
+ by: :login_email_rate_limit_key,
51
+ with: :respond_to_login_rate_limit,
52
+ only: :create,
53
+ name: "login-email"
54
+ )
55
+
56
+ # Devise owns the authentication pipeline and yields only after credentials
57
+ # and active_for_authentication? have been accepted and the web session has
58
+ # been established. LesliShield adds its application session and audit event
59
+ # without reimplementing Devise's create action.
51
60
  def create
61
+ super do |user|
62
+ persist_application_session!(user)
63
+ record_successful_login_analytics
64
+ # Devise sets the successful-login notice before yielding.
65
+ flash.delete(:notice)
66
+ end
67
+ end
52
68
 
53
- # Use guarden to check if the users credetials are valid
54
- self.resource = warden.authenticate(auth_options)
69
+ protected
55
70
 
56
- # respond with a no valid credentials generic error if warden
57
- # cannot validate the user
58
- unless resource
59
- danger(I18n.t("lesli_shield.devise/sessions.message_not_valid_credentials"))
60
- redirect_to user_session_path(r: sign_in_params[:redirect]) and return
61
- end
71
+ # Devise calls this method only after a successful authentication. url_from
72
+ # accepts local paths and URLs for the current host while rejecting external
73
+ # and protocol-relative URLs.
74
+ def after_sign_in_path_for(_user)
75
+ url_from(requested_redirect) || configured_after_sign_in_path
76
+ end
62
77
 
63
- user = resource
78
+ private
79
+
80
+ # Devise has already established the web session when its create action
81
+ # yields. If either mandatory write fails, remove that session so the request
82
+ # cannot leave a partially completed login behind.
83
+ def persist_application_session!(user)
84
+ application_session_id = create_application_session_and_audit!(user)
85
+ session[:user_session_id] = application_session_id
86
+ rescue StandardError
87
+ rollback_web_session
88
+ raise
89
+ end
64
90
 
65
- # check if user has a valid account
66
- unless user.account
67
- danger(I18n.t("lesli_shield.devise/sessions.message_not_confirmed_account"))
68
- redirect_to user_session_path(r: sign_in_params[:redirect]) and return
91
+ # The LesliShield session and its single successful-login audit event are
92
+ # committed atomically.
93
+ def create_application_session_and_audit!(user)
94
+ Lesli::User.transaction do
95
+ application_session = LesliShield::UserSessionService
96
+ .new(user)
97
+ .create(request.remote_ip, detected_user_agent)
98
+ .result
99
+
100
+ user.log(
101
+ engine: LesliShield,
102
+ source: self.class.name,
103
+ action: action_name,
104
+ operation: "session_creation_succeeded",
105
+ description: "Session creation successful",
106
+ session_id: application_session.id
107
+ )
108
+
109
+ application_session.id
69
110
  end
111
+ end
70
112
 
71
- log = nil
113
+ def rollback_web_session
114
+ session.delete(:user_session_id)
115
+ sign_out(resource_name)
116
+ end
72
117
 
73
- # Save a log for the current login attempt
74
- log = user.log(
75
- engine: LesliShield,
76
- source: self.class.name,
77
- action: action_name,
78
- operation: 'session_new',
79
- description: 'Session creation attempt'
80
- ) if defined?(LesliAudit)
118
+ # Request and device records are analytics, not additional authentication
119
+ # audit events.
120
+ def record_successful_login_analytics
121
+ log_requests if respond_to?(:log_requests)
122
+ log_devices if respond_to?(:log_devices)
123
+ end
81
124
 
82
- # check if user meet requirements to create a new session
83
- LesliShield::UserValidatorService.new(user).valid? do |valid, failures|
125
+ def detected_user_agent
126
+ get_user_agent if respond_to?(:get_user_agent)
127
+ end
84
128
 
85
- # if user do not meet requirements to login
86
- unless valid
129
+ def configured_after_sign_in_path
130
+ Lesli.config.shield.dig(:path_after_login) || root_path
131
+ end
87
132
 
88
- failures_string = failures.join(", ")
89
- danger(failures_string)
90
- log.update(description: failures_string) if log
91
- redirect_to user_session_path(r: sign_in_params[:redirect]) and return
92
- end
93
- end
133
+ def requested_redirect
134
+ login_request_params[:redirect]
135
+ end
94
136
 
95
- # create a new session for the user
96
- current_session = LesliShield::UserSessionService.new(user)
97
- .create(request.remote_ip, (get_user_agent(false) if log))
98
- .result
137
+ def login_request_params
138
+ params.fetch(:user, {}).permit(
139
+ :email,
140
+ :password,
141
+ :remember_me,
142
+ :redirect
143
+ )
144
+ end
99
145
 
100
- # make session id globally available
101
- session[:user_session_id] = current_session[:id]
146
+ def login_email_rate_limit_key
147
+ normalized_email = login_request_params[:email].to_s.strip.downcase
148
+ identity = normalized_email.presence || "blank:#{request.remote_ip}"
102
149
 
103
- # do a user login
104
- sign_in(resource_name, user)
150
+ OpenSSL::HMAC.hexdigest(
151
+ "SHA256",
152
+ Rails.application.secret_key_base,
153
+ identity
154
+ )
155
+ end
105
156
 
106
- # update logs with a successful login
107
- log.update(
108
- description: "Session creation successful",
109
- session_id: current_session[:id]
110
- ) if log
157
+ def respond_to_login_rate_limit
158
+ message = I18n.t("lesli_shield.devise_sessions.message_too_many_attempts")
159
+ response.set_header("Retry-After", LOGIN_RATE_LIMIT_WINDOW.to_i.to_s)
111
160
 
112
- # respond successful and send the path user should go
113
- # respond_with_successful({ default_path: user.has_role_with_default_path?() })
114
- # respond_with_successful({ default_path: Lesli.config.path_after_login || "/" })
115
- redirect_to safe_redirect_path(sign_in_params[:redirect])
161
+ respond_to do |format|
162
+ format.html do
163
+ self.resource = resource_class.new(
164
+ email: login_request_params[:email]
165
+ )
116
166
 
117
- # Save the user_agent for every new session
118
- log_devices if log
119
- end
167
+ flash.now[:alert] = message
168
+ render :new, status: :too_many_requests
169
+ end
120
170
 
121
- private
171
+ format.json do
172
+ render json: { error: message }, status: :too_many_requests
173
+ end
122
174
 
123
- def safe_redirect_path redirect_path
124
- if redirect_path.present?
125
- uri = URI.parse(redirect_path)
126
- # Ensures it's a local path
127
- return redirect_path if uri.relative?
175
+ format.any { head :too_many_requests }
128
176
  end
129
- Lesli.config.shield.dig(:path_after_login) || root_path
130
- rescue URI::InvalidURIError
131
- # If the URL is invalid, fallback to root
132
- root_path
133
- end
134
-
135
- def sign_in_params
136
- params.fetch(:user, {}).permit(:email, :password, :redirect)
137
177
  end
138
178
  end
@@ -31,7 +31,7 @@ module LesliShield
31
31
  :operation => :authorize_request,
32
32
  :description => "Privilege not found for: #{request.path}"
33
33
  )
34
- return respond_with_unauthorized({ controller: params[:controller], action: params[:action] })
34
+ return respond_with_forbidden({ controller: params[:controller], action: params[:action] })
35
35
  end
36
36
 
37
37
  unless granted
@@ -39,7 +39,7 @@ module LesliShield
39
39
  :operation => :authorize_request,
40
40
  :description => "Privilege not granted for: #{request.path}"
41
41
  )
42
- return respond_with_unauthorized({ controller: params[:controller], action: params[:action] })
42
+ return respond_with_forbidden({ controller: params[:controller], action: params[:action] })
43
43
  end
44
44
  end
45
45
  end
@@ -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
@@ -32,34 +32,41 @@ Building a better future, one line of code at a time.
32
32
  module LesliShield
33
33
  class DeviseMailer < Lesli::ApplicationLesliMailer
34
34
 
35
+ default from: -> { configured_sender }
36
+
35
37
  # Sends an email with instructions to allow the user reset the password
36
38
  def reset_password_instructions(user, token, opts = {})
37
39
 
38
40
  # defaults for new accounts/users
39
41
  email_template = "devise/reset_password_instructions"
40
- email_subject = I18n.t("core.users/confirmations.mailer_email_verification")
42
+ email_subject = I18n.t(
43
+ "lesli_shield.devise_passwords.mailer_subject"
44
+ )
41
45
 
42
46
  # email parameters
43
47
  params = {
44
- url: build_url("/password/edit", { reset_password_token: token}),
48
+ url: password_reset_url(token),
45
49
  full_name: user.full_name
46
50
  }
47
51
 
48
52
  # send email
49
- email(
50
- params,
53
+ send_email(
54
+ params: params,
55
+ options: opts,
51
56
  to: user.email,
52
57
  subject: email_subject,
53
58
  template_name: email_template
54
59
  )
55
- end
60
+ end
56
61
 
57
62
  # Sends an email to allow the user confirm the email address
58
63
  def confirmation_instructions(user, token, opts = {})
59
64
 
60
65
  # defaults for new accounts/users
61
66
  email_template = "devise/confirmation_instructions"
62
- email_subject = I18n.t("core.users/confirmations.mailer_email_verification")
67
+ email_subject = I18n.t(
68
+ "lesli_shield.devise_confirmations.mailer_confirmation_subject"
69
+ )
63
70
 
64
71
  # # custom email and subject if user is changin his email address
65
72
  # if !record.unconfirmed_email.blank?
@@ -69,30 +76,79 @@ module LesliShield
69
76
 
70
77
  # Depending on wheter there is a new user or they are changing their email,
71
78
  # one or another field will be used
72
- email_recipient = user.unconfirmed_email || user.email
79
+ email_recipient = user.unconfirmed_email.presence || user.email
73
80
 
74
81
  # email parameters
75
82
  params = {
76
- url: build_url("/confirmation", { confirmation_token: token})
83
+ url: account_confirmation_url(token)
77
84
  }
78
85
 
79
86
  # send email
80
- email(
81
- params,
87
+ send_email(
88
+ params: params,
89
+ options: opts,
82
90
  to: email_recipient,
83
91
  subject: email_subject,
84
92
  template_name: email_template
85
93
  )
86
94
  end
87
95
 
88
- def welcome(user)
89
- email_recipient = user.unconfirmed_email || user.email
90
- email(
91
- { :user => user },
92
- to: email_recipient,
93
- subject: "test",
94
- template_name: "devise/welcome"
96
+ def welcome(user, opts = {})
97
+ email_recipient = user.unconfirmed_email.presence || user.email
98
+ send_email(
99
+ params: { user: user },
100
+ options: opts,
101
+ to: email_recipient,
102
+ subject: I18n.t(
103
+ "lesli_shield.devise_confirmations.mailer_welcome_subject"
104
+ ),
105
+ template_name: "lesli/welcome"
106
+ )
107
+ end
108
+
109
+ private
110
+
111
+ def send_email(params:, options:, to:, subject:, template_name:)
112
+ headers = { to: to, subject: subject }.merge(options)
113
+ headers.delete(:template_name)
114
+
115
+ @params = @params.merge(params)
116
+ build_app_from_params(params)
117
+
118
+ mail(
119
+ **headers,
120
+ template_name: template_name
95
121
  )
96
122
  end
123
+
124
+ def configured_sender
125
+ sender = Devise.mailer_sender
126
+ sender = sender.call(:user) if sender.respond_to?(:call)
127
+
128
+ sender.presence ||
129
+ Rails.application.config.action_mailer.default_options&.dig(:from).presence ||
130
+ "no-reply@lesli.dev"
131
+ end
132
+
133
+ # Use the host application's named route so password recovery continues
134
+ # to work when authentication is mounted under a custom path.
135
+ def password_reset_url(token)
136
+ Rails.application.routes.url_helpers.edit_user_password_url(
137
+ **action_mailer_url_options,
138
+ reset_password_token: token
139
+ )
140
+ end
141
+
142
+ def account_confirmation_url(token)
143
+ Rails.application.routes.url_helpers.user_confirmation_url(
144
+ **action_mailer_url_options,
145
+ confirmation_token: token
146
+ )
147
+ end
148
+
149
+ def action_mailer_url_options
150
+ options = Rails.application.config.action_mailer.default_url_options
151
+ (options || {}).to_h.symbolize_keys
152
+ end
97
153
  end
98
154
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ class InvitationMailer < Lesli::ApplicationLesliMailer
5
+ default from: -> { configured_sender }
6
+
7
+ def account_invitation(invitation, acceptance_url)
8
+ account = invitation.account.account
9
+ invited_by = invitation.invited_by
10
+ inviter_name = invited_by.full_name.to_s.strip.presence ||
11
+ invited_by.email
12
+
13
+ email_params = {
14
+ user: invited_by,
15
+ title: I18n.t("lesli_shield.invites.mailer_title"),
16
+ description: I18n.t(
17
+ "lesli_shield.invites.mailer_description",
18
+ account: account.name
19
+ ),
20
+ invited_by: I18n.t(
21
+ "lesli_shield.invites.mailer_invited_by",
22
+ inviter: inviter_name
23
+ ),
24
+ expiration: I18n.t(
25
+ "lesli_shield.invites.mailer_expiration",
26
+ expiration: I18n.l(invitation.expires_at, format: :long)
27
+ ),
28
+ action: I18n.t("lesli_shield.invites.mailer_action"),
29
+ ignore_notice: I18n.t(
30
+ "lesli_shield.invites.mailer_ignore_notice"
31
+ ),
32
+ message: invitation.notes.to_s.strip.presence,
33
+ acceptance_url: acceptance_url
34
+ }
35
+
36
+ @params = @params.merge(email_params)
37
+ build_app_from_params(email_params)
38
+
39
+ mail(
40
+ to: invitation.email,
41
+ subject: I18n.t("lesli_shield.invites.mailer_subject"),
42
+ template_path: "lesli_shield/invitation_mailer",
43
+ template_name: "account_invitation"
44
+ )
45
+ end
46
+
47
+ private
48
+
49
+ def configured_sender
50
+ sender = Devise.mailer_sender
51
+ sender = sender.call(:user) if sender.respond_to?(:call)
52
+
53
+ sender.presence ||
54
+ Rails.application.config.action_mailer.default_options&.dig(:from).presence ||
55
+ "no-reply@lesli.dev"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ module RoleManagement
5
+ extend ActiveSupport::Concern
6
+
7
+ SYSTEM_ROLE_NAMES = %w[owner admin guest].freeze
8
+ PROTECTED_ROLE_NAMES = %w[owner].freeze
9
+
10
+ def system_role?
11
+ system_role_name.present?
12
+ end
13
+
14
+ def protected_role?
15
+ PROTECTED_ROLE_NAMES.include?(system_role_name)
16
+ end
17
+
18
+ # A protected role may be inspected, but it cannot be changed through
19
+ # role administration. Equal permission levels are manageable; only a
20
+ # strictly higher role is outside the actor's management boundary.
21
+ def manageable_by?(actor)
22
+ return false if actor.blank? || protected_role?
23
+ return false unless actor.respond_to?(:account_id)
24
+ return false unless actor.respond_to?(:max_level_permission)
25
+ return false if account_id.blank? || actor.account_id != account_id
26
+ return false if permission_level.blank?
27
+
28
+ permission_level <= actor.max_level_permission
29
+ end
30
+
31
+ def assigned?
32
+ return false unless persisted?
33
+
34
+ LesliShield::User::Role.where(role_id: id).exists?
35
+ end
36
+
37
+ def deletable_by?(actor)
38
+ manageable_by?(actor) && !system_role? && !assigned?
39
+ end
40
+
41
+ # Permission changes and privilege compilation intentionally happen at
42
+ # different times. These methods expose that state without using
43
+ # updated_at, which also changes when ordinary role fields are edited.
44
+ def permissions_pending_deployment?
45
+ modified_at.present? && (
46
+ deployed_at.blank? || modified_at > deployed_at
47
+ )
48
+ end
49
+
50
+ def permissions_deployment_status
51
+ return :pending if permissions_pending_deployment?
52
+ return :never_deployed if deployed_at.blank?
53
+
54
+ :deployed
55
+ end
56
+
57
+ private
58
+
59
+ # Account initialization creates stable codes such as owner1, admin2,
60
+ # and guest3. Comparing the complete code avoids treating a custom role
61
+ # like "admin247" as a system role by accident.
62
+ def system_role_name
63
+ normalized_code = code.to_s.strip.downcase
64
+
65
+ SYSTEM_ROLE_NAMES.find do |system_name|
66
+ normalized_code == "#{system_name}#{id}"
67
+ end
68
+ end
69
+ end
70
+ end