lesli_shield 1.1.7 → 1.1.9

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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/lesli_shield/application.css +1036 -32
  3. data/app/assets/stylesheets/lesli_shield/confirmations.css +89 -557
  4. data/app/assets/stylesheets/lesli_shield/invitations.css +544 -0
  5. data/app/assets/stylesheets/lesli_shield/passwords.css +76 -542
  6. data/app/assets/stylesheets/lesli_shield/registrations.css +66 -539
  7. data/app/assets/stylesheets/lesli_shield/sessions.css +98 -564
  8. data/app/controllers/lesli_shield/invites_controller.rb +100 -49
  9. data/app/controllers/lesli_shield/profiles_controller.rb +9 -3
  10. data/app/controllers/lesli_shield/role/actions_controller.rb +134 -38
  11. data/app/controllers/lesli_shield/roles_controller.rb +114 -141
  12. data/app/controllers/lesli_shield/users_controller.rb +11 -4
  13. data/app/controllers/users/confirmations_controller.rb +2 -2
  14. data/app/controllers/users/invitations_controller.rb +101 -0
  15. data/app/controllers/users/passwords_controller.rb +1 -1
  16. data/app/controllers/users/registrations_controller.rb +2 -2
  17. data/app/controllers/users/sessions_controller.rb +3 -1
  18. data/app/jobs/lesli_shield/invitation_delivery_job.rb +123 -0
  19. data/app/mailers/lesli_shield/devise_mailer.rb +3 -3
  20. data/app/mailers/lesli_shield/invitation_mailer.rb +58 -0
  21. data/app/models/concerns/lesli_shield/role_management.rb +70 -0
  22. data/app/models/concerns/lesli_shield/user_security.rb +22 -19
  23. data/app/models/lesli_shield/invite.rb +183 -12
  24. data/app/services/lesli_shield/invitation_audit.rb +34 -0
  25. data/app/services/lesli_shield/invitation_email_policy.rb +64 -0
  26. data/app/services/lesli_shield/invitation_service.rb +405 -0
  27. data/app/services/lesli_shield/invite_service.rb +34 -5
  28. data/app/services/lesli_shield/profile_service.rb +1 -0
  29. data/app/services/lesli_shield/role_action_service.rb +37 -92
  30. data/app/services/lesli_shield/role_audit.rb +42 -0
  31. data/app/services/lesli_shield/role_permission_service.rb +347 -0
  32. data/app/services/lesli_shield/role_privilege_deployment_service.rb +79 -0
  33. data/app/services/lesli_shield/role_privilege_service.rb +69 -105
  34. data/app/services/lesli_shield/role_service.rb +431 -0
  35. data/app/services/lesli_shield/user_registration_service.rb +1 -1
  36. data/app/services/lesli_shield/user_validator_service.rb +9 -9
  37. data/app/views/devise/confirmations/new.html.erb +6 -6
  38. data/app/views/devise/confirmations/show.html.erb +6 -6
  39. data/app/views/devise/passwords/edit.html.erb +8 -8
  40. data/app/views/devise/passwords/new.html.erb +9 -9
  41. data/app/views/devise/registrations/new.html.erb +16 -16
  42. data/app/views/devise/sessions/new.html.erb +9 -9
  43. data/app/views/devise/shared/_application-devise-simple.erb +1 -1
  44. data/app/views/devise/shared/_application-devise.html.erb +4 -4
  45. data/app/views/devise/shared/_demo.html.erb +5 -5
  46. data/app/views/devise/shared/_error_messages.html.erb +5 -5
  47. data/app/views/devise/shared/_links.html.erb +12 -15
  48. data/app/views/lesli_shield/invites/_form.html.erb +64 -7
  49. data/app/views/lesli_shield/invites/_invite.html.erb +55 -2
  50. data/app/views/lesli_shield/invites/_invite_actions.html.erb +34 -0
  51. data/app/views/lesli_shield/invites/_status_badge.html.erb +13 -0
  52. data/app/views/lesli_shield/invites/index.html.erb +119 -63
  53. data/app/views/lesli_shield/invites/new.html.erb +11 -36
  54. data/app/views/lesli_shield/invites/show.html.erb +13 -52
  55. data/app/views/lesli_shield/profiles/_information-form.html.erb +11 -0
  56. data/app/views/lesli_shield/profiles/_password-form.html.erb +1 -1
  57. data/app/views/lesli_shield/profiles/show.html.erb +1 -4
  58. data/app/views/lesli_shield/role/actions/_custom_actions.html.erb +24 -0
  59. data/app/views/lesli_shield/role/actions/_deployment_status.html.erb +14 -0
  60. data/app/views/lesli_shield/role/actions/_matrix.html.erb +69 -0
  61. data/app/views/lesli_shield/role/actions/_permissions.html.erb +95 -0
  62. data/app/views/lesli_shield/role/actions/_toggle.html.erb +34 -0
  63. data/app/views/lesli_shield/role/actions/index.html.erb +13 -10
  64. data/app/views/lesli_shield/roles/_deployment_badge.html.erb +15 -0
  65. data/app/views/lesli_shield/roles/_empty_state.html.erb +20 -0
  66. data/app/views/lesli_shield/roles/_field_errors.html.erb +5 -0
  67. data/app/views/lesli_shield/roles/_filters.html.erb +66 -0
  68. data/app/views/lesli_shield/roles/_form.html.erb +149 -18
  69. data/app/views/lesli_shield/roles/_management_menu.html.erb +37 -0
  70. data/app/views/lesli_shield/roles/_pagination.html.erb +43 -0
  71. data/app/views/lesli_shield/roles/_role_list.html.erb +116 -0
  72. data/app/views/lesli_shield/roles/_status_badge.html.erb +11 -0
  73. data/app/views/lesli_shield/roles/_type_badge.html.erb +9 -0
  74. data/app/views/lesli_shield/roles/index.html.erb +15 -38
  75. data/app/views/lesli_shield/roles/new.html.erb +11 -0
  76. data/app/views/lesli_shield/roles/show.html.erb +79 -7
  77. data/app/views/lesli_shield/users/_information-card.html.erb +3 -3
  78. data/app/views/lesli_shield/users/_information-form.html.erb +6 -0
  79. data/app/views/lesli_shield/users/_management-privileges.html.erb +2 -2
  80. data/app/views/users/invitations/show.html.erb +142 -0
  81. data/app/views/users/invitations/unavailable.html.erb +19 -0
  82. data/config/locales/devise.en.yml +8 -0
  83. data/config/locales/devise.es.yml +8 -0
  84. data/config/locales/translations.en.yml +313 -78
  85. data/config/locales/translations.es.yml +313 -78
  86. data/config/routes.rb +10 -3
  87. data/db/migrate/v1/0801120110_create_lesli_shield_invites.rb +40 -6
  88. data/lib/lesli_shield/engine.rb +4 -0
  89. data/lib/lesli_shield/router.rb +10 -0
  90. data/lib/lesli_shield/version.rb +2 -2
  91. data/lib/tasks/lesli_shield_tasks.rake +1 -1
  92. data/readme.md +13 -1
  93. metadata +35 -11
  94. data/app/assets/stylesheets/lesli_shield/confirmations.tailwind.css +0 -918
  95. data/app/assets/stylesheets/lesli_shield/devise/oauth.css +0 -33
  96. data/app/assets/stylesheets/lesli_shield/passwords.tailwind.css +0 -873
  97. data/app/assets/stylesheets/lesli_shield/registrations.tailwind.css +0 -908
  98. data/app/assets/stylesheets/lesli_shield/sessions.tailwind.css +0 -984
  99. data/app/assets/stylesheets/lesli_shield/users.css +0 -31
  100. data/app/mailers/lesli_shield/invitation.html.erb +0 -23
  101. data/app/views/lesli_shield/invites/edit.html.erb +0 -12
  102. data/app/views/lesli_shield/role/actions/_form.html.erb +0 -75
