usman 0.1.4 → 0.1.5dev

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/usman/admin/dashboard_controller.rb +1 -1
  3. data/app/controllers/usman/admin/features_controller.rb +2 -0
  4. data/app/controllers/usman/admin/permissions_controller.rb +8 -15
  5. data/app/controllers/usman/admin/roles_controller.rb +3 -2
  6. data/app/controllers/usman/admin/user_roles_controller.rb +142 -0
  7. data/app/controllers/usman/admin/users_controller.rb +3 -1
  8. data/app/helpers/usman/authentication_helper.rb +18 -7
  9. data/app/models/feature.rb +16 -29
  10. data/app/models/permission.rb +3 -8
  11. data/app/models/role.rb +3 -6
  12. data/app/models/user.rb +43 -63
  13. data/app/models/usman/application_record.rb +4 -0
  14. data/app/views/layouts/kuppayam/_sidebar.html.erb +8 -7
  15. data/app/views/usman/admin/features/_form.html.erb +5 -2
  16. data/app/views/usman/admin/features/_index.html.erb +4 -1
  17. data/app/views/usman/admin/features/index.html.erb +20 -3
  18. data/app/views/usman/admin/permissions/_show.html.erb +2 -10
  19. data/app/views/usman/admin/permissions/index.html.erb +20 -3
  20. data/app/views/usman/admin/roles/_form.html.erb +1 -1
  21. data/app/views/usman/admin/roles/_index.html.erb +3 -3
  22. data/app/views/usman/admin/roles/_row.html.erb +2 -2
  23. data/app/views/usman/admin/roles/_show.html.erb +41 -7
  24. data/app/views/usman/admin/roles/index.html.erb +22 -3
  25. data/app/views/usman/admin/user_roles/_form.html.erb +24 -0
  26. data/app/views/usman/admin/user_roles/_index.html.erb +59 -0
  27. data/app/views/usman/admin/user_roles/_row.html.erb +30 -0
  28. data/app/views/usman/admin/user_roles/_show.html.erb +74 -0
  29. data/app/views/usman/admin/users/_index.html.erb +21 -15
  30. data/app/views/usman/admin/users/_row.html.erb +3 -3
  31. data/app/views/usman/admin/users/_show.html.erb +1 -1
  32. data/app/views/usman/admin/users/index.html.erb +3 -1
  33. data/app/views/usman/sessions/_form.html.erb +18 -6
  34. data/config/locales/kuppayam/authentication.en.yml +1 -1
  35. data/config/routes.rb +3 -1
  36. data/db/import_data/dummy/roles.csv +9 -0
  37. data/db/import_data/dummy/users.csv +3 -3
  38. data/db/import_data/roles.csv +5 -0
  39. data/lib/tasks/usman/all.rake +34 -10
  40. data/lib/{tasks/usman → temp}/features.rake +3 -6
  41. data/lib/{tasks/usman → temp}/permissions.rake +3 -5
  42. data/lib/temp/roles.rake +24 -0
  43. data/lib/{tasks/usman → temp}/users.rake +3 -5
  44. data/lib/usman/engine.rb +0 -2
  45. data/lib/usman/version.rb +1 -1
  46. metadata +17 -37
  47. data/app/views/usman/admin/features/_action_buttons.html.erb +0 -0
  48. data/app/views/usman/admin/features/temp/create.js.erb +0 -29
  49. data/app/views/usman/admin/features/temp/destroy.js.erb +0 -16
  50. data/app/views/usman/admin/features/temp/edit.js.erb +0 -7
  51. data/app/views/usman/admin/features/temp/index.js.erb +0 -8
  52. data/app/views/usman/admin/features/temp/new.js.erb +0 -7
  53. data/app/views/usman/admin/features/temp/row.js.erb +0 -10
  54. data/app/views/usman/admin/features/temp/show.js.erb +0 -8
  55. data/app/views/usman/admin/features/temp/update.js.erb +0 -16
  56. data/app/views/usman/admin/permissions/temp/create.js.erb +0 -29
  57. data/app/views/usman/admin/permissions/temp/destroy.js.erb +0 -22
  58. data/app/views/usman/admin/permissions/temp/edit.js.erb +0 -4
  59. data/app/views/usman/admin/permissions/temp/index.js.erb +0 -14
  60. data/app/views/usman/admin/permissions/temp/new.js.erb +0 -4
  61. data/app/views/usman/admin/permissions/temp/row.js.erb +0 -24
  62. data/app/views/usman/admin/permissions/temp/show.js.erb +0 -13
  63. data/app/views/usman/admin/permissions/temp/update.js.erb +0 -29
  64. data/db/import_data/dummy/images/users/guna.neweast.png +0 -0
  65. data/db/import_data/dummy/images/users/junaid.ramzan.jpg +0 -0
  66. data/db/import_data/dummy/images/users/kpvarma.png +0 -0
  67. data/db/import_data/dummy/images/users/stephen.price.png +0 -0
  68. data/db/import_data/dummy/images/users/vinodh.jpg +0 -0
  69. data/db/import_data/features.csv +0 -10
  70. data/db/import_data/images/users/junaid.ramzan.jpg +0 -0
  71. data/db/import_data/images/users/kpvarma.png +0 -0
  72. data/db/import_data/images/users/vinodh.jpg +0 -0
  73. data/db/import_data/permissions.csv +0 -30
  74. data/lib/usman/extras/import_error_handler.rb +0 -79
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 603c14d27aa8016f2b9c6b141b268aba78f2bc1a
4
- data.tar.gz: 7ddfdfed5beae3b998be632647d7636367e8d2c2
3
+ metadata.gz: c65c43b24f186560b1f39335dbea222f3ea9b22a
4
+ data.tar.gz: f527e47ba03c22de8fe1bdb6bc3b569c77f3b6f2
5
5
  SHA512:
