mks_auth 1.0.14 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +17 -3
  3. data/app/assets/config/mks_auth_manifest.js +2 -0
  4. data/app/assets/javascripts/mks_auth/application.js +13 -0
  5. data/app/assets/stylesheets/mks_auth/application.css +15 -0
  6. data/app/controllers/mks/auth/access_controller.rb +51 -0
  7. data/app/controllers/mks/auth/application_controller.rb +13 -39
  8. data/app/controllers/mks/auth/application_modules_controller.rb +2 -0
  9. data/app/controllers/mks/auth/user_roles_controller.rb +32 -40
  10. data/app/controllers/mks/auth/users_controller.rb +29 -47
  11. data/app/helpers/mks/auth/access_helper.rb +47 -0
  12. data/app/helpers/mks/auth/application_helper.rb +10 -0
  13. data/app/jobs/mks_auth/application_job.rb +4 -0
  14. data/app/mailers/mks_auth/application_mailer.rb +6 -0
  15. data/app/models/mks/auth/application_module.rb +3 -3
  16. data/app/models/mks/auth/menu.rb +5 -5
  17. data/app/models/mks/auth/user.rb +9 -35
  18. data/app/models/mks/auth/user_role.rb +5 -10
  19. data/config/routes.rb +20 -16
  20. data/db/migrate/20161029065810_create_mks_auth_application_modules.rb +1 -1
  21. data/db/migrate/20161029065959_create_mks_auth_users.rb +4 -1
  22. data/db/migrate/20161029070807_create_mks_auth_user_roles.rb +2 -3
  23. data/db/migrate/20161029071047_create_mks_users_user_roles.rb +4 -4
  24. data/db/migrate/20161029072256_create_mks_auth_menus.rb +3 -3
  25. data/db/migrate/20161029074023_create_mks_menus_user_roles.rb +3 -3
  26. data/lib/mks/auth/engine.rb +12 -5
  27. data/lib/mks/auth/version.rb +1 -1
  28. data/lib/mks_auth.rb +0 -1
  29. data/spec/controllers/mks/auth/users_controller_spec.rb +2 -91
  30. data/spec/dummy/config/application.rb +0 -2
  31. data/spec/dummy/config/database.yml +3 -5
  32. data/spec/dummy/config/initializers/new_framework_defaults.rb +1 -1
  33. data/spec/dummy/db/schema.rb +35 -442
  34. data/spec/factories/application_modules.rb +1 -1
  35. data/spec/factories/menus.rb +2 -2
  36. data/spec/factories/user_roles.rb +1 -2
  37. data/spec/factories/users.rb +3 -2
  38. data/spec/models/mks/auth/application_module_spec.rb +1 -2
  39. data/spec/models/mks/auth/user_role_spec.rb +0 -6
  40. data/spec/models/mks/auth/user_spec.rb +0 -33
  41. data/spec/rails_helper.rb +0 -1
  42. data/spec/spec_helper.rb +1 -1
  43. data/spec/support/factory_girl.rb +2 -2
  44. metadata +95 -140
  45. data/app/controllers/mks/auth/menus_controller.rb +0 -29
  46. data/db/migrate/20180201104912_create_mks_users_application_modules.rb +0 -12
  47. data/lib/mks/auth/token_auth.rb +0 -19
  48. data/spec/controllers/mks/auth/menus_controller_spec.rb +0 -53
  49. data/spec/controllers/mks/auth/user_roles_controller_spec.rb +0 -129
  50. data/spec/dummy/log/development.log +0 -16526
  51. data/spec/dummy/log/test.log +0 -49665
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b4b274de2615dc2349c98ab4541369830d40dc34b08fa03d0b0097353c5f121
4
- data.tar.gz: 0b9f5fe417c673964aa754a5fb0e37fd588e919825a27086181e9331fd9e4f89
3
+ metadata.gz: c9befc13ee79dba3fce47aa04621b0cbab4983d6f32e22eefa8f8ea038dc0235
4
+ data.tar.gz: 8799d30c67e91c2f773bae8715e17a6b17bbf2fcff1bb1a232761955f142778a
5
5
  SHA512:
