usman 0.1.6dev2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/android-icon-144x144.png +0 -0
  3. data/app/assets/images/android-icon-192x192.png +0 -0
  4. data/app/assets/images/android-icon-36x36.png +0 -0
  5. data/app/assets/images/android-icon-48x48.png +0 -0
  6. data/app/assets/images/android-icon-72x72.png +0 -0
  7. data/app/assets/images/android-icon-96x96.png +0 -0
  8. data/app/assets/images/apple-icon-114x114.png +0 -0
  9. data/app/assets/images/apple-icon-120x120.png +0 -0
  10. data/app/assets/images/apple-icon-144x144.png +0 -0
  11. data/app/assets/images/apple-icon-152x152.png +0 -0
  12. data/app/assets/images/apple-icon-180x180.png +0 -0
  13. data/app/assets/images/apple-icon-57x57.png +0 -0
  14. data/app/assets/images/apple-icon-60x60.png +0 -0
  15. data/app/assets/images/apple-icon-72x72.png +0 -0
  16. data/app/assets/images/apple-icon-76x76.png +0 -0
  17. data/app/assets/images/apple-icon-precomposed.png +0 -0
  18. data/app/assets/images/apple-icon.png +0 -0
  19. data/app/assets/images/browserconfig.xml +2 -0
  20. data/app/assets/images/favicon-16x16.png +0 -0
  21. data/app/assets/images/favicon-32x32.png +0 -0
  22. data/app/assets/images/favicon-96x96.png +0 -0
  23. data/app/assets/images/favicon.ico +0 -0
  24. data/app/assets/images/logo-on-white.png +0 -0
  25. data/app/assets/images/logo-small.png +0 -0
  26. data/app/assets/images/logo.png +0 -0
  27. data/app/assets/images/manifest.json +41 -0
  28. data/app/assets/images/ms-icon-144x144.png +0 -0
  29. data/app/assets/images/ms-icon-150x150.png +0 -0
  30. data/app/assets/images/ms-icon-310x310.png +0 -0
  31. data/app/assets/images/ms-icon-70x70.png +0 -0
  32. data/app/assets/sketches/logo.sketch +0 -0
  33. data/app/controllers/api/v1/base_controller.rb +13 -0
  34. data/app/controllers/api/v1/registrations_controller.rb +41 -0
  35. data/app/controllers/usman/admin_controller.rb +26 -0
  36. data/app/controllers/usman/application_controller.rb +16 -0
  37. data/app/controllers/usman/dashboard_controller.rb +26 -0
  38. data/app/controllers/usman/features_controller.rb +83 -0
  39. data/app/controllers/usman/my_account_controller.rb +25 -0
  40. data/app/controllers/usman/permissions_controller.rb +85 -0
  41. data/app/controllers/usman/resource_controller.rb +21 -0
  42. data/app/controllers/usman/roles_controller.rb +68 -0
  43. data/app/controllers/usman/sessions_controller.rb +4 -1
  44. data/app/controllers/usman/user_roles_controller.rb +141 -0
  45. data/app/controllers/usman/users_controller.rb +133 -0
  46. data/app/helpers/usman/api_helper.rb +79 -0
  47. data/app/helpers/usman/authentication_helper.rb +21 -7
  48. data/app/models/device.rb +149 -0
  49. data/app/models/feature.rb +11 -7
  50. data/app/models/permission.rb +17 -13
  51. data/app/models/registration.rb +108 -0
  52. data/app/models/role.rb +11 -7
  53. data/app/models/user.rb +19 -13
  54. data/app/services/usman/mobile_registration_service.rb +127 -0
  55. data/app/views/layouts/kuppayam/_sidebar.html.erb +39 -19
  56. data/app/views/layouts/kuppayam/profile.html.erb +120 -0
  57. data/app/views/usman/dashboard/index.html.erb +118 -0
  58. data/app/views/usman/{admin/features → features}/_form.html.erb +1 -1
  59. data/app/views/usman/{admin/features → features}/_index.html.erb +10 -10
  60. data/app/views/usman/{admin/features → features}/_row.html.erb +10 -10
  61. data/app/views/usman/{admin/features → features}/_show.html.erb +2 -2
  62. data/app/views/usman/{admin/features → features}/index.html.erb +4 -4
  63. data/app/views/usman/my_account/index.html.erb +1 -0
  64. data/app/views/usman/{admin/permissions → permissions}/_form.html.erb +1 -1
  65. data/app/views/usman/{admin/permissions → permissions}/_index.html.erb +2 -2
  66. data/app/views/usman/{admin/permissions → permissions}/_row.html.erb +2 -2
  67. data/app/views/usman/{admin/permissions → permissions}/_show.html.erb +2 -2
  68. data/app/views/usman/{admin/permissions → permissions}/index.html.erb +4 -4
  69. data/app/views/usman/{admin/roles → roles}/_form.html.erb +1 -1
  70. data/app/views/usman/{admin/roles → roles}/_index.html.erb +3 -3
  71. data/app/views/usman/{admin/roles → roles}/_row.html.erb +3 -3
  72. data/app/views/usman/{admin/roles → roles}/_show.html.erb +6 -6
  73. data/app/views/usman/{admin/roles → roles}/index.html.erb +5 -5
  74. data/app/views/usman/sessions/_form.html.erb +1 -1
  75. data/app/views/usman/{admin/user_roles → user_roles}/_form.html.erb +1 -1
  76. data/app/views/usman/{admin/user_roles → user_roles}/_index.html.erb +2 -2
  77. data/app/views/usman/{admin/user_roles → user_roles}/_row.html.erb +3 -3
  78. data/app/views/usman/{admin/users → users}/_form.html.erb +1 -1
  79. data/app/views/usman/{admin/users → users}/_index.html.erb +9 -9
  80. data/app/views/usman/{admin/users → users}/_row.html.erb +9 -9
  81. data/app/views/usman/{admin/users → users}/_show.html.erb +8 -8
  82. data/app/views/usman/{admin/users → users}/index.html.erb +8 -8
  83. data/config/locales/usman/mobile_registration.ar.yml +14 -0
  84. data/config/locales/usman/mobile_registration.en.yml +14 -0
  85. data/config/routes.rb +25 -21
  86. data/db/master_data/roles.csv +2 -0
  87. data/db/migrate/{20170728095744_create_otp_registrations.rb → 20170819113217_create_registrations.rb} +4 -4
  88. data/db/migrate/20170819113218_add_registration_id_to_users.rb +7 -0
  89. data/db/migrate/20170819113219_create_devices.rb +41 -0
  90. data/lib/tasks/usman/data.rake +85 -0
  91. data/lib/tasks/usman/master_data.rake +44 -0
  92. data/lib/usman/engine.rb +2 -1
  93. data/lib/usman/version.rb +1 -1
  94. metadata +111 -52
  95. data/app/controllers/usman/admin/base_controller.rb +0 -46
  96. data/app/controllers/usman/admin/dashboard_controller.rb +0 -26
  97. data/app/controllers/usman/admin/features_controller.rb +0 -83
  98. data/app/controllers/usman/admin/permissions_controller.rb +0 -85
  99. data/app/controllers/usman/admin/resource_controller.rb +0 -17
  100. data/app/controllers/usman/admin/roles_controller.rb +0 -68
  101. data/app/controllers/usman/admin/user_roles_controller.rb +0 -142
  102. data/app/controllers/usman/admin/users_controller.rb +0 -133
  103. data/app/views/usman/admin/dashboard/index.html.erb +0 -51
  104. data/config/initializers/overide_kuppayam_controllers.rb +0 -74
  105. data/lib/tasks/usman/all.rake +0 -73
  106. /data/app/views/usman/{admin/permissions → permissions}/_action_buttons.html.erb +0 -0
  107. /data/config/locales/{kuppayam → usman}/authentication.ar.yml +0 -0
  108. /data/config/locales/{kuppayam → usman}/authentication.en.yml +0 -0
  109. /data/db/{import_data → data}/dummy/features.csv +0 -0
  110. /data/db/{import_data → data}/dummy/permissions.csv +0 -0
  111. /data/db/{import_data → data}/dummy/roles.csv +0 -0
  112. /data/db/{import_data → data}/dummy/users.csv +0 -0
  113. /data/db/{import_data → data}/features.csv +0 -0
  114. /data/db/{import_data → data}/permissions.csv +0 -0
  115. /data/db/{import_data → data}/roles.csv +0 -0
  116. /data/db/{import_data → data}/users.csv +0 -0