6
- metadata.gz: 5e9aea7072f19c0e92d10ba2480bd322ac32c05fccf2ea5b410509353e4bba3734c4bfc5667caeee1bb65e12b3416c128c853e9efc4f5967c23d359e185d1c93
7
- data.tar.gz: 94712419531933914e1a80f7de6fd76afd6ad21e0ac3ca01fba0c5212ad25c019ebf1bd4ecbba35f6cb719b864e25f7209f344c6881b8987521679a34078f079
6
+ metadata.gz: 4fb1ef097e0a139368176c48695857aae00e8fe8087e3d2b47793cc34848485306d9d41fed93a927d1ff14e937b086a32885b6b13c64c01c394e4401a12899cd
7
+ data.tar.gz: 65cbc70c65a69ac79a26c76c172a53bcb6004a0399d01a4990487c366d7db5369dd64ac4f53b88ef0b95694793bfd2fd22235d63cde824fefeee048924aa1b5c
@@ -10,7 +10,7 @@ module Usman
10
10
 
11
11
  def breadcrumbs_configuration
12
12
  {
13
- heading: "Usman Dashboard",
13
+ heading: "User Dashboard",
14
14
  description: "A Quick view of users and roles",
15
15
  links: [{name: "Dashboard", link: admin_dashboard_path, icon: 'fa-dashboard'}]
16
16
  }
@@ -54,6 +54,8 @@ module Usman
54
54
 
55
55
  def resource_controller_configuration
56
56
  {
57
+ page_title: "Features",
58
+ js_view_path: "/kuppayam/workflows/parrot",
57
59
  view_path: "/usman/admin/features"
58
60
  }
59
61
  end
@@ -2,29 +2,20 @@ 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
-
13
5
  def create
14
- @permission = Permission.where(" user_id = ? AND feature_id = ? ", permitted_params[:user_id], permitted_params[:feature_id]).first || Permission.new
6
+ @permission = @r_object = Permission.where(" user_id = ? AND feature_id = ? ", permitted_params[:user_id], permitted_params[:feature_id]).first || Permission.new
15
7
  @permission.assign_attributes(permitted_params)
16
- save_resource(@permission)
17
-
8
+ save_resource
18
9
  get_collections