6
- metadata.gz: 043fecd8660069c8c8c2f62241ddd7dbac398e0fee0bc4cc18c6de8a9a0c7414c078aa4547d1c535c53e9906cc156cd02bd869546b17038ee6493be1e7aa8377
7
- data.tar.gz: c4cdb4314dc64bcf83de5b2271f229fe8de09be45c8027df76dbcfeb7a9cea75037ec465b874c52c33f9085cc5d805ccf2588f1c6cbbecefaf05dd47bbd65da3
6
+ metadata.gz: bded2f7264869bf5dfb2437d39f5e6c5950a1c949c9effc36522fc547f40299dabb9e9e72b87a7f852af39f4daf7f4e3bd6b0ad2a0f5cbe02a278b190eb21e70
7
+ data.tar.gz: 510531747665163e392c2593ce3f9815c506d7555347a7e2a20da552954b803bf7cbd0aa7a2bcbbab34242a062d99cc5f29ba9b236c89b00cd031e8882a81a17
data/Rakefile CHANGED
@@ -14,10 +14,24 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
- APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
17
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
18
+
18
19
  load 'rails/tasks/engine.rake'
20
+
19
21
  load 'rails/tasks/statistics.rake'
20
22
 
21
- # require 'bundler/gem_tasks'
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
26
+
27
+ require 'bundler/gem_tasks'
28
+
29
+ require 'rspec/core'
30
+
22
31
  require 'rspec/core/rake_task'
23
- RSpec::Core::RakeTask.new(spec: 'app:db:test:prepare')
32
+
33
+ desc 'Run all specs in spec directory (excluding plugin specs)'
34
+
35
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
36
+
37
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/mks_auth .js
2
+ //= link_directory ../stylesheets/mks_auth .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,51 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
3
+ module Mks
4
+ module Auth
5
+ class AccessController < ApplicationController
6
+ before_action :confirm_logged_in, :except => [:attempt_login, :logout, :menu, :csrf_token]
7
+
8
+ def csrf_token
9
+ cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
10
+ render json: {success: true}
11
+ end
12
+
13
+ def attempt_login
14
+ code = Rails.configuration.app_code
15
+ app_module = ApplicationModule.find_by(code: code)
16
+ user = User.find_by(email: params[:email].downcase)
17
+
18
+ if user && user.application_module.id == app_module.id
19
+ if user.authenticate(params[:password])
20
+ login_user user
21
+ roles = user.roles.map(&:name)
22
+ response = { success: true, data: {user_id: user.id, user_full_name: user.full_name, roles: roles }}
23
+ render json: response
24
+ else
25
+ render json: {success: false, errors: "Invalid username or password"}
26
+ end
27
+ else
28
+ render json: {success: false, errors: "User doesn't exist or is not allowed!"}
29
+ end
30
+ end
31
+
32
+ def logout
33
+ logout_user if logged_in?
34
+ render json: { success: true }
35
+ end
36
+
37
+ def menu
38
+ render json: {success: true, data: fetch_menus}
39
+ end
40
+
41
+ def check_login
42
+ if session[:user_id]
43
+ user = User.find(session[:user_id])
44
+ render json: {success: true, data: user.full_name}
45
+ else
46
+ render json: {success: false}
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,50 +1,24 @@
1
- require 'mks/auth/token_auth'
2
-
3
1
  module Mks
4
2
  module Auth
5
- class ApplicationController < ActionController::API
6
- before_action :authenticate
7
-
8
- def logged_in?
9
- !current_user.nil?
10
- end
11
-
12
- def current_user
13
- return unless auth_present?
14
- user = User.find(auth['id'])
15
- @current_user ||= user if user
16
- end
17
-
18
- def current_user_id
19
- auth['id']
20
- end
21
-
22
- def app_code
23
- Rails.application.config.app_code
24
- end
3
+ class ApplicationController < ActionController::Base
4
+ include AccessHelper
5
+ include ApplicationHelper
25
6
 
26
7
  private
27
8
 
28
- def authenticate
29
- render json: { error: 'Unauthorized' }, status: 401 unless logged_in?
30
- end
31
-
32
- def token
33
- request.env['HTTP_AUTHORIZATION'].scan(/Bearer (.*)$/).flatten.last
9
+ def confirm_logged_in
10
+ if session[:user_id]
11
+ true
12
+ else
13
+ redirect_to '/'
14
+ false
15
+ end
34
16
  end
35
17
 
36
- def auth
37
- TokenAuth.decode(token)
38
- end
39
-
40
- def auth_present?
41
- bearer = request.env.fetch('HTTP_AUTHORIZATION', '').scan(/Bearer/).flatten.first
42
- !bearer.nil?
43
- end
18
+ protected
44
19
 
