anoubis 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +28 -0
  4. data/Rakefile +38 -0
  5. data/app/controllers/anoubis/application_controller.rb +78 -0
  6. data/app/controllers/anoubis/core/application_controller.rb +343 -0
  7. data/app/controllers/anoubis/core/data/actions.rb +962 -0
  8. data/app/controllers/anoubis/core/data/callbacks.rb +68 -0
  9. data/app/controllers/anoubis/core/data/convert.rb +407 -0
  10. data/app/controllers/anoubis/core/data/defaults.rb +217 -0
  11. data/app/controllers/anoubis/core/data/get.rb +531 -0
  12. data/app/controllers/anoubis/core/data/load.rb +89 -0
  13. data/app/controllers/anoubis/core/data/set.rb +49 -0
  14. data/app/controllers/anoubis/core/data/setup.rb +104 -0
  15. data/app/controllers/anoubis/core/data_controller.rb +28 -0
  16. data/app/controllers/anoubis/core/index/actions.rb +53 -0
  17. data/app/controllers/anoubis/core/index/callbacks.rb +23 -0
  18. data/app/controllers/anoubis/core/index_controller.rb +36 -0
  19. data/app/controllers/anoubis/etc/base.rb +52 -0
  20. data/app/controllers/anoubis/etc/data.rb +89 -0
  21. data/app/controllers/anoubis/etc/field.rb +468 -0
  22. data/app/controllers/anoubis/etc/field_options.rb +83 -0
  23. data/app/controllers/anoubis/etc/field_order.rb +51 -0
  24. data/app/controllers/anoubis/etc/filter.rb +251 -0
  25. data/app/controllers/anoubis/etc/menu.rb +101 -0
  26. data/app/controllers/anoubis/etc/model.rb +67 -0
  27. data/app/controllers/anoubis/etc/tab_item.rb +91 -0
  28. data/app/controllers/anoubis/etc.rb +8 -0
  29. data/app/controllers/anoubis/export.rb +47 -0
  30. data/app/controllers/anoubis/output/autocomplete.rb +30 -0
  31. data/app/controllers/anoubis/output/basic.rb +86 -0
  32. data/app/controllers/anoubis/output/data.rb +101 -0
  33. data/app/controllers/anoubis/output/delete.rb +41 -0
  34. data/app/controllers/anoubis/output/edit.rb +55 -0
  35. data/app/controllers/anoubis/output/frame.rb +227 -0
  36. data/app/controllers/anoubis/output/login.rb +71 -0
  37. data/app/controllers/anoubis/output/menu.rb +220 -0
  38. data/app/controllers/anoubis/output/update.rb +43 -0
  39. data/app/controllers/anoubis/sso/client/application_controller.rb +139 -0
  40. data/app/controllers/anoubis/sso/client/data/actions.rb +5 -0
  41. data/app/controllers/anoubis/sso/client/data/callbacks.rb +5 -0
  42. data/app/controllers/anoubis/sso/client/data/convert.rb +5 -0
  43. data/app/controllers/anoubis/sso/client/data/defaults.rb +5 -0
  44. data/app/controllers/anoubis/sso/client/data/get.rb +5 -0
  45. data/app/controllers/anoubis/sso/client/data/load.rb +26 -0
  46. data/app/controllers/anoubis/sso/client/data/set.rb +5 -0
  47. data/app/controllers/anoubis/sso/client/data/setup.rb +5 -0
  48. data/app/controllers/anoubis/sso/client/data_controller.rb +21 -0
  49. data/app/controllers/anoubis/sso/client/index/actions.rb +79 -0
  50. data/app/controllers/anoubis/sso/client/index/callbacks.rb +13 -0
  51. data/app/controllers/anoubis/sso/client/index_controller.rb +18 -0
  52. data/app/controllers/anoubis/sso/server/application_controller.rb +49 -0
  53. data/app/controllers/anoubis/sso/server/login_controller.rb +342 -0
  54. data/app/controllers/anoubis/sso/server/user_controller.rb +142 -0
  55. data/app/controllers/anoubis/tenant/application_controller.rb +54 -0
  56. data/app/controllers/anoubis/tenant/data/actions.rb +11 -0
  57. data/app/controllers/anoubis/tenant/data/callbacks.rb +11 -0
  58. data/app/controllers/anoubis/tenant/data/convert.rb +11 -0
  59. data/app/controllers/anoubis/tenant/data/defaults.rb +11 -0
  60. data/app/controllers/anoubis/tenant/data/get.rb +11 -0
  61. data/app/controllers/anoubis/tenant/data/load.rb +52 -0
  62. data/app/controllers/anoubis/tenant/data/set.rb +11 -0
  63. data/app/controllers/anoubis/tenant/data/setup.rb +11 -0
  64. data/app/controllers/anoubis/tenant/data_controller.rb +28 -0
  65. data/app/controllers/anoubis/tenant/index/actions.rb +191 -0
  66. data/app/controllers/anoubis/tenant/index/callbacks.rb +11 -0
  67. data/app/controllers/anoubis/tenant/index_controller.rb +38 -0
  68. data/app/controllers/anoubis/tenants_controller.rb +7 -0
  69. data/app/controllers/anoubis/users_controller.rb +7 -0
  70. data/app/jobs/anoubis/application_job.rb +6 -0
  71. data/app/mailers/anoubis/application_mailer.rb +8 -0
  72. data/app/models/anoubis/application_record.rb +45 -0
  73. data/app/models/anoubis/core/application_record.rb +250 -0
  74. data/app/models/anoubis/core/locales.rb +27 -0
  75. data/app/models/anoubis/sso/client/application_record.rb +3 -0
  76. data/app/models/anoubis/sso/client/group.rb +19 -0
  77. data/app/models/anoubis/sso/client/group_menu.rb +109 -0
  78. data/app/models/anoubis/sso/client/menu.rb +145 -0
  79. data/app/models/anoubis/sso/client/user.rb +81 -0
  80. data/app/models/anoubis/sso/client/user_group.rb +32 -0
  81. data/app/models/anoubis/sso/server/system.rb +36 -0
  82. data/app/models/anoubis/sso/server/user.rb +79 -0
  83. data/app/models/anoubis/tenant/application_record.rb +41 -0
  84. data/app/models/anoubis/tenant/group.rb +95 -0
  85. data/app/models/anoubis/tenant/group_locale.rb +19 -0
  86. data/app/models/anoubis/tenant/group_menu.rb +84 -0
  87. data/app/models/anoubis/tenant/menu.rb +156 -0
  88. data/app/models/anoubis/tenant/menu_locale.rb +27 -0
  89. data/app/models/anoubis/tenant/system.rb +127 -0
  90. data/app/models/anoubis/tenant/system_locale.rb +19 -0
  91. data/app/models/anoubis/tenant/system_menu.rb +51 -0
  92. data/app/models/anoubis/tenant/tenant.rb +107 -0
  93. data/app/models/anoubis/tenant/tenant_system.rb +19 -0
  94. data/app/models/anoubis/tenant/user.rb +225 -0
  95. data/app/models/anoubis/tenant/user_group.rb +32 -0
  96. data/app/services/anoubis/core_service.rb +16 -0
  97. data/app/services/anoubis/session_service.rb +17 -0
  98. data/app/validators/presence_in_tenant_validator.rb +20 -0
  99. data/config/initializers/mime_type.rb +1 -0
  100. data/config/locales/en.yml +120 -0
  101. data/config/locales/ru.yml +245 -0
  102. data/config/routes.rb +74 -0
  103. data/db/migrate/20181018085843_create_tenants.rb +13 -0
  104. data/db/migrate/20181018111217_create_systems.rb +10 -0
  105. data/db/migrate/20181018111713_create_tenant_systems.rb +11 -0
  106. data/db/migrate/20181018111925_create_groups.rb +13 -0
  107. data/db/migrate/20181018112151_create_users.rb +25 -0
  108. data/db/migrate/20181018115737_add_title_to_users.rb +10 -0
  109. data/db/migrate/20181022060211_create_menus.rb +18 -0
  110. data/db/migrate/20181115055245_create_group_menus.rb +12 -0
  111. data/db/migrate/20181115060830_create_system_menus.rb +11 -0
  112. data/db/migrate/20181122062131_create_user_groups.rb +11 -0
  113. data/db/migrate/20181221060727_create_menu_locales.rb +14 -0
  114. data/db/migrate/20181225062303_create_system_locales.rb +11 -0
  115. data/db/migrate/20181225062339_create_group_locales.rb +11 -0
  116. data/db/seeds.rb +268 -0
  117. data/lib/anoubis/engine.rb +13 -0
  118. data/lib/anoubis/version.rb +5 -0
  119. data/lib/anoubis.rb +213 -0
  120. data/lib/tasks/anubis_tasks.rake +10 -0
  121. data/lib/tasks/sessions/clear_sessions.rake +10 -0
  122. data/spec/anubis_spec.rb +5 -0
  123. data/spec/controllers/anoubis/index_controller_spec.rb +77 -0
  124. data/spec/dummy/Rakefile +3 -0
  125. data/spec/dummy/app/assets/config/manifest.js +2 -0
  126. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  127. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  128. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  129. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  130. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  131. data/spec/dummy/app/jobs/application_job.rb +2 -0
  132. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  133. data/spec/dummy/app/models/application_record.rb +3 -0
  134. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  135. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  136. data/spec/dummy/bin/bundle +3 -0
  137. data/spec/dummy/bin/rails +4 -0
  138. data/spec/dummy/bin/rake +4 -0
  139. data/spec/dummy/bin/setup +33 -0
  140. data/spec/dummy/bin/update +28 -0
  141. data/spec/dummy/config/application.rb +14 -0
  142. data/spec/dummy/config/boot.rb +5 -0
  143. data/spec/dummy/config/cable.yml +10 -0
  144. data/spec/dummy/config/database.yml +54 -0
  145. data/spec/dummy/config/environment.rb +5 -0
  146. data/spec/dummy/config/environments/development.rb +54 -0
  147. data/spec/dummy/config/environments/production.rb +85 -0
  148. data/spec/dummy/config/environments/test.rb +46 -0
  149. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  150. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  151. data/spec/dummy/config/initializers/cors.rb +16 -0
  152. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  153. data/spec/dummy/config/initializers/inflections.rb +16 -0
  154. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  155. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  156. data/spec/dummy/config/locales/en.yml +33 -0
  157. data/spec/dummy/config/puma.rb +34 -0
  158. data/spec/dummy/config/routes.rb +3 -0
  159. data/spec/dummy/config/spring.rb +6 -0
  160. data/spec/dummy/config/storage.yml +34 -0
  161. data/spec/dummy/config.ru +5 -0
  162. data/spec/dummy/db/schema.rb +167 -0
  163. data/spec/dummy/db/seeds.rb +1 -0
  164. data/spec/factories/anubis_group_locales.rb +7 -0
  165. data/spec/factories/anubis_group_menus.rb +7 -0
  166. data/spec/factories/anubis_groups.rb +6 -0
  167. data/spec/factories/anubis_menu_locales.rb +9 -0
  168. data/spec/factories/anubis_menus.rb +6 -0
  169. data/spec/factories/anubis_system_locales.rb +7 -0
  170. data/spec/factories/anubis_system_menus.rb +6 -0
  171. data/spec/factories/anubis_systems.rb +5 -0
  172. data/spec/factories/anubis_tenants.rb +7 -0
  173. data/spec/factories/anubis_users.rb +10 -0
  174. data/spec/integration/navigation_test.rb +7 -0
  175. data/spec/models/anoubis/group_locale_spec.rb +25 -0
  176. data/spec/models/anoubis/group_menu_spec.rb +50 -0
  177. data/spec/models/anoubis/group_spec.rb +52 -0
  178. data/spec/models/anoubis/menu_locale_spec.rb +31 -0
  179. data/spec/models/anoubis/menu_spec.rb +48 -0
  180. data/spec/models/anoubis/system_locale_spec.rb +20 -0
  181. data/spec/models/anoubis/system_menu_spec.rb +49 -0
  182. data/spec/models/anoubis/system_spec.rb +53 -0
  183. data/spec/models/anoubis/tenant_spec.rb +67 -0
  184. data/spec/models/anoubis/user_spec.rb +57 -0
  185. data/spec/rails_helper.rb +32 -0
  186. data/spec/requests/anoubis/users_request_spec.rb +5 -0
  187. data/spec/spec_helper.rb +13 -0
  188. metadata +408 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c2ef6417845dbb20f56a6bb3bdd5e304b442f681928f9f64765d68d1bf5f4c57