19
10
  end
20
11
 
21
12
  def update
22
- @permission = Permission.find_by_id(params[:id])
13
+ @permission = @r_object = Permission.find_by_id(params[:id])
23
14
  # The form will not submit can_create 0 if it is not selected
24
15
  # hence making it false by default and letting it update by itself.
25
16
  @permission.assign_attributes({"can_create": "0", "can_read": "0", "can_update": "0", "can_delete": "0"})
26
17
  @permission.assign_attributes(permitted_params)
27
- save_resource(@permission)
18
+ save_resource
28
19
  get_collections
29
20
  end
30
21
 
@@ -36,7 +27,7 @@ module Usman
36
27
  parse_filters
37
28
  apply_filters
38
29
 
39
- @permissions = @relation.includes(:user, :feature).page(@current_page).per(@per_page)
30
+ @permissions = @r_objects = @relation.includes(:user, :feature).page(@current_page).per(@per_page)
40
31
 
41
32
  return true
42
33
  end
@@ -65,7 +56,9 @@ module Usman
65
56
 
66
57
  def resource_controller_configuration
67
58
  {
68
- view_path: "/demo/permissions"
59
+ page_title: "Permissions",
60
+ js_view_path: "/kuppayam/workflows/parrot",
61
+ view_path: "/usman/admin/permissions"
69
62
  }
70
63
  end
71
64
 
@@ -18,7 +18,7 @@ module Usman
18
18
  def apply_filters
19
19
  @relation = @relation.search(@query) if @query
20
20
 
21
- @order_by = "created_at desc" unless @order_by
21
+ @order_by = "name ASC" unless @order_by
22
22
  @relation = @relation.order(@order_by)
23
23
  end
24
24
 
@@ -39,7 +39,8 @@ module Usman
39
39
 
40
40
  def resource_controller_configuration
41
41
  {
42
- js_view_path: "/kuppayam/workflows/parrot",
42
+ page_title: "Roles",
43
+ js_view_path: "/kuppayam/workflows/peacock",
43
44
  view_path: "/usman/admin/roles"
44
45
  }
45
46
  end