@@ -0,0 +1,141 @@
1
+ module Usman
2
+ class UserRolesController < ResourceController
3
+
4
+ before_action :require_site_admin
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([@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_create: false,
117
+ show_modal_after_update: false,
118
+ page_title: "Manage User Roles",
119
+ js_view_path: "/kuppayam/workflows/parrot",
120
+ view_path: "/usman/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: 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
@@ -0,0 +1,133 @@
1
+ module Usman
2
+ class UsersController < ResourceController
3
+
4
+ before_action :require_site_admin
5
+
6
+ def make_super_admin
7
+ @user = @r_object = User.find(params[:id])
8
+ if @user
9
+ @user.super_admin = true
10
+ if @user.valid?
11
+ @user.save
12
+ set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: @user.status))
13
+ else
14
+ set_notification(false, I18n.t('status.error'), I18n.translate("error"), @user.errors.full_messages.join("<br>"))
15
+ end
16
+ else
17
+ set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
18
+ end
19
+ render_row
20
+ end
21
+
22
+ def remove_super_admin
23
+ @user = @r_object = User.find(params[:id])
24
+ if @user
25
+ @user.super_admin = false
26
+ if @user.valid?
27
+ @user.save
28
+ set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: @user.status))
29
+ else
30
+ set_notification(false, I18n.t('status.error'), I18n.translate("error"), @user.errors.full_messages.join("<br>"))
31
+ end
32
+ else
33
+ set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
34
+ end
35
+ render_row
36
+ end
37
+
38
+ def masquerade
39
+ @user = @r_object = User.find(params[:id])
40
+ masquerade_as_user(@user)
41
+ end
42
+
43
+ private
44
+
45
+ def get_collections
46
+ # Fetching the users
47
+ @relation = User.includes(:profile_picture).where("")
48
+
49
+ parse_filters
50
+ apply_filters
51
+
52
+ @users = @r_objects = @relation.page(@current_page).per(@per_page)
53
+
54
+ return true
55
+ end
56
+
57
+ def apply_filters
58
+ @relation = @relation.search(@query) if @query
59
+ @relation = @relation.status(@status) if @status
60
+
61
+ # Normal users should not be able to view super admins
62
+ # He should not be seeing admins even while searching
63
+ if @current_user.super_admin?
64
+ @relation = @relation.where("super_admin IS #{@super_admin.to_s.upcase}") if @super_admin.nil? == false && @query.nil?
65
+ else
66
+ @relation = @relation.where("super_admin IS FALSE")
67
+ end
68
+
69
+ @order_by = "created_at desc" unless @order_by
70
+ @relation = @relation.order(@order_by)
71
+ end
72
+
73
+ def configure_filter_settings
74
+ @filter_settings = {
75
+ string_filters: [
76
+ { filter_name: :query },
77
+ { filter_name: :status }
78
+ ],
79
+
80
+ boolean_filters: [
81
+ { filter_name: :super_admin, options: {default: false }}
82
+ ],
83
+
84
+ reference_filters: [],
85
+ variable_filters: [],
86
+ }
87
+ end
88
+
89
+ def configure_filter_ui_settings
90
+ @filter_ui_settings = {
91
+ status: {
92
+ object_filter: false,
93
+ select_label: "Select Status",
94
+ display_hash: User::STATUS,
95
+ current_value: @status,
96
+ values: User::STATUS_REVERSE,
97
+ current_filters: @filters,
98
+ filters_to_remove: [],
99
+ filters_to_add: {},
100
+ url_method_name: 'users_url',
101
+ show_all_filter_on_top: true
102
+ }
103
+ }
104
+ end
105
+
106
+ def resource_controller_configuration
107
+ {
108
+ page_title: "Users",
109
+ js_view_path: "/kuppayam/workflows/peacock",
110
+ view_path: "/usman/users"
111
+ }
112
+ end
113
+
114
+ def breadcrumbs_configuration
115
+ {
116
+ heading: "Manage Users",
117
+ icon: "fa-user",
118
+ description: "Listing all Users",
119
+ links: [{name: "Home", link: dashboard_path, icon: 'fa-home'},
120
+ {name: "Manage Users", link: users_path, icon: 'fa-user', active: true}]
121
+ }
122
+ end
123
+
124
+ def permitted_params
125
+ params.require(:user).permit(:name, :username, :email, :designation, :phone, :password, :password_confirmation)
126
+ end
127
+
128
+ def set_navs
129
+ set_nav("admin/users")
130
+ end
131
+
132
+ end
133
+ end
@@ -0,0 +1,79 @@
1
+ module Usman
2
+ module ApiHelper
3
+
4
+ def current_user
5
+ # Return if @current_user is already initialized else check if the user exists with the auth token present in request header
6
+ @current_user ||= authenticate_with_http_token { |token, options| User.find_by(auth_token: token)}
7
+ end
8
+
9
+ def require_auth_token
10
+ current_user
11
+ unless @current_user
12
+ proc_code = Proc.new do
13
+ set_notification_messages("authentication.permission_denied", :error)
14
+ raise AuthenticationError
15
+ end
16
+ render_json_response(proc_code)
17
+ return
18
+ end
19
+ end
20
+
21
+ def require_super_admin_auth_token
22
+ current_user
23
+ unless @current_user && @current_user.is_super_admin?
24
+ proc_code = Proc.new do
25
+ set_notification_messages("authentication.permission_denied", :error)
26
+ raise AuthenticationError
27
+ end
28
+ render_json_response(proc_code)
29
+ return
30
+ end
31
+ end
32
+
33
+ def require_admin_auth_token
34
+ current_user
35
+ unless @current_user && @current_user.is_admin?
36
+ proc_code = Proc.new do
37
+ set_notification_messages("authentication.permission_denied", :error)
38
+ raise AuthenticationError
39
+ end
40
+ render_json_response(proc_code)
41
+ return
42
+ end
43
+ end
44
+
45
+ def embed_stack_in_json_response?
46
+ ["true", "t", "1", "yes"].include?(params[:debug].to_s.downcase.strip) # || Rails.env == "development"
47
+ end
48
+
49
+ ## This method will accept a proc, execute it and render the json
50
+ def render_json_response(proc_code)
51
+
52
+ begin
53
+ proc_code.call
54
+ @success = @success == false ? (false) : (true)
55
+ rescue Exception => e
56
+ @success = false
57
+ @errors = {
58
+ heading: I18n.translate("response.unexpected_failure.heading"),
59
+ message: e.message.underscore,
60
+ details: I18n.translate("response.#{e.message.underscore}.details"),
61
+ stacktrace: (embed_stack_in_json_response? ? e.backtrace : nil)
62
+ }
63
+ end
64
+ @status ||= 200
65
+
66
+ response_hash = {success: @success}
67
+ response_hash[:alert] = @alert unless @alert.blank?
68
+ response_hash[:data] = @data unless @data.blank?
69
+ response_hash[:errors] = @errors unless @errors.blank?
70
+
71
+ response_hash[:total_data] = @total_data unless @total_data.blank?
72
+ response_hash[:per_page] = @per_page unless @per_page.blank?
73
+ response_hash[:current_page] = @current_page unless @current_page.blank?
74
+
75
+ render status: @status, json: response_hash
76
+ return
77
+ end
78
+ end
79
+ end
@@ -11,13 +11,6 @@ module Usman
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
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
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
@@ -109,6 +102,27 @@ module Usman
109
102
  end
