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.
- checksums.yaml +4 -4
- data/Rakefile +3 -0
- data/app/assets/stylesheets/lesli_shield/application.css +1222 -1
- data/app/assets/stylesheets/lesli_shield/confirmations.css +917 -18
- data/app/assets/stylesheets/lesli_shield/confirmations.tailwind.css +918 -0
- data/app/assets/stylesheets/lesli_shield/invitations.css +1008 -0
- data/app/assets/stylesheets/lesli_shield/passwords.css +872 -18
- data/app/assets/stylesheets/lesli_shield/passwords.tailwind.css +873 -0
- data/app/assets/stylesheets/lesli_shield/registrations.css +904 -832
- data/app/assets/stylesheets/lesli_shield/registrations.tailwind.css +908 -0
- data/app/assets/stylesheets/lesli_shield/sessions.css +984 -32
- data/app/assets/stylesheets/lesli_shield/sessions.tailwind.css +983 -1
- data/app/controllers/lesli_shield/invites_controller.rb +100 -49
- data/app/controllers/lesli_shield/profile/passwords_controller.rb +119 -0
- data/app/controllers/lesli_shield/profiles_controller.rb +69 -0
- data/app/controllers/lesli_shield/role/actions_controller.rb +134 -38
- data/app/controllers/lesli_shield/roles_controller.rb +114 -141
- data/app/controllers/lesli_shield/sessions_controller.rb +1 -2
- data/app/controllers/lesli_shield/users_controller.rb +11 -4
- data/app/controllers/users/confirmations_controller.rb +162 -49
- data/app/controllers/users/invitations_controller.rb +101 -0
- data/app/controllers/users/passwords_controller.rb +211 -28
- data/app/controllers/users/registrations_controller.rb +108 -83
- data/app/controllers/users/sessions_controller.rb +124 -84
- data/app/interfaces/lesli_shield/authorization_interface.rb +2 -2
- data/app/jobs/lesli_shield/invitation_delivery_job.rb +123 -0
- data/app/mailers/lesli_shield/devise_mailer.rb +73 -17
- data/app/mailers/lesli_shield/invitation_mailer.rb +58 -0
- data/app/models/concerns/lesli_shield/role_management.rb +70 -0
- data/app/models/concerns/lesli_shield/user_authentication.rb +54 -0
- data/app/models/concerns/lesli_shield/user_security.rb +22 -19
- data/app/models/lesli_shield/account.rb +4 -1
- data/app/models/lesli_shield/invite.rb +183 -12
- data/app/models/lesli_shield/user/role.rb +1 -1
- data/app/models/lesli_shield/user.rb +5 -2
- data/app/services/lesli_shield/invitation_audit.rb +34 -0
- data/app/services/lesli_shield/invitation_email_policy.rb +64 -0
- data/app/services/lesli_shield/invitation_service.rb +405 -0
- data/app/services/lesli_shield/invite_service.rb +34 -5
- data/app/services/lesli_shield/profile/password_service.rb +47 -0
- data/app/services/lesli_shield/profile_service.rb +25 -0
- data/app/services/lesli_shield/role_action_service.rb +37 -92
- data/app/services/lesli_shield/role_audit.rb +42 -0
- data/app/services/lesli_shield/role_permission_service.rb +347 -0
- data/app/services/lesli_shield/role_privilege_deployment_service.rb +79 -0
- data/app/services/lesli_shield/role_privilege_service.rb +69 -105
- data/app/services/lesli_shield/role_service.rb +431 -0
- data/app/services/lesli_shield/user_registration_service.rb +41 -74
- data/app/services/lesli_shield/user_session_service.rb +1 -1
- data/app/services/lesli_shield/user_validator_service.rb +37 -39
- data/app/views/devise/confirmations/new.html.erb +58 -12
- data/app/views/devise/confirmations/show.html.erb +27 -60
- data/app/views/devise/passwords/edit.html.erb +59 -25
- data/app/views/devise/passwords/new.html.erb +61 -26
- data/app/views/devise/registrations/new.html.erb +83 -56
- data/app/views/devise/sessions/new.html.erb +58 -41
- data/app/views/devise/shared/_application-devise-simple.erb +18 -19
- data/app/views/devise/shared/_demo.html.erb +8 -6
- data/app/views/devise/shared/_error_messages.html.erb +45 -10
- data/app/views/devise/shared/_links.html.erb +10 -20
- data/app/views/lesli_shield/invites/_form.html.erb +64 -7
- data/app/views/lesli_shield/invites/_invite.html.erb +55 -2
- data/app/views/lesli_shield/invites/_invite_actions.html.erb +34 -0
- data/app/views/lesli_shield/invites/_status_badge.html.erb +13 -0
- data/app/views/lesli_shield/invites/index.html.erb +119 -62
- data/app/views/lesli_shield/invites/new.html.erb +11 -36
- data/app/views/lesli_shield/invites/show.html.erb +13 -52
- data/app/views/lesli_shield/profiles/_information-form.html.erb +11 -0
- data/app/views/lesli_shield/profiles/_password-form.html.erb +68 -0
- data/app/views/lesli_shield/profiles/show.html.erb +26 -0
- data/app/views/lesli_shield/role/actions/_custom_actions.html.erb +24 -0
- data/app/views/lesli_shield/role/actions/_deployment_status.html.erb +14 -0
- data/app/views/lesli_shield/role/actions/_matrix.html.erb +69 -0
- data/app/views/lesli_shield/role/actions/_permissions.html.erb +95 -0
- data/app/views/lesli_shield/role/actions/_toggle.html.erb +34 -0
- data/app/views/lesli_shield/role/actions/index.html.erb +14 -10
- data/app/views/lesli_shield/roles/_deployment_badge.html.erb +15 -0
- data/app/views/lesli_shield/roles/_empty_state.html.erb +20 -0
- data/app/views/lesli_shield/roles/_field_errors.html.erb +5 -0
- data/app/views/lesli_shield/roles/_filters.html.erb +66 -0
- data/app/views/lesli_shield/roles/_form.html.erb +149 -18
- data/app/views/lesli_shield/roles/_management_menu.html.erb +37 -0
- data/app/views/lesli_shield/roles/_pagination.html.erb +43 -0
- data/app/views/lesli_shield/roles/_role_list.html.erb +116 -0
- data/app/views/lesli_shield/roles/_status_badge.html.erb +11 -0
- data/app/views/lesli_shield/roles/_type_badge.html.erb +9 -0
- data/app/views/lesli_shield/roles/index.html.erb +15 -38
- data/app/views/lesli_shield/roles/new.html.erb +11 -0
- data/app/views/lesli_shield/roles/show.html.erb +79 -7
- data/app/views/lesli_shield/users/_information-form.html.erb +15 -1
- data/app/views/users/invitations/show.html.erb +142 -0
- data/app/views/users/invitations/unavailable.html.erb +19 -0
- data/config/initializers/devise.rb +52 -328
- data/config/locales/devise.en.yml +8 -0
- data/config/locales/devise.es.yml +8 -0
- data/config/locales/translations.en.yml +314 -16
- data/config/locales/translations.es.yml +314 -16
- data/config/routes.rb +14 -3
- data/db/migrate/v1/0801120110_create_lesli_shield_invites.rb +40 -6
- data/lib/lesli_shield/engine.rb +42 -4
- data/lib/lesli_shield/router.rb +62 -18
- data/lib/lesli_shield/version.rb +2 -2
- data/lib/tasks/lesli_shield_tasks.rake +1 -1
- data/readme.md +17 -3
- metadata +63 -8
- data/app/assets/config/lesli_shield_manifest.js +0 -48
- data/app/assets/stylesheets/lesli_shield/application.tailwind.css +0 -2
- data/app/mailers/lesli_shield/invitation.html.erb +0 -23
- data/app/views/lesli_shield/invites/edit.html.erb +0 -12
- 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)
|
|
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
|
-
|
|
33
|
+
require "openssl"
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
54
|
-
self.resource = warden.authenticate(auth_options)
|
|
69
|
+
protected
|
|
55
70
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
113
|
+
def rollback_web_session
|
|
114
|
+
session.delete(:user_session_id)
|
|
115
|
+
sign_out(resource_name)
|
|
116
|
+
end
|
|
72
117
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
83
|
-
|
|
125
|
+
def detected_user_agent
|
|
126
|
+
get_user_agent if respond_to?(:get_user_agent)
|
|
127
|
+
end
|
|
84
128
|
|
|
85
|
-
|
|
86
|
-
|
|
129
|
+
def configured_after_sign_in_path
|
|
130
|
+
Lesli.config.shield.dig(:path_after_login) || root_path
|
|
131
|
+
end
|
|
87
132
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
101
|
-
|
|
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
|
-
|
|
104
|
-
|
|
150
|
+
OpenSSL::HMAC.hexdigest(
|
|
151
|
+
"SHA256",
|
|
152
|
+
Rails.application.secret_key_base,
|
|
153
|
+
identity
|
|
154
|
+
)
|
|
155
|
+
end
|
|
105
156
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
167
|
+
flash.now[:alert] = message
|
|
168
|
+
render :new, status: :too_many_requests
|
|
169
|
+
end
|
|
120
170
|
|
|
121
|
-
|
|
171
|
+
format.json do
|
|
172
|
+
render json: { error: message }, status: :too_many_requests
|
|
173
|
+
end
|
|
122
174
|
|
|
123
|
-
|
|
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
|
|
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
|
|
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(
|
|
42
|
+
email_subject = I18n.t(
|
|
43
|
+
"lesli_shield.devise_passwords.mailer_subject"
|
|
44
|
+
)
|
|
41
45
|
|
|
42
46
|
# email parameters
|
|
43
47
|
params = {
|
|
44
|
-
url:
|
|
48
|
+
url: password_reset_url(token),
|
|
45
49
|
full_name: user.full_name
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
# send email
|
|
49
|
-
|
|
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(
|
|
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:
|
|
83
|
+
url: account_confirmation_url(token)
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
# send email
|
|
80
|
-
|
|
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
|
-
|
|
91
|
-
{ :
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|