mks_auth 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +28 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/mks_auth_manifest.js +2 -0
  6. data/app/assets/javascripts/mks_auth/application.js +13 -0
  7. data/app/assets/stylesheets/mks_auth/application.css +15 -0
  8. data/app/controllers/mks/auth/access_controller.rb +51 -0
  9. data/app/controllers/mks/auth/application_controller.rb +25 -0
  10. data/app/controllers/mks/auth/application_modules_controller.rb +60 -0
  11. data/app/controllers/mks/auth/user_roles_controller.rb +52 -0
  12. data/app/controllers/mks/auth/users_controller.rb +67 -0
  13. data/app/helpers/mks/auth/access_helper.rb +47 -0
  14. data/app/helpers/mks/auth/application_helper.rb +10 -0
  15. data/app/jobs/mks_auth/application_job.rb +4 -0
  16. data/app/mailers/mks_auth/application_mailer.rb +6 -0
  17. data/app/models/mks/auth/application_module.rb +13 -0
  18. data/app/models/mks/auth/application_record.rb +7 -0
  19. data/app/models/mks/auth/menu.rb +12 -0
  20. data/app/models/mks/auth/user.rb +24 -0
  21. data/app/models/mks/auth/user_role.rb +11 -0
  22. data/config/routes.rb +32 -0
  23. data/db/migrate/20161029065810_create_mks_auth_application_modules.rb +10 -0
  24. data/db/migrate/20161029065959_create_mks_auth_users.rb +16 -0
  25. data/db/migrate/20161029070807_create_mks_auth_user_roles.rb +9 -0
  26. data/db/migrate/20161029071047_create_mks_users_user_roles.rb +11 -0
  27. data/db/migrate/20161029072256_create_mks_auth_menus.rb +17 -0
  28. data/db/migrate/20161029074023_create_mks_menus_user_roles.rb +11 -0
  29. data/lib/mks/auth.rb +4 -0
  30. data/lib/mks/auth/engine.rb +28 -0
  31. data/lib/mks/auth/version.rb +5 -0
  32. data/lib/mks_auth.rb +2 -0
  33. data/lib/tasks/mks_auth_tasks.rake +4 -0
  34. data/spec/controllers/mks/auth/users_controller_spec.rb +20 -0
  35. data/spec/dummy/Rakefile +6 -0
  36. data/spec/dummy/app/assets/config/manifest.js +5 -0
  37. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  38. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  39. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  40. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  41. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  42. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/jobs/application_job.rb +2 -0
  45. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  46. data/spec/dummy/app/models/application_record.rb +3 -0
  47. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  48. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  49. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  50. data/spec/dummy/bin/bundle +3 -0
  51. data/spec/dummy/bin/rails +4 -0
  52. data/spec/dummy/bin/rake +4 -0
  53. data/spec/dummy/bin/setup +34 -0
  54. data/spec/dummy/bin/update +29 -0
  55. data/spec/dummy/config.ru +5 -0
  56. data/spec/dummy/config/application.rb +23 -0
  57. data/spec/dummy/config/boot.rb +5 -0
  58. data/spec/dummy/config/cable.yml +9 -0
  59. data/spec/dummy/config/database.yml +23 -0
  60. data/spec/dummy/config/environment.rb +5 -0
  61. data/spec/dummy/config/environments/development.rb +54 -0
  62. data/spec/dummy/config/environments/production.rb +86 -0
  63. data/spec/dummy/config/environments/test.rb +42 -0
  64. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  65. data/spec/dummy/config/initializers/assets.rb +11 -0
  66. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  67. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  68. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  69. data/spec/dummy/config/initializers/inflections.rb +16 -0
  70. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  71. data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
  72. data/spec/dummy/config/initializers/session_store.rb +3 -0
  73. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  74. data/spec/dummy/config/locales/en.yml +23 -0
  75. data/spec/dummy/config/puma.rb +47 -0
  76. data/spec/dummy/config/routes.rb +3 -0
  77. data/spec/dummy/config/secrets.yml +22 -0
  78. data/spec/dummy/config/spring.rb +6 -0
  79. data/spec/dummy/db/schema.rb +75 -0
  80. data/spec/dummy/log/development.log +3 -0
  81. data/spec/dummy/public/404.html +67 -0
  82. data/spec/dummy/public/422.html +67 -0
  83. data/spec/dummy/public/500.html +66 -0
  84. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  85. data/spec/dummy/public/apple-touch-icon.png +0 -0
  86. data/spec/dummy/public/favicon.ico +0 -0
  87. data/spec/factories/application_modules.rb +6 -0
  88. data/spec/factories/menus.rb +10 -0
  89. data/spec/factories/user_roles.rb +17 -0
  90. data/spec/factories/users.rb +28 -0
  91. data/spec/models/mks/auth/application_module_spec.rb +36 -0
  92. data/spec/models/mks/auth/menu_spec.rb +16 -0
  93. data/spec/models/mks/auth/user_role_spec.rb +32 -0
  94. data/spec/models/mks/auth/user_spec.rb +46 -0
  95. data/spec/rails_helper.rb +57 -0
  96. data/spec/spec_helper.rb +109 -0
  97. data/spec/support/factory_girl.rb +5 -0
  98. metadata +302 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a19971c004cdf2626990b271aad7b31cf7e34b8