110
103
  end
111
104
 
105
+ def require_site_admin
106
+ return true if @current_user && @current_user.super_admin?
107
+ unless @current_user && @current_user.has_role?("Site Admin")
108
+ respond_to do |format|
109
+ format.html {
110
+ #text = "#{I18n.t("authentication.permission_denied.heading")}: #{I18n.t("authentication.permission_denied.message")}"
111
+ #set_flash_message(text, :error, false) if defined?(flash) && flash
112
+ redirect_after_unsuccessful_authentication
113
+ }
114
+ format.js {
115
+ @params_hsh = {}
116
+ @params_hsh[:client_app] = params[:client_app] if params[:client_app]
117
+ @params_hsh[:redirect_back_url] = params[:redirect_back_url] if params[:redirect_back_url]
118
+ @params_hsh[:requested_url] = request.original_url if request.get?
119
+
120
+ render(:partial => 'usman/sessions/sign_in.js.erb', :handlers => [:erb], :formats => [:js])
121
+ }
122
+ end
123
+ end
124
+ end
125
+
112
126
  # This method is only used for masquerading. When admin masquerade as user A and then as B, when he logs out as B he should be logged in back as A
113
127
  # This is accomplished by storing the last user id in session and activating it when user is logged off
114
128
  def restore_last_user