@@ -0,0 +1,405 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Owns every invitation lifecycle mutation that crosses model boundaries.
5
+ # Administrative operations require an actor; public acceptance authenticates
6
+ # the operation with the purpose-bound signed invitation token instead.
7
+ class InvitationService < Lesli::ApplicationLesliService
8
+ ACCEPTANCE_PURPOSE = :account_invitation
9
+ TOKEN_LIFETIME = 7.days
10
+ USER_ATTRIBUTES = %i[
11
+ first_name
12
+ last_name
13
+ telephone
14
+ password
15
+ password_confirmation
16
+ ].freeze
17
+
18
+ def create_and_send!(attributes)
19
+ invitation_attributes = attributes.to_h.symbolize_keys
20
+ invitation_attributes[:email] = normalize_email(
21
+ invitation_attributes[:email]
22
+ )
23
+
24
+ invitation = nil
25
+
26
+ Invite.transaction do |transaction|
27
+ shield_account = current_shield_account!
28
+ role = assignable_role!(
29
+ shield_account,
30
+ invitation_attributes.delete(:role_id)
31
+ )
32
+
33
+ invitation = shield_account.invites.new(
34
+ invitation_attributes.slice(
35
+ :email,
36
+ :full_name,
37
+ :telephone,
38
+ :notes
39
+ )
40
+ )
41
+ invitation.invited_by = current_user
42
+ invitation.role = role
43
+ invitation.sent_at = Time.current
44
+ invitation.save!
45
+
46
+ enqueue_delivery_after_commit(
47
+ transaction,
48
+ invitation,
49
+ operation: :invitation_sent,
50
+ actor: current_user
51
+ )
52
+ end
53
+
54
+ invitation
55
+ end
56
+
57
+ def resend!(invitation)
58
+ resent_invitation = nil
59
+
60
+ Invite.transaction do |transaction|
61
+ resent_invitation = lock_owned_invitation!(invitation)
62
+ validate_invitation_email!(resent_invitation)
63
+ validate_assignable_role!(resent_invitation.role)
64
+
65
+ if resent_invitation.pending? &&
66
+ !resent_invitation.acceptance_available?
67
+ resent_invitation.expire!
68
+ end
69
+
70
+ resent_invitation.resend!
71
+ resent_invitation.update!(sent_at: Time.current)
72
+
73
+ enqueue_delivery_after_commit(
74
+ transaction,
75
+ resent_invitation,
76
+ operation: :invitation_resent,
77
+ actor: current_user
78
+ )
79
+ end
80
+
81
+ resent_invitation
82
+ end
83
+
84
+ def cancel!(invitation)
85
+ cancelled_invitation = nil
86
+
87
+ Invite.transaction do
88
+ cancelled_invitation = lock_owned_invitation!(invitation)
89
+ cancelled_invitation.cancel!
90
+ record_audit_event!(
91
+ :invitation_cancelled,
92
+ cancelled_invitation,
93
+ actor: current_user
94
+ )
95
+ end
96
+
97
+ cancelled_invitation
98
+ end
99
+
100
+ def accept!(token:, user_attributes:)
101
+ accepted_user = nil
102
+
103
+ Invite.transaction do
104
+ invitation = lock_signed_invitation!(token)
105
+ role = validate_token_constraints!(
106
+ invitation,
107
+ lock_associations: true
108
+ )
109
+
110
+ decision = locked_email_decision(invitation)
111
+ validate_email_decision!(invitation, decision)
112
+
113
+ accepted_user = build_or_recover_user(
114
+ invitation,
115
+ decision,
116
+ user_attributes
117
+ )
118
+ persist_user!(accepted_user)
119
+ assign_role!(accepted_user, role)
120
+ confirm_user!(accepted_user)
121
+ invitation.accept!(accepted_user)
122
+ record_audit_event!(
123
+ :invitation_accepted,
124
+ invitation,
125
+ actor: accepted_user
126
+ )
127
+ end
128
+
129
+ accepted_user
130
+ end
131
+
132
+ def invitation_from_token!(token)
133
+ invitation = signed_invitation!(token)
134
+ validate_token_constraints!(invitation)
135
+ invitation
136
+ end
137
+
138
+ def signed_acceptance_url(invitation)
139
+ token = invitation.signed_id(
140
+ purpose: ACCEPTANCE_PURPOSE,
141
+ expires_in: signed_token_lifetime(invitation)
142
+ )
143
+
144
+ Rails.application.routes.url_helpers.user_invitation_url(
145
+ **action_mailer_url_options,
146
+ invitation_token: token
147
+ )
148
+ end
149
+
150
+ private
151
+
152
+ def normalize_email(email)
153
+ email.to_s.strip.downcase.presence
154
+ end
155
+
156
+ def current_shield_account!
157
+ current_user&.account&.shield || raise(ActiveRecord::RecordNotFound)
158
+ end
159
+
160
+ def assignable_role!(shield_account, role_id)
161
+ invitation = shield_account.invites.new
162
+ invitation.invited_by = current_user
163
+
164
+ if role_id.blank?
165
+ invalid_invitation!(
166
+ invitation,
167
+ :role,
168
+ I18n.t("lesli_shield.invites.role_required")
169
+ )
170
+ end
171
+
172
+ role = shield_account.account.roles.find(role_id)
173
+ return role if current_user.can_assign_role?(role)
174
+
175
+ raise ActiveRecord::RecordNotFound
176
+ end
177
+
178
+ def validate_assignable_role!(role)
179
+ return if current_user.can_assign_role?(role)
180
+
181
+ raise ActiveRecord::RecordNotFound
182
+ end
183
+
184
+ def lock_owned_invitation!(invitation)
185
+ current_shield_account!
186
+ .invites
187
+ .lock
188
+ .find(invitation.id)
189
+ end
190
+
191
+ def lock_signed_invitation!(token)
192
+ signed_invitation = signed_invitation!(token)
193
+ Invite.lock.find(signed_invitation.id)
194
+ end
195
+
196
+ def signed_invitation!(token)
197
+ Invite.find_signed!(token, purpose: ACCEPTANCE_PURPOSE)
198
+ end
199
+
200
+ def validate_token_constraints!(invitation, lock_associations: false)
201
+ invitation_is_available = invitation.acceptance_available? &&
202
+ invitation.accepted_at.nil? &&
203
+ invitation.accepted_user_id.nil? &&
204
+ invitation.cancelled_at.nil?
205
+
206
+ unless invitation_is_available
207
+ invalid_invitation!(
208
+ invitation,
209
+ :base,
210
+ I18n.t("lesli_shield.invites.not_acceptable")
211
+ )
212
+ end
213
+
214
+ account_relation = relation_for(
215
+ Lesli::Account,
216
+ lock: lock_associations
217
+ )
218
+ account = account_relation.find_by(
219
+ id: invitation.account&.account_id
220
+ )
221
+
222
+ unless account&.active?
223
+ invalid_invitation!(
224
+ invitation,
225
+ :base,
226
+ I18n.t("lesli_shield.invites.not_acceptable")
227
+ )
228
+ end
229
+
230
+ role_relation = relation_for(
231
+ Lesli::Role,
232
+ lock: lock_associations
233
+ )
234
+ role = role_relation.find_by(id: invitation.role_id)
235
+ valid_role = role&.active? &&
236
+ role.account_id == account.id
237
+ return role if valid_role
238
+
239
+ invalid_invitation!(
240
+ invitation,
241
+ :role,
242
+ I18n.t("lesli_shield.invites.role_account_mismatch")
243
+ )
244
+ end
245
+
246
+ def validate_invitation_email!(invitation)
247
+ decision = InvitationEmailPolicy.new(
248
+ account: invitation.account.account,
249
+ email: invitation.email
250
+ ).evaluate
251
+
252
+ validate_email_decision!(invitation, decision)
253
+ end
254
+
255
+ def locked_email_decision(invitation)
256
+ policy = InvitationEmailPolicy.new(
257
+ account: invitation.account.account,
258
+ email: invitation.email
259
+ )
260
+ decision = policy.evaluate
261
+
262
+ if decision.recoverable?
263
+ decision.user.lock!
264
+ decision = policy.evaluate
265
+ end
266
+
267
+ decision
268
+ end
269
+
270
+ def validate_email_decision!(invitation, decision)
271
+ return if decision.allowed?
272
+
273
+ invalid_invitation!(
274
+ invitation,
275
+ :email,
276
+ I18n.t("lesli_shield.invites.email_#{decision.status}")
277
+ )
278
+ end
279
+
280
+ def build_or_recover_user(invitation, decision, attributes)
281
+ user = decision.recoverable? ? decision.user : Lesli::User.new
282
+ permitted_attributes = attributes.to_h
283
+ .symbolize_keys
284
+ .slice(*USER_ATTRIBUTES)
285
+
286
+ user.assign_attributes(permitted_attributes)
287
+ user.email = invitation.email
288
+ user.account = invitation.account.account
289
+ user.skip_confirmation_notification!
290
+
291
+ if permitted_attributes[:password].blank?
292
+ user.errors.add(:password, :blank)
293
+ raise ActiveRecord::RecordInvalid, user
294
+ end
295
+
296
+ user
297
+ end
298
+
299
+ def persist_user!(user)
300
+ user.save!
301
+ end
302
+
303
+ def confirm_user!(user)
304
+ confirmed = user.confirm(ensure_valid: true)
305
+ raise ActiveRecord::RecordInvalid, user unless confirmed
306
+
307
+ user.update!(
308
+ confirmation_token: nil,
309
+ confirmation_sent_at: nil
310
+ )
311
+ end
312
+
313
+ def assign_role!(user, role)
314
+ user.user_roles.find_or_create_by!(role: role)
315
+ end
316
+
317
+ def record_audit_event!(operation, invitation, actor:)
318
+ InvitationAudit.record!(
319
+ operation,
320
+ invitation: invitation,
321
+ actor: actor,
322
+ source: self.class
323
+ )
324
+ end
325
+
326
+ def enqueue_delivery_after_commit(
327
+ transaction,
328
+ invitation,
329
+ operation:,
330
+ actor:
331
+ )
332
+ acceptance_url = signed_acceptance_url(invitation)
333
+
334
+ transaction.after_commit do
335
+ enqueue_invitation_delivery(
336
+ invitation,
337
+ acceptance_url,
338
+ operation: operation,
339
+ actor: actor
340
+ )
341
+ end
342
+ end
343
+
344
+ def enqueue_invitation_delivery(
345
+ invitation,
346
+ acceptance_url,
347
+ operation:,
348
+ actor:
349
+ )
350
+ delivery_job = InvitationDeliveryJob.perform_later(
351
+ invitation.id,
352
+ acceptance_url,
353
+ operation.to_s,
354
+ actor.id
355
+ )
356
+
357
+ unless delivery_job.successfully_enqueued?
358
+ enqueue_error = delivery_job.enqueue_error ||
359
+ RuntimeError.new("Invitation delivery was not enqueued")
360
+
361
+ InvitationDeliveryJob.report_enqueue_failure(
362
+ invitation.id,
363
+ actor.id,
364
+ enqueue_error
365
+ )
366
+ end
367
+ rescue StandardError => exception
368
+ InvitationDeliveryJob.report_enqueue_failure(
369
+ invitation.id,
370
+ actor.id,
371
+ exception
372
+ )
373
+ end
374
+
375
+ def signed_token_lifetime(invitation)
376
+ remaining_lifetime = if invitation.expires_at.present?
377
+ invitation.expires_at - Time.current
378
+ end
379
+
380
+ if remaining_lifetime&.positive?
381
+ return [TOKEN_LIFETIME, remaining_lifetime].min
382
+ end
383
+
384
+ invalid_invitation!(
385
+ invitation,
386
+ :base,
387
+ I18n.t("lesli_shield.invites.not_acceptable")
388
+ )
389
+ end
390
+
391
+ def relation_for(model, lock:)
392
+ lock ? model.lock : model.all
393
+ end
394
+
395
+ def action_mailer_url_options
396
+ options = Rails.application.config.action_mailer.default_url_options
397
+ (options || {}).to_h.symbolize_keys
398
+ end
399
+
400
+ def invalid_invitation!(invitation, attribute, message)
401
+ invitation.errors.add(attribute, message)
402
+ raise ActiveRecord::RecordInvalid, invitation
403
+ end
404
+ end
405
+ end
@@ -32,12 +32,41 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module LesliShield
34
34
  class InviteService < Lesli::ApplicationLesliService
