lesli 5.1.13 → 5.1.14
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/app/controllers/lesli/application_lesli_controller.rb +27 -0
- data/app/controllers/lesli/items/attachments_controller.rb +3 -3
- data/app/controllers/lesli/items/discussions_controller.rb +2 -2
- data/app/controllers/lesli/items/tasks_controller.rb +2 -2
- data/app/controllers/lesli/roles_controller.rb +1 -6
- data/app/controllers/lesli/users_controller.rb +1 -5
- data/app/helpers/lesli/announcements_helper.rb +14 -0
- data/app/helpers/lesli/navigation_helper.rb +1 -1
- data/app/interfaces/lesli/responder_interface.rb +11 -4
- data/app/models/concerns/lesli/account_initializer.rb +5 -45
- data/app/models/concerns/lesli/user_extensions.rb +4 -2
- data/app/models/lesli/items/attachment.rb +3 -3
- data/app/models/lesli/role.rb +14 -2
- data/app/models/lesli/user.rb +7 -0
- data/app/operators/lesli/account_operator.rb +84 -0
- data/app/operators/lesli/resource_operator.rb +123 -0
- data/app/services/lesli/application_lesli_service.rb +33 -116
- data/app/services/lesli/resource_service.rb +12 -159
- data/app/services/lesli/role_service.rb +11 -229
- data/app/services/lesli/user_service.rb +37 -117
- data/app/views/lesli/layouts/application-lesli.html.erb +1 -1
- data/app/views/lesli/partials/_application-lesli-announcements.html.erb +28 -0
- data/app/views/lesli/partials/_application-lesli-header.html.erb +3 -2
- data/app/views/lesli/partials/_application-lesli-turbo.html.erb +5 -0
- data/config/initializers/lesli.rb +7 -2
- data/config/locales/translations.en.yml +57 -48
- data/config/locales/translations.es.yml +55 -45
- data/db/migrate/v1/0000000210_create_lesli_users.rb +1 -0
- data/db/seed/xyz.rb +1 -3
- data/lib/generators/lesli/base_generator.rb +41 -3
- data/lib/generators/lesli/controller/controller_generator.rb +1 -1
- data/lib/generators/lesli/controller/templates/controller.rb.tt +12 -7
- data/lib/generators/lesli/model/model_generator.rb +1 -1
- data/lib/generators/lesli/resource/resource_generator.rb +290 -0
- data/lib/generators/lesli/resource/templates/controller.rb.tt +99 -0
- data/lib/generators/lesli/resource/templates/locale.en.yml.tt +13 -0
- data/lib/generators/lesli/resource/templates/migration.rb.tt +28 -0
- data/lib/generators/lesli/resource/templates/model.rb.tt +38 -0
- data/lib/generators/lesli/resource/templates/operator.rb.tt +15 -0
- data/lib/generators/lesli/resource/templates/service.rb.tt +52 -0
- data/lib/generators/lesli/resource/templates/tests/controller_test.rb.tt +14 -0
- data/lib/generators/lesli/resource/templates/tests/model_test.rb.tt +24 -0
- data/lib/generators/lesli/resource/templates/tests/operator_test.rb.tt +15 -0
- data/lib/generators/lesli/resource/templates/tests/service_test.rb.tt +17 -0
- data/lib/generators/lesli/resource/templates/views/_form.html.erb.tt +17 -0
- data/lib/generators/lesli/resource/templates/views/edit.html.erb.tt +4 -0
- data/lib/generators/lesli/resource/templates/views/index.html.erb.tt +22 -0
- data/lib/generators/lesli/resource/templates/views/new.html.erb.tt +4 -0
- data/lib/generators/lesli/resource/templates/views/show.html.erb.tt +17 -0
- data/lib/generators/lesli/scaffold/scaffold_generator.rb +4 -39
- data/lib/generators/lesli/service/service_generator.rb +1 -1
- data/lib/generators/lesli/service/templates/service.rb.tt +15 -7
- data/lib/generators/lesli/views/views_generator.rb +1 -1
- data/lib/generators/lesli_generator.rb +11 -0
- data/lib/lesli/courier.rb +66 -34
- data/lib/lesli/database_tasks.rb +145 -0
- data/lib/lesli/version.rb +2 -2
- data/lib/tasks/lesli/db.rake +16 -93
- data/lib/tasks/lesli/resources.rake +1 -1
- data/readme.md +3 -1
- metadata +24 -3
- data/app/views/lesli/partials/_application-lesli-sidebar.html.erb +0 -50
|
@@ -33,137 +33,67 @@ Building a better future, one line of code at a time.
|
|
|
33
33
|
# ·
|
|
34
34
|
module Lesli
|
|
35
35
|
class ApplicationLesliService
|
|
36
|
+
attr_reader :current_user, :query, :errors
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
@resource;
|
|
41
|
-
@failures;
|
|
42
|
-
@current_user;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
# Service constructor
|
|
46
|
-
# current_user is always required to initialize a service object
|
|
47
|
-
# current user is used to get the data only from the account context
|
|
48
|
-
def initialize current_user=nil, query={}
|
|
49
|
-
|
|
50
|
-
# make the current user globaly available in the service object
|
|
38
|
+
# Services receive their request context explicitly. A system operation
|
|
39
|
+
# without an authenticated user must pass nil intentionally.
|
|
40
|
+
def initialize(current_user, query = {})
|
|
51
41
|
@current_user = current_user
|
|
52
|
-
|
|
53
|
-
# stores the resources from the database as result of the active record queries
|
|
54
|
-
@resource = nil
|
|
55
|
-
|
|
56
|
-
# stores any error found during the life-cycle of the service object
|
|
57
|
-
@failures = []
|
|
58
|
-
|
|
59
|
-
# standard conditions to query the database
|
|
60
42
|
@query = query
|
|
43
|
+
@resource = nil
|
|
44
|
+
@errors = []
|
|
61
45
|
end
|
|
62
46
|
|
|
63
|
-
|
|
64
|
-
#
|
|
65
|
-
def find
|
|
66
|
-
|
|
67
|
-
# self.resource = current_user.account.users.find_by_id(id) # example
|
|
68
|
-
|
|
69
|
-
# Should always return self
|
|
70
|
-
self.resource = resource if resource
|
|
71
|
-
self
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
# Standard method to list data from the database
|
|
76
|
-
def list params=nil
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
# Standard method to index data from the database
|
|
81
|
-
def index params=nil
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
# Standard method to show data from the database
|
|
86
|
-
def show
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
# Standard method to create new resource into the database
|
|
91
|
-
def create params
|
|
92
|
-
# Example:
|
|
93
|
-
# user = current_user.account.users.new(params)
|
|
94
|
-
|
|
95
|
-
# if user.save
|
|
96
|
-
# self.resource = user
|
|
97
|
-
# else
|
|
98
|
-
# self.error(user.errors.full_messages.to_sentence)
|
|
99
|
-
# end
|
|
100
|
-
self.resource = params
|
|
47
|
+
# Store the result of an account-scoped lookup and return the service so
|
|
48
|
+
# controllers can inspect found? and result through one contract.
|
|
49
|
+
def find(resource)
|
|
50
|
+
self.resource = resource
|
|
101
51
|
self
|
|
102
52
|
end
|
|
103
53
|
|
|
104
|
-
|
|
105
|
-
# Standard method to update resources into the database
|
|
106
|
-
def update params
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
# Standard method to delete data from the database
|
|
111
|
-
def delete
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
# Method to check if the service object has data available
|
|
116
|
-
# this method ment to be used together and after the find method
|
|
117
54
|
def found?
|
|
118
|
-
!
|
|
55
|
+
!resource.nil?
|
|
119
56
|
end
|
|
120
57
|
|
|
121
|
-
|
|
122
|
-
# Check if the service object has errors
|
|
123
58
|
def successful?
|
|
124
|
-
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
# Get the list of erros
|
|
129
|
-
def errors
|
|
130
|
-
self.failures
|
|
59
|
+
errors.empty?
|
|
131
60
|
end
|
|
132
61
|
|
|
133
|
-
|
|
134
|
-
# Get the list of erros as single string
|
|
135
62
|
def errors_as_sentence
|
|
136
|
-
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
# Register a new error for the current service object
|
|
141
|
-
def error error
|
|
142
|
-
self.failures.push(error)
|
|
63
|
+
errors.to_sentence
|
|
143
64
|
end
|
|
144
65
|
|
|
145
|
-
|
|
146
|
-
# Return the resource as a result
|
|
147
66
|
def result
|
|
148
|
-
|
|
67
|
+
resource
|
|
149
68
|
end
|
|
150
69
|
|
|
151
|
-
def cache_key_for_account(method=nil)
|
|
152
|
-
cache_key_builder(
|
|
70
|
+
def cache_key_for_account(method = nil)
|
|
71
|
+
cache_key_builder("account", current_user.account.id, method)
|
|
153
72
|
end
|
|
154
73
|
|
|
155
|
-
def cache_key_for_user(method=nil)
|
|
156
|
-
cache_key_builder(
|
|
74
|
+
def cache_key_for_user(method = nil)
|
|
75
|
+
cache_key_builder("user", current_user.id, method)
|
|
157
76
|
end
|
|
158
77
|
|
|
159
|
-
|
|
78
|
+
protected
|
|
160
79
|
|
|
161
|
-
|
|
80
|
+
attr_reader :resource
|
|
81
|
+
attr_writer :resource
|
|
82
|
+
|
|
83
|
+
# Store the result of a mutation and preserve the service response
|
|
84
|
+
# contract used by controllers.
|
|
85
|
+
def response(resource)
|
|
162
86
|
self.resource = resource
|
|
163
87
|
self
|
|
164
88
|
end
|
|
165
89
|
|
|
166
|
-
def
|
|
90
|
+
def error(message)
|
|
91
|
+
errors << message
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def cache_key_builder(namespace, namespace_id, method)
|
|
167
97
|
[
|
|
168
98
|
namespace,
|
|
169
99
|
namespace_id,
|
|
@@ -171,19 +101,6 @@ module Lesli
|
|
|
171
101
|
method,
|
|
172
102
|
query[:cacheKey]
|
|
173
103
|
].compact.join(":").tr("/_", "-")
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
attr_reader :current_user
|
|
177
|
-
|
|
178
|
-
attr_reader :resource
|
|
179
|
-
attr_writer :resource
|
|
180
|
-
|
|
181
|
-
attr_reader :successful
|
|
182
|
-
attr_writer :successful
|
|
183
|
-
|
|
184
|
-
attr_reader :failures
|
|
185
|
-
attr_writer :failures
|
|
186
|
-
|
|
187
|
-
attr_reader :query
|
|
104
|
+
end
|
|
188
105
|
end
|
|
189
106
|
end
|
|
@@ -32,21 +32,9 @@ Building a better future, one line of code at a time.
|
|
|
32
32
|
|
|
33
33
|
module Lesli
|
|
34
34
|
class ResourceService < Lesli::ApplicationLesliService
|
|
35
|
+
def index(matrix: false)
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
"users/registrations",
|
|
38
|
-
"users/sessions",
|
|
39
|
-
"users/passwords",
|
|
40
|
-
"users/confirmations"
|
|
41
|
-
]
|
|
42
|
-
|
|
43
|
-
def initialize
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def index matrix:false
|
|
47
|
-
|
|
48
|
-
# get a matrix of controllers and actions
|
|
49
|
-
c = Resource.actions.joins(:parent).select(
|
|
37
|
+
actions = Resource.actions.joins(:parent).select(
|
|
50
38
|
"parents_lesli_resources.engine as engine",
|
|
51
39
|
"parents_lesli_resources.route as route",
|
|
52
40
|
"parents_lesli_resources.identifier as controller",
|
|
@@ -70,153 +58,18 @@ module Lesli
|
|
|
70
58
|
.where("parents_lesli_resources.deleted_at is NULL")
|
|
71
59
|
.order("importance DESC")
|
|
72
60
|
|
|
73
|
-
return
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
engine = c[:engine]
|
|
83
|
-
controller = c[:controller]
|
|
84
|
-
|
|
85
|
-
# create a uniq container for every action that belongs to a specific controller
|
|
86
|
-
if cc[engine].blank?
|
|
87
|
-
cc[engine] = {}
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
# create a uniq container for every action that belongs to a specific controller
|
|
91
|
-
if cc[engine][controller].blank?
|
|
92
|
-
cc[engine][controller] = {
|
|
93
|
-
id: c[:controller_id],
|
|
94
|
-
name: c[:controller_name],
|
|
95
|
-
route: c[:route],
|
|
96
|
-
actions: []
|
|
97
|
-
}
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
# push every action to his specic controller
|
|
101
|
-
cc[engine][controller][:actions].push({
|
|
102
|
-
id: c[:action_id],
|
|
103
|
-
action: c[:action]
|
|
61
|
+
return actions unless matrix
|
|
62
|
+
|
|
63
|
+
actions.each_with_object({}) do |action, engines|
|
|
64
|
+
controllers = (engines[action[:engine]] ||= {})
|
|
65
|
+
controller = (controllers[action[:controller]] ||= {
|
|
66
|
+
id: action[:controller_id],
|
|
67
|
+
name: action[:controller_name],
|
|
68
|
+
route: action[:route],
|
|
69
|
+
actions: []
|
|
104
70
|
})
|
|
71
|
+
controller[:actions] << { id: action[:action_id], action: action[:action] }
|
|
105
72
|
end
|
|
106
|
-
|
|
107
|
-
return cc
|
|
108
|
-
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
# Scan new routes added and create role privileges
|
|
112
|
-
def build
|
|
113
|
-
|
|
114
|
-
# get all the engines, controllers and actions
|
|
115
|
-
engines = scan_for_engine_controllers
|
|
116
|
-
|
|
117
|
-
# Register descriptors and privileges for all the accounts
|
|
118
|
-
engines.each do |engine, controllers|
|
|
119
|
-
|
|
120
|
-
controllers.each do |controller_route, controller_actions|
|
|
121
|
-
|
|
122
|
-
# Build a strig with the standard name of a Rails controller from the standard routes
|
|
123
|
-
# Examples:
|
|
124
|
-
# users converts to Users
|
|
125
|
-
# cloud_bell/notifications converts to CloudBell::Notifications
|
|
126
|
-
# sometimes we need a second split to deal with third level deep of controllers
|
|
127
|
-
# Example: "Account::Currency::ExchangeRatesController" from "account/currency/exchange_rates"
|
|
128
|
-
identifier = controller_route
|
|
129
|
-
.camelize
|
|
130
|
-
.gsub('/', '::')
|
|
131
|
-
|
|
132
|
-
name = identifier.demodulize.humanize
|
|
133
|
-
|
|
134
|
-
controller = Lesli::Resource.find_or_initialize_by(route: controller_route)
|
|
135
|
-
controller.assign_attributes(
|
|
136
|
-
label: name,
|
|
137
|
-
engine: engine,
|
|
138
|
-
identifier: identifier
|
|
139
|
-
)
|
|
140
|
-
controller.save!
|
|
141
|
-
|
|
142
|
-
controller_actions.each do |action_name|
|
|
143
|
-
controller.children.find_or_create_by!(
|
|
144
|
-
action: action_name,
|
|
145
|
-
identifier: action_name,
|
|
146
|
-
label: action_name.humanize
|
|
147
|
-
)
|
|
148
|
-
end
|
|
149
|
-
end
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
private
|
|
154
|
-
|
|
155
|
-
def scan_for_engine_controllers
|
|
156
|
-
|
|
157
|
-
# Global container
|
|
158
|
-
controller_list = {
|
|
159
|
-
"rails_app" => {},
|
|
160
|
-
"lesli" => {}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
# Get the list of controllers and actions of the main rails app
|
|
164
|
-
Rails.application.routes.routes.each do |route|
|
|
165
|
-
|
|
166
|
-
list = "rails_app"
|
|
167
|
-
route = route.defaults
|
|
168
|
-
|
|
169
|
-
# filter the non-used main app routes
|
|
170
|
-
next if route[:controller].blank?
|
|
171
|
-
next if route[:controller].include? "rails"
|
|
172
|
-
next if route[:controller].include? "action_mailbox"
|
|
173
|
-
next if route[:controller].include? "active_storage"
|
|
174
|
-
next if route[:controller].include? "view_components"
|
|
175
|
-
next if route[:controller].include? "turbo/native/navigation"
|
|
176
|
-
next if route[:controller].include? "lesli/abouts" #default welcome page
|
|
177
|
-
|
|
178
|
-
if DEVISE_CONTROLLERS.include?(route[:controller])
|
|
179
|
-
list = "lesli"
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
# create a container for the actions related to a controller
|
|
183
|
-
controller_list[list][route[:controller]] = [] unless controller_list[list][route[:controller]]
|
|
184
|
-
|
|
185
|
-
# assign and group all the actions related to the controller
|
|
186
|
-
controller_list[list][route[:controller]].push(route[:action])
|
|
187
|
-
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
# Get the list of controllers and actions from engines
|
|
191
|
-
LesliSystem.engines.each do |engine, engine_info|
|
|
192
|
-
|
|
193
|
-
# Do not process main Rails app
|
|
194
|
-
next if engine == "Root"
|
|
195
|
-
|
|
196
|
-
# load and retrieve the list of controllers and actions from an engine
|
|
197
|
-
routes = "#{engine}::Engine".constantize.routes.routes.each do |route|
|
|
198
|
-
route = route.defaults
|
|
199
|
-
|
|
200
|
-
# validate if route has information, some special routes like redirects
|
|
201
|
-
# can generate an empty entry in the route hash
|
|
202
|
-
next if route.empty?
|
|
203
|
-
next if route[:controller].include? "rails/health"
|
|
204
|
-
|
|
205
|
-
# get the engine code
|
|
206
|
-
engine_code = engine_info[:code]
|
|
207
|
-
|
|
208
|
-
# create a container for the controllers related to the engine
|
|
209
|
-
controller_list[engine_code] = {} if controller_list[engine_code].blank?
|
|
210
|
-
|
|
211
|
-
# assign and group all the actions related to the controller
|
|
212
|
-
controller_list[engine_code][route[:controller]] = [] if controller_list[engine_code][route[:controller]].blank?
|
|
213
|
-
|
|
214
|
-
# assign and group all the actions related to the controller
|
|
215
|
-
controller_list[engine_code][route[:controller]].push(route[:action])
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
return controller_list
|
|
220
73
|
end
|
|
221
74
|
end
|
|
222
75
|
end
|
|
@@ -1,238 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Lesli
|
|
4
|
-
|
|
5
|
-
Copyright (c) 2025, Lesli Technologies, S. A.
|
|
6
|
-
|
|
7
|
-
This program is free software: you can redistribute it and/or modify
|
|
8
|
-
it under the terms of the GNU General Public License as published by
|
|
9
|
-
the Free Software Foundation, either version 3 of the License, or
|
|
10
|
-
(at your option) any later version.
|
|
11
|
-
|
|
12
|
-
This program is distributed in the hope that it will be useful,
|
|
13
|
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
-
GNU General Public License for more details.
|
|
16
|
-
|
|
17
|
-
You should have received a copy of the GNU General Public License
|
|
18
|
-
along with this program. If not, see http://www.gnu.org/licenses/.
|
|
19
|
-
|
|
20
|
-
Lesli · Ruby on Rails SaaS Development Framework.
|
|
21
|
-
|
|
22
|
-
Made with ♥ by LesliTech
|
|
23
|
-
Building a better future, one line of code at a time.
|
|
24
|
-
|
|
25
|
-
@contact hello@lesli.tech
|
|
26
|
-
@website https://www.lesli.tech
|
|
27
|
-
@license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
|
|
28
|
-
|
|
29
|
-
// · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
|
|
30
|
-
// ·
|
|
31
|
-
=end
|
|
1
|
+
# frozen_string_literal: true
|
|
32
2
|
|
|
33
3
|
module Lesli
|
|
4
|
+
# Core exposes only the account-scoped role lookup needed by selectors.
|
|
5
|
+
# Role administration belongs to the authentication/authorization engine
|
|
6
|
+
# and is implemented by LesliShield::RoleService.
|
|
34
7
|
class RoleService < Lesli::ApplicationLesliService
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
self.resource = current_user.account.roles.find_by_id(id)
|
|
38
|
-
self
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Return a list of roles that the user is able to work with
|
|
42
|
-
# according to object level permission
|
|
43
|
-
def list(params)
|
|
44
|
-
current_user.account.roles
|
|
45
|
-
.where("permission_level <= ?", current_user.max_level_permission)
|
|
46
|
-
.order(permission_level: :desc, name: :asc)
|
|
47
|
-
.select(:id, :name, :permission_level)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
# @return [Array] Paginated index of users.
|
|
52
|
-
# @description Return a paginated array of users, used mostly in frontend views
|
|
53
|
-
def index
|
|
54
|
-
|
|
55
|
-
users_subquery = <<-SQL
|
|
56
|
-
left join (
|
|
57
|
-
select
|
|
58
|
-
count(1) users,
|
|
59
|
-
role_id
|
|
60
|
-
from lesli_shield_user_roles
|
|
61
|
-
inner join lesli_users as u
|
|
62
|
-
on u.id = lesli_shield_user_roles.user_id
|
|
63
|
-
and u.deleted_at is null
|
|
64
|
-
where lesli_shield_user_roles.deleted_at is null
|
|
65
|
-
group by (role_id)
|
|
66
|
-
) users on users.role_id = lesli_roles.id
|
|
67
|
-
SQL
|
|
68
|
-
|
|
69
|
-
actions_subquery = <<-SQL
|
|
70
|
-
left join (
|
|
71
|
-
select
|
|
72
|
-
count(1) actions,
|
|
73
|
-
role_id
|
|
74
|
-
from lesli_shield_role_actions
|
|
75
|
-
group by role_id
|
|
76
|
-
) actions on actions.role_id = lesli_roles.id
|
|
77
|
-
SQL
|
|
78
|
-
|
|
79
|
-
current_user.account.roles
|
|
80
|
-
.where.not(:name => ['owner'])
|
|
81
|
-
.where("lesli_roles.permission_level <= ?", current_user.max_level_permission)
|
|
82
|
-
.joins(users_subquery)
|
|
83
|
-
.joins(actions_subquery)
|
|
84
|
-
.select(
|
|
85
|
-
:id,
|
|
86
|
-
:name,
|
|
87
|
-
:active,
|
|
88
|
-
:isolated,
|
|
89
|
-
:description,
|
|
90
|
-
:path_default,
|
|
91
|
-
:permission_level,
|
|
92
|
-
"users.users",
|
|
93
|
-
"actions.actions"
|
|
94
|
-
)
|
|
95
|
-
.page(query[:pagination][:page])
|
|
96
|
-
.per(query[:pagination][:perPage])
|
|
97
|
-
.order(permission_level: :desc, name: :asc)
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
# @overwrite
|
|
101
|
-
# @return {Object}
|
|
102
|
-
# @description Retrives the role
|
|
103
|
-
def show
|
|
104
|
-
self.resource
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
# @overwrite
|
|
108
|
-
# @return {Object}
|
|
109
|
-
# @param {params} Hash of the permitted attributes for a role
|
|
110
|
-
# @description Creates a new role
|
|
111
|
-
def create params
|
|
112
|
-
|
|
113
|
-
role = current_user.account.roles.new(params)
|
|
114
|
-
|
|
115
|
-
unless current_user.can_work_with_role?(role)
|
|
116
|
-
self.error(I18n.t("core.roles.messages_danger_creating_role_object_level_permission_too_high"))
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
# check if user can work with that object level permission
|
|
120
|
-
if role.object_level_permission.to_f >= current_user.roles.map(&:object_level_permission).max()
|
|
121
|
-
self.error(I18n.t("core.roles.messages_danger_creating_role_object_level_permission_too_high"))
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
# do not create if errors found
|
|
125
|
-
return self unless self.successful?
|
|
126
|
-
|
|
127
|
-
# Try to save role and logs if it went OK
|
|
128
|
-
if role.save
|
|
129
|
-
self.resource = role
|
|
130
|
-
#Role::Activity.log_create(current_user, self.resource)
|
|
131
|
-
else
|
|
132
|
-
self.error(role.errors.full_messages.to_sentence)
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
self
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
# @overwrite
|
|
139
|
-
# @return {Object}
|
|
140
|
-
# @param {params} Hash of the permitted attributes for a role
|
|
141
|
-
# @description Updates role's attributes and saves logs if it went without problem
|
|
142
|
-
def update params
|
|
143
|
-
old_attributes = self.resource.attributes
|
|
144
|
-
|
|
145
|
-
unless self.resource.update(params)
|
|
146
|
-
self.error(self.resource.errors.full_messages.to_sentence)
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
if self.successful?
|
|
150
|
-
new_attributes = self.resource.attributes
|
|
151
|
-
|
|
152
|
-
#LesliSecurity::Role::Activity.log_update(current_user, role, old_attributes, new_attributes)
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
self
|
|
8
|
+
def find(id)
|
|
9
|
+
super(current_user.account.roles.find_by(id: id))
|
|
156
10
|
end
|
|
157
11
|
|
|
158
|
-
#
|
|
159
|
-
|
|
160
|
-
# @description Deletes the role
|
|
161
|
-
def destroy
|
|
162
|
-
unless self.resource.destroy
|
|
163
|
-
self.error(self.resource.errors.full_messages.to_sentence)
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
if self.successful?
|
|
167
|
-
LesliSecurity::Role::Activity.log_destroy(current_user, self.resource)
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
self
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
def options
|
|
174
|
-
{
|
|
175
|
-
:object_level_permissions => self.roles_with_object_level_permission
|
|
176
|
-
}
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
private
|
|
180
|
-
|
|
181
|
-
def roles_with_object_level_permission
|
|
182
|
-
levels = {}
|
|
183
|
-
|
|
184
|
-
# get all the different object level permission registered in the roles
|
|
185
|
-
existing_levels = current_user.account.roles
|
|
186
|
-
.select(:object_level_permission)
|
|
187
|
-
.order(object_level_permission: :desc)
|
|
188
|
-
.distinct
|
|
189
|
-
.map { |level| level.object_level_permission }
|
|
190
|
-
|
|
191
|
-
# Build the next available object levels
|
|
192
|
-
# basically we need to add the possibles object level permissions between the
|
|
193
|
-
# existing ones
|
|
194
|
-
existing_levels.each_with_index do |level_current, i|
|
|
195
|
-
|
|
196
|
-
level_next = 0
|
|
197
|
-
|
|
198
|
-
# get the next OLP in the list of the existing roles
|
|
199
|
-
level_next = existing_levels.to_a[i+1] unless existing_levels.to_a[i+1].nil?
|
|
200
|
-
|
|
201
|
-
# calculate the new next level, basically we get the level right in the middle
|
|
202
|
-
# between the existing levels, example:
|
|
203
|
-
# 1000 existing level
|
|
204
|
-
# 750 new projected level
|
|
205
|
-
# 500 existing level
|
|
206
|
-
level_new = (level_current + level_next) / 2
|
|
207
|
-
|
|
208
|
-
# add the levels to the levels object
|
|
209
|
-
levels[level_current] = []
|
|
210
|
-
|
|
211
|
-
next if level_next == 0
|
|
212
|
-
|
|
213
|
-
levels[level_new] = []
|
|
214
|
-
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
# Get all the existing roles
|
|
12
|
+
# Return the roles available to the current user for selection.
|
|
13
|
+
def list
|
|
218
14
|
current_user.account.roles
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
# push the role grouping by the object level permission
|
|
223
|
-
levels[role.object_level_permission].push(role)
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
levels_sorted = []
|
|
227
|
-
|
|
228
|
-
levels.keys.each do |key|
|
|
229
|
-
levels_sorted.push({
|
|
230
|
-
level: key,
|
|
231
|
-
roles: levels[key]
|
|
232
|
-
})
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
levels_sorted
|
|
15
|
+
.where(permission_level: ..current_user.max_level_permission)
|
|
16
|
+
.order(permission_level: :desc, name: :asc)
|
|
17
|
+
.select(:id, :name)
|
|
236
18
|
end
|
|
237
19
|
end
|
|
238
20
|
end
|