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
|
@@ -1,184 +1,157 @@
|
|
|
1
|
-
|
|
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 :
|
|
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
|
-
@
|
|
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
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
105
|
-
@role
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
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
|
-
|
|
94
|
+
def role_service
|
|
95
|
+
@role_service ||= RoleService.new(current_user, query)
|
|
96
|
+
end
|
|
127
97
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
@
|
|
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
|
-
|
|
39
|
-
|
|
93
|
+
resource_class.transaction do
|
|
94
|
+
confirmed_user = resource_class.confirm_by_token(
|
|
95
|
+
params[:confirmation_token]
|
|
96
|
+
)
|
|
40
97
|
|
|
41
|
-
|
|
42
|
-
|
|
98
|
+
if confirmed_user.errors.empty?
|
|
99
|
+
LesliShield::UserRegistrationService
|
|
100
|
+
.new(confirmed_user)
|
|
101
|
+
.provision_account!
|
|
43
102
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
103
|
+
confirmed_user.update!(confirmation_token: nil)
|
|
104
|
+
record_successful_confirmation!(confirmed_user)
|
|
105
|
+
end
|
|
47
106
|
end
|
|
48
107
|
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
53
|
-
if user
|
|
54
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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
|
-
|
|
67
|
-
LesliShield::DeviseMailer.
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
174
|
+
def confirmation_request_params
|
|
175
|
+
params
|
|
176
|
+
.fetch(resource_name, ActionController::Parameters.new)
|
|
177
|
+
.permit(:email)
|
|
76
178
|
end
|
|
77
179
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|