tb_core 1.4.3.1 → 1.4.4
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/README.md +18 -54
- data/app/assets/javascripts/admin/core/application.js +8 -4
- data/app/assets/javascripts/admin/core/dashboard.js +2 -3
- data/app/assets/javascripts/admin/core/date_picker.js +2 -2
- data/app/assets/javascripts/admin/core/editor.js +2 -2
- data/app/assets/javascripts/admin/core/modal.js +1 -1
- data/app/assets/javascripts/admin/core/users.js +5 -5
- data/app/assets/javascripts/tb_core.js +8 -0
- data/app/assets/libs/sortable/sortable.js +152 -91
- data/app/controllers/admin/application_controller.rb +1 -2
- data/app/controllers/admin/dashboard_controller.rb +2 -2
- data/app/controllers/admin/settings_controller.rb +2 -2
- data/app/controllers/admin/users_controller.rb +4 -7
- data/app/controllers/concerns/tb_core/error_handling.rb +16 -7
- data/app/controllers/concerns/tb_core/user_authentication.rb +5 -5
- data/app/controllers/tb_core/application_controller.rb +1 -1
- data/app/controllers/user_sessions_controller.rb +2 -2
- data/app/helpers/tb_core/application_helper.rb +4 -36
- data/app/mailers/tb_core_mailer.rb +3 -3
- data/app/models/concerns/tb_core/user_model.rb +131 -0
- data/app/models/spud/spud_user_model.rb +6 -138
- data/app/models/spud_permission.rb +4 -4
- data/app/models/spud_role.rb +1 -1
- data/app/models/spud_user.rb +2 -1
- data/app/views/admin/dashboard/index.html.erb +1 -1
- data/app/views/admin/settings/_form.html.erb +1 -1
- data/app/views/admin/settings/edit.html.erb +1 -1
- data/app/views/admin/setup/new.html.erb +1 -1
- data/app/views/admin/users/_form.html.erb +1 -1
- data/app/views/admin/users/index.html.erb +2 -2
- data/app/views/admin/users/show.html.erb +1 -1
- data/app/views/layouts/admin/application.html.erb +6 -6
- data/app/views/layouts/admin/login.html.erb +1 -1
- data/app/views/tb_core_mailer/forgot_password_notification.html.erb +2 -2
- data/app/views/tb_core_mailer/user_credentials.html.erb +2 -2
- data/app/views/user_sessions/_form.html.erb +1 -1
- data/app/views/user_sessions/new.html.erb +1 -1
- data/lib/generators/spud/module_generator.rb +7 -7
- data/lib/generators/spud/setup_generator.rb +6 -7
- data/lib/generators/spud/templates/assets/admin/application.js +1 -1
- data/lib/generators/spud/templates/assets/application.js +3 -4
- data/lib/generators/spud/templates/controller_spec.rb.erb +5 -5
- data/lib/generators/spud/templates/views/admin/_form.html.erb +1 -1
- data/lib/generators/spud/templates/views/admin/index.html.erb +1 -1
- data/lib/generators/spud/templates/views/admin/show.html.erb +1 -1
- data/lib/generators/spud/templates/views/frontend/index.html.erb +1 -1
- data/lib/generators/spud/templates/views/frontend/show.html.erb +1 -1
- data/lib/tb_core.rb +3 -5
- data/lib/tb_core/belongs_to_app.rb +47 -46
- data/lib/tb_core/catch_all_route.rb +3 -0
- data/lib/tb_core/configuration.rb +28 -0
- data/lib/tb_core/engine.rb +61 -0
- data/lib/tb_core/errors.rb +50 -0
- data/lib/tb_core/responder.rb +0 -26
- data/lib/{spud_core → tb_core}/searchable.rb +4 -1
- data/lib/tb_core/spud_core.rb +23 -0
- data/lib/{spud_core → tb_core}/test_files.rb +0 -0
- data/lib/tb_core/test_helper.rb +3 -3
- data/lib/tb_core/version.rb +3 -0
- data/spec/controllers/admin/application_controller_spec.rb +5 -5
- data/spec/controllers/admin/dashboard_controller_spec.rb +5 -5
- data/spec/controllers/admin/password_reset_controller_spec.rb +1 -1
- data/spec/controllers/admin/settings_controller_spec.rb +1 -1
- data/spec/controllers/admin/setup_controller_spec.rb +2 -2
- data/spec/controllers/admin/user_sessions_controller_spec.rb +2 -2
- data/spec/controllers/admin/users_controller_spec.rb +17 -17
- data/spec/dummy/app/assets/javascripts/admin/application.js +1 -1
- data/spec/dummy/app/assets/javascripts/application.js +1 -1
- data/spec/dummy/config/application.rb +1 -1
- data/spec/factories/spud_admin_permission_factories.rb +2 -2
- data/spec/factories/spud_role_factories.rb +2 -2
- data/spec/factories/spud_user_factories.rb +6 -6
- data/spec/helpers/spud/admin/application_helper_spec.rb +1 -1
- data/spec/helpers/tb_core/application_helper_spec.rb +0 -35
- data/spec/lib/spud_core/configuration_spec.rb +2 -2
- data/spec/lib/tb_core/belongs_to_app_spec.rb +4 -3
- data/spec/models/spud_role_spec.rb +2 -2
- data/spec/models/spud_user_spec.rb +13 -13
- data/spec/rails_helper.rb +1 -1
- metadata +59 -67
- data/app/assets/javascripts/tb_core.js.erb +0 -9
- data/app/assets/javascripts/tb_core/configuration.js.erb +0 -9
- data/app/assets/javascripts/tb_core/console.js +0 -12
- data/app/assets/javascripts/tb_core/remote.js +0 -237
- data/app/assets/javascripts/tb_core/utility.js +0 -55
- data/app/controllers/concerns/tb_core/sortable_params.rb +0 -80
- data/app/controllers/spud/application_controller.rb +0 -10
- data/lib/generators/spud/templates/assets/bootstrap.js +0 -14
- data/lib/spud_core/catch_all_route.rb +0 -3
- data/lib/spud_core/configuration.rb +0 -56
- data/lib/spud_core/engine.rb +0 -76
- data/lib/spud_core/errors.rb +0 -47
- data/lib/spud_core/version.rb +0 -5
- data/lib/tb_core/table_header.rb +0 -92
- data/spec/controllers/tb_core/sortable_params_spec.rb +0 -64
|
@@ -5,7 +5,7 @@ class Admin::DashboardController < Admin::ApplicationController
|
|
|
5
5
|
|
|
6
6
|
def index
|
|
7
7
|
@setting = SpudUserSetting.find_by(spud_user_id: current_user.id, key: 'dash_icon_order')
|
|
8
|
-
apps =
|
|
8
|
+
apps = TbCore.admin_applications
|
|
9
9
|
if @setting.nil? == false
|
|
10
10
|
apps = arrange_apps(apps)
|
|
11
11
|
end
|
|
@@ -27,7 +27,7 @@ class Admin::DashboardController < Admin::ApplicationController
|
|
|
27
27
|
|
|
28
28
|
def badges
|
|
29
29
|
data = []
|
|
30
|
-
@admin_applications =
|
|
30
|
+
@admin_applications = TbCore.admin_applications.select do |admin_application|
|
|
31
31
|
if current_user.can_view_app?(admin_application)
|
|
32
32
|
if admin_application[:badge]
|
|
33
33
|
data << {key: admin_application[:key], badge_count: admin_application[:badge].call(current_user)}
|
|
@@ -4,7 +4,7 @@ class Admin::SettingsController < Admin::ApplicationController
|
|
|
4
4
|
skip_before_action :require_admin_user
|
|
5
5
|
|
|
6
6
|
before_action do |c|
|
|
7
|
-
if current_user
|
|
7
|
+
if current_user&.admin_rights?
|
|
8
8
|
add_breadcrumb 'Settings', :admin_settings_path
|
|
9
9
|
end
|
|
10
10
|
end
|
|
@@ -34,7 +34,7 @@ private
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def check_password
|
|
37
|
-
if user_params[:password].
|
|
37
|
+
if user_params[:password].blank?
|
|
38
38
|
return true
|
|
39
39
|
else
|
|
40
40
|
if !current_user.valid_password?(params[:current_password])
|
|
@@ -25,8 +25,8 @@ class Admin::UsersController < Admin::ApplicationController
|
|
|
25
25
|
|
|
26
26
|
def export
|
|
27
27
|
users = SpudUser.all
|
|
28
|
-
export_columns = %w
|
|
29
|
-
file_name =
|
|
28
|
+
export_columns = %w[created_at first_name last_name email last_login_at]
|
|
29
|
+
file_name = TbCore.site_name + ' Users Export.csv'
|
|
30
30
|
send_data users.as_csv(export_columns),
|
|
31
31
|
type: 'text/csv; charset=iso-8859-1; header=present',
|
|
32
32
|
disposition: "attachment; filename=#{file_name}"
|
|
@@ -83,10 +83,7 @@ class Admin::UsersController < Admin::ApplicationController
|
|
|
83
83
|
private
|
|
84
84
|
|
|
85
85
|
def load_user
|
|
86
|
-
@user = SpudUser.
|
|
87
|
-
if @user.blank?
|
|
88
|
-
raise Spud::NotFoundError.new('user')
|
|
89
|
-
end
|
|
86
|
+
@user = SpudUser.find_by!(id: params[:id])
|
|
90
87
|
end
|
|
91
88
|
|
|
92
89
|
def user_params
|
|
@@ -94,7 +91,7 @@ private
|
|
|
94
91
|
end
|
|
95
92
|
|
|
96
93
|
def send_credentials_email
|
|
97
|
-
if params[:send_email] && user_params[:password] &&
|
|
94
|
+
if params[:send_email] && user_params[:password] && @user.errors.none?
|
|
98
95
|
TbCoreMailer.user_credentials(@user, user_params[:password]).deliver_later
|
|
99
96
|
end
|
|
100
97
|
return true
|
|
@@ -3,7 +3,7 @@ module TbCore
|
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
4
|
|
|
5
5
|
included do
|
|
6
|
-
rescue_from
|
|
6
|
+
rescue_from RequestError, with: :handle_request_error
|
|
7
7
|
rescue_from ActiveRecord::RecordNotFound, with: :handle_record_not_found
|
|
8
8
|
rescue_from ActionController::UnknownFormat, with: :handle_unknown_format_error
|
|
9
9
|
end
|
|
@@ -12,7 +12,7 @@ module TbCore
|
|
|
12
12
|
error.request_url = request.original_url
|
|
13
13
|
error.template = template_for_request_error() if respond_to?(:template_for_request_error, true)
|
|
14
14
|
|
|
15
|
-
if error.is_a?(
|
|
15
|
+
if error.is_a?(UnauthorizedError) && request.format.html?
|
|
16
16
|
redirect_to(login_path_for_require_user)
|
|
17
17
|
return false
|
|
18
18
|
end
|
|
@@ -35,15 +35,24 @@ module TbCore
|
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
def handle_record_not_found(
|
|
39
|
-
error =
|
|
38
|
+
def handle_record_not_found(e)
|
|
39
|
+
error = NotFoundError.new(class_string(e.model))
|
|
40
40
|
handle_request_error(error)
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
def
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
def class_string(model_name)
|
|
44
|
+
string = 'record'
|
|
45
|
+
begin
|
|
46
|
+
object_class = Object.const_get(model_name)
|
|
47
|
+
string = object_class.model_name.human
|
|
48
|
+
rescue NameError # rubocop:disable Lint/HandleExceptions
|
|
49
|
+
end
|
|
50
|
+
string
|
|
46
51
|
end
|
|
47
52
|
|
|
53
|
+
def handle_unknown_format_error(_e)
|
|
54
|
+
error = NotFoundError.new()
|
|
55
|
+
handle_request_error(error)
|
|
56
|
+
end
|
|
48
57
|
end
|
|
49
58
|
end
|
|
@@ -15,7 +15,7 @@ module TbCore
|
|
|
15
15
|
|
|
16
16
|
def current_user
|
|
17
17
|
return @current_user if defined?(@current_user)
|
|
18
|
-
@current_user = current_user_session
|
|
18
|
+
@current_user = current_user_session&.spud_user
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def current_user_id
|
|
@@ -24,13 +24,13 @@ module TbCore
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def require_user
|
|
27
|
-
raise
|
|
27
|
+
raise UnauthorizedError.new unless current_user
|
|
28
28
|
true
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def require_admin_user
|
|
32
|
-
raise
|
|
33
|
-
raise
|
|
32
|
+
raise UnauthorizedError.new unless current_user
|
|
33
|
+
raise AccessDeniedError.new unless current_user.admin_rights?
|
|
34
34
|
true
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -43,7 +43,7 @@ module TbCore
|
|
|
43
43
|
|
|
44
44
|
def set_time_zone
|
|
45
45
|
old_time_zone = Time.zone
|
|
46
|
-
Time.zone = current_user.time_zone if current_user
|
|
46
|
+
Time.zone = current_user.time_zone if current_user&.time_zone.present?
|
|
47
47
|
yield
|
|
48
48
|
ensure
|
|
49
49
|
Time.zone = old_time_zone
|
|
@@ -14,7 +14,7 @@ class UserSessionsController < ApplicationController
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def create
|
|
17
|
-
@user_session = SpudUserSession.new(user_session_params
|
|
17
|
+
@user_session = SpudUserSession.new(user_session_params)
|
|
18
18
|
if @user_session.save()
|
|
19
19
|
respond_with @user_session do |format|
|
|
20
20
|
format.html{
|
|
@@ -35,7 +35,7 @@ class UserSessionsController < ApplicationController
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def destroy
|
|
38
|
-
current_user_session.destroy
|
|
38
|
+
current_user_session.destroy if current_user_session.present?
|
|
39
39
|
respond_with({}) do |format|
|
|
40
40
|
format.html{
|
|
41
41
|
flash[:notice] = 'Logout successful!'
|
|
@@ -58,49 +58,17 @@ module TbCore::ApplicationHelper
|
|
|
58
58
|
|
|
59
59
|
def tb_page_title
|
|
60
60
|
if content_for?(:title)
|
|
61
|
-
title = content_for(:title) + ' | ' +
|
|
61
|
+
title = content_for(:title) + ' | ' + TbCore.site_name
|
|
62
62
|
elsif @page_title
|
|
63
|
-
title = @page_title + ' | ' +
|
|
63
|
+
title = @page_title + ' | ' + TbCore.site_name
|
|
64
64
|
else
|
|
65
|
-
title =
|
|
65
|
+
title = TbCore.site_name
|
|
66
66
|
end
|
|
67
67
|
return content_tag :title, title
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
-
# Build a table header for a model
|
|
71
|
-
#
|
|
72
|
-
# * path_helper: The helper method you want to use to generate URLs.
|
|
73
|
-
# * model: The class we should use for translations (optional)
|
|
74
|
-
# * permit: Array of request params that are forwarded to the sort links
|
|
75
|
-
#
|
|
76
|
-
# Example:
|
|
77
|
-
#
|
|
78
|
-
# <%= tb_table_header :admin_users_path, model: SpudUser do |t| %>
|
|
79
|
-
# <%= t.sortable :name %>
|
|
80
|
-
# <%= t.sortable :email %>
|
|
81
|
-
# <%= t.header :last_login %>
|
|
82
|
-
# <th></th>
|
|
83
|
-
# <% end %>
|
|
84
|
-
#
|
|
85
|
-
# Header labels will be pulled from en.yml. To provide a different
|
|
86
|
-
# label pass the label: option
|
|
87
|
-
#
|
|
88
|
-
# Example:
|
|
89
|
-
#
|
|
90
|
-
# <%= t.header :name, label: 'Full Name' %>
|
|
91
|
-
#
|
|
92
|
-
def tb_table_header(path_helper, model: nil, permit: [], &block)
|
|
93
|
-
header = TbCore::TableHeader.new(
|
|
94
|
-
path_helper: path_helper,
|
|
95
|
-
model: model,
|
|
96
|
-
params: params.permit(permit.concat([:sort, :dir, :search, :tab])),
|
|
97
|
-
context: self)
|
|
98
|
-
header.capture(block) if block
|
|
99
|
-
header.to_html
|
|
100
|
-
end
|
|
101
|
-
|
|
102
70
|
def current_site_name
|
|
103
|
-
return
|
|
71
|
+
return TbCore.config.site_name
|
|
104
72
|
end
|
|
105
73
|
|
|
106
74
|
def cache_key_for_spud_collection(collection, key:'view', cache_params:[], for_user:false)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class TbCoreMailer < ActionMailer::Base
|
|
2
|
-
default from:
|
|
2
|
+
default from: TbCore.from_address
|
|
3
3
|
layout 'mailer'
|
|
4
4
|
|
|
5
5
|
def forgot_password_notification(user, url)
|
|
@@ -7,7 +7,7 @@ class TbCoreMailer < ActionMailer::Base
|
|
|
7
7
|
@url = url
|
|
8
8
|
mail(
|
|
9
9
|
to: to_address_for_user(user),
|
|
10
|
-
subject: default_i18n_subject(site_name:
|
|
10
|
+
subject: default_i18n_subject(site_name: TbCore.site_name)
|
|
11
11
|
)
|
|
12
12
|
end
|
|
13
13
|
|
|
@@ -16,7 +16,7 @@ class TbCoreMailer < ActionMailer::Base
|
|
|
16
16
|
@password = password
|
|
17
17
|
mail(
|
|
18
18
|
to: to_address_for_user(user),
|
|
19
|
-
subject: default_i18n_subject(site_name:
|
|
19
|
+
subject: default_i18n_subject(site_name: TbCore.site_name)
|
|
20
20
|
)
|
|
21
21
|
end
|
|
22
22
|
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
module TbCore
|
|
2
|
+
module UserModel
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
included do
|
|
6
|
+
self.table_name = 'spud_users'
|
|
7
|
+
|
|
8
|
+
acts_as_authentic do |c|
|
|
9
|
+
c.transition_from_crypto_providers = Authlogic::CryptoProviders::Sha512
|
|
10
|
+
c.crypto_provider = Authlogic::CryptoProviders::SCrypt
|
|
11
|
+
c.logged_in_timeout = 24.hours
|
|
12
|
+
c.login_field = :email if TbCore.config.use_email_as_login
|
|
13
|
+
if TbCore.config.user_password_length
|
|
14
|
+
c.merge_validates_length_of_password_field_options(minimum: TbCore.config.user_password_length)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
belongs_to :role,
|
|
19
|
+
class_name: SpudRole.to_s, foreign_key: :spud_role_id, required: false
|
|
20
|
+
has_many :spud_user_settings,
|
|
21
|
+
dependent: :destroy, foreign_key: :spud_user_id
|
|
22
|
+
|
|
23
|
+
validates :first_name, :last_name, presence: true
|
|
24
|
+
before_validation :set_login_to_email, if: -> { TbCore.config.use_email_as_login }
|
|
25
|
+
before_update :unset_requires_password_change
|
|
26
|
+
|
|
27
|
+
scope :admins, lambda {
|
|
28
|
+
where('super_admin = 1 OR role_id IS NOT NULL')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
scope :ordered, -> { order('last_name asc, first_name asc, email asc') }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
module ClassMethods
|
|
35
|
+
|
|
36
|
+
# Returns an ActiveRecord::Relation performing a LIKE query against name columns
|
|
37
|
+
def where_name_like(string)
|
|
38
|
+
full_name = Arel::Nodes::NamedFunction.new(
|
|
39
|
+
'concat',
|
|
40
|
+
[arel_table[:first_name], Arel::Nodes.build_quoted(' '), arel_table[:last_name]]
|
|
41
|
+
)
|
|
42
|
+
search = '%' + string + '%'
|
|
43
|
+
where(full_name.matches(search))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Return an array of users who have the requested permission
|
|
47
|
+
#
|
|
48
|
+
# * tag - Desired permission tag string (required)
|
|
49
|
+
# * include_supers - Whether to include super user (default: true)
|
|
50
|
+
def with_permission(tag, include_supers: true)
|
|
51
|
+
role_ids = SpudRolePermission.where(spud_permission_tag: tag).pluck(:spud_role_id).uniq()
|
|
52
|
+
if include_supers
|
|
53
|
+
where(super_admin: true).or(where(spud_role_id: role_ids))
|
|
54
|
+
else
|
|
55
|
+
where(spud_role_id: role_ids)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def as_csv(column_names)
|
|
60
|
+
CSV.generate do |csv|
|
|
61
|
+
csv << column_names
|
|
62
|
+
all.find_each do |item|
|
|
63
|
+
csv << item.attributes.values_at(*column_names)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def full_name
|
|
71
|
+
return login if first_name.blank? && last_name.blank?
|
|
72
|
+
[first_name, last_name].reject(&:blank?).join(' ')
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def full_name_with_email
|
|
76
|
+
"#{full_name} (#{email})"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Returns true if user can view at least one dashboard app
|
|
80
|
+
def admin_rights?
|
|
81
|
+
return true if super_admin
|
|
82
|
+
TbCore.admin_applications.find { |app| can_view_app?(app) }.present?
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Returns true if the user can view a spud app based on it's key
|
|
86
|
+
def can_view_app?(admin_application)
|
|
87
|
+
return true if super_admin?
|
|
88
|
+
key = admin_application[:key]
|
|
89
|
+
permissions.find { |p| p.apps.include?(key) }.present?
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Check if a user has a given list of permissions
|
|
93
|
+
#
|
|
94
|
+
# * if one tag is supplied, return true if the tag matches
|
|
95
|
+
# * if multiple tags are supplied, return true if ALL tags match
|
|
96
|
+
def permission?(*tags)
|
|
97
|
+
return true if super_admin?
|
|
98
|
+
my_tags = permissions.collect(&:tag)
|
|
99
|
+
tags.find { |tag| !my_tags.include?(tag) }.blank?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Check if a user has at least one out of a given list of permissions
|
|
103
|
+
#
|
|
104
|
+
# * if one tag is supplied, return true if the tag matches
|
|
105
|
+
# * if multiple tags are supplied, return true if ANY tag matches
|
|
106
|
+
def any_permission?(*tags)
|
|
107
|
+
return true if super_admin?
|
|
108
|
+
permissions.find { |p| tags.include?(p.tag) }.present?
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Return a list of SpudPermission objects for the user's SpudRole
|
|
112
|
+
def permissions
|
|
113
|
+
return [] if role.blank?
|
|
114
|
+
role.permissions
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
private
|
|
118
|
+
|
|
119
|
+
def set_login_to_email
|
|
120
|
+
self.login = email if email.present?
|
|
121
|
+
true
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def unset_requires_password_change
|
|
125
|
+
if password_changed? && !requires_password_change_changed?(to: true)
|
|
126
|
+
self.requires_password_change = false
|
|
127
|
+
end
|
|
128
|
+
true
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -1,144 +1,12 @@
|
|
|
1
1
|
class Spud::SpudUserModel < ActiveRecord::Base
|
|
2
|
-
self.table_name = 'spud_users'
|
|
3
2
|
self.abstract_class = true
|
|
3
|
+
include TbCore::UserModel
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if Spud::Core.config.use_email_as_login
|
|
10
|
-
c.login_field = :email
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
belongs_to :role, class_name: 'SpudRole', foreign_key: 'spud_role_id'
|
|
15
|
-
has_many :spud_user_settings
|
|
16
|
-
|
|
17
|
-
validates :first_name, :last_name, presence: true
|
|
18
|
-
before_validation :set_login_to_email, if: ->{ Spud::Core.config.use_email_as_login }
|
|
19
|
-
before_update :unset_requires_password_change
|
|
20
|
-
|
|
21
|
-
scope :admins, ->{
|
|
22
|
-
where('super_admin = 1 OR role_id IS NOT NULL')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
scope :ordered, ->{ order('last_name asc, first_name asc, email asc') }
|
|
26
|
-
|
|
27
|
-
def full_name
|
|
28
|
-
if first_name.blank? && last_name.blank?
|
|
29
|
-
return self.login
|
|
30
|
-
end
|
|
31
|
-
if self.first_name.blank?
|
|
32
|
-
return self.last_name
|
|
33
|
-
elsif self.last_name.blank?
|
|
34
|
-
return self.first_name
|
|
35
|
-
end
|
|
36
|
-
return "#{self.first_name} #{self.last_name}"
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def full_name_with_email
|
|
40
|
-
return "#{full_name} (#{email})"
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# Returns true if user can view at least one dashboard app
|
|
44
|
-
def has_admin_rights?
|
|
45
|
-
if self.super_admin?
|
|
46
|
-
return true
|
|
47
|
-
else
|
|
48
|
-
return Spud::Core.admin_applications.find{ |app| self.can_view_app?(app) }.present?
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Returns true if the user can view a spud app based on it's key
|
|
53
|
-
def can_view_app?(admin_application)
|
|
54
|
-
if self.super_admin?
|
|
55
|
-
return true
|
|
56
|
-
else
|
|
57
|
-
key = admin_application[:key]
|
|
58
|
-
return self.permissions.find{ |p| p.apps.include?(key) }.present?
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Check if a user has a given list of permissions
|
|
63
|
-
#
|
|
64
|
-
# * if one tag is supplied, return true if the tag matches
|
|
65
|
-
# * if multiple tags are supplied, return true if ALL tags match
|
|
66
|
-
def has_permission?(*tags)
|
|
67
|
-
if self.super_admin?
|
|
68
|
-
return true
|
|
69
|
-
else
|
|
70
|
-
my_tags = self.permissions.collect(&:tag)
|
|
71
|
-
return tags.find{ |tag| !my_tags.include?(tag) }.blank?
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
# Check if a user has at least one out of a given list of permissions
|
|
76
|
-
#
|
|
77
|
-
# * if one tag is supplied, return true if the tag matches
|
|
78
|
-
# * if multiple tags are supplied, return true if ANY tag matches
|
|
79
|
-
def has_any_permission?(*tags)
|
|
80
|
-
if self.super_admin?
|
|
81
|
-
return true
|
|
82
|
-
else
|
|
83
|
-
return self.permissions.find{ |p| tags.include?(p.tag) }.present?
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
# Return a list of SpudPermission objects for the user's SpudRole
|
|
88
|
-
def permissions
|
|
89
|
-
if !self.role
|
|
90
|
-
return []
|
|
91
|
-
else
|
|
92
|
-
return self.role.permissions
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
# Returns an ActiveRecord::Relation performing a LIKE query against name columns
|
|
97
|
-
def self.where_name_like(string)
|
|
98
|
-
full_name = Arel::Nodes::NamedFunction.new(
|
|
99
|
-
'concat',
|
|
100
|
-
[arel_table[:first_name], Arel::Nodes.build_quoted(' '), arel_table[:last_name]]
|
|
5
|
+
def initialize(*args)
|
|
6
|
+
ActiveSupport::Deprecation.warn(
|
|
7
|
+
'Spud::SpudUserModel has been replaced by TbCore::UserModel. See the README for details.',
|
|
8
|
+
caller
|
|
101
9
|
)
|
|
102
|
-
|
|
103
|
-
where(full_name.matches(search))
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
# Return an array of users who have the requested permission
|
|
107
|
-
#
|
|
108
|
-
# * tag - Desired permission tag string (required)
|
|
109
|
-
# * include_supers - Whether to include super user (default: true)
|
|
110
|
-
def self.with_permission(tag, include_supers: true)
|
|
111
|
-
role_ids = SpudRolePermission.where(spud_permission_tag: tag).pluck(:spud_role_id).uniq()
|
|
112
|
-
if include_supers
|
|
113
|
-
where(super_admin: true).or(where(spud_role_id: role_ids))
|
|
114
|
-
else
|
|
115
|
-
where(spud_role_id: role_ids)
|
|
116
|
-
end
|
|
10
|
+
super
|
|
117
11
|
end
|
|
118
|
-
|
|
119
|
-
def self.as_csv(column_names, options = {})
|
|
120
|
-
CSV.generate do |csv|
|
|
121
|
-
csv << column_names
|
|
122
|
-
all.each do |item|
|
|
123
|
-
csv << item.attributes.values_at(*column_names)
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
private
|
|
129
|
-
|
|
130
|
-
def set_login_to_email
|
|
131
|
-
if self.email.present?
|
|
132
|
-
self.login = self.email
|
|
133
|
-
end
|
|
134
|
-
return true
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def unset_requires_password_change
|
|
138
|
-
if password_changed? && !requires_password_change_changed?(to: true)
|
|
139
|
-
self.requires_password_change = false
|
|
140
|
-
end
|
|
141
|
-
return true
|
|
142
|
-
end
|
|
143
|
-
|
|
144
12
|
end
|