35
+ STATUS_FILTERS = %w[pending accepted cancelled expired].freeze
35
36
 
36
- def index params
37
- Invite.all
38
- .page(query[:pagination][:page])
39
- .per(query[:pagination][:perPage])
40
- .order(updated_at: :desc)
37
+ def self.status_filter(value)
38
+ value.to_s.presence_in(STATUS_FILTERS)
39
+ end
40
+
41
+ def index(params)
42
+ invitations = Invite
43
+ .for_account(current_user.account.shield)
44
+ .includes(:invited_by, :role)
45
+
46
+ invitations = filter_by_status(
47
+ invitations,
48
+ self.class.status_filter(params&.[](:status))
49
+ )
50
+
51
+ invitations
52
+ .page(query[:pagination][:page])
53
+ .per(query[:pagination][:perPage])
54
+ .order(created_at: :desc)
55
+ end
56
+
57
+ private
58
+
59
+ def filter_by_status(invitations, status)
60
+ case status
61
+ when "pending"
62
+ invitations.active
63
+ when "expired"
64
+ invitations.expired
65
+ when "accepted", "cancelled"
66
+ invitations.where(status: Invite.statuses.fetch(status))
67
+ else
68
+ invitations
69
+ end
41
70
  end
42
71
  end
43
72
  end