@@ -0,0 +1,142 @@
1
+ module Usman
2
+ module Admin
3
+ class UserRolesController < ResourceController
4
+
5
+ before_action :get_role
6
+
7
+ def index
8
+ get_collections
9
+ respond_to do |format|
10
+ format.html {}
11
+ format.js {
12
+ js_view_path = @resource_options && @resource_options[:js_view_path] ? "#{@resource_options[:js_view_path]}/index" : :index
13
+ render js_view_path
14
+ }
15
+ end
16
+ end
17
+
18
+ def show
19
+ @user = @r_object = @resource_options[:class].find_by_id(params[:id])
20
+ set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize)) unless @r_object
21
+ render_accordingly
22
+ end
23
+
24
+ def new
25
+ @user = User.new
26
+ render_accordingly
27
+ end
28
+
29
+ def create
30
+ @user = @r_object = User.find_by_id(permitted_params[:id])
31
+ if @user.add_role(@role)
32
+ set_notification(true, I18n.t('status.success'), "Role '#{@role.name}' has been assigned to the user '#{@user.name}'")
33
+ else
34
+ set_notification(false, I18n.t('status.success'), "Failed to assign the Role '#{@role.name}'")
35
+ end
36
+ action_name = params[:action].to_s == "create" ? "new" : "edit"
37
+ render_or_redirect(false, resource_url(@r_object), action_name)
38
+ end
39
+
40
+ def destroy
41
+ @user = @r_object = User.find_by_id(params[:id])
42
+ if @user
43
+ if @user.remove_role(@role)
44
+ get_collections
45
+ set_flash_message(I18n.t('success.deleted'), :success)
46
+ set_notification(true, I18n.t('status.success'), "Role '#{@role.name}' has been removed for the user '#{@user.name}'")
47
+ @destroyed = true
48
+ else
49
+ message = I18n.t('errors.failed_to_delete', item: default_item_name.titleize)
50
+ set_flash_message(message, :failure)
51
+ set_notification(false, I18n.t('status.success'), "Failed to remove the Role '#{@role.name}'")
52
+ @destroyed = false
53
+ end
54
+ else
55
+ set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize))
56
+ end
57
+
58
+ respond_to do |format|
59
+ format.html {}
60
+ format.js {
61
+ js_view_path = @resource_options && @resource_options[:js_view_path] ? "#{@resource_options[:js_view_path]}/destroy" : :destroy
62
+ render js_view_path
63
+ }
64
+ end
65
+
66
+ end
67
+
68
+ private
69
+
70
+ def get_role
71
+ @role = Role.find_by_id(params[:role_id])
72
+ end
73
+
74
+ def get_collections
75
+ @relation = @role.users.where("")
76
+
77
+ parse_filters
78
+ apply_filters
79
+
80
+ @users = @r_objects = @relation.page(@current_page).per(@per_page)
81
+
82
+ return true
83
+ end
84
+
85
+ def apply_filters
86
+ @relation = @relation.search(@query) if @query
87
+
88
+ @order_by = "name ASC" unless @order_by
89
+ @relation = @relation.order(@order_by)
90
+ end
91
+
92
+ def configure_filter_settings
93
+ @filter_settings = {
94
+ string_filters: [
95
+ { filter_name: :query }
96
+ ],
97
+ boolean_filters: [],
98
+ reference_filters: [],
99
+ variable_filters: [],
100
+ }
101
+ end
102
+
103
+ def configure_filter_ui_settings
104
+ @filter_ui_settings = {}
105
+ end
106
+
107
+ def resource_url(obj)
108
+ url_for([:admin, @role, obj])
109
+ end
110
+
111
+ def resource_controller_configuration
112
+ {
113
+ collection_name: :users,
114
+ item_name: :user,
115
+ class: User,
116
+ show_modal_after_update: false,
117
+ show_modal_after_update: false,
118
+ page_title: "Manage User Roles",
119
+ js_view_path: "/kuppayam/workflows/parrot",
120
+ view_path: "/usman/admin/user_roles"
121
+ }
122
+ end
123
+
124
+ def breadcrumbs_configuration
125
+ {
126
+ heading: "Manage User Roles",
127
+ description: "Listing all User Roles",
128
+ links: [{name: "Home", link: admin_dashboard_path, icon: 'fa-home'}]
129
+ }
130
+ end
131
+
132
+ def permitted_params
133
+ params.require(:user).permit(:id)
134
+ end
135
+
136
+ def set_navs
137
+ set_nav("admin/roles/user_roles")
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -104,7 +104,9 @@ module Usman
104
104
 
105
105
  def resource_controller_configuration
106
106
  {
107
- view_path: "usman/admin/users"
107
+ page_title: "Users",
108
+ js_view_path: "/kuppayam/workflows/peacock",
109
+ view_path: "/usman/admin/users"
108
110
  }
109
111
  end
110
112
 
@@ -10,7 +10,14 @@ 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
- usman.admin_dashboard_url
13
+ main_app.user_landing_url
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
14
21
  end
15
22
 
16
23
  # Returns the default URL to which the system should redirect the user after an unsuccessful attempt to authorise a resource/page
@@ -22,11 +29,11 @@ module Usman
22
29
  # This method should also handle the redirection if it has come through a client appliction for authentication
23
30
  # In that case, it should persist the params passed by the client application
24
31
  def redirect_after_unsuccessful_authentication
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)
32
+ @params_hsh = {}
33
+ @params_hsh[:client_app] = params[:client_app] if params[:client_app]
34
+ @params_hsh[:redirect_back_url] = params[:redirect_back_url] if params[:redirect_back_url]
35
+ @params_hsh[:requested_url] = request.original_url if request.get?
36
+ redirect_to add_query_params(default_sign_in_url, @params_hsh)
30
37
  return
31
38
  end
32
39
 
@@ -49,6 +56,11 @@ module Usman
49
56
  redirect_after_unsuccessful_authentication