4
+ data.tar.gz: 8f8fab8db2e21550287d3587812aa474b3993c91
5
+ SHA512:
6
+ metadata.gz: ed68674e71fbe258b766159790b33e6d1204587659678ab82c495218176c172fd6574169292c62efd6c94e9f727a88388f3b9d0ef106462c181d6146dda3092e
7
+ data.tar.gz: eac9332b341c7ab51e78b7d118cdc649ca9fba7571fbd600a03a726d6de1c7a72e3bc82182289a67303c20ef1cbc6f1fee57a61cdfe56d301747aad18fd3c44e
@@ -0,0 +1,20 @@
1
+ Copyright 2016
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.
@@ -0,0 +1,28 @@
1
+ # MksAuth
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 'mks_auth'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install mks_auth
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](http://opensource.org/licenses/MIT).
@@ -0,0 +1,37 @@
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 = 'MksAuth'
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', __FILE__)
18
+
19
+ load 'rails/tasks/engine.rake'
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
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
+
31
+ require 'rspec/core/rake_task'
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
@@ -0,0 +1,25 @@
1
+ module Mks
2
+ module Auth
3
+ class ApplicationController < ActionController::Base
4
+ include AccessHelper
5
+ include ApplicationHelper
6
+
7
+ private
8
+
9
+ def confirm_logged_in
10
+ if session[:user_id]
11
+ true
12
+ else
13
+ redirect_to '/'
14
+ false
15
+ end
16
+ end
17
+
18
+ protected
19
+
20
+ def verified_request?
21
+ super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN'])
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
3
+ module Mks
4
+ module Auth
5
+ class ApplicationModulesController < ApplicationController
6
+ before_action :set_application_module, only: [:show, :edit, :update, :destroy]
7
+
8
+ # GET /application_modules
9
+ def index
10
+ @application_modules = ApplicationModule.all
11
+ end
12
+
13
+ # GET /application_modules/new
14
+ def new
15
+ @application_module = ApplicationModule.new
16
+ end
17
+
18
+ # GET /application_modules/1/edit
19
+ def edit
20
+ end
21
+
22
+ # POST /application_modules
23
+ def create
24
+ @application_module = ApplicationModule.new(application_module_params)
25
+
26
+ if @application_module.save
27
+ redirect_to @application_module, notice: 'Application module was successfully created.'
28
+ else
29
+ render :new
30
+ end
31
+ end
32
+
33
+ # PATCH/PUT /application_modules/1
34
+ def update
35
+ if @application_module.update(application_module_params)
36
+ redirect_to @application_module, notice: 'Application module was successfully updated.'
37
+ else
38
+ render :edit
39
+ end
40
+ end
41
+
42
+ # DELETE /application_modules/1
43
+ def destroy
44
+ @application_module.destroy
45
+ redirect_to application_modules_url, notice: 'Application module was successfully destroyed.'
46
+ end
47
+
48
+ private
49
+ # Use callbacks to share common setup or constraints between actions.
50
+ def set_application_module
51
+ @application_module = ApplicationModule.find(params[:id])
52
+ end
53
+
54
+ # Only allow a trusted parameter "white list" through.
55
+ def application_module_params
56
+ params.require(:application_module).permit(:code, :name)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,52 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
3
+ module Mks
4
+ module Auth
5
+ class UserRolesController < ApplicationController
6
+ # before_action :confirm_logged_in
7
+
8
+ # GET /user_roles
9
+ def index
10
+ @user_roles = UserRole.all.order(:name)
11
+ response = { success: true, data: @user_roles }
12
+ render json: response
13
+ end
14
+
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
28
+ response = { success: true, data: data }
29
+ render json: response
30
+ end
31
+
32
+ def assign_roles
33
+ user = User.find(params[:user_id])
34
+ roles = params[:roles]
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
44
+
45
+ user.save
46
+
47
+ response = { success: true, message: 'Role assignment successful!' }
48
+ render json: response
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,67 @@
1
+ require_dependency 'mks/auth/application_controller'
2
+
3
+ module Mks
4
+ module Auth
5
+ class UsersController < ApplicationController
6
+ before_action :set_user, only: [:update]
7
+
8
+ def index
9
+ @users = User.where(application_module_id: app_module.id)
10
+ response = { success: true, data: @users }
11
+ render json: response
12
+ end
13
+
14
+ def 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
19
+ end
20
+
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
29
+ end
30
+
31
+ def create
32
+ @user = User.new(user_params)
33
+ @user.application_module_id = app_module.id
34
+ if @user.save
35
+ response = { success: true, message: 'User saved successfully' }
36
+ render json: response
37
+ else
38
+ errors = Mks::Common::Util.error_messages @user, 'User'
39
+ response = { success: false, errors: errors }
40
+ render json: response
41
+ end
42
+ end
43
+
44
+ def update
45
+ if @user.update(user_params)
46
+ response = { success: true, message: 'User updated successfully' }
47
+ render json: response
48
+ else
49
+ errors = Mks::Common::Util.error_messages @user, 'User'
50
+ response = { success: false, errors: errors }
51
+ render json: response
52
+ end
53
+ end
54
+
55
+ private
56
+ # Use callbacks to share common setup or constraints between actions.
57
+ def set_user
58
+ @user = User.find(params[:id])
59
+ end
60
+
61
+ # Never trust parameters from the scary internet, only allow the white list through.
62
+ def user_params
63
+ params.require(:user).permit(:first_name, :last_name, :email, :password)
64
+ end
65
+ end
66
+ end
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
@@ -0,0 +1,10 @@
1
+ module Mks
2
+ module Auth
3
+ module ApplicationHelper
4
+ def app_module
5
+ code = Rails.configuration.app_code
6
+ ApplicationModule.find_by(code: code)
7
+ end
8
+ end
9
+ end
10
+ end