@@ -11,6 +11,7 @@ module LesliShield
11
11
 
12
12
  def update(params)
13
13
  resource.assign_attributes(params)
14
+ resource.time_zone = resource.time_zone.presence
14
15
  resource.alias = resource.full_name_initials if resource.alias.blank?
15
16
 
16
17
  unless resource.save
@@ -1,115 +1,60 @@
1
- module LesliShield
2
- class RoleActionService < Lesli::ApplicationLesliService
3
-
4
- def find id
5
- super(Role::Action.with_deleted.find(id))
6
- end
7
-
8
- def index role_id
9
-
10
- def clean action
11
- {
12
- :id => action.id,
13
- :role_id => action.role_id,
14
- :action_id => action.action_id,
15
- :deleted_at => action.deleted_at,
16
- :active => action.active
17
- }
18
- end
19
-
20
- role_actions = {}
21
-
22
- Role::Action.with_deleted.joins(action: :parent)
23
- .where(:role_id => role_id)
24
- .order('parents_lesli_resources.route')
25
- .select(
26
- :id,
27
- :role_id,
28
- :deleted_at,
29
- "parents_lesli_resources.id as controller_id",
30
- "parents_lesli_resources.label as controller_name",
31
- "parents_lesli_resources.route as controller_route",
32
- "lesli_resources.action as action_name",
33
- "lesli_resources.id as action_id",
34
- "case when lesli_shield_role_actions.deleted_at is null then TRUE else FALSE end active"
35
- ).each do |action|
36
-
37
- unless role_actions.has_key?(action[:controller_route])
38
- role_actions[action[:controller_route]] = {
39
- index: nil,
40
- show:nil,
41
- create:nil,
42
- update:nil,
43
- destroy:nil
44
- }
45
- end
46
-
47
- if action[:action_name] == "index"
48
- role_actions[action[:controller_route]][:index] = clean(action)
49
- end
50
-
51
- if action[:action_name] == "show"
52
- role_actions[action[:controller_route]][:show] = clean(action)
53
- end
54
-
55
- if action[:action_name] == "create"
56
- role_actions[action[:controller_route]][:create] = clean(action)
57
- end
58
-
59
- if action[:action_name] == "update"
60
- role_actions[action[:controller_route]][:update] = clean(action)
61
- end
1
+ # frozen_string_literal: true
62
2
 
