usman 0.1.5dev4 → 0.1.5
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/usman/admin/dashboard_controller.rb +1 -1
- data/app/controllers/usman/admin/features_controller.rb +0 -2
- data/app/controllers/usman/admin/permissions_controller.rb +15 -8
- data/app/controllers/usman/admin/roles_controller.rb +2 -3
- data/app/controllers/usman/admin/users_controller.rb +2 -4
- data/app/helpers/usman/authentication_helper.rb +9 -20
- data/app/models/feature.rb +37 -37
- data/app/models/permission.rb +8 -3
- data/app/models/role.rb +6 -3
- data/app/models/user.rb +79 -70
- data/app/models/usman/application_record.rb +0 -3
- data/app/views/layouts/kuppayam/_sidebar.html.erb +7 -8
- data/app/views/usman/admin/features/_action_buttons.html.erb +0 -0
- data/app/views/usman/admin/features/_form.html.erb +2 -5
- data/app/views/usman/admin/features/_index.html.erb +1 -4
- data/app/views/usman/admin/features/index.html.erb +3 -20
- data/app/views/usman/admin/features/temp/create.js.erb +29 -0
- data/app/views/usman/admin/features/temp/destroy.js.erb +16 -0
- data/app/views/usman/admin/features/temp/edit.js.erb +7 -0
- data/app/views/usman/admin/features/temp/index.js.erb +8 -0
- data/app/views/usman/admin/features/temp/new.js.erb +7 -0
- data/app/views/usman/admin/features/temp/row.js.erb +10 -0
- data/app/views/usman/admin/features/temp/show.js.erb +8 -0
- data/app/views/usman/admin/features/temp/update.js.erb +16 -0
- data/app/views/usman/admin/permissions/_show.html.erb +10 -2
- data/app/views/usman/admin/permissions/index.html.erb +3 -20
- data/app/views/usman/admin/permissions/temp/create.js.erb +29 -0
- data/app/views/usman/admin/permissions/temp/destroy.js.erb +22 -0
- data/app/views/usman/admin/permissions/temp/edit.js.erb +4 -0
- data/app/views/usman/admin/permissions/temp/index.js.erb +14 -0
- data/app/views/usman/admin/permissions/temp/new.js.erb +4 -0
- data/app/views/usman/admin/permissions/temp/row.js.erb +24 -0
- data/app/views/usman/admin/permissions/temp/show.js.erb +13 -0
- data/app/views/usman/admin/permissions/temp/update.js.erb +29 -0
- data/app/views/usman/admin/roles/_form.html.erb +1 -1
- data/app/views/usman/admin/roles/_index.html.erb +3 -3
- data/app/views/usman/admin/roles/_row.html.erb +2 -2
- data/app/views/usman/admin/roles/_show.html.erb +7 -41
- data/app/views/usman/admin/roles/index.html.erb +3 -22
- data/app/views/usman/admin/users/_index.html.erb +16 -22
- data/app/views/usman/admin/users/_row.html.erb +4 -4
- data/app/views/usman/admin/users/_show.html.erb +1 -1
- data/app/views/usman/admin/users/index.html.erb +2 -4
- data/app/views/usman/sessions/_form.html.erb +7 -19
- data/config/locales/kuppayam/authentication.en.yml +1 -1
- data/config/routes.rb +1 -3
- data/db/import_data/dummy/images/users/guna.neweast.png +0 -0
- data/db/import_data/dummy/images/users/junaid.ramzan.jpg +0 -0
- data/db/import_data/dummy/images/users/kpvarma.png +0 -0
- data/db/import_data/dummy/images/users/stephen.price.png +0 -0
- data/db/import_data/dummy/images/users/vinodh.jpg +0 -0
- data/db/import_data/dummy/users.csv +3 -3
- data/db/import_data/features.csv +10 -0
- data/db/import_data/images/users/junaid.ramzan.jpg +0 -0
- data/db/import_data/images/users/kpvarma.png +0 -0
- data/db/import_data/images/users/vinodh.jpg +0 -0
- data/db/import_data/permissions.csv +30 -0
- data/db/migrate/20170000000100_create_users.rb +1 -1
- data/db/migrate/20170000000101_create_features.rb +1 -1
- data/db/migrate/20170000000102_create_roles.rb +1 -1
- data/lib/tasks/usman/all.rake +10 -34
- data/lib/{temp → tasks/usman}/features.rake +6 -3
- data/lib/{temp → tasks/usman}/permissions.rake +5 -3
- data/lib/{temp → tasks/usman}/users.rake +5 -3
- data/lib/usman/engine.rb +2 -0
- data/lib/usman/extras/import_error_handler.rb +79 -0
- data/lib/usman/version.rb +1 -1
- metadata +37 -17
- data/app/controllers/usman/admin/user_roles_controller.rb +0 -142
- data/app/views/usman/admin/user_roles/_form.html.erb +0 -24
- data/app/views/usman/admin/user_roles/_index.html.erb +0 -59
- data/app/views/usman/admin/user_roles/_row.html.erb +0 -30
- data/app/views/usman/admin/user_roles/_show.html.erb +0 -74
- data/db/import_data/dummy/roles.csv +0 -9
- data/db/import_data/roles.csv +0 -5
- data/lib/temp/roles.rake +0 -24
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a630b45bb4de548d1b215cce4650ee42985682a1
|
|
4
|
+
data.tar.gz: b634c8f8410fc48d28e622f823efdfb546f86b15
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 190ac35f562e78b71ed702b800c543a307155fd558d0014b5ad38480335d799f305e0fdd12a35b3bcac7c03160222b0c590226c2859eea8af9ac69a1c7169bbc
|
|
7
|
+
data.tar.gz: e018c0a66b0b40fe71e42d9251cfa3017aef88584460b39b5ba9f61886d4f1f1ad57f91ce1205c8da83bedc12e9dfa585e39b8a38d17a992d2316d4cfc34e047
|
|
@@ -2,20 +2,29 @@ module Usman
|
|
|
2
2
|
module Admin
|
|
3
3
|
class PermissionsController < ResourceController
|
|
4
4
|
|
|
5
|
+
def index
|
|
6
|
+
@heading = "Manage Permissions"
|
|
7
|
+
@description = "Listing all permissions"
|
|
8
|
+
@links = [{name: "Dashboard", link: admin_dashboard_path, icon: 'fa-home'},
|
|
9
|
+
{name: "Manage Permissions", link: admin_permissions_path, icon: 'fa-user', active: true}]
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
5
13
|
def create
|
|
6
|
-
@permission =
|
|
14
|
+
@permission = Permission.where(" user_id = ? AND feature_id = ? ", permitted_params[:user_id], permitted_params[:feature_id]).first || Permission.new
|
|
7
15
|
@permission.assign_attributes(permitted_params)
|
|
8
|
-
save_resource
|
|
16
|
+
save_resource(@permission)
|
|
17
|
+
|
|
9
18
|
get_collections
|
|
10
19
|
end
|
|
11
20
|
|
|
12
21
|
def update
|
|
13
|
-
@permission =
|
|
22
|
+
@permission = Permission.find_by_id(params[:id])
|
|
14
23
|
# The form will not submit can_create 0 if it is not selected
|
|
15
24
|
# hence making it false by default and letting it update by itself.
|
|
16
25
|
@permission.assign_attributes({"can_create": "0", "can_read": "0", "can_update": "0", "can_delete": "0"})
|
|
17
26
|
@permission.assign_attributes(permitted_params)
|
|
18
|
-
save_resource
|
|
27
|
+
save_resource(@permission)
|
|
19
28
|
get_collections
|
|
20
29
|
end
|
|
21
30
|
|
|
@@ -27,7 +36,7 @@ module Usman
|
|
|
27
36
|
parse_filters
|
|
28
37
|
apply_filters
|
|
29
38
|
|
|
30
|
-
@permissions = @
|
|
39
|
+
@permissions = @relation.includes(:user, :feature).page(@current_page).per(@per_page)
|
|
31
40
|
|
|
32
41
|
return true
|
|
33
42
|
end
|
|
@@ -56,9 +65,7 @@ module Usman
|
|
|
56
65
|
|
|
57
66
|
def resource_controller_configuration
|
|
58
67
|
{
|
|
59
|
-
|
|
60
|
-
js_view_path: "/kuppayam/workflows/parrot",
|
|
61
|
-
view_path: "/usman/admin/permissions"
|
|
68
|
+
view_path: "/demo/permissions"
|
|
62
69
|
}
|
|
63
70
|
end
|
|
64
71
|
|
|
@@ -18,7 +18,7 @@ module Usman
|
|
|
18
18
|
def apply_filters
|
|
19
19
|
@relation = @relation.search(@query) if @query
|
|
20
20
|
|
|
21
|
-
@order_by = "
|
|
21
|
+
@order_by = "created_at desc" unless @order_by
|
|
22
22
|
@relation = @relation.order(@order_by)
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -39,8 +39,7 @@ module Usman
|
|
|
39
39
|
|
|
40
40
|
def resource_controller_configuration
|
|
41
41
|
{
|
|
42
|
-
|
|
43
|
-
js_view_path: "/kuppayam/workflows/peacock",
|
|
42
|
+
js_view_path: "/kuppayam/workflows/parrot",
|
|
44
43
|
view_path: "/usman/admin/roles"
|
|
45
44
|
}
|
|
46
45
|
end
|
|
@@ -59,7 +59,7 @@ module Usman
|
|
|
59
59
|
|
|
60
60
|
# Normal users should not be able to view super admins
|
|
61
61
|
# He should not be seeing admins even while searching
|
|
62
|
-
if @current_user.
|
|
62
|
+
if @current_user.is_super_admin?
|
|
63
63
|
@relation = @relation.where("super_admin IS #{@super_admin.to_s.upcase}") if @super_admin.nil? == false && @query.nil?
|
|
64
64
|
else
|
|
65
65
|
@relation = @relation.where("super_admin IS FALSE")
|
|
@@ -104,9 +104,7 @@ module Usman
|
|
|
104
104
|
|
|
105
105
|
def resource_controller_configuration
|
|
106
106
|
{
|
|
107
|
-
|
|
108
|
-
js_view_path: "/kuppayam/workflows/peacock",
|
|
109
|
-
view_path: "/usman/admin/users"
|
|
107
|
+
view_path: "usman/admin/users"
|
|
110
108
|
}
|
|
111
109
|
end
|
|
112
110
|
|
|
@@ -10,14 +10,7 @@ module Usman
|
|
|
10
10
|
|
|
11
11
|
# Returns the default URL to which the system should redirect the user after successful authentication
|
|
12
12
|
def default_redirect_url_after_sign_in
|
|
13
|
-
|
|
14
|
-
# if @current_user.has_role?("Admin")
|
|
15
|
-
# main_app.admin_dashboard_url
|
|
16
|
-
# elsif @current_user.has_role?("Admin")
|
|
17
|
-
# main_app.store_dashboard_url
|
|
18
|
-
# else
|
|
19
|
-
# main_app.admin_dashboard_url
|
|
20
|
-
# end
|
|
13
|
+
usman.admin_dashboard_url
|
|
21
14
|
end
|
|
22
15
|
|
|
23
16
|
# Returns the default URL to which the system should redirect the user after an unsuccessful attempt to authorise a resource/page
|
|
@@ -29,11 +22,11 @@ module Usman
|
|
|
29
22
|
# This method should also handle the redirection if it has come through a client appliction for authentication
|
|
30
23
|
# In that case, it should persist the params passed by the client application
|
|
31
24
|
def redirect_after_unsuccessful_authentication
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
redirect_to add_query_params(default_sign_in_url,
|
|
25
|
+
params_hsh = {}
|
|
26
|
+
params_hsh[:client_app] = params[:client_app] if params[:client_app]
|
|
27
|
+
params_hsh[:redirect_back_url] = params[:redirect_back_url] if params[:redirect_back_url]
|
|
28
|
+
params_hsh[:requested_url] = request.original_url if request.get?
|
|
29
|
+
redirect_to add_query_params(default_sign_in_url, params_hsh)
|
|
37
30
|
return
|
|
38
31
|
end
|
|
39
32
|
|
|
@@ -56,11 +49,6 @@ module Usman
|
|
|
56
49
|
redirect_after_unsuccessful_authentication
|
|
57
50
|
}
|
|
58
51
|
format.js {
|
|
59
|
-
@params_hsh = {}
|
|
60
|
-
@params_hsh[:client_app] = params[:client_app] if params[:client_app]
|
|
61
|
-
@params_hsh[:redirect_back_url] = params[:redirect_back_url] if params[:redirect_back_url]
|
|
62
|
-
@params_hsh[:requested_url] = request.original_url if request.get?
|
|
63
|
-
|
|
64
52
|
render(:partial => 'usman/sessions/sign_in.js.erb', :handlers => [:erb], :formats => [:js])
|
|
65
53
|
}
|
|
66
54
|
end
|
|
@@ -89,7 +77,7 @@ module Usman
|
|
|
89
77
|
redirect_or_popup_to_default_sign_in_page
|
|
90
78
|
return
|
|
91
79
|
else
|
|
92
|
-
@current_user.update_token
|
|
80
|
+
@current_user.update_token if @current_user.token_about_to_expire?
|
|
93
81
|
end
|
|
94
82
|
else
|
|
95
83
|
text = "#{I18n.t("authentication.permission_denied.heading")}: #{I18n.t("authentication.permission_denied.message")}"
|
|
@@ -102,7 +90,7 @@ module Usman
|
|
|
102
90
|
|
|
103
91
|
# This method is usually used as a before filter from admin controllers to ensure that the logged in user is a super admin
|
|
104
92
|
def require_super_admin
|
|
105
|
-
unless @current_user.
|
|
93
|
+
unless @current_user.is_super_admin?
|
|
106
94
|
text = "#{I18n.t("authentication.permission_denied.heading")}: #{I18n.t("authentication.permission_denied.message")}"
|
|
107
95
|
set_flash_message(text, :error, false) if defined?(flash) && flash
|
|
108
96
|
|
|
@@ -130,6 +118,7 @@ module Usman
|
|
|
130
118
|
session[:last_user_id] = current_user.id if current_user
|
|
131
119
|
user.start_session
|
|
132
120
|
session[:id] = user.id
|
|
121
|
+
default_redirect_url_after_sign_in
|
|
133
122
|
redirect_to default_redirect_url_after_sign_in
|
|
134
123
|
end
|
|
135
124
|
|
data/app/models/feature.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
class Feature < Usman::ApplicationRecord
|
|
2
2
|
|
|
3
|
+
require 'import_error_handler.rb'
|
|
4
|
+
extend Usman::ImportErrorHandler
|
|
5
|
+
|
|
3
6
|
# Constants
|
|
4
7
|
UNPUBLISHED = "unpublished"
|
|
5
8
|
PUBLISHED = "published"
|
|
@@ -23,7 +26,7 @@ class Feature < Usman::ApplicationRecord
|
|
|
23
26
|
has_one :feature_image, :as => :imageable, :dependent => :destroy, :class_name => "Image::FeatureImage"
|
|
24
27
|
|
|
25
28
|
# Validations
|
|
26
|
-
validates :name, presence: true
|
|
29
|
+
validates :name, presence: true
|
|
27
30
|
validates :status, :presence => true, :inclusion => {:in => STATUS.keys, :presence_of => :status, :message => "%{value} is not a valid status" }
|
|
28
31
|
|
|
29
32
|
# ------------------
|
|
@@ -44,7 +47,9 @@ class Feature < Usman::ApplicationRecord
|
|
|
44
47
|
scope :published, -> { where(status: PUBLISHED) }
|
|
45
48
|
scope :disabled, -> { where(status: DISABLED) }
|
|
46
49
|
|
|
47
|
-
def self.save_row_data(row)
|
|
50
|
+
def self.save_row_data(row, base_path)
|
|
51
|
+
|
|
52
|
+
image_base_path = base_path + "images/"
|
|
48
53
|
|
|
49
54
|
row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
|
|
50
55
|
|
|
@@ -55,24 +60,45 @@ class Feature < Usman::ApplicationRecord
|
|
|
55
60
|
feature.status = Feature::UNPUBLISHED
|
|
56
61
|
|
|
57
62
|
# Initializing error hash for displaying all errors altogether
|
|
58
|
-
error_object =
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
error_object = Usman::ErrorHash.new
|
|
64
|
+
|
|
65
|
+
## Adding a profile picture
|
|
66
|
+
begin
|
|
67
|
+
image_path = image_base_path + "features/#{feature.name.parameterize}.png"
|
|
68
|
+
image_path = image_base_path + "features/#{feature.name.parameterize}}.jpg" unless File.exists?(image_path)
|
|
69
|
+
if File.exists?(image_path)
|
|
70
|
+
feature.build_feature_image
|
|
71
|
+
feature.feature_image.image = File.open(image_path)
|
|
72
|
+
else
|
|
73
|
+
summary = "Feature Image not found for feature: #{feature.name}"
|
|
74
|
+
details = "#{image_path}/png doesn't exists"
|
|
75
|
+
error_object.warnings << { summary: summary, details: details }
|
|
76
|
+
end
|
|
77
|
+
rescue => e
|
|
78
|
+
summary = "Error during processing: #{$!}"
|
|
79
|
+
details = "Feature: #{feature.name}, Image Path: #{image_path}"
|
|
80
|
+
stack_trace = "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
|
|
81
|
+
error_object.errors << { summary: summary, details: details, stack_trace: stack_trace }
|
|
82
|
+
end if feature.feature_image.blank?
|
|
83
|
+
|
|
84
|
+
if feature.valid? && (feature.feature_image.blank? || feature.feature_image.valid?)
|
|
61
85
|
feature.save!
|
|
62
86
|
else
|
|
63
87
|
summary = "Error while saving feature: #{feature.name}"
|
|
64
88
|
details = "Error! #{feature.errors.full_messages.to_sentence}"
|
|
89
|
+
details << ", #{feature.feature_image.errors.full_messages.to_sentence}" if feature.feature_image
|
|
65
90
|
error_object.errors << { summary: summary, details: details }
|
|
66
91
|
end
|
|
67
92
|
return error_object
|
|
68
93
|
end
|
|
69
94
|
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
95
|
+
# * Return full name
|
|
96
|
+
# == Examples
|
|
97
|
+
# >>> feature.display_name
|
|
98
|
+
# => "Products"
|
|
99
|
+
def display_name
|
|
100
|
+
"#{name}"
|
|
101
|
+
end
|
|
76
102
|
|
|
77
103
|
# * Return true if the user is not published, else false.
|
|
78
104
|
# == Examples
|
|
@@ -125,34 +151,8 @@ class Feature < Usman::ApplicationRecord
|
|
|
125
151
|
self.update_attribute(:status, DISABLED)
|
|
126
152
|
end
|
|
127
153
|
|
|
128
|
-
# Permission Methods
|
|
129
|
-
# ------------------
|
|
130
|
-
|
|
131
154
|
def can_be_destroyed?
|
|
132
155
|
return true
|
|
133
156
|
end
|
|
134
|
-
|
|
135
|
-
def can_be_published?
|
|
136
|
-
unpublished? or disabled?
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def can_be_unpublished?
|
|
140
|
-
published? or disabled?
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
def can_be_removed?
|
|
144
|
-
published? or unpublished? or disabled?
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
# Other Methods
|
|
148
|
-
# -------------
|
|
149
|
-
|
|
150
|
-
# * Return full name
|
|
151
|
-
# == Examples
|
|
152
|
-
# >>> feature.display_name
|
|
153
|
-
# => "Products"
|
|
154
|
-
def display_name
|
|
155
|
-
"#{name}"
|
|
156
|
-
end
|
|
157
157
|
|
|
158
158
|
end
|
data/app/models/permission.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
class Permission < Usman::ApplicationRecord
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require 'import_error_handler.rb'
|
|
4
|
+
extend Usman::ImportErrorHandler
|
|
5
|
+
|
|
6
|
+
# Associations
|
|
4
7
|
belongs_to :user
|
|
5
8
|
belongs_to :feature
|
|
6
9
|
|
|
@@ -27,14 +30,16 @@ class Permission < Usman::ApplicationRecord
|
|
|
27
30
|
LOWER(f.name) LIKE LOWER('%#{query}%')")}
|
|
28
31
|
|
|
29
32
|
|
|
30
|
-
def self.save_row_data(row)
|
|
33
|
+
def self.save_row_data(row, base_path)
|
|
34
|
+
|
|
35
|
+
image_base_path = base_path + "images/"
|
|
31
36
|
|
|
32
37
|
row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
|
|
33
38
|
|
|
34
39
|
return if row[:user].blank? || row[:feature].blank?
|
|
35
40
|
|
|
36
41
|
# Initializing error hash for displaying all errors altogether
|
|
37
|
-
error_object =
|
|
42
|
+
error_object = Usman::ErrorHash.new
|
|
38
43
|
|
|
39
44
|
user = User.find_by_username(row[:user])
|
|
40
45
|
unless user
|
data/app/models/role.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
class Role < Usman::ApplicationRecord
|
|
2
2
|
|
|
3
|
+
require 'import_error_handler.rb'
|
|
4
|
+
extend Usman::ImportErrorHandler
|
|
5
|
+
|
|
3
6
|
# Associations
|
|
4
7
|
has_and_belongs_to_many :users
|
|
5
8
|
|
|
@@ -15,10 +18,10 @@ class Role < Usman::ApplicationRecord
|
|
|
15
18
|
# == Examples
|
|
16
19
|
# >>> role.search(query)
|
|
17
20
|
# => ActiveRecord::Relation object
|
|
18
|
-
scope :search, lambda {|query| where("LOWER(
|
|
21
|
+
scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%')")
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
def self.save_row_data(row)
|
|
24
|
+
def self.save_row_data(row, base_path)
|
|
22
25
|
|
|
23
26
|
row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
|
|
24
27
|
|
|
@@ -28,7 +31,7 @@ class Role < Usman::ApplicationRecord
|
|
|
28
31
|
role.name = row[:name]
|
|
29
32
|
|
|
30
33
|
# Initializing error hash for displaying all errors altogether
|
|
31
|
-
error_object =
|
|
34
|
+
error_object = Usman::ErrorHash.new
|
|
32
35
|
|
|
33
36
|
if role.valid?
|
|
34
37
|
role.save!
|
data/app/models/user.rb
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
class User < Usman::ApplicationRecord
|
|
2
2
|
|
|
3
|
+
require 'import_error_handler.rb'
|
|
4
|
+
extend Usman::ImportErrorHandler
|
|
5
|
+
extend KuppayamValidators
|
|
6
|
+
|
|
3
7
|
# including Password Methods
|
|
4
8
|
has_secure_password
|
|
5
9
|
|
|
@@ -22,10 +26,10 @@ class User < Usman::ApplicationRecord
|
|
|
22
26
|
|
|
23
27
|
EXCLUDED_JSON_ATTRIBUTES = [:confirmation_token, :password_digest, :reset_password_token, :unlock_token, :status, :reset_password_sent_at, :remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :failed_attempts, :locked_at, :created_at, :updated_at]
|
|
24
28
|
DEFAULT_PASSWORD = "Password@1"
|
|
25
|
-
SESSION_TIME_OUT =
|
|
29
|
+
SESSION_TIME_OUT = 30.minutes
|
|
26
30
|
|
|
27
31
|
# Validations
|
|
28
|
-
validates :name, presence: true
|
|
32
|
+
validates :name, presence: true
|
|
29
33
|
validate_username :username
|
|
30
34
|
validate_email :email
|
|
31
35
|
validate_password :password, condition_method: :should_validate_password?
|
|
@@ -39,7 +43,7 @@ class User < Usman::ApplicationRecord
|
|
|
39
43
|
has_one :profile_picture, :as => :imageable, :dependent => :destroy, :class_name => "Image::ProfilePicture"
|
|
40
44
|
has_many :permissions
|
|
41
45
|
has_many :features, through: :permissions
|
|
42
|
-
has_and_belongs_to_many :
|
|
46
|
+
has_and_belongs_to_many :users
|
|
43
47
|
|
|
44
48
|
|
|
45
49
|
# ------------------
|
|
@@ -55,10 +59,10 @@ class User < Usman::ApplicationRecord
|
|
|
55
59
|
# == Examples
|
|
56
60
|
# >>> user.search(query)
|
|
57
61
|
# => ActiveRecord::Relation object
|
|
58
|
-
scope :search, lambda {|query| where("LOWER(
|
|
59
|
-
LOWER(
|
|
60
|
-
LOWER(
|
|
61
|
-
LOWER(
|
|
62
|
+
scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%') OR\
|
|
63
|
+
LOWER(username) LIKE LOWER('%#{query}%') OR\
|
|
64
|
+
LOWER(email) LIKE LOWER('%#{query}%') OR\
|
|
65
|
+
LOWER(designation) LIKE LOWER('%#{query}%')")
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
scope :status, lambda { |status| where("LOWER(status)='#{status}'") }
|
|
@@ -66,11 +70,10 @@ class User < Usman::ApplicationRecord
|
|
|
66
70
|
scope :pending, -> { where(status: PENDING) }
|
|
67
71
|
scope :approved, -> { where(status: APPROVED) }
|
|
68
72
|
scope :suspended, -> { where(status: SUSPENDED) }
|
|
69
|
-
|
|
70
|
-
scope :super_admins, -> { where(super_admin: TRUE) }
|
|
71
|
-
scope :normal_users, -> { where(super_admin: FALSE) }
|
|
72
73
|
|
|
73
|
-
def self.save_row_data(row)
|
|
74
|
+
def self.save_row_data(row, base_path)
|
|
75
|
+
|
|
76
|
+
image_base_path = base_path + "images/"
|
|
74
77
|
|
|
75
78
|
row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
|
|
76
79
|
|
|
@@ -89,7 +92,7 @@ class User < Usman::ApplicationRecord
|
|
|
89
92
|
user.assign_default_password
|
|
90
93
|
|
|
91
94
|
# Initializing error hash for displaying all errors altogether
|
|
92
|
-
error_object =
|
|
95
|
+
error_object = Usman::ErrorHash.new
|
|
93
96
|
|
|
94
97
|
if user.valid?
|
|
95
98
|
user.save!
|
|
@@ -99,15 +102,47 @@ class User < Usman::ApplicationRecord
|
|
|
99
102
|
error_object.errors << { summary: summary, details: details }
|
|
100
103
|
end
|
|
101
104
|
|
|
105
|
+
## Adding a profile picture
|
|
106
|
+
begin
|
|
107
|
+
image_path = image_base_path + "users/#{user.username}.png"
|
|
108
|
+
image_path = image_base_path + "users/#{user.username}.jpg" unless File.exists?(image_path)
|
|
109
|
+
if File.exists?(image_path)
|
|
110
|
+
user.build_profile_picture
|
|
111
|
+
user.profile_picture.image = File.open(image_path)
|
|
112
|
+
if user.profile_picture.valid?
|
|
113
|
+
user.profile_picture.save
|
|
114
|
+
else
|
|
115
|
+
summary = "Error while saving user: #{user.name}"
|
|
116
|
+
details = "Error! #{user.errors.full_messages.to_sentence}"
|
|
117
|
+
details << ", #{user.profile_picture.errors.full_messages.to_sentence}" if user.profile_picture
|
|
118
|
+
error_object.errors << { summary: summary, details: details }
|
|
119
|
+
end
|
|
120
|
+
else
|
|
121
|
+
summary = "Profile Picture not found for user: #{user.name}"
|
|
122
|
+
details = "#{image_path}/png doesn't exists"
|
|
123
|
+
error_object.warnings << { summary: summary, details: details }
|
|
124
|
+
end
|
|
125
|
+
rescue => e
|
|
126
|
+
summary = "Error during processing: #{$!}"
|
|
127
|
+
details = "User: #{user.name}, Image Path: #{image_path}"
|
|
128
|
+
stack_trace = "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
|
|
129
|
+
error_object.errors << { summary: summary, details: details, stack_trace: stack_trace }
|
|
130
|
+
end if user.profile_picture.blank?
|
|
131
|
+
|
|
102
132
|
return error_object
|
|
103
133
|
end
|
|
104
134
|
|
|
105
135
|
# ------------------
|
|
106
136
|
# Instance variables
|
|
107
137
|
# ------------------
|
|
108
|
-
|
|
109
|
-
#
|
|
110
|
-
#
|
|
138
|
+
|
|
139
|
+
# * Return full name
|
|
140
|
+
# == Examples
|
|
141
|
+
# >>> user.display_name
|
|
142
|
+
# => "Joe Black"
|
|
143
|
+
def display_name
|
|
144
|
+
"#{name}"
|
|
145
|
+
end
|
|
111
146
|
|
|
112
147
|
# * Return true if the user is not approved, else false.
|
|
113
148
|
# == Examples
|
|
@@ -160,8 +195,9 @@ class User < Usman::ApplicationRecord
|
|
|
160
195
|
self.update_attribute(:status, SUSPENDED)
|
|
161
196
|
end
|
|
162
197
|
|
|
163
|
-
|
|
164
|
-
|
|
198
|
+
def is_super_admin?
|
|
199
|
+
super_admin
|
|
200
|
+
end
|
|
165
201
|
|
|
166
202
|
def start_session
|
|
167
203
|
# FIX ME - specs are not written to ensure that all these data are saved
|
|
@@ -179,19 +215,14 @@ class User < Usman::ApplicationRecord
|
|
|
179
215
|
|
|
180
216
|
def end_session
|
|
181
217
|
# Reseting the auth token for user when he logs out.
|
|
182
|
-
# Resetting the token_created_at to nil
|
|
183
218
|
# (Time.now - 1.second)
|
|
184
219
|
self.update_attributes auth_token: SecureRandom.hex, token_created_at: nil
|
|
185
220
|
end
|
|
186
221
|
|
|
187
|
-
def update_token
|
|
222
|
+
def update_token
|
|
188
223
|
self.update_attribute(:token_created_at, Time.now)
|
|
189
224
|
end
|
|
190
225
|
|
|
191
|
-
def expire_token!
|
|
192
|
-
self.update_attribute(:token_created_at, (Time.now - (SESSION_TIME_OUT + 1.minute)))
|
|
193
|
-
end
|
|
194
|
-
|
|
195
226
|
def token_about_to_expire?
|
|
196
227
|
return self.token_created_at.nil? || (Time.now > self.token_created_at + (SESSION_TIME_OUT - 1.minute))
|
|
197
228
|
end
|
|
@@ -210,8 +241,9 @@ class User < Usman::ApplicationRecord
|
|
|
210
241
|
self.reset_password_sent_at = Time.now unless self.reset_password_sent_at
|
|
211
242
|
end
|
|
212
243
|
|
|
213
|
-
|
|
214
|
-
|
|
244
|
+
def default_image_url(size="small")
|
|
245
|
+
"/assets/kuppayam/defaults/user-#{size}.png"
|
|
246
|
+
end
|
|
215
247
|
|
|
216
248
|
def set_permission(feature_name, **options)
|
|
217
249
|
options.reverse_merge!(
|
|
@@ -269,57 +301,13 @@ class User < Usman::ApplicationRecord
|
|
|
269
301
|
end
|
|
270
302
|
|
|
271
303
|
def can_be_deleted?
|
|
272
|
-
|
|
304
|
+
return true
|
|
273
305
|
end
|
|
274
306
|
|
|
275
307
|
def can_be_edited?
|
|
276
308
|
!suspended?
|
|
277
309
|
end
|
|
278
310
|
|
|
279
|
-
# Role Methods
|
|
280
|
-
# ------------
|
|
281
|
-
|
|
282
|
-
def add_role(role)
|
|
283
|
-
return false unless self.approved?
|
|
284
|
-
role = Role.find_by_name(role) if role.is_a?(String)
|
|
285
|
-
if role
|
|
286
|
-
self.roles << role unless self.has_role?(role)
|
|
287
|
-
return true
|
|
288
|
-
else
|
|
289
|
-
return false
|
|
290
|
-
end
|
|
291
|
-
end
|
|
292
|
-
|
|
293
|
-
def remove_role(role)
|
|
294
|
-
role = Role.find_by_name(role) if role.is_a?(String)
|
|
295
|
-
self.roles.delete(role) if role
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
def has_role?(role)
|
|
299
|
-
role = Role.find_by_name(role) if role.is_a?(String)
|
|
300
|
-
if role && role.persisted?
|
|
301
|
-
return true if self.super_admin
|
|
302
|
-
self.roles.exists?(:id => [role.id])
|
|
303
|
-
else
|
|
304
|
-
return false
|
|
305
|
-
end
|
|
306
|
-
end
|
|
307
|
-
|
|
308
|
-
# Other Methods
|
|
309
|
-
# -------------
|
|
310
|
-
|
|
311
|
-
# * Return full name
|
|
312
|
-
# == Examples
|
|
313
|
-
# >>> user.display_name
|
|
314
|
-
# => "Joe Black"
|
|
315
|
-
def display_name
|
|
316
|
-
"#{name}"
|
|
317
|
-
end
|
|
318
|
-
|
|
319
|
-
def default_image_url(size="small")
|
|
320
|
-
"/assets/kuppayam/defaults/user-#{size}.png"
|
|
321
|
-
end
|
|
322
|
-
|
|
323
311
|
private
|
|
324
312
|
|
|
325
313
|
def should_validate_password?
|
|
@@ -344,4 +332,25 @@ class User < Usman::ApplicationRecord
|
|
|
344
332
|
return feature
|
|
345
333
|
end
|
|
346
334
|
|
|
335
|
+
def get_role(role_name)
|
|
336
|
+
self.roles.find_by_id(role_name) || self.roles.find_by_name(role_name)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def add_role(role_name)
|
|
340
|
+
role = self.get_role(role_name)
|
|
341
|
+
self.roles << role if role && role.persists?
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def remove_role(role_name)
|
|
345
|
+
role = self.get_role(role_name)
|
|
346
|
+
if role
|
|
347
|
+
self.roles.delete(role)
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def has_role?(role_name)
|
|
352
|
+
role = self.get_role(role_name)
|
|
353
|
+
role && role.persists?
|
|
354
|
+
end
|
|
355
|
+
|
|
347
356
|
end
|
|
@@ -37,7 +37,6 @@
|
|
|
37
37
|
|
|
38
38
|
<!-- class "auto-inherit-active-class" will automatically add "active" class for parent elements who are marked already with class "active" -->
|
|
39
39
|
|
|
40
|
-
<!-- Admin Dashboard -->
|
|
41
40
|
<li class="<%= nav_active?('admin/dashboard') ? 'active' : '' %>">
|
|
42
41
|
<%= link_to raw("<i class=\"linecons-desktop\"></i> <span class='title'>Dashboard</span>"), usman.admin_dashboard_url %>
|
|
43
42
|
</li>
|
|
@@ -53,29 +52,29 @@
|
|
|
53
52
|
<li class="<%= nav_class("admin/users") %>">
|
|
54
53
|
<%= link_to raw("<i class=\"linecons-user\"></i> <span class='title'>Manage Users</span>"), usman.admin_users_url %>
|
|
55
54
|
</li>
|
|
55
|
+
|
|
56
|
+
<li class="<%= nav_class("admin/users") %>">
|
|
57
|
+
<%= link_to raw("<i class=\"linecons-lock\"></i> <span class='title'>Manage Permissions</span>"), usman.admin_permissions_url %>
|
|
58
|
+
</li>
|
|
56
59
|
</ul>
|
|
60
|
+
|
|
57
61
|
</li>
|
|
58
62
|
|
|
59
|
-
<% if @current_user.super_admin? %>
|
|
60
63
|
<li class="">
|
|
61
64
|
<a href="/xenon/xenon-files/html/dashboard-1.html">
|
|
62
65
|
<i class="linecons-database"></i>
|
|
63
66
|
<span class="title">Master Data</span>
|
|
64
67
|
</a>
|
|
65
|
-
|
|
66
68
|
<ul>
|
|
67
69
|
<li class="">
|
|
68
70
|
<%= link_to raw("<i class=\"linecons-diamond\"></i> <span class='title'>Manage Features</span>"), usman.admin_features_url %>
|
|
69
71
|
</li>
|
|
70
72
|
|
|
71
|
-
<li class="
|
|
72
|
-
<%= link_to raw("<i class=\"linecons-
|
|
73
|
+
<li class="">
|
|
74
|
+
<%= link_to raw("<i class=\"linecons-diamond\"></i> <span class='title'>Manage Roles</span>"), usman.admin_roles_url %>
|
|
73
75
|
</li>
|
|
74
76
|
</ul>
|
|
75
|
-
|
|
76
77
|
</li>
|
|
77
|
-
<% end %>
|
|
78
|
-
|
|
79
78
|
</ul>
|
|
80
79
|
|
|
81
80
|
</div>
|
|
File without changes
|