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
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LesliShield
|
|
4
|
+
# Owns role administration rules. Every lookup starts from the actor's
|
|
5
|
+
# account and is restricted by the actor's active permission level.
|
|
6
|
+
class RoleService
|
|
7
|
+
DEFAULT_PER_PAGE = 12
|
|
8
|
+
MAX_PER_PAGE = 100
|
|
9
|
+
|
|
10
|
+
STATUS_FILTERS = %w[active inactive].freeze
|
|
11
|
+
TYPE_FILTERS = %w[system custom].freeze
|
|
12
|
+
WRITABLE_ATTRIBUTES = %i[
|
|
13
|
+
name
|
|
14
|
+
description
|
|
15
|
+
active
|
|
16
|
+
path_default
|
|
17
|
+
permission_level
|
|
18
|
+
].freeze
|
|
19
|
+
|
|
20
|
+
SORT_COLUMNS = {
|
|
21
|
+
"id" => "lesli_roles.id",
|
|
22
|
+
"name" => "LOWER(lesli_roles.name)",
|
|
23
|
+
"description" => "LOWER(lesli_roles.description)",
|
|
24
|
+
"active" => "lesli_roles.active",
|
|
25
|
+
"permission_level" => "lesli_roles.permission_level",
|
|
26
|
+
"modified_at" => "COALESCE(lesli_roles.modified_at, lesli_roles.updated_at)",
|
|
27
|
+
"users" => "users",
|
|
28
|
+
"actions" => "actions"
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
SYSTEM_ROLE_SQL = RoleManagement::SYSTEM_ROLE_NAMES.map do |name|
|
|
32
|
+
"LOWER(COALESCE(lesli_roles.code, '')) = " \
|
|
33
|
+
"'#{name}' || CAST(lesli_roles.id AS VARCHAR)"
|
|
34
|
+
end.join(" OR ").freeze
|
|
35
|
+
|
|
36
|
+
ACTIVE_USERS_SQL = <<~SQL.squish.freeze
|
|
37
|
+
(
|
|
38
|
+
SELECT COUNT(DISTINCT role_users.user_id)
|
|
39
|
+
FROM lesli_shield_user_roles role_users
|
|
40
|
+
INNER JOIN lesli_users users
|
|
41
|
+
ON users.id = role_users.user_id
|
|
42
|
+
WHERE role_users.role_id = lesli_roles.id
|
|
43
|
+
AND role_users.deleted_at IS NULL
|
|
44
|
+
AND users.deleted_at IS NULL
|
|
45
|
+
AND users.active = TRUE
|
|
46
|
+
)
|
|
47
|
+
SQL
|
|
48
|
+
|
|
49
|
+
ACTIVE_ACTIONS_SQL = <<~SQL.squish.freeze
|
|
50
|
+
(
|
|
51
|
+
SELECT COUNT(DISTINCT role_actions.action_id)
|
|
52
|
+
FROM lesli_shield_role_actions role_actions
|
|
53
|
+
WHERE role_actions.role_id = lesli_roles.id
|
|
54
|
+
AND role_actions.deleted_at IS NULL
|
|
55
|
+
)
|
|
56
|
+
SQL
|
|
57
|
+
|
|
58
|
+
class Error < StandardError
|
|
59
|
+
attr_reader :record
|
|
60
|
+
|
|
61
|
+
def initialize(message, record: nil)
|
|
62
|
+
@record = record
|
|
63
|
+
super(message)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
class DuplicateNameError < Error; end
|
|
68
|
+
class PermissionLevelError < Error; end
|
|
69
|
+
class ProtectedRoleError < Error; end
|
|
70
|
+
class SystemRoleError < Error; end
|
|
71
|
+
class AssignedUsersError < Error; end
|
|
72
|
+
|
|
73
|
+
class ValidationError < Error
|
|
74
|
+
def initialize(record)
|
|
75
|
+
super(record.errors.full_messages.to_sentence, record: record)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def initialize(actor, query = {})
|
|
80
|
+
@actor = actor
|
|
81
|
+
@query = query || {}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def list(filters = {})
|
|
85
|
+
roles = visible_roles
|
|
86
|
+
roles = apply_search(roles, filters)
|
|
87
|
+
roles = apply_status(roles, filters)
|
|
88
|
+
roles = apply_type(roles, filters)
|
|
89
|
+
|
|
90
|
+
roles
|
|
91
|
+
.select(
|
|
92
|
+
"lesli_roles.*",
|
|
93
|
+
"#{ACTIVE_USERS_SQL} AS users",
|
|
94
|
+
"#{ACTIVE_ACTIONS_SQL} AS actions"
|
|
95
|
+
)
|
|
96
|
+
.order(Arel.sql("#{sort_column} #{sort_direction}"))
|
|
97
|
+
.order(id: :asc)
|
|
98
|
+
.page(page)
|
|
99
|
+
.per(per_page)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def find!(id, manageable: false)
|
|
103
|
+
role = visible_roles.find(id)
|
|
104
|
+
|
|
105
|
+
if manageable && !role.manageable_by?(actor)
|
|
106
|
+
raise ActiveRecord::RecordNotFound
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
role
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def build(attributes = {})
|
|
113
|
+
actor.account.roles.new(
|
|
114
|
+
initial_attributes.merge(normalize_attributes(attributes))
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def summary(role_or_id)
|
|
119
|
+
role = find!(record_id(role_or_id))
|
|
120
|
+
|
|
121
|
+
{
|
|
122
|
+
members: User::Role
|
|
123
|
+
.where(role_id: role.id)
|
|
124
|
+
.distinct
|
|
125
|
+
.count(:user_id),
|
|
126
|
+
permissions: role.actions.distinct.count(:action_id)
|
|
127
|
+
}
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def create!(attributes)
|
|
131
|
+
Lesli::Role.transaction do
|
|
132
|
+
role = persist_new_role!(attributes)
|
|
133
|
+
audit!(:role_created, role)
|
|
134
|
+
role
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def update!(role_or_id, attributes)
|
|
139
|
+
role = manageable_role!(role_or_id)
|
|
140
|
+
|
|
141
|
+
role.with_lock do
|
|
142
|
+
manageable_role!(role.id)
|
|
143
|
+
role.assign_attributes(normalize_attributes(attributes))
|
|
144
|
+
|
|
145
|
+
validate_permission_level!(role)
|
|
146
|
+
validate_unique_name!(role)
|
|
147
|
+
changes = role.changes.slice(*WRITABLE_ATTRIBUTES.map(&:to_s))
|
|
148
|
+
next if changes.empty?
|
|
149
|
+
|
|
150
|
+
# Capture changes before saving: model callbacks may perform
|
|
151
|
+
# another save and replace Active Record's saved_changes.
|
|
152
|
+
operation = update_operation(role, changes)
|
|
153
|
+
save!(role)
|
|
154
|
+
audit!(operation, role, changes: changes)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
role
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def deactivate!(role_or_id)
|
|
161
|
+
update!(role_or_id, active: false)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def destroy!(role_or_id)
|
|
165
|
+
role = manageable_role!(role_or_id)
|
|
166
|
+
|
|
167
|
+
role.with_lock do
|
|
168
|
+
manageable_role!(role.id)
|
|
169
|
+
|
|
170
|
+
if role.system_role?
|
|
171
|
+
raise SystemRoleError.new(
|
|
172
|
+
I18n.t("lesli_shield.roles.errors_system_role"),
|
|
173
|
+
record: role
|
|
174
|
+
)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
if role.assigned?
|
|
178
|
+
raise AssignedUsersError.new(
|
|
179
|
+
I18n.t("lesli_shield.roles.errors_assigned_users"),
|
|
180
|
+
record: role
|
|
181
|
+
)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
role.destroy!
|
|
185
|
+
audit!(:role_deleted, role)
|
|
186
|
+
end
|
|
187
|
+
role
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Copies configuration and selected active actions, but never user
|
|
191
|
+
# assignments, compiled privileges, or deployment state.
|
|
192
|
+
def duplicate!(role_or_id, attributes = {})
|
|
193
|
+
source = find!(record_id(role_or_id))
|
|
194
|
+
overrides = normalize_attributes(attributes)
|
|
195
|
+
overrides[:name] ||= available_copy_name(source.name)
|
|
196
|
+
|
|
197
|
+
Lesli::Role.transaction do
|
|
198
|
+
# Duplication is one operation, not a separate creation event.
|
|
199
|
+
duplicate = persist_new_role!(
|
|
200
|
+
{
|
|
201
|
+
name: overrides.delete(:name),
|
|
202
|
+
description: source.description,
|
|
203
|
+
path_default: source.path_default,
|
|
204
|
+
permission_level: source.permission_level,
|
|
205
|
+
active: false
|
|
206
|
+
}.merge(overrides)
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
RolePermissionService
|
|
210
|
+
.new(actor)
|
|
211
|
+
.duplicate_from!(source, duplicate)
|
|
212
|
+
audit!(
|
|
213
|
+
:role_duplicated,
|
|
214
|
+
duplicate,
|
|
215
|
+
source_role_id: source.id,
|
|
216
|
+
source_role_name: source.name
|
|
217
|
+
)
|
|
218
|
+
duplicate.reload
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
private
|
|
223
|
+
|
|
224
|
+
attr_reader :actor, :query
|
|
225
|
+
|
|
226
|
+
# Shared persistence without auditing. Public operations own their
|
|
227
|
+
# transaction and record exactly one event with the correct meaning.
|
|
228
|
+
def persist_new_role!(attributes)
|
|
229
|
+
role = build(attributes)
|
|
230
|
+
validate_permission_level!(role)
|
|
231
|
+
validate_unique_name!(role)
|
|
232
|
+
save!(role)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def update_operation(role, changes)
|
|
236
|
+
return :role_updated unless changes.key?("active")
|
|
237
|
+
|
|
238
|
+
role.active? ? :role_activated : :role_deactivated
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def audit!(operation, role, **details)
|
|
242
|
+
RoleAudit.record!(
|
|
243
|
+
operation,
|
|
244
|
+
role: role,
|
|
245
|
+
actor: actor,
|
|
246
|
+
source: self.class,
|
|
247
|
+
details: details
|
|
248
|
+
)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def visible_roles
|
|
252
|
+
actor.account.roles.where(
|
|
253
|
+
permission_level: ..actor.max_level_permission
|
|
254
|
+
)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def manageable_role!(role_or_id)
|
|
258
|
+
role = visible_roles.find(record_id(role_or_id))
|
|
259
|
+
|
|
260
|
+
if role.protected_role?
|
|
261
|
+
raise ProtectedRoleError.new(
|
|
262
|
+
I18n.t("lesli_shield.roles.errors_protected_role"),
|
|
263
|
+
record: role
|
|
264
|
+
)
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
unless role.manageable_by?(actor)
|
|
268
|
+
raise PermissionLevelError.new(
|
|
269
|
+
I18n.t(
|
|
270
|
+
"lesli_shield.roles.errors_permission_level",
|
|
271
|
+
maximum: actor.max_level_permission
|
|
272
|
+
),
|
|
273
|
+
record: role
|
|
274
|
+
)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
role
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def record_id(role_or_id)
|
|
281
|
+
role_or_id.respond_to?(:id) ? role_or_id.id : role_or_id
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def initial_attributes
|
|
285
|
+
{
|
|
286
|
+
active: true,
|
|
287
|
+
isolated: false,
|
|
288
|
+
path_limited: false,
|
|
289
|
+
path_default: nil,
|
|
290
|
+
# Creating a role requires an explicit authorization level.
|
|
291
|
+
# Assigning nil also overrides the database column default,
|
|
292
|
+
# preventing callers from accidentally creating a level-10 role.
|
|
293
|
+
permission_level: nil
|
|
294
|
+
}
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def normalize_attributes(attributes)
|
|
298
|
+
normalized = attributes.to_h.symbolize_keys.slice(*WRITABLE_ATTRIBUTES)
|
|
299
|
+
|
|
300
|
+
if normalized.key?(:name)
|
|
301
|
+
normalized[:name] = normalized[:name].to_s.squish
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
if normalized.key?(:description)
|
|
305
|
+
normalized[:description] = normalized[:description]
|
|
306
|
+
.to_s
|
|
307
|
+
.squish
|
|
308
|
+
.presence
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
normalized
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def validate_permission_level!(role)
|
|
315
|
+
return if role.permission_level.blank?
|
|
316
|
+
return if role.permission_level.between?(0, actor.max_level_permission)
|
|
317
|
+
|
|
318
|
+
message = I18n.t(
|
|
319
|
+
"lesli_shield.roles.errors_permission_level",
|
|
320
|
+
maximum: actor.max_level_permission
|
|
321
|
+
)
|
|
322
|
+
role.errors.add(:permission_level, message)
|
|
323
|
+
|
|
324
|
+
raise PermissionLevelError.new(
|
|
325
|
+
message,
|
|
326
|
+
record: role
|
|
327
|
+
)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def validate_unique_name!(role)
|
|
331
|
+
return if role.name.blank?
|
|
332
|
+
|
|
333
|
+
duplicates = actor.account.roles
|
|
334
|
+
.where("LOWER(name) = ?", role.name.downcase)
|
|
335
|
+
duplicates = duplicates.where.not(id: role.id) if role.persisted?
|
|
336
|
+
return unless duplicates.exists?
|
|
337
|
+
|
|
338
|
+
role.errors.add(
|
|
339
|
+
:name,
|
|
340
|
+
I18n.t("lesli_shield.roles.errors_duplicate_name")
|
|
341
|
+
)
|
|
342
|
+
raise DuplicateNameError.new(
|
|
343
|
+
role.errors.full_messages.to_sentence,
|
|
344
|
+
record: role
|
|
345
|
+
)
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def save!(role)
|
|
349
|
+
role.save!
|
|
350
|
+
role
|
|
351
|
+
rescue ActiveRecord::RecordInvalid
|
|
352
|
+
raise ValidationError, role
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def available_copy_name(source_name)
|
|
356
|
+
base_name = "#{source_name} copy"
|
|
357
|
+
candidate = base_name
|
|
358
|
+
suffix = 2
|
|
359
|
+
|
|
360
|
+
while name_exists?(candidate)
|
|
361
|
+
candidate = "#{base_name} #{suffix}"
|
|
362
|
+
suffix += 1
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
candidate
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def name_exists?(name)
|
|
369
|
+
actor.account.roles.where("LOWER(name) = ?", name.downcase).exists?
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
def apply_search(roles, filters)
|
|
373
|
+
search = filter_value(filters, :search) || query[:search]
|
|
374
|
+
search = search.to_s.strip.presence
|
|
375
|
+
return roles unless search
|
|
376
|
+
|
|
377
|
+
pattern = "%#{ActiveRecord::Base.sanitize_sql_like(search.downcase)}%"
|
|
378
|
+
roles.where(
|
|
379
|
+
"LOWER(lesli_roles.name) LIKE :pattern " \
|
|
380
|
+
"OR LOWER(COALESCE(lesli_roles.description, '')) LIKE :pattern",
|
|
381
|
+
pattern: pattern
|
|
382
|
+
)
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
def apply_status(roles, filters)
|
|
386
|
+
case filter_value(filters, :status).to_s
|
|
387
|
+
when "active"
|
|
388
|
+
roles.where(active: true)
|
|
389
|
+
when "inactive"
|
|
390
|
+
roles.where(active: false)
|
|
391
|
+
else
|
|
392
|
+
roles
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
def apply_type(roles, filters)
|
|
397
|
+
case filter_value(filters, :type).to_s
|
|
398
|
+
when "system"
|
|
399
|
+
roles.where(SYSTEM_ROLE_SQL)
|
|
400
|
+
when "custom"
|
|
401
|
+
roles.where("NOT (#{SYSTEM_ROLE_SQL})")
|
|
402
|
+
else
|
|
403
|
+
roles
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
def filter_value(filters, key)
|
|
408
|
+
filters&.[](key) || filters&.[](key.to_s)
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def sort_column
|
|
412
|
+
SORT_COLUMNS.fetch(query.dig(:order, :by).to_s, SORT_COLUMNS.fetch("id"))
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
def sort_direction
|
|
416
|
+
query.dig(:order, :dir).to_s.casecmp?("asc") ? "ASC" : "DESC"
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def page
|
|
420
|
+
requested = query.dig(:pagination, :page).to_i
|
|
421
|
+
requested.positive? ? requested : 1
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
def per_page
|
|
425
|
+
requested = query.dig(:pagination, :perPage).to_i
|
|
426
|
+
return DEFAULT_PER_PAGE unless requested.positive?
|
|
427
|
+
|
|
428
|
+
[requested, MAX_PER_PAGE].min
|
|
429
|
+
end
|
|
430
|
+
end
|
|
431
|
+
end
|
|
@@ -33,96 +33,63 @@ Building a better future, one line of code at a time.
|
|
|
33
33
|
module LesliShield
|
|
34
34
|
class UserRegistrationService < Lesli::ApplicationLesliService
|
|
35
35
|
|
|
36
|
-
def initialize(
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
def initialize(user)
|
|
37
|
+
super(user)
|
|
38
|
+
self.resource = user
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
# force token deletion so we are sure nobody
|
|
48
|
-
# will be able to use the token again
|
|
49
|
-
current_user.update(confirmation_token: nil)
|
|
50
|
-
|
|
51
|
-
# Minimum security settings required
|
|
52
|
-
#self.settings.create_with(:value => false).find_or_create_by(:name => "mfa_enabled")
|
|
53
|
-
#self.settings.create_with(:value => :email).find_or_create_by(:name => "mfa_method")
|
|
41
|
+
# Devise owns confirmation. This service performs only the Lesli account
|
|
42
|
+
# provisioning that must happen after Devise accepts the token.
|
|
43
|
+
def provision_account!
|
|
44
|
+
assign_account! if resource.account.blank?
|
|
45
|
+
assign_initial_role! unless resource.user_roles.exists?
|
|
46
|
+
synchronize_role_privileges!
|
|
54
47
|
|
|
48
|
+
resource
|
|
55
49
|
end
|
|
56
50
|
|
|
57
|
-
|
|
51
|
+
private
|
|
58
52
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
LesliShield::RolePrivilegeService.new(nil).synchronize(role)
|
|
53
|
+
def assign_account!
|
|
54
|
+
account = if multiaccount?
|
|
55
|
+
create_owned_account!
|
|
56
|
+
else
|
|
57
|
+
Lesli::Account.first!
|
|
65
58
|
end
|
|
66
59
|
|
|
67
|
-
|
|
68
|
-
resource.save
|
|
69
|
-
|
|
70
|
-
# # initialize user dependencies
|
|
71
|
-
# def after_account_assignation
|
|
72
|
-
# return unless self.account
|
|
73
|
-
|
|
74
|
-
# #Courier::One::Firebase::User.sync_user(self)
|
|
75
|
-
# # Lesli::Courier.new(:lesli_calendar).from(:calendar_service, self).create({
|
|
76
|
-
# # name: "Personal Calendar",
|
|
77
|
-
# # default: true
|
|
78
|
-
# # })
|
|
79
|
-
# end
|
|
80
|
-
|
|
60
|
+
resource.update!(account: account)
|
|
81
61
|
end
|
|
82
62
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
# proprietary instance (only one company is using the platform)
|
|
92
|
-
# Another scenario where it is not needed to create a new account for the
|
|
93
|
-
# user is when the user is confirming the account through an invitation
|
|
94
|
-
def add_account
|
|
95
|
-
|
|
96
|
-
if ALLOW_MULTI_ACCOUNT === true
|
|
97
|
-
# create new account for the new user only if multi-account is allowed
|
|
98
|
-
account = Lesli::Account.create!({
|
|
99
|
-
user: resource, # set user as owner of his just created account
|
|
100
|
-
name: "Lesli", # temporary company name
|
|
101
|
-
email: resource.email,
|
|
102
|
-
status: :active # account is active due user already confirmed his email
|
|
103
|
-
})
|
|
104
|
-
else
|
|
105
|
-
# if multi-account is not allowed user belongs to the first account in instance
|
|
106
|
-
account = Lesli::Account.first
|
|
107
|
-
end
|
|
63
|
+
def create_owned_account!
|
|
64
|
+
Lesli::Account.create!(
|
|
65
|
+
user: resource,
|
|
66
|
+
name: "Lesli",
|
|
67
|
+
email: resource.email,
|
|
68
|
+
status: :active
|
|
69
|
+
)
|
|
70
|
+
end
|
|
108
71
|
|
|
109
|
-
|
|
110
|
-
resource.account
|
|
72
|
+
def assign_initial_role!
|
|
73
|
+
role = resource.account.roles.find_by!(name: initial_role_name)
|
|
74
|
+
resource.user_roles.create!(role: role)
|
|
111
75
|
end
|
|
112
76
|
|
|
113
|
-
def
|
|
114
|
-
|
|
115
|
-
# add owner role to user only if multi-account is allowed
|
|
116
|
-
resource.user_roles.create({ role: account.roles.find_by(name: "owner") })
|
|
117
|
-
else
|
|
118
|
-
# Assigning default role if defined in account settings
|
|
119
|
-
# Otherwise, the default role is "limited"
|
|
120
|
-
default_role_name = Lesli.config.shield.dig(:default_role) || "guest"
|
|
77
|
+
def initial_role_name
|
|
78
|
+
return "owner" if multiaccount?
|
|
121
79
|
|
|
122
|
-
|
|
80
|
+
Lesli.config.shield.dig(:default_role).presence || "guest"
|
|
81
|
+
end
|
|
123
82
|
|
|
124
|
-
|
|
83
|
+
def synchronize_role_privileges!
|
|
84
|
+
resource.roles.find_each do |role|
|
|
85
|
+
LesliShield::RolePrivilegeService.new.rebuild!(role)
|
|
125
86
|
end
|
|
126
87
|
end
|
|
88
|
+
|
|
89
|
+
# Read the setting at execution time. Applications and tests can change
|
|
90
|
+
# Lesli configuration after this service class has been loaded.
|
|
91
|
+
def multiaccount?
|
|
92
|
+
Lesli.config.security.dig(:allow_multiaccount) == true
|
|
93
|
+
end
|
|
127
94
|
end
|
|
128
95
|
end
|
|
@@ -55,7 +55,7 @@ module LesliShield
|
|
|
55
55
|
def create(remote_ip, user_agent=nil, session_source="devise_standard_session")
|
|
56
56
|
|
|
57
57
|
# register a new unique session
|
|
58
|
-
current_session = current_user.sessions.create({
|
|
58
|
+
current_session = current_user.sessions.create!({
|
|
59
59
|
:remote => remote_ip,
|
|
60
60
|
|
|
61
61
|
:agent_os => user_agent&.dig(:os) || "unknown",
|