63
- if action[:action_name] == "destroy"
64
- role_actions[action[:controller_route]][:destroy] = clean(action)
3
+ module LesliShield
4
+ # Assigns the initial permission selections required by system roles.
5
+ class RoleActionService < Lesli::ApplicationLesliService
6
+ GUEST_PERMISSIONS = {
7
+ "lesli/users" => %w[update],
8
+ "lesli/abouts" => %w[show],
9
+ "lesli/user/sessions" => %w[index],
10
+ "lesli_admin/profiles" => %w[show]
11
+ }.freeze
12
+
13
+ def add_guest_actions(role)
14
+ actions = GUEST_PERMISSIONS.flat_map do |controller, action_names|
15
+ action_names.filter_map do |action_name|
16
+ find_catalog_action(controller, action_name)
65
17
  end
66
18
  end
67
19
 
68
- role_actions
20
+ assign_actions(role, actions)
69
21
  end
70
22
 
71
- def add_guest_actions role
72
-
73
- # Adding default system actions for profile descriptor
74
- [
75
- { controller: "lesli/users", actions: ["update"] }, # enable user edition
76
- { controller: "lesli/abouts", actions: ["show"] }, # system status
77
- { controller: "lesli/user/sessions", actions: ["index"] }, # session management
78
- { controller: "lesli_admin/profiles", actions: ["show"] } # enable profile view
79
- ].each do |controller_action|
80
-
81
- controller_action[:actions].each do |action_name|
82
-
83
- system_controller_action = Lesli::Resource.actions.joins(:parent)
84
- .where("parents_lesli_resources.route = ?", controller_action[:controller])
85
- .where("lesli_resources.action = ?", action_name)
86
-
87
- role.actions.find_or_create_by(
88
- action: system_controller_action.first
89
- )
90
- end
91
- end
23
+ def add_owner_actions(role)
24
+ assign_actions(role, active_catalog.to_a)
92
25
  end