45
- def app_module
46
- code = Rails.configuration.app_code
47
- ApplicationModule.find_by(code: code)
20
+ def verified_request?
21
+ super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN'])
48
22
  end
49
23
  end
50
24
  end
@@ -1,3 +1,5 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
1
3
  module Mks
2
4
  module Auth
3
5
  class ApplicationModulesController < ApplicationController
@@ -1,59 +1,51 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
1
3
  module Mks
2
4
  module Auth
3
5
  class UserRolesController < ApplicationController
4
- before_action :set_role, only: %i[users]
5
- before_action :set_user, only: %i[assigned_roles assign_roles]
6
+ # before_action :confirm_logged_in
6
7
 
7
8
  # GET /user_roles
8
9
  def index
9
- user_roles = UserRole.joins(:application_module)
10
- .where("mks_auth_application_modules.code = '#{app_code}'")
11
- .order(:name)
12
- render json: user_roles
13
- end
14
-
15
- def users
16
- render json: @role.users
10
+ @user_roles = UserRole.all.order(:name)
11
+ response = { success: true, data: @user_roles }
12
+ render json: response
17
13
  end
18
14
 
19
- def assigned_roles
20
- selected = @user.roles.map { |ur| { id: ur.id, name: ur.name, selected: true }}
21
- ids = @user.roles.map(&:id)
22
- user_roles = UserRole.joins(:application_module)
23
- .where("mks_auth_application_modules.code = '#{app_code}'")
24
- .where.not('mks_auth_user_roles.id IN (?)', ids)
25
-
26
- not_selected = user_roles.map { |ur| { id: ur.id, name: ur.name, selected: false }}
27
- data = (selected + not_selected).sort_by{ |r| r[:name] }
28
-
15
+ def get_assigned_roles
16
+ user = User.find(params[:user_id])
17
+ user_roles = UserRole.all.order(:name)
18
+ data = []
19
+ user_roles.each do |user_role|
20
+ item = {id: user_role.id, name: user_role.name}
21
+ if user.roles.include? user_role
22
+ item[:selected] = true
23
+ else
24
+ item[:selected] = false
25
+ end
26
+ data << item
27
+ end
29
28
  response = { success: true, data: data }
30
29
  render json: response
31
30
  end
32
31
 
33
- # A method which assigns selected roles to a user.
34
- # This method expects that only selected roles
35
- # (i.e. roles with `selected` attribute set to true).
36
- # It removes all other roles and assigns those selected
37
- # roles to the user.
38
32
  def assign_roles
39
- selected_roles = params[:roles]&.map { |r| r[:id] }
40
- user_roles = UserRole.where(id: selected_roles)
41
- @user.roles.destroy_all
42
- @user.roles = user_roles
43
-
44
- response = { success: true, message: 'Role assignment successful!' }
45
- render json: response
46
- end
33
+ user = User.find(params[:user_id])
34
+ roles = params[:roles]
47
35
 
36
+ roles.each do |role|
37
+ user_role = UserRole.find role[:id]
38
+ if role[:selected]
39
+ user.roles << user_role
40
+ else
41
+ user.roles.destroy user_role
42
+ end
43
+ end
48
44
 
49
- private
45
+ user.save
50
46
 
51
- def set_role
52
- @role = UserRole.find(params[:id])
53
- end
54
-
55
- def set_user
56
- @user = User.find(params[:id])
47
+ response = { success: true, message: 'Role assignment successful!' }
48
+ render json: response
57
49
  end
58
50
  end
59
51
  end
@@ -1,68 +1,58 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
1
3
  module Mks
2
4
  module Auth
3
5
  class UsersController < ApplicationController
4
- skip_before_action :authenticate, raise: false, only: %i[login]
5
- before_action :set_user, only: %i[update user_roles save_selected_roles]
6
+ before_action :set_user, only: [:update]
6
7
 
7
8
  def index
8
- @users = ApplicationModule.find_by(code: app_code).users
9
- render json: @users
9
+ @users = User.where(application_module_id: app_module.id)
10
+ response = { success: true, data: @users }
11
+ render json: response
10
12
  end
11
13
 
12
14
  def roles
13
- user = User.find(params[:id])
14
- render json: user.roles
15
- end
16
-
17
- # A method to fetch all roles, with roles of
18
- # a user marked as selected
19
- def user_roles
20
- all_roles = UserRole.joins(:application_module).where(mks_auth_application_modules: { code: app_code })
21
- roles = all_roles.map { |r| { id: r.id, name: r.name, selected: @user.roles.include?(r) } }
22
- render json: roles
15
+ user = User.find(session[:user_id])
16
+ data = user.roles.map { |role| {id: role.id, name: role.name} }
17
+ response = {success: true, data: data}
18
+ render json: response
23
19
  end