50
57
  }
51
58
  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
+
52
64
  render(:partial => 'usman/sessions/sign_in.js.erb', :handlers => [:erb], :formats => [:js])
53
65
  }
54
66
  end
@@ -118,7 +130,6 @@ module Usman
118
130
  session[:last_user_id] = current_user.id if current_user
119
131
  user.start_session
120
132
  session[:id] = user.id
121
- default_redirect_url_after_sign_in
122
133
  redirect_to default_redirect_url_after_sign_in
123
134
  end
124
135
 
@@ -1,8 +1,5 @@
1
1
  class Feature < Usman::ApplicationRecord
2
2
 
3
- require 'import_error_handler.rb'
4
- extend Usman::ImportErrorHandler
5
-
6
3
  # Constants
7
4
  UNPUBLISHED = "unpublished"
8
5
  PUBLISHED = "published"
@@ -47,9 +44,7 @@ class Feature < Usman::ApplicationRecord
47
44
  scope :published, -> { where(status: PUBLISHED) }
48
45
  scope :disabled, -> { where(status: DISABLED) }
49
46
 
50
- def self.save_row_data(row, base_path)
51
-
52
- image_base_path = base_path + "images/"
47
+ def self.save_row_data(row)
53
48
 
54
49
  row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
55
50
 
@@ -60,33 +55,13 @@ class Feature < Usman::ApplicationRecord
60
55
  feature.status = Feature::UNPUBLISHED
61
56
 
62
57
  # Initializing error hash for displaying all errors altogether
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?)
58
+ error_object = Kuppayam::Importer::ErrorHash.new
59
+
60
+ if feature.valid?
85
61
  feature.save!
86
62
  else
87
63
  summary = "Error while saving feature: #{feature.name}"
88
64
  details = "Error! #{feature.errors.full_messages.to_sentence}"
89
- details << ", #{feature.feature_image.errors.full_messages.to_sentence}" if feature.feature_image
90
65
  error_object.errors << { summary: summary, details: details }
91
66
  end
92
67
  return error_object
@@ -154,5 +129,17 @@ class Feature < Usman::ApplicationRecord
154
129
  def can_be_destroyed?
155
130
  return true
156
131
  end
132
+
133
+ def can_be_published?
134
+ unpublished? or disabled?
135
+ end
136
+
137
+ def can_be_unpublished?
138
+ published? or disabled?
139
+ end
140
+
141
+ def can_be_removed?
142
+ published? or unpublished? or disabled?
143
+ end
157
144
 
158
145
  end
@@ -1,9 +1,6 @@
1
1
  class Permission < Usman::ApplicationRecord
2
2
 
3
- require 'import_error_handler.rb'
4
- extend Usman::ImportErrorHandler
5
-
6
- # Associations
3
+ # Associations
7
4
  belongs_to :user
8
5
  belongs_to :feature
9
6
 
@@ -30,16 +27,14 @@ class Permission < Usman::ApplicationRecord
30
27
  LOWER(f.name) LIKE LOWER('%#{query}%')")}
31
28
 
32
29
 
33
- def self.save_row_data(row, base_path)
34
-
35
- image_base_path = base_path + "images/"
30
+ def self.save_row_data(row)
36
31
 
37
32
  row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
38
33
 
39
34
  return if row[:user].blank? || row[:feature].blank?
40
35
 
41
36
  # Initializing error hash for displaying all errors altogether
42
- error_object = Usman::ErrorHash.new
37
+ error_object = Kuppayam::Importer::ErrorHash.new
43
38
 
44
39
  user = User.find_by_username(row[:user])
45
40
  unless user
data/app/models/role.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  class Role < Usman::ApplicationRecord
2
2
 
3
- require 'import_error_handler.rb'
4
- extend Usman::ImportErrorHandler
5
-
6
3
  # Associations
7
4
  has_and_belongs_to_many :users
8
5
 
@@ -18,10 +15,10 @@ class Role < Usman::ApplicationRecord
18
15
  # == Examples