93
26
 
94
- def add_owner_actions role
27
+ private
95
28
 
96
- now = Time.current
29
+ def active_catalog
30
+ Lesli::Resource.actions.joins(:parent)
31
+ end
97
32
 
98
- # Adding default system actions for profile descriptor
99
- actions = Lesli::Resource.actions
33
+ def find_catalog_action(controller, action_name)
34
+ active_catalog.find_by(
35
+ parents_lesli_resources: { route: controller },
36
+ action: action_name
37
+ )
38
+ end
100
39
 
40
+ def assign_actions(role, actions)
41
+ timestamp = Time.current
101
42
  records = actions.map do |action|
102
43
  {
103
44
  role_id: role.id,
104
45
  action_id: action.id,
105
- created_at: now,
106
- updated_at: now
46
+ deleted_at: nil,
47
+ created_at: timestamp,
48
+ updated_at: timestamp
107
49
  }
108
50
  end
51
+ return role if records.empty?
109
52
 
110
- role.actions.upsert_all(records,
53
+ Role::Action.upsert_all(
54
+ records,
111
55
  unique_by: :index_role_actions_on_role_and_action
112
56
  )
57
+ role
113
58
  end
114
59
  end
115
60
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Defines the durable audit vocabulary for role administration.
5
+ class RoleAudit
6
+ EVENT_DESCRIPTIONS = {
7
+ "role_created" => "Role created",
8
+ "role_updated" => "Role updated",
9
+ "role_deactivated" => "Role deactivated",
10
+ "role_activated" => "Role activated",
11
+ "role_duplicated" => "Role duplicated",
12
+ "role_deleted" => "Role deleted",
13
+ "role_privileges_deployed" => "Role privileges deployed"
14
+ }.freeze
15
+
16
+ class << self
17
+ # Call inside the mutation's transaction. Audit persistence errors
18
+ # must propagate so the role and its event cannot diverge.
19
+ def record!(operation, role:, actor:, source:, details: {})
20
+ operation = operation.to_s
21
+ description = EVENT_DESCRIPTIONS.fetch(operation)
22
+
23
+ raise ArgumentError, "audit actor is required" unless actor
24
+
25
+ actor.log(
26
+ engine: LesliShield,
27
+ source: source,
28
+ action: operation,
29
+ operation: operation,
30
+ description: description,
31
+ subject: role,
32
+ details: details.merge(
33
+ role_id: role.id,
34
+ role_name: role.name,
35
+ role_active: role.active?,
36
+ account_id: role.account_id
37
+ )
38
+ )
39
+ end
40
+ end
41
+ end
42
+ end