4
+ data.tar.gz: d2a87698442e6d1807ae9ed3104a706053f45654f80da248db873bc7a0a16a80
5
+ SHA512:
6
+ metadata.gz: 9bba0811a68af029fbe976c8369372e7026d328386895892c9300d2e5f4f448863f3e5fb54e123d2987e1d214620cde0d7fa9d43244a64ab7438110949e91ad0
7
+ data.tar.gz: a588b8aff9758aa25f20e709ceb2f86d72090a3d281b7cdb71ad97ac93fe9d771414b09daa571e7511c2049e723b8a32e92d894dc8c4cd4ec29c3af1eaecb470
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Andrey Ryabov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Anoubis
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'anoubis'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install anoubis
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Anoubis'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ desc "Run all specs in spec directory (excluding plugin specs)"
25
+ #RSpec::Core::RakeTask.new(:spec => 'app:db:migrate')
26
+ #RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
27
+
28
+ #task :default => :spec
29
+
30
+ #require 'rake/testtask'
31
+
32
+ #Rake::TestTask.new(:test) do |t|
33
+ # t.libs << 'test'
34
+ # t.pattern = 'test/**/*_test.rb'
35
+ # t.verbose = false
36
+ #end
37
+
38
+ #task default: :test
@@ -0,0 +1,78 @@
1
+ ## Main application controller class inherited from {https://api.rubyonrails.org/v6.1.4/classes/ActionController/API.html ActionController::API}
2
+ class Anoubis::ApplicationController < ActionController::API
3
+ prepend_before_action :pba_anoubis_application
4
+
5
+ include ActionController::Cookies
6
+
7
+ ## Redis database variable
8
+ attr_accessor :redis
9
+
10
+ ## Current used locale
11
+ attr_accessor :locale
12
+
13
+ ##
14
+ # Returns default locale initialized in application configuration file. Variable is taken from {https://guides.rubyonrails.org/i18n.html Rails.configuration.i18n.default_locale} parameter
15
+ # @return [String] default locale
16
+ def default_locale
17
+ Rails.configuration.i18n.default_locale.to_s
18
+ end
19
+
20
+ ## Returns {https://github.com/redis/redis-rb Redis} prefix for storing cache data
21
+ attr_accessor :redis_prefix
22
+
23
+ ##
24
+ # Returns {https://github.com/redis/redis-rb Redis database} class
25
+ # @return [Class] {https://github.com/redis/redis-rb Redis} class reference
26
+ def redis
27
+ @redis ||= Redis.new
28
+ end
29
+
30
+ ##
31
+ # Returns {https://github.com/redis/redis-rb Redis} prefix for storing cache data. Prefix can be set in Rails.configuration.anoubis_redis_prefix configuration parameter.
32
+ # @return [String] {https://github.com/redis/redis-rb Redis} prefix
33
+ def redis_prefix
34
+ @redis_prefix ||= get_redis_prefix
35
+ end
36
+
37
+ private def get_redis_prefix
38
+ begin
39
+ value = Rails.configuration.anoubis_redis_prefix
40
+ rescue
41
+ return ''
42
+ end
43
+
44
+ value + ':'
45
+ end
46
+
47
+ ##
48
+ # Procedure fires before any action and setup default variables.
49
+ def pba_anoubis_application
50
+ self.locale = params[:locale] if params.has_key? :locale
51
+ self.locale = default_locale unless self.locale
52
+ self.locale = default_locale if self.locale == ''
53
+ begin
54
+ I18n.locale = locale
55
+ rescue
56
+ I18n.locale = default_locale
57
+ end
58
+ end
59
+
60
+ ##
61
+ # Generates options headers for CORS requests
62
+ # @param methods [String] list of allowed HTTP actions separated by space <i>(e.g. 'GET POST DELETE')</i>
63
+ def options(methods = 'POST')
64
+ if check_origin
65
+ headers['Access-Control-Allow-Origin'] = request.headers['origin']
66
+ headers['Access-Control-Allow-Methods'] = methods
67
+ headers['Access-Control-Max-Age'] = '1000'
68
+ headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Content-Type,Authorization'
69
+ end
70
+ end
71
+
72
+ ##
73
+ # Check current origin of header. By default origin always valid
74
+ # @return [Boolean] request host origin validation
75
+ def check_origin
76
+ true
77
+ end
78
+ end
@@ -0,0 +1,343 @@
1
+ ##
2
+ # Application controller for Anubis library.
3
+ class Anubis::Core::ApplicationController < ActionController::API
4
+ prepend_before_action :anubis_core_initialization
5
+
6
+ #include AbstractController::Translation
7
+ include ActionController::MimeResponds
8
+ #include ActionController::Parameters
9
+ #include ActionDispatch::Http::Parameters
10
+ #include ActionDispatch::Request
11
+
12
+ # @!attribute [rw] version
13
+ # @return [Integer] Specifies the api version. Parameters receive from URL <i>(defaults to: 0)</i>.
14
+ attr_accessor :version
15
+
16
+ # @!attribute [rw] locale
17
+ # @return [String] Specifies the current language locale <i>(defaults to: 'ru')</i>.
18
+ # Parameters receive from URL or user definition
19
+ attr_accessor :locale
20
+
21
+ # @!attribute [rw] current_user
22
+ # @return [ActiveRecord] Specifies current user <i>(defaults to: nil)</i>.
23
+ attr_accessor :current_user
24
+
25
+ # @!attribute [rw] output
26
+ # @return [Anubis::Output] standard output.
27
+ attr_accessor :output
28
+
29
+ # @!attribute [rw] writer
30
+ # @return [Object] Specifies access of current user to this controller <i>(defaults to: false)</i>.
31
+ attr_accessor :writer
32
+
33
+ # @!attribute [rw] etc
34
+ # @return [Anubis::Etc::Base] global system parameters
35
+ attr_accessor :etc
36
+
37
+ # @!attribute [rw] exports
38
+ # @return [Anubis::Export] Export data class
39
+ attr_accessor :exports
40
+
41
+ ##
42
+ # Returns redis database class
43
+ def redis
44
+ @redis ||= Redis.new
45
+ end
46
+
47
+ ##
48
+ # Sets default parameters for application controller.
49
+ def anubis_core_initialization
50
+ self.version = 0
51
+
52
+ if defined? params
53
+ self.etc = Anubis::Etc::Base.new({ params: params })
54
+ else
55
+ self.etc = Anubis::Etc::Base.new
56
+ end
57
+ self.output = nil
58
+ self.exports = nil
59
+ self.writer = false
60
+
61
+ self.current_user = nil
62
+ self.locale = params[:locale] if params.has_key? :locale
63
+ self.locale = 'ru' unless self.locale
64
+ self.locale = 'ru' if self.locale == ''
65
+ begin
66
+ I18n.locale = self.locale
67
+ rescue
68
+ I18n.locale = 'ru'
69
+ end
70
+
71
+ return if request.method == 'OPTIONS'
72
+
73
+ if !params.has_key? :version
74
+ self.error_exit({ error: I18n.t('errors.no_api_version') })
75
+ return
76
+ end
77
+
78
+ if self.access_allowed?
79
+ self.set_access_control_headers
80
+ else
81
+ self.error_exit({ error: I18n.t('errors.access_not_allowed') })
82
+ end
83
+
84
+ self.version = params[:version]
85
+
86
+ if self.authenticate?
87
+ if self.authentication
88
+ if self.check_menu_access?
89
+ return if !self.menu_access params[:controller]
90
+ end
91
+ end
92
+ end
93
+
94
+ #self.user_time_zone if self.current_user
95
+ Time.zone = self.current_user.timezone if self.current_user
96
+ self.after_initialization
97
+ end
98
+
99
+ ##
100
+ # Calls after first controller initialization
101
+ def after_initialization
102
+
103
+ end
104
+
105
+
106
+ ##
107
+ # Gracefully terminate script execution with code 422 (Unprocessable entity). And JSON data
108
+ # @param data [Hash] Resulting data
109
+ # @option data [Integer] :code resulting error code
110
+ # @option data [String] :error resulting error message
111
+ def error_exit(data)
112
+ result = {
113
+ result: -1,
114
+ message: 'Error'
115
+ }
116
+ result[:result] = data[:code] if data.has_key? :code
117
+ result[:message] = data[:error] if data.has_key? :error
118
+ respond_to do |format|
119
+ format.json { render json: result, status: :unprocessable_entity }
120
+ end
121
+ begin
122
+ exit
123
+ rescue SystemExit => e
124
+
125
+ end
126
+ end
127
+
128
+ ##
129
+ # Get current user model
130
+ # @return [ActiveRecord] defined user model. It is used for get current user data. May be redefined when user model is changed
131
+ def get_user_model
132
+ nil
133
+ end
134
+
135
+ ##
136
+ # Get current user model filed json exception
137
+ # @return [Array] defined user exception for to_json function
138
+ def get_user_model_except
139
+ []
140
+ end
141
+
142
+ ##
143
+ # @!group Block of authorization
144
+
145
+ ##
146
+ # Checks if needed user authentication.
147
+ # @return [Boolean] if true, then user must be authenticated.
148
+ def authenticate?
149
+ return true
150
+ end
151
+
152
+ ##
153
+ # Authenticates user in the system
154
+ def authentication
155
+ if !self.token
156
+ self.error_exit({ error: I18n.t('errors.authentication_required') })
157
+ return false
158
+ end
159
+
160
+ # Check session presence
161
+ session = self.redis.get(self.redis_prefix + 'session:' + self.token)
162
+ if !session
163
+ self.error_exit({ error: I18n.t('errors.session_expired') })
164
+ return false
165
+ end
166
+
167
+ session = JSON.parse(session, { symbolize_names: true })
168
+
169
+ if !session.has_key?(:uuid) || !session.has_key?(:ttl)
170
+ self.error_exit({ error: I18n.t('errors.session_expired') })
171
+ return false
172
+ end
173
+
174
+ if session[:ttl] < Time.now
175
+ self.error_exit({ error: I18n.t('errors.session_expired') })
176
+ self.redis.del(self.redis_prefix + 'session:' + self.token)
177
+ return false
178
+ end
179
+
180
+ # Load user data from redis database
181
+ user_json = self.redis.get(self.redis_prefix + 'user:' + session[:uuid])
182
+ if !user_json
183
+ # Check user presence based on session user UUID
184
+ user = self.get_user_model.where(uuid_bin: self.uuid_to_bin(session[:uuid])).first
185
+ if !user
186
+ self.error_exit({ error: I18n.t('errors.authentication_required') })
187
+ return false
188
+ end
189
+ user_json = self.redis_save_user user
190
+ end
191
+
192
+ begin
193
+ self.current_user = self.get_user_model.new(JSON.parse(user_json,{ symbolize_names: true }))
194
+ rescue
195
+ self.current_user = nil
196
+ end
197
+
198
+ if !self.current_user
199
+ self.error_exit({ error: I18n.t('errors.authentication_required') })
200
+ return false
201
+ end
202
+
203
+ session[:time] = Time.now
204
+ session[:ttl] = session[:time] + self.current_user.timeout
205
+ self.redis.set(self.redis_prefix + 'session:' + self.token, session.to_json)
206
+
207
+ true
208
+ end
209
+
210
+ ##
211
+ # Checks user must have access for current controller.
212
+ # @return [Boolean] if true, then user must have access for this controller.
213
+ def check_menu_access?
214
+ true
215
+ end
216
+
217
+ ##
218
+ # Check menu access for current user of current controller
219
+ # @return [Boolean] if true, then user have access for this controller.
220
+ def menu_access(controller, exit = true)
221
+ self.writer = true
222
+
223
+ true
224
+ end
225
+
226
+ ##
227
+ # Get current token based on HTTP Authorization
228
+ # @return [String] current token
229
+ def token
230
+ if Rails.env.development?
231
+ return params[:token] if params[:token]
232
+ end
233
+ request.env.fetch('HTTP_AUTHORIZATION', '').scan(/Bearer (.*)$/).flatten.last
234
+ end
235
+
236
+ # @!endgroup
237
+
238
+ ##
239
+ # Check access for API.
240
+ # @return [Boolean] access for requested client
241
+ def access_allowed?
242
+ allowed_sites = [request.env['HTTP_ORIGIN']]
243
+
244
+ allowed_sites.include?(request.env['HTTP_ORIGIN'])
245
+ end
246
+
247
+ ##
248
+ # Set allow header information for multi-domain requests. Requested for browsers when API is not in the same
249
+ # address as Frontend application.
250
+ def set_access_control_headers
251
+ headers['Access-Control-Allow-Origin'] = request.env['HTTP_ORIGIN']
252
+ headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, DELETE, PUT, PATCH'
253
+ headers['Access-Control-Max-Age'] = '1000'
254
+ headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Content-Type,Authorization'
255
+ end
256
+
257
+ ##
258
+ # @!group Block of UUID functions
259
+
260
+ ##
261
+ # Decodes binary UUID data into the UUID string
262
+ # @param data [Binary] binary representation of UUID
263
+ # @return [String, nil] string representation of UUID or nil if can't be decoded
264
+ def bin_to_uuid(data)
265
+ begin
266
+ data = data.unpack('H*')[0]
267
+ return data[0..7]+'-'+data[8..11]+'-'+data[12..15]+'-'+data[16..19]+'-'+data[20..31]
268
+ rescue
269
+ return nil
270
+ end
271
+ end
272
+
273
+ ##
274
+ # Encodes string UUID data into the binary UUID
275
+ # @param data [Binary] string representation of UUID
276
+ # @return [Binary, nil] binary representation of UUID or nil if can't be encoded
277
+ def uuid_to_bin(data)
278
+ begin
279
+ return [data.delete('-')].pack('H*')
280
+ rescue
281
+ return nil
282
+ end
283
+ end
284
+
285
+ ##
286
+ # Generates new UUID data
287
+ # @return [String] string representation of UUID
288
+ def new_uuid
289
+ SecureRandom.uuid
290
+ end
291
+
292
+ ##
293
+ # Generates new session ID
294
+ # @return [string] string representation of session (64 bytes)
295
+ def new_session_id
296
+ SecureRandom.hex(32)
297
+ end
298
+
299
+ # @!endgroup
300
+
301
+ ##
302
+ # Saves user data into redis database and returns user JSON representation
303
+ # @param user [ActiveRecord] current user data
304
+ # @return [String] JSON representation of user data
305
+ def redis_save_user(user)
306
+ user_json = user.to_json(except: self.get_user_model_except)
307
+ user_hash = JSON.parse user_json, { symbolize_names: true }
308
+ user_hash[:uuid] = user.uuid
309
+ user_json = user_hash.to_json
310
+ self.redis.set(self.redis_prefix + 'user:' + user.uuid, user_json)
311
+
312
+ user_json
313
+ end
314
+
315
+ ##
316
+ # Returns defined application prefix for redis cache for controller. Default value ''
317
+ def redis_prefix
318
+ begin
319
+ value = Rails.configuration.redis_prefix
320
+ rescue
321
+ return ''
322
+ end
323
+
324
+ value + ':'
325
+ end
326
+
327
+ ##
328
+ # Returns default defined locale
329
+ def default_locale
330
+ Rails.configuration.i18n.default_locale.to_s
331
+ end
332
+
333
+ ##
334
+ # Default route for OPTIONS method
335
+ def options
336
+ if self.access_allowed?
337
+ self.set_access_control_headers
338
+ head :ok
339
+ else
340
+ head :forbidden
341
+ end
342
+ end
343
+ end