19
16
  # >>> role.search(query)
20
17
  # => ActiveRecord::Relation object
21
- scope :search, lambda {|query| where("LOWER(name) LIKE LOWER('%#{query}%')")
18
+ scope :search, lambda {|query| where("LOWER(roles.name) LIKE LOWER('%#{query}%')")
22
19
  }
23
20
 
24
- def self.save_row_data(row, base_path)
21
+ def self.save_row_data(row)
25
22
 
26
23
  row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
27
24
 
@@ -31,7 +28,7 @@ class Role < Usman::ApplicationRecord
31
28
  role.name = row[:name]
32
29
 
33
30
  # Initializing error hash for displaying all errors altogether
34
- error_object = Usman::ErrorHash.new
31
+ error_object = Kuppayam::Importer::ErrorHash.new
35
32
 
36
33
  if role.valid?
37
34
  role.save!
data/app/models/user.rb CHANGED
@@ -1,9 +1,5 @@
1
1
  class User < Usman::ApplicationRecord
2
2
 
3
- require 'import_error_handler.rb'
4
- extend Usman::ImportErrorHandler
5
- extend KuppayamValidators
6
-
7
3
  # including Password Methods
8
4
  has_secure_password
9
5
 
@@ -26,7 +22,7 @@ class User < Usman::ApplicationRecord
26
22
 
27
23
  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]
28
24
  DEFAULT_PASSWORD = "Password@1"
29
- SESSION_TIME_OUT = 30.minutes
25
+ SESSION_TIME_OUT = 120.minutes
30
26
 
31
27
  # Validations
32
28
  validates :name, presence: true
@@ -43,7 +39,7 @@ class User < Usman::ApplicationRecord
43
39
  has_one :profile_picture, :as => :imageable, :dependent => :destroy, :class_name => "Image::ProfilePicture"
44
40
  has_many :permissions
45
41
  has_many :features, through: :permissions
46
- has_and_belongs_to_many :users
42
+ has_and_belongs_to_many :roles
47
43
 
48
44
 
49
45
  # ------------------
@@ -59,10 +55,10 @@ class User < Usman::ApplicationRecord
59
55
  # == Examples
60
56
  # >>> user.search(query)