24
20
 
25
- def save_selected_roles
26
- @user.roles.delete_all
27
- selected = save_selected_roles_params['roles'].select { |r| r['selected'] }.map{ |r| r['id'] }
28
- roles = UserRole.where(id: selected)
29
- @user.roles << roles
30
- render json: { success: true }
21
+ def fetch_by_role
22
+ r = params[:role]
23
+ role = UserRole.find_by(name: r)
24
+ unless role
25
+ raise 'Role not found'
26
+ end
27
+ response = { success: true, data: role.users }
28
+ render json: response
31
29
  end
32
30
 
33
31
  def create
34
32
  @user = User.new(user_params)
35
- app_module = ApplicationModule.find_by(code: app_code)
36
- @user.application_modules = [app_module]
33
+ @user.application_module_id = app_module.id
37
34
  if @user.save
38
- render json: { success: true, data: @user }
35
+ response = { success: true, message: 'User saved successfully' }
36
+ render json: response
39
37
  else
40
- render json: { success: false, error: @user.errors.full_messages[0] }, status: :uprocessable_entity
38
+ errors = Mks::Common::Util.error_messages @user, 'User'
39
+ response = { success: false, errors: errors }
40
+ render json: response
41
41
  end
42
42
  end
43
43
 
44
44
  def update
45
45
  if @user.update(user_params)
46
- render json: { success: true, data: @user }
47
- else
48
- render json: { success: false, error: @user.errors.full_messages[0] }, status: :unprocessable_entity
49
- end
50
- end
51
-
52
- def login
53
- user = User.find_by(email: auth_params[:email])
54
- if user.authenticate(auth_params[:password]) &&
55
- user.has_module(auth_params[:app_module])
56
-
57
- jwt = TokenAuth.issue(user.to_token_payload)
58
- render json: { jwt: jwt }
46
+ response = { success: true, message: 'User updated successfully' }
47
+ render json: response
59
48
  else
60
- render json: {}, status: 400
49
+ errors = Mks::Common::Util.error_messages @user, 'User'
50
+ response = { success: false, errors: errors }
51
+ render json: response
61
52
  end
62
53
  end
63
54
 
64
55
  private
65
-
66
56
  # Use callbacks to share common setup or constraints between actions.
67
57
  def set_user
68
58
  @user = User.find(params[:id])
@@ -72,14 +62,6 @@ module Mks
72
62
  def user_params
73
63
  params.require(:user).permit(:first_name, :last_name, :email, :password)
74
64
  end
75
-
76
- def auth_params
77
- params.require(:auth).permit(:email, :password, :app_module)
78
- end
79
-
80
- def save_selected_roles_params
81
- params.permit(roles: %i[id selected])
82
- end
83
65
  end
84
66
  end
85
67
  end
@@ -0,0 +1,47 @@
1
+ module Mks
2
+ module Auth
3
+ module AccessHelper
4
+ def login_user(user)
5
+ session[:user_id] = user.id
6
+ end
7
+
8
+ def current_user
9
+ @current_user ||= User.find_by(id: session[:user_id])
10
+ end
11
+
12
+ def logged_in?
13
+ !current_user.nil?
14
+ end
15
+
16
+ def logout_user
17
+ session.delete(:user_id)
18
+ @current_user = nil
19
+ @menus = nil
20
+ end
21
+
22
+ def fetch_menus
23
+ if @menus.nil?
24
+ roles = current_user.roles
25
+ app_module = current_user.application_module
26
+
27
+ @menus = []
28
+ roles.each do |role|
29
+ if role
30
+ menu_list = role.menus.where(:parent => nil, :application_module => app_module)
31
+ menu_list.each do |menu|
32
+ children = []
33
+ menu.children.order(:text).each do |child|
34
+ if child.roles.include? role
35
+ children << {'text': child.text, 'className': child.class_name, 'iconCls': child.icon_cls}
36
+ end
37
+ end
38
+ @menus << {'text': menu.text, 'children': children, 'iconCls': menu.icon_cls}
39
+ end
40
+ end
41
+ end
42
+ end
43
+ @menus
44
+ end
45
+ end
46
+ end
47
+ end