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,10 +1,13 @@
|
|
|
1
1
|
module LesliShield
|
|
2
2
|
class InvitesController < ApplicationController
|
|
3
|
-
before_action :set_invite, only: %i[
|
|
3
|
+
before_action :set_invite, only: %i[show resend cancel]
|
|
4
|
+
before_action :set_assignable_roles, only: %i[new create]
|
|
4
5
|
|
|
5
6
|
# GET /invites
|
|
6
7
|
def index
|
|
7
|
-
@
|
|
8
|
+
@status_filter = InviteService.status_filter(params[:status])
|
|
9
|
+
invite_service = LesliShield::InviteService.new(current_user, query)
|
|
10
|
+
@invites = respond_with_pagination(invite_service.index(params))
|
|
8
11
|
end
|
|
9
12
|
|
|
10
13
|
# GET /invites/1
|
|
@@ -13,68 +16,116 @@ module LesliShield
|
|
|
13
16
|
|
|
14
17
|
# GET /invites/new
|
|
15
18
|
def new
|
|
16
|
-
@invite =
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# GET /invites/1/edit
|
|
20
|
-
def edit
|
|
19
|
+
@invite = current_account_invites.new
|
|
21
20
|
end
|
|
22
21
|
|
|
23
22
|
# POST /invites
|
|
24
23
|
def create
|
|
25
|
-
@invite =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
else
|
|
40
|
-
respond_with_lesli(
|
|
41
|
-
turbo: stream_notification_danger(@invite.errors.full_messages.to_sentence)
|
|
42
|
-
)
|
|
43
|
-
end
|
|
24
|
+
@invite = invitation_service.create_and_send!(invite_params)
|
|
25
|
+
|
|
26
|
+
success(I18n.t("lesli_shield.invites.created_successfully"))
|
|
27
|
+
respond_with_invitation_redirect(@invite)
|
|
28
|
+
rescue ActiveRecord::RecordInvalid => exception
|
|
29
|
+
@invite = exception.record
|
|
30
|
+
respond_with_lesli(
|
|
31
|
+
turbo: stream_notification_danger(
|
|
32
|
+
@invite.errors.full_messages.to_sentence
|
|
33
|
+
),
|
|
34
|
+
json: { errors: @invite.errors },
|
|
35
|
+
html: "lesli_shield/invites/new",
|
|
36
|
+
status: :unprocessable_content
|
|
37
|
+
)
|
|
44
38
|
end
|
|
45
39
|
|
|
46
|
-
#
|
|
47
|
-
def
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
:turbo => stream_notification_danger(@invite.errors.full_messages.to_sentence)
|
|
59
|
-
)
|
|
60
|
-
end
|
|
40
|
+
# POST /invites/1/resend
|
|
41
|
+
def resend
|
|
42
|
+
@invite = invitation_service.resend!(@invite)
|
|
43
|
+
|
|
44
|
+
success(I18n.t("lesli_shield.invites.resent_successfully"))
|
|
45
|
+
respond_with_invitation_redirect(@invite)
|
|
46
|
+
rescue Invite::InvalidStatusTransition,
|
|
47
|
+
ActiveRecord::RecordInvalid => exception
|
|
48
|
+
respond_with_lesli(
|
|
49
|
+
turbo: stream_notification_danger(error_message(exception)),
|
|
50
|
+
redirect: invite_path(@invite)
|
|
51
|
+
)
|
|
61
52
|
end
|
|
62
53
|
|
|
63
|
-
#
|
|
64
|
-
def
|
|
65
|
-
|
|
66
|
-
|
|
54
|
+
# PATCH /invites/1/cancel
|
|
55
|
+
def cancel
|
|
56
|
+
@invite = invitation_service.cancel!(@invite)
|
|
57
|
+
|
|
58
|
+
success(I18n.t("lesli_shield.invites.cancelled_successfully"))
|
|
59
|
+
respond_with_invitation_redirect(@invite)
|
|
60
|
+
rescue Invite::InvalidStatusTransition,
|
|
61
|
+
ActiveRecord::RecordInvalid => exception
|
|
62
|
+
respond_with_lesli(
|
|
63
|
+
turbo: stream_notification_danger(error_message(exception)),
|
|
64
|
+
redirect: invite_path(@invite)
|
|
65
|
+
)
|
|
67
66
|
end
|
|
68
67
|
|
|
69
68
|
private
|
|
70
|
-
|
|
69
|
+
|
|
71
70
|
def set_invite
|
|
72
|
-
|
|
71
|
+
# Scoping the lookup makes an invitation from another account
|
|
72
|
+
# indistinguishable from a resource that does not exist.
|
|
73
|
+
@invite = current_account_invites.find(params.expect(:id))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def set_assignable_roles
|
|
77
|
+
@assignable_roles = current_user.account.roles
|
|
78
|
+
.where(active: true)
|
|
79
|
+
.where(permission_level: (..current_user.max_level_permission))
|
|
80
|
+
.order(permission_level: :desc, name: :asc)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def current_account_invites
|
|
84
|
+
current_user.account.shield.invites
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def invitation_service
|
|
88
|
+
@invitation_service ||= InvitationService.new(current_user)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def error_message(exception)
|
|
92
|
+
return exception.record.errors.full_messages.to_sentence if
|
|
93
|
+
exception.respond_to?(:record)
|
|
94
|
+
|
|
95
|
+
exception.message
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def respond_with_invitation_redirect(invitation)
|
|
99
|
+
destination = invite_path(invitation)
|
|
100
|
+
|
|
101
|
+
if Lesli::ResponderInterface.const_defined?(
|
|
102
|
+
:NATIVE_TURBO_REDIRECTS,
|
|
103
|
+
false
|
|
104
|
+
)
|
|
105
|
+
return respond_with_lesli(redirect: destination)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Compatibility with Lesli versions released before native Turbo
|
|
109
|
+
# redirects were part of ResponderInterface. This branch can be
|
|
110
|
+
# removed when Shield's minimum Lesli version includes that API.
|
|
111
|
+
respond_to do |format|
|
|
112
|
+
format.html do
|
|
113
|
+
redirect_to destination, status: :see_other
|
|
114
|
+
end
|
|
115
|
+
format.turbo_stream do
|
|
116
|
+
redirect_to destination, status: :see_other
|
|
117
|
+
end
|
|
118
|
+
format.json { head :ok }
|
|
119
|
+
end
|
|
73
120
|
end
|
|
74
121
|
|
|
75
|
-
# Only allow a list of trusted parameters through.
|
|
76
122
|
def invite_params
|
|
77
|
-
params.require(:invite).permit(
|
|
123
|
+
params.require(:invite).permit(
|
|
124
|
+
:email,
|
|
125
|
+
:full_name,
|
|
126
|
+
:notes,
|
|
127
|
+
:role_id
|
|
128
|
+
)
|
|
78
129
|
end
|
|
79
130
|
end
|
|
80
131
|
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
module LesliShield
|
|
2
|
+
module Profile
|
|
3
|
+
class PasswordsController < ApplicationController
|
|
4
|
+
PASSWORD_CHANGE_RATE_LIMIT_WINDOW = 15.minutes
|
|
5
|
+
|
|
6
|
+
skip_before_action :authorize_request
|
|
7
|
+
|
|
8
|
+
rate_limit(
|
|
9
|
+
to: 10,
|
|
10
|
+
within: PASSWORD_CHANGE_RATE_LIMIT_WINDOW,
|
|
11
|
+
by: -> { current_user&.id || request.remote_ip },
|
|
12
|
+
with: :respond_to_password_change_rate_limit,
|
|
13
|
+
only: :update,
|
|
14
|
+
name: "profile-password-update"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
def update
|
|
18
|
+
@active_profile_tab = "security"
|
|
19
|
+
@password = PasswordService
|
|
20
|
+
.new(current_user)
|
|
21
|
+
.update(
|
|
22
|
+
password_params,
|
|
23
|
+
current_session_id: session[:user_session_id]
|
|
24
|
+
)
|
|
25
|
+
@user = @password.result
|
|
26
|
+
|
|
27
|
+
if @password.successful?
|
|
28
|
+
bypass_sign_in(@user)
|
|
29
|
+
record_password_event(
|
|
30
|
+
operation: "password_change_succeeded",
|
|
31
|
+
description: "Password changed successfully"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
respond_with_lesli(
|
|
35
|
+
turbo: [
|
|
36
|
+
stream_notification_success(success_message),
|
|
37
|
+
stream_redirection(profile_path(tab: "security"))
|
|
38
|
+
],
|
|
39
|
+
json: { message: success_message },
|
|
40
|
+
redirect: profile_path(tab: "security")
|
|
41
|
+
)
|
|
42
|
+
else
|
|
43
|
+
record_password_event(
|
|
44
|
+
operation: "password_change_failed",
|
|
45
|
+
description: "Password change failed validation"
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
respond_with_lesli(
|
|
49
|
+
turbo: [
|
|
50
|
+
stream_notification_danger(@password.errors_as_sentence),
|
|
51
|
+
turbo_stream.replace(
|
|
52
|
+
"shield-profile-password-form",
|
|
53
|
+
partial: "lesli_shield/profiles/password-form",
|
|
54
|
+
locals: { user: @user }
|
|
55
|
+
)
|
|
56
|
+
],
|
|
57
|
+
json: { errors: @password.errors },
|
|
58
|
+
html: "lesli_shield/profiles/show",
|
|
59
|
+
status: :unprocessable_content
|
|
60
|
+
)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def password_params
|
|
67
|
+
params.require(:user).permit(
|
|
68
|
+
:current_password,
|
|
69
|
+
:password,
|
|
70
|
+
:password_confirmation
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def success_message
|
|
75
|
+
I18n.t("lesli_shield.profiles.password_updated")
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def record_password_event(operation:, description:)
|
|
79
|
+
log(operation: operation, description: description)
|
|
80
|
+
rescue StandardError => exception
|
|
81
|
+
Rails.error.report(
|
|
82
|
+
exception,
|
|
83
|
+
handled: true,
|
|
84
|
+
context: {
|
|
85
|
+
controller: self.class.name,
|
|
86
|
+
operation: operation
|
|
87
|
+
}
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def respond_to_password_change_rate_limit
|
|
92
|
+
@active_profile_tab = "security"
|
|
93
|
+
@user = current_user
|
|
94
|
+
message = I18n.t(
|
|
95
|
+
"lesli_shield.profiles.password_too_many_attempts"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
response.set_header(
|
|
99
|
+
"Retry-After",
|
|
100
|
+
PASSWORD_CHANGE_RATE_LIMIT_WINDOW.to_i.to_s
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
respond_with_lesli(
|
|
104
|
+
turbo: [
|
|
105
|
+
stream_notification_danger(message),
|
|
106
|
+
turbo_stream.replace(
|
|
107
|
+
"shield-profile-password-form",
|
|
108
|
+
partial: "lesli_shield/profiles/password-form",
|
|
109
|
+
locals: { user: @user }
|
|
110
|
+
)
|
|
111
|
+
],
|
|
112
|
+
json: { error: message },
|
|
113
|
+
html: "lesli_shield/profiles/show",
|
|
114
|
+
status: :too_many_requests
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module LesliShield
|
|
2
|
+
class ProfilesController < ApplicationController
|
|
3
|
+
PROFILE_FIELDS = %i[
|
|
4
|
+
id
|
|
5
|
+
uid
|
|
6
|
+
alias
|
|
7
|
+
title
|
|
8
|
+
email
|
|
9
|
+
first_name
|
|
10
|
+
last_name
|
|
11
|
+
telephone
|
|
12
|
+
time_zone
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
skip_before_action :authorize_request
|
|
16
|
+
before_action :set_profile
|
|
17
|
+
|
|
18
|
+
def show
|
|
19
|
+
respond_with_lesli(json: profile_payload)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def update
|
|
23
|
+
@profile.update(profile_params)
|
|
24
|
+
@user = @profile.result
|
|
25
|
+
|
|
26
|
+
if @profile.successful?
|
|
27
|
+
respond_with_lesli(
|
|
28
|
+
turbo: stream_notification_success(t("lesli_shield.profiles.updated")),
|
|
29
|
+
json: profile_payload,
|
|
30
|
+
redirect: profile_path
|
|
31
|
+
)
|
|
32
|
+
else
|
|
33
|
+
respond_with_lesli(
|
|
34
|
+
turbo: [
|
|
35
|
+
stream_notification_danger(@profile.errors_as_sentence),
|
|
36
|
+
turbo_stream.replace("shield-profile-information-form",
|
|
37
|
+
partial: "lesli_shield/profiles/information-form")
|
|
38
|
+
],
|
|
39
|
+
json: { errors: @profile.errors },
|
|
40
|
+
html: :show,
|
|
41
|
+
status: :unprocessable_content
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def set_profile
|
|
49
|
+
@profile = LesliShield::ProfileService.new(current_user)
|
|
50
|
+
@user = @profile.show
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def profile_params
|
|
54
|
+
params.require(:user).permit(
|
|
55
|
+
:alias,
|
|
56
|
+
:title,
|
|
57
|
+
:email,
|
|
58
|
+
:first_name,
|
|
59
|
+
:last_name,
|
|
60
|
+
:telephone,
|
|
61
|
+
:time_zone
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def profile_payload
|
|
66
|
+
@user.as_json(only: PROFILE_FIELDS)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -1,58 +1,154 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module LesliShield
|
|
2
4
|
class Role::ActionsController < ApplicationController
|
|
3
|
-
before_action :set_role
|
|
4
|
-
before_action :
|
|
5
|
+
before_action :set_role
|
|
6
|
+
before_action :set_permission_filters
|
|
5
7
|
|
|
6
|
-
def index
|
|
7
|
-
|
|
8
|
+
def index
|
|
9
|
+
load_permission_matrix
|
|
8
10
|
end
|
|
9
11
|
|
|
10
|
-
def
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
stream_notification_success("Role privileges added successfully!"),
|
|
16
|
-
turbo_stream.replace('shield-role-actions-form', partial: 'lesli_shield/role/actions/form')
|
|
17
|
-
]
|
|
18
|
-
)
|
|
19
|
-
else
|
|
20
|
-
respond_with_lesli(
|
|
21
|
-
:turbo => stream_notification_danger(@role_action.errors_as_sentence)
|
|
22
|
-
)
|
|
23
|
-
end
|
|
12
|
+
def create
|
|
13
|
+
persist_permission!(
|
|
14
|
+
action_id: params.require(:action_id),
|
|
15
|
+
enabled: true
|
|
16
|
+
)
|
|
24
17
|
end
|
|
25
18
|
|
|
26
19
|
def destroy
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
persist_permission!(action_id: params[:id], enabled: false)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def persist_permission!(action_id:, enabled:)
|
|
26
|
+
role_permission_service.set!(
|
|
27
|
+
role: @role,
|
|
28
|
+
action_id: action_id,
|
|
29
|
+
enabled: enabled
|
|
30
|
+
)
|
|
31
|
+
respond_with_lesli(
|
|
32
|
+
turbo: permission_update_streams,
|
|
33
|
+
json: {
|
|
34
|
+
role_id: @role.id,
|
|
35
|
+
action_id: action_id.to_i,
|
|
36
|
+
enabled: enabled
|
|
37
|
+
},
|
|
38
|
+
redirect: permission_index_path
|
|
39
|
+
)
|
|
40
|
+
rescue RolePermissionService::Error => exception
|
|
41
|
+
respond_with_lesli(
|
|
42
|
+
turbo: stream_notification_danger(exception.message),
|
|
43
|
+
json: { message: exception.message },
|
|
44
|
+
redirect: permission_index_path,
|
|
45
|
+
status: :unprocessable_content
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def set_role
|
|
50
|
+
@role = role_service.find!(params[:role_id], manageable: true)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def set_permission_filters
|
|
54
|
+
@engine_slug = params[:engine_slug].presence
|
|
55
|
+
@search = params[:search].to_s.strip
|
|
56
|
+
|
|
57
|
+
if @engine_slug && !role_permission_service.engine_available?(@engine_slug)
|
|
58
|
+
raise ActiveRecord::RecordNotFound
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Only Turbo needs refreshed HTML. Redirects and JSON responses do not
|
|
63
|
+
# need to load the permission catalog after saving a single checkbox.
|
|
64
|
+
def permission_update_streams
|
|
65
|
+
return unless request.format.turbo_stream?
|
|
66
|
+
|
|
67
|
+
@role.reload
|
|
68
|
+
load_permission_matrix
|
|
69
|
+
|
|
70
|
+
[
|
|
71
|
+
stream_notification_success(
|
|
72
|
+
I18n.t("lesli_shield.roles.permissions_update_success")
|
|
73
|
+
),
|
|
74
|
+
turbo_stream.replace(
|
|
75
|
+
"shield-role-permissions",
|
|
76
|
+
partial: "lesli_shield/role/actions/permissions"
|
|
77
|
+
),
|
|
78
|
+
turbo_stream.replace(
|
|
79
|
+
"role-permission-deployment-status",
|
|
80
|
+
partial: "lesli_shield/role/actions/deployment_status"
|
|
34
81
|
)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
82
|
+
]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def role_service
|
|
86
|
+
@role_service ||= RoleService.new(current_user)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def role_permission_service
|
|
90
|
+
@role_permission_service ||= RolePermissionService.new(current_user)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def load_permission_matrix
|
|
94
|
+
@permission_matrix = role_permission_service.matrix(@role)
|
|
95
|
+
@permission_engines = @permission_matrix.map do |engine|
|
|
96
|
+
{
|
|
97
|
+
slug: engine[:engine].tr("_", "-"),
|
|
98
|
+
label: engine[:label],
|
|
99
|
+
count: engine[:resources].sum { |resource| resource[:actions].size }
|
|
100
|
+
}
|
|
101
|
+
end
|
|
102
|
+
if @engine_slug
|
|
103
|
+
@permission_matrix = @permission_matrix.select do |engine|
|
|
104
|
+
engine[:engine].tr("_", "-") == @engine_slug
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
if @search.present?
|
|
109
|
+
@permission_matrix = filter_permission_matrix(
|
|
110
|
+
@permission_matrix,
|
|
111
|
+
@search
|
|
38
112
|
)
|
|
39
113
|
end
|
|
40
114
|
end
|
|
41
115
|
|
|
42
|
-
|
|
116
|
+
def filter_permission_matrix(matrix, search)
|
|
117
|
+
query = search.downcase
|
|
43
118
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
119
|
+
matrix.filter_map do |engine|
|
|
120
|
+
resources = engine[:resources].filter_map do |resource|
|
|
121
|
+
actions = resource[:actions].select do |action|
|
|
122
|
+
permission_matches?(engine, resource, action, query)
|
|
123
|
+
end
|
|
124
|
+
resource.merge(actions: actions) if actions.any?
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
engine.merge(resources: resources) if resources.any?
|
|
128
|
+
end
|
|
129
|
+
end
|
|
47
130
|
|
|
48
|
-
def
|
|
49
|
-
|
|
131
|
+
def permission_matches?(engine, resource, action, query)
|
|
132
|
+
[
|
|
133
|
+
engine[:engine],
|
|
134
|
+
engine[:label],
|
|
135
|
+
resource[:identifier],
|
|
136
|
+
resource[:label],
|
|
137
|
+
resource[:route],
|
|
138
|
+
action[:identifier],
|
|
139
|
+
action[:label],
|
|
140
|
+
action[:display_label],
|
|
141
|
+
action[:action]
|
|
142
|
+
].any? { |value| value.to_s.downcase.include?(query) }
|
|
50
143
|
end
|
|
51
144
|
|
|
52
|
-
def
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
145
|
+
def permission_index_path
|
|
146
|
+
options = { search: @search.presence }
|
|
147
|
+
if @engine_slug
|
|
148
|
+
return engine_role_actions_path(@role, engine_slug: @engine_slug, **options)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
role_actions_path(@role, **options)
|
|
56
152
|
end
|
|
57
153
|
end
|
|
58
154
|
end
|