61
57
  # => ActiveRecord::Relation object
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}%')")
58
+ scope :search, lambda {|query| where("LOWER(users.name) LIKE LOWER('%#{query}%') OR\
59
+ LOWER(users.username) LIKE LOWER('%#{query}%') OR\
60
+ LOWER(users.email) LIKE LOWER('%#{query}%') OR\
61
+ LOWER(users.designation) LIKE LOWER('%#{query}%')")
66
62
  }
67
63
 
68
64
  scope :status, lambda { |status| where("LOWER(status)='#{status}'") }
@@ -70,10 +66,11 @@ class User < Usman::ApplicationRecord
70
66
  scope :pending, -> { where(status: PENDING) }
71
67
  scope :approved, -> { where(status: APPROVED) }
72
68
  scope :suspended, -> { where(status: SUSPENDED) }
69
+
70
+ scope :super_admins, -> { where(super_admin: TRUE) }
71
+ scope :normal_users, -> { where(super_admin: FALSE) }
73
72
 
74
- def self.save_row_data(row, base_path)
75
-
76
- image_base_path = base_path + "images/"
73
+ def self.save_row_data(row)
77
74
 
78
75
  row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
79
76
 
@@ -92,7 +89,7 @@ class User < Usman::ApplicationRecord
92
89
  user.assign_default_password
93
90
 
94
91
  # Initializing error hash for displaying all errors altogether
95
- error_object = Usman::ErrorHash.new
92
+ error_object = Kuppayam::Importer::ErrorHash.new
96
93
 
97
94
  if user.valid?
98
95
  user.save!
@@ -102,33 +99,6 @@ class User < Usman::ApplicationRecord
102
99
  error_object.errors << { summary: summary, details: details }
103
100
  end
104
101
 
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
-
132
102
  return error_object
133
103
  end
134
104
 
@@ -223,6 +193,10 @@ class User < Usman::ApplicationRecord
223
193
  self.update_attribute(:token_created_at, Time.now)
224
194
  end
225
195
 
196
+ def expire_token!
197
+ self.update_attribute(:token_created_at, (Time.now - (SESSION_TIME_OUT + 1.minute)))
198
+ end
199
+
226
200
  def token_about_to_expire?
227
201
  return self.token_created_at.nil? || (Time.now > self.token_created_at + (SESSION_TIME_OUT - 1.minute))
228
202
  end
@@ -301,13 +275,40 @@ class User < Usman::ApplicationRecord
301
275
  end
302
276
 
303
277
  def can_be_deleted?
304
- return true
278
+ suspended?
305
279
  end
306
280
 
307
281
  def can_be_edited?
308
282
  !suspended?
309
283
  end
310
284
 
285
+ def add_role(role)
286
+ return false unless self.approved?
287
+ role = Role.find_by_name(role) if role.is_a?(String)
288
+ if role
289
+ self.roles << role unless self.has_role?(role)
290
+ return true
291
+ else
292
+ return false
293
+ end
294
+ end
295
+
296
+ def remove_role(role)
297
+ role = Role.find_by_name(role) if role.is_a?(String)
298
+ self.roles.delete(role) if role
299
+ end
300
+
301
+ def has_role?(role)
302
+ role = Role.find_by_name(role) if role.is_a?(String)
303
+ if role && role.persisted?
304
+ return true if self.super_admin
305
+ self.roles.exists?(:id => [role.id])
306
+ else
307
+ return false
308
+ end
309
+
310
+ end
311
+
311
312
  private
312
313
 
313
314
  def should_validate_password?
@@ -332,25 +333,4 @@ class User < Usman::ApplicationRecord
332
333
  return feature
333
334
  end
334
335
 
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
-
356
336
  end
@@ -1,5 +1,9 @@
1
1
  module Usman
2
2
  class ApplicationRecord < ActiveRecord::Base
3
3
  self.abstract_class = true
4
+
5
+ require 'kuppayam/importer.rb'
6
+ extend Kuppayam::Importer
7
+ extend KuppayamValidators
4
8
  end
5
9
  end
@@ -37,6 +37,7 @@
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 -->
40
41
  <li class="<%= nav_active?('admin/dashboard') ? 'active' : '' %>">
41
42
  <%= link_to raw("<i class=\"linecons-desktop\"></i> <span class='title'>Dashboard</span>"), usman.admin_dashboard_url %>
42
43
  </li>
@@ -52,29 +53,29 @@
52
53
  <li class="<%= nav_class("admin/users") %>">
53
54
  <%= link_to raw("<i class=\"linecons-user\"></i> <span class='title'>Manage Users</span>"), usman.admin_users_url %>
54
55
  </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>
59
56
  </ul>
60
-
61
57
  </li>
62
58
 
59
+ <% if @current_user.super_admin? %>
63
60
  <li class="">
64
61
  <a href="/xenon/xenon-files/html/dashboard-1.html">
65
62
  <i class="linecons-database"></i>
66
63
  <span class="title">Master Data</span>
67
64
  </a>
65
+
68
66
  <ul>
69
67
  <li class="">
70
68
  <%= link_to raw("<i class=\"linecons-diamond\"></i> <span class='title'>Manage Features</span>"), usman.admin_features_url %>
71
69
  </li>
72
70
 
73
- <li class="">
74
- <%= link_to raw("<i class=\"linecons-diamond\"></i> <span class='title'>Manage Roles</span>"), usman.admin_roles_url %>
71
+ <li class="<%= nav_class("admin/users") %>">
72
+ <%= link_to raw("<i class=\"linecons-lock\"></i> <span class='title'>Manage Permissions</span>"), usman.admin_permissions_url %>
75
73
  </li>
76
74
  </ul>
75
+
77
76
  </li>
77
+ <% end %>
78
+
78
79
  </ul>
79
80
 
80
81
  </div>