@@ -0,0 +1,149 @@
1
+ class Device < ApplicationRecord
2
+
3
+ # Constants
4
+ PENDING = "pending"
5
+ VERIFIED = "verified"
6
+ BLOCKED = "blocked"
7
+
8
+ STATUS = {
9
+ PENDING => "Pending",
10
+ VERIFIED => "Verified",
11
+ BLOCKED => "Blocked"
12
+ }
13
+
14
+ STATUS_REVERSE = {
15
+ "Pending" => PENDING,
16
+ "Verified" => VERIFIED,
17
+ "Blocked" => BLOCKED
18
+ }
19
+
20
+ # Associations
21
+ belongs_to :user, optional: true
22
+ belongs_to :registration
23
+
24
+ # Validations
25
+ validates :uuid, presence: true, length: {maximum: 1024}
26
+ validates :device_token, presence: true, length: {maximum: 1024}
27
+
28
+ validates :device_name, allow_blank: true, length: {maximum: 64}
29
+ validates :device_type, allow_blank: true, length: {maximum: 64}
30
+ validates :operating_system, allow_blank: true, length: {maximum: 64}
31
+ validates :software_version, allow_blank: true, length: {maximum: 64}
32
+ validates :last_accessed_api, allow_blank: true, length: {maximum: 1024}
33
+
34
+ validates :otp, allow_blank: true, length: {minimum: 5, maximum: 5}
35
+ validates :api_token, allow_blank: true, length: {maximum: 256}
36
+
37
+ validates :status, :presence => true, :inclusion => {:in => STATUS.keys, :presence_of => :status, :message => "%{value} is not a valid status" }
38
+
39
+ # ------------------
40
+ # Class Methods
41
+ # ------------------
42
+
43
+ # return an active record relation object with the search query in its where clause
44
+ # Return the ActiveRecord::Relation object
45
+ # == Examples
46
+ # >>> device.search(query)
47
+ # => ActiveRecord::Relation object
48
+ scope :search, lambda {|query| joins("INNER JOIN registrations on registrations.id = devices.registration_id
49
+ LEFT JOIN users on users.id = devices.user_id").
50
+ where("LOWER(devices.uuid) LIKE LOWER('%#{query}%') OR
51
+ LOWER(devices.device_token) LIKE LOWER('%#{query}%') OR
52
+ LOWER(devices.device_name) LIKE LOWER('%#{query}%') OR
53
+ LOWER(devices.device_type) LIKE LOWER('%#{query}%') OR
54
+ LOWER(registrations.mobile_number) LIKE LOWER('%#{query}%') OR
55
+ LOWER(users.name) LIKE LOWER('%#{query}%')")}
56
+ scope :status, lambda { |status| where("LOWER(status)='#{status}'") }
57
+
58
+ scope :pending, -> { where(status: PENDING) }
59
+ scope :verified, -> { where(status: VERIFIED) }
60
+ scope :blocked, -> { where(status: BLOCKED) }
61
+
62
+ # ------------------
63
+ # Instance Methods
64
+ # ------------------
65
+
66
+ # Status Methods
67
+ # --------------
68
+
69
+ # * Return true if the user is pending, else false.
70
+ # == Examples
71
+ # >>> device.pending?
72
+ # => true
73
+ def pending?
74
+ (status == PENDING)
75
+ end
76
+
77
+ # * Return true if the user is not verified, else false.
78
+ # == Examples
79
+ # >>> device.verified?
80
+ # => true
81
+ def verified?
82
+ (status == VERIFIED)
83
+ end
84
+
85
+ # * Return true if the user is not blocked, else false.
86
+ # == Examples
87
+ # >>> device.blocked?
88
+ # => true
89
+ def blocked?
90
+ (status == BLOCKED)
91
+ end
92
+
93
+ # change the status to :pending
94
+ # Return the status
95
+ # == Examples
96
+ # >>> device.pending!
97
+ # => "pending"
98
+ def pending!
99
+ self.update_attribute(:status, PENDING)
100
+ end
101
+
102
+ # change the status to :verified
103
+ # Return the status
104
+ # == Examples
105
+ # >>> device.verify!
106
+ # => "verified"
107
+ def verify!
108
+ self.update_attribute(:status, VERIFIED)
109
+ end
110
+
111
+ # change the status to :blocked
112
+ # Return the status
113
+ # == Examples
114
+ # >>> device.block!
115
+ # => "blocked"
116
+ def block!
117
+ self.update_attribute(:status, BLOCKED)
118
+ end
119
+
120
+ # Permission Methods
121
+ # ------------------
122
+
123
+ def can_be_edited?
124
+ false
125
+ end
126
+
127
+ def can_be_deleted?
128
+ false
129
+ end
130
+
131
+ # Authentication Methods
132
+ # ----------------------
133
+
134
+ def generate_otp
135
+ self.otp = rand(10000..99999)
136
+ end
137
+
138
+ # Other Methods
139
+ # -------------
140
+
141
+ # * Return full name
142
+ # == Examples
143
+ # >>> device.display_mobile_number
144
+ # => "+919880123456"
145
+ def display_name
146
+ "#{self.device_name} - #{self.uuid}"
147
+ end
148
+
149
+ end
@@ -44,21 +44,25 @@ class Feature < Usman::ApplicationRecord
44
44
  scope :published, -> { where(status: PUBLISHED) }
45
45
  scope :disabled, -> { where(status: DISABLED) }
46
46
 
47
- def self.save_row_data(row)
47
+ def self.save_row_data(hsh)
48
48
 
49
- row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
49
+ return if hsh[:name].blank?
50
50
 
51
- return if row[:name].blank?
52
-
53
- feature = Feature.find_by_name(row[:name]) || Feature.new
54
- feature.name = row[:name]
51
+ feature = Feature.find_by_name(hsh[:name]) || Feature.new
52
+ feature.name = hsh[:name]
55
53
  feature.status = Feature::UNPUBLISHED
56
54
 
57
55
  # Initializing error hash for displaying all errors altogether
58
56
  error_object = Kuppayam::Importer::ErrorHash.new
59
57
 
60
58
  if feature.valid?
61
- feature.save!
59
+ begin
60
+ feature.save!
61
+ rescue Exception => e
62
+ summary = "uncaught #{e} exception while handling connection: #{e.message}"
63
+ details = "Stack trace: #{e.backtrace.map {|l| " #{l}\n"}.join}"
64
+ error_object.errors << { summary: summary, details: details }
65
+ end
62
66
  else
63
67
  summary = "Error while saving feature: #{feature.name}"
64
68
  details = "Error! #{feature.errors.full_messages.to_sentence}"
@@ -27,25 +27,23 @@ class Permission < Usman::ApplicationRecord
27
27
  LOWER(f.name) LIKE LOWER('%#{query}%')")}
28
28
 
29
29
 
30
- def self.save_row_data(row)
30
+ def self.save_row_data(hsh)
31
31
 
32
- row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
33
-
34
- return if row[:user].blank? || row[:feature].blank?
32
+ return if hsh[:user].blank? || hsh[:feature].blank?
35
33
 
36
34
  # Initializing error hash for displaying all errors altogether
37
35
  error_object = Kuppayam::Importer::ErrorHash.new
38
36
 
39
- user = User.find_by_username(row[:user])
37
+ user = User.find_by_username(hsh[:user])
40
38
  unless user
41
- summary = "User '#{row[:user]}' doesn't exist"
39
+ summary = "User '#{hsh[:user]}' doesn't exist"
42
40
  error_object.errors << { summary: summary }
43
41
  return error_object
44
42
  end
45
43
 
46
- feature = Feature.find_by_name(row[:feature])
44
+ feature = Feature.find_by_name(hsh[:feature])
47
45
  unless feature
48
- summary = "Feature '#{row[:feature]}' doesn't exist"
46
+ summary = "Feature '#{hsh[:feature]}' doesn't exist"
49
47
  error_object.errors << { summary: summary }
50
48
  return error_object
51
49
  end
@@ -53,13 +51,19 @@ class Permission < Usman::ApplicationRecord
53
51
  permission = Permission.where("user_id = ? AND feature_id = ?", user.id, feature.id).first || Permission.new
54
52
  permission.user = user
55
53
  permission.feature = feature
56
- permission.can_create = row[:can_create]
57
- permission.can_read = row[:can_read]
58
- permission.can_update = row[:can_update]
59
- permission.can_delete = row[:can_delete]
54
+ permission.can_create = hsh[:can_create]
55
+ permission.can_read = hsh[:can_read]
56
+ permission.can_update = hsh[:can_update]
57
+ permission.can_delete = hsh[:can_delete]
60
58
 
61
59
  if permission.valid?
62
- permission.save!
60
+ begin
61
+ permission.save!
62
+ rescue Exception => e
63
+ summary = "uncaught #{e} exception while handling connection: #{e.message}"
64
+ details = "Stack trace: #{e.backtrace.map {|l| " #{l}\n"}.join}"
65
+ error_object.errors << { summary: summary, details: details }
66
+ end
63
67
  else
64
68
  summary = "Error while saving permission: #{user.name} - #{feature.name}"
65
69
  details = "Error! #{permission.errors.full_messages.to_sentence}"