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,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
@@ -39,7 +39,7 @@ module LesliShield
39
39
 
40
40
  # get the max level permission from roles assigned to the user
41
41
  def max_level_permission
42
- self.roles.maximum(:permission_level) || 0
42
+ roles.where(active: true).maximum(:permission_level) || 0
43
43
  end
44
44
 
45
45
  # check if user has roles with specific names
@@ -51,7 +51,10 @@ module LesliShield
51
51
  # to perform a specific action in a specific controller
52
52
  def has_privileges_for?(controller, action)
53
53
  begin
54
- return self.privileges.where(
54
+ active_role_ids = roles.where(active: true).select(:id)
55
+
56
+ return Role::Privilege.where(
57
+ role_id: active_role_ids,
55
58
  controller: controller,
56
59
  action: action,
57
60
  active: true
@@ -62,26 +65,26 @@ module LesliShield
62
65
  end
63
66
  end
64
67
 
65
- # Check if user has enough privilege to work with the given role
66
- def can_work_with_role?(role_id)
67
-
68
- # get the role if only id is given
69
- role = self.account.roles.find_by(:id => role_id)
70
-
71
- # false if role not found
72
- return false if role.blank?
68
+ # A user can assign only active roles from their own account and never
69
+ # roles above the highest active permission level they already hold.
70
+ def can_assign_role?(role)
71
+ return false if role.blank? || !role.active?
72
+ return false unless role.account_id == account_id
73
+ return false if role.permission_level.blank?
73
74
 
74
- # not valid role without object levelpermission defined
75
- return false if role.level_permission.blank?
75
+ role.permission_level <= max_level_permission
76
+ end
76
77
 
77
- # get the max level permission from the roles the user has assigned
78
- user_role_max_level_permission = self.roles.map(&:level_permission).max()
78
+ # Preserve the existing public API while using the current
79
+ # permission_level column and the stricter assignment rules.
80
+ def can_work_with_role?(role_or_id)
81
+ role = if role_or_id.is_a?(Lesli::Role)
82
+ role_or_id
83
+ else
84
+ account.roles.find_by(id: role_or_id)
85
+ end
79
86
 
80
- # check if user can work with the level permission of the role is trying to modify
81
- # Note: user only can assigned an level permission below the max of his own roles
82
- # Current user cannot assign role if role to assign is the same of the greater role
83
- # assigned to the current user
84
- user_role_max_level_permission >= role.level_permission
87
+ can_assign_role?(role)
85
88
  end
86
89
 
87
90
  # Checks configuration of all the roles assigned to the user
@@ -1,24 +1,195 @@
1
1
  module LesliShield
2
2
  class Invite < ApplicationRecord
3
- belongs_to :account, class_name: "LesliShield::Account"
4
- belongs_to :user, class_name: "Lesli::User"
3
+ class InvalidStatusTransition < StandardError; end
5
4
 
6
- validates :email, presence: true, on: :create
5
+ DEFAULT_EXPIRATION = 7.days
6
+ STATUS_TRANSITIONS = {
7
+ "pending" => %w[accepted cancelled expired],
8
+ "expired" => %w[pending],
9
+ "accepted" => [],
10
+ "cancelled" => []
11
+ }.freeze
7
12
 
8
- before_create :before_create_invite
13
+ belongs_to :account, class_name: "LesliShield::Account"
14
+ belongs_to :invited_by,
15
+ class_name: "Lesli::User",
16
+ foreign_key: :invited_by_id
17
+ belongs_to :role,
18
+ class_name: "Lesli::Role",
19
+ foreign_key: :role_id,
20
+ optional: true
21
+ belongs_to :accepted_user,
22
+ class_name: "Lesli::User",
23
+ foreign_key: :accepted_user_id,
24
+ optional: true
9
25
 
10
- enum :status, {
11
- created: 0,
12
- accepted: 1,
26
+ enum :status, {
27
+ pending: 0,
28
+ accepted: 1,
13
29
  cancelled: 2,
14
- sent: 5,
15
- requested: 6
30
+ expired: 3
31
+ }, default: :pending, validate: true, scopes: false
32
+
33
+ scope :pending, -> {
34
+ where(status: statuses.fetch("pending"))
35
+ }
36
+ scope :active, -> {
37
+ pending.where(expires_at: Time.current..)
38
+ }
39
+ scope :expired, -> {
40
+ expired_by_status = where(status: statuses.fetch("expired"))
41
+ expired_by_time = pending.where(expires_at: (..Time.current))
42
+
43
+ expired_by_status.or(expired_by_time)
16
44
  }
45
+ scope :for_account, ->(account) {
46
+ where(account: account)
47
+ }
48
+
49
+ before_validation :normalize_email
50
+ before_validation :set_default_expiration, on: :create
51
+
52
+ validates :email,
53
+ presence: true,
54
+ format: { with: URI::MailTo::EMAIL_REGEXP }
55
+ validate :email_can_be_invited, on: :create
56
+ validate :role_belongs_to_inviting_account
57
+ validate :status_transition_is_allowed, on: :update
58
+
59
+ class << self
60
+ def expiration_period
61
+ configured_expiration = Lesli.config.shield&.dig(
62
+ :invitation_expiration
63
+ )
64
+
65
+ configured_expiration.presence || DEFAULT_EXPIRATION
66
+ end
67
+
68
+ def allowed_status_transition?(from:, to:)
69
+ STATUS_TRANSITIONS.fetch(from.to_s, []).include?(to.to_s)
70
+ end
71
+ end
72
+
73
+ # Accepting an invitation is intentionally a terminal operation. The
74
+ # acceptance service will call this only after creating the user,
75
+ # assigning its account and role, and confirming its email atomically.
76
+ def accept!(accepted_user)
77
+ transition_to!(
78
+ :accepted,
79
+ accepted_user: accepted_user,
80
+ accepted_at: Time.current
81
+ )
82
+ end
83
+
84
+ def cancel!
85
+ transition_to!(
86
+ :cancelled,
87
+ cancelled_at: Time.current
88
+ )
89
+ end
90
+
91
+ def expire!
92
+ transition_to!(:expired)
93
+ end
94
+
95
+ # Resending is the only operation that can reactivate an expired
96
+ # invitation. No user record is created or modified here.
97
+ def resend!
98
+ unless resendable?
99
+ raise InvalidStatusTransition,
100
+ "cannot resend an invitation with status #{status}"
101
+ end
102
+
103
+ transition_to!(
104
+ :pending,
105
+ expires_at: Time.current + self.class.expiration_period
106
+ )
107
+ end
108
+
109
+ def resendable?
110
+ expired?
111
+ end
112
+
113
+ def resend_available?
114
+ resendable? || (pending? && expires_at.present? && expires_at.past?)
115
+ end
116
+
117
+ def cancellable?
118
+ pending?
119
+ end
120
+
121
+ def lifecycle_status
122
+ return "expired" if pending? && expires_at.present? && expires_at.past?
123
+
124
+ status
125
+ end
126
+
127
+ def acceptance_available?
128
+ pending? && expires_at.present? && expires_at.future?
129
+ end
130
+
131
+ private
132
+
133
+ def normalize_email
134
+ self.email = email.to_s.strip.downcase.presence
135
+ end
136
+
137
+ def set_default_expiration
138
+ self.expires_at ||= Time.current + self.class.expiration_period
139
+ end
140
+
141
+ def email_can_be_invited
142
+ destination_account = account&.account
143
+ return if destination_account.blank? || email.blank?
144
+
145
+ decision = InvitationEmailPolicy.new(
146
+ account: destination_account,
147
+ email: email
148
+ ).evaluate
149
+ return if decision.allowed?
150
+
151
+ errors.add(
152
+ :email,
153
+ I18n.t("lesli_shield.invites.email_#{decision.status}")
154
+ )
155
+ end
156
+
157
+ def role_belongs_to_inviting_account
158
+ return if role.blank? || account.blank?
159
+ return if role.account == account.account
160
+
161
+ errors.add(
162
+ :role,
163
+ I18n.t("lesli_shield.invites.role_account_mismatch")
164
+ )
165
+ end
166
+
167
+ def transition_to!(target_status, attributes = {})
168
+ unless self.class.allowed_status_transition?(
169
+ from: status,
170
+ to: target_status
171
+ )
172
+ raise InvalidStatusTransition,
173
+ "cannot transition invitation from #{status} to #{target_status}"
174
+ end
175
+
176
+ update!(attributes.merge(status: target_status))
177
+ end
17
178
 
18
- private
179
+ def status_transition_is_allowed
180
+ previous_status, next_status = status_change_to_be_saved
181
+ return if previous_status.nil?
182
+ return if self.class.allowed_status_transition?(
183
+ from: previous_status,
184
+ to: next_status
185
+ )
19
186
 
20
- def before_create_invite
21
- self.status = :created
187
+ errors.add(
188
+ :status,
189
+ I18n.t("lesli_shield.invites.invalid_status_transition"),
190
+ from: previous_status,
191
+ to: next_status
192
+ )
22
193
  end
23
194
  end
24
195
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Provides one explicit audit vocabulary for the invitation lifecycle.
5
+ # This component does not hide persistence errors; each caller decides
6
+ # whether the event belongs to a database transaction or an async delivery.
7
+ class InvitationAudit
8
+ EVENT_DESCRIPTIONS = {
9
+ "invitation_sent" => "Invitation delivered",
10
+ "invitation_resent" => "Invitation delivered again",
11
+ "invitation_cancelled" => "Invitation cancelled",
12
+ "invitation_accepted" => "Invitation accepted",
13
+ "invitation_delivery_failed" => "Invitation delivery failed"
14
+ }.freeze
15
+
16
+ class << self
17
+ def record!(operation, invitation:, actor:, source:)
18
+ operation = operation.to_s
19
+ description = EVENT_DESCRIPTIONS.fetch(operation)
20
+
21
+ raise ArgumentError, "audit actor is required" unless actor
22
+
23
+ actor.log(
24
+ engine: LesliShield,
25
+ source: source,
26
+ action: operation,
27
+ operation: operation,
28
+ description: description,
29
+ subject: invitation
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Classifies an invitation recipient without creating or modifying a user.
5
+ # The result is evaluated again while accepting the invitation to protect
6
+ # against a user being created between invitation delivery and acceptance.
7
+ class InvitationEmailPolicy
8
+ AVAILABLE = :available
9
+ MEMBER = :member
10
+ OTHER_ACCOUNT = :other_account
11
+ RECOVERABLE = :recoverable
12
+ DELETED = :deleted
13
+ EXISTING_WITHOUT_ACCOUNT = :existing_without_account
14
+
15
+ ALLOWED_STATUSES = [AVAILABLE, RECOVERABLE].freeze
16
+
17
+ Decision = Struct.new(:status, :user, keyword_init: true) do
18
+ def allowed?
19
+ ALLOWED_STATUSES.include?(status)
20
+ end
21
+
22
+ def rejected?
23
+ !allowed?
24
+ end
25
+
26
+ def recoverable?
27
+ status == RECOVERABLE
28
+ end
29
+ end
30
+
31
+ attr_reader :account, :email
32
+
33
+ def initialize(account:, email:)
34
+ raise ArgumentError, "account is required" unless account
35
+
36
+ @account = account
37
+ @email = email.to_s.strip.downcase
38
+ end
39
+
40
+ def evaluate
41
+ user = find_user
42
+
43
+ return decision(AVAILABLE) unless user
44
+ return decision(DELETED, user) if user.deleted_at.present?
45
+ return decision(MEMBER, user) if user.account_id == account.id
46
+ return decision(OTHER_ACCOUNT, user) if user.account_id.present?
47
+ return decision(RECOVERABLE, user) unless user.confirmed?
48
+
49
+ decision(EXISTING_WITHOUT_ACCOUNT, user)
50
+ end
51
+
52
+ private
53
+
54
+ def find_user
55
+ Lesli::User
56
+ .with_deleted
57
+ .find_by("LOWER(email) = ?", email)
58
+ end
59
+
60
+ def decision(status, user = nil)
61
+ Decision.new(status: status, user: user)
62
+ end
63
+ end
64
+ end