mks_auth 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +37 -0
- data/app/assets/config/mks_auth_manifest.js +2 -0
- data/app/assets/javascripts/mks_auth/application.js +13 -0
- data/app/assets/stylesheets/mks_auth/application.css +15 -0
- data/app/controllers/mks/auth/access_controller.rb +51 -0
- data/app/controllers/mks/auth/application_controller.rb +25 -0
- data/app/controllers/mks/auth/application_modules_controller.rb +60 -0
- data/app/controllers/mks/auth/user_roles_controller.rb +52 -0
- data/app/controllers/mks/auth/users_controller.rb +67 -0
- data/app/helpers/mks/auth/access_helper.rb +47 -0
- data/app/helpers/mks/auth/application_helper.rb +10 -0
- data/app/jobs/mks_auth/application_job.rb +4 -0
- data/app/mailers/mks_auth/application_mailer.rb +6 -0
- data/app/models/mks/auth/application_module.rb +13 -0
- data/app/models/mks/auth/application_record.rb +7 -0
- data/app/models/mks/auth/menu.rb +12 -0
- data/app/models/mks/auth/user.rb +24 -0
- data/app/models/mks/auth/user_role.rb +11 -0
- data/config/routes.rb +32 -0
- data/db/migrate/20161029065810_create_mks_auth_application_modules.rb +10 -0
- data/db/migrate/20161029065959_create_mks_auth_users.rb +16 -0
- data/db/migrate/20161029070807_create_mks_auth_user_roles.rb +9 -0
- data/db/migrate/20161029071047_create_mks_users_user_roles.rb +11 -0
- data/db/migrate/20161029072256_create_mks_auth_menus.rb +17 -0
- data/db/migrate/20161029074023_create_mks_menus_user_roles.rb +11 -0
- data/lib/mks/auth.rb +4 -0
- data/lib/mks/auth/engine.rb +28 -0
- data/lib/mks/auth/version.rb +5 -0
- data/lib/mks_auth.rb +2 -0
- data/lib/tasks/mks_auth_tasks.rake +4 -0
- data/spec/controllers/mks/auth/users_controller_spec.rb +20 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +5 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +34 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +9 -0
- data/spec/dummy/config/database.yml +23 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +54 -0
- data/spec/dummy/config/environments/production.rb +86 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/puma.rb +47 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/db/schema.rb +75 -0
- data/spec/dummy/log/development.log +3 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/application_modules.rb +6 -0
- data/spec/factories/menus.rb +10 -0
- data/spec/factories/user_roles.rb +17 -0
- data/spec/factories/users.rb +28 -0
- data/spec/models/mks/auth/application_module_spec.rb +36 -0
- data/spec/models/mks/auth/menu_spec.rb +16 -0
- data/spec/models/mks/auth/user_role_spec.rb +32 -0
- data/spec/models/mks/auth/user_spec.rb +46 -0
- data/spec/rails_helper.rb +57 -0
- data/spec/spec_helper.rb +109 -0
- data/spec/support/factory_girl.rb +5 -0
- metadata +302 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
module Mks
|
2
|
+
module Auth
|
3
|
+
class ApplicationModule < ApplicationRecord
|
4
|
+
#self.table_name = 'mks_application_modules'
|
5
|
+
|
6
|
+
validates :code, presence: true
|
7
|
+
validates :code, presence: true, uniqueness: true
|
8
|
+
|
9
|
+
has_many :users, class_name: 'Mks::Auth::User'
|
10
|
+
has_many :menus, class_name: 'Mks::Auth::Menu'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Mks
|
2
|
+
module Auth
|
3
|
+
class Menu < ApplicationRecord
|
4
|
+
# self.table_name = 'mks_menus'
|
5
|
+
|
6
|
+
belongs_to :application_module, class_name: 'Mks::Auth::ApplicationModule'
|
7
|
+
belongs_to :parent, class_name: 'Mks::Auth::Menu', optional: true
|
8
|
+
has_many :children, class_name: 'Mks::Auth::Menu', :foreign_key => 'parent_id'
|
9
|
+
has_and_belongs_to_many :roles, class_name: 'Mks::Auth::UserRole', :join_table => :mks_auth_menus_user_roles
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Mks
|
2
|
+
module Auth
|
3
|
+
class User < ApplicationRecord
|
4
|
+
# self.table_name = 'mks_users'
|
5
|
+
|
6
|
+
belongs_to :application_module, class_name: 'Mks::Auth::ApplicationModule'
|
7
|
+
has_and_belongs_to_many :roles, class_name: 'Mks::Auth::UserRole', join_table: :mks_auth_users_user_roles
|
8
|
+
has_secure_password
|
9
|
+
|
10
|
+
before_save { email.downcase! }
|
11
|
+
|
12
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
|
13
|
+
validates :first_name, presence: true, length: {maximum: 30}
|
14
|
+
validates :last_name, presence: true, length: {maximum: 30}
|
15
|
+
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false}
|
16
|
+
validates :password, length: { minimum: 6 }
|
17
|
+
validates :active, presence: true
|
18
|
+
|
19
|
+
def full_name
|
20
|
+
"#{first_name} #{last_name}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Mks
|
2
|
+
module Auth
|
3
|
+
class UserRole < ApplicationRecord
|
4
|
+
# self.table_name = 'mks_user_roles'
|
5
|
+
|
6
|
+
validates :name, presence: true, uniqueness: true
|
7
|
+
has_and_belongs_to_many :users, :join_table => :mks_auth_users_user_roles
|
8
|
+
has_and_belongs_to_many :menus, :join_table => :mks_auth_menus_user_roles
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
Mks::Auth::Engine.routes.draw do
|
2
|
+
get '/csrf_token', to: 'access#csrf_token'
|
3
|
+
|
4
|
+
get '/attempt_login', to: 'access#attempt_login'
|
5
|
+
|
6
|
+
get '/logout', to: 'access#logout'
|
7
|
+
|
8
|
+
get '/menu', to: 'access#menu'
|
9
|
+
|
10
|
+
get '/check_login', to: 'access#check_login'
|
11
|
+
|
12
|
+
post '/login', to: 'access#attempt_login'
|
13
|
+
|
14
|
+
resources :application_modules
|
15
|
+
|
16
|
+
# get '/users', to: 'users#index'
|
17
|
+
|
18
|
+
resources :users, except: [:new, :edit, :show, :destroy]
|
19
|
+
|
20
|
+
get '/users/roles', controller: :users, action: :roles
|
21
|
+
|
22
|
+
get '/users/fetch_by_role', to: 'users#fetch_by_role'
|
23
|
+
|
24
|
+
# get '/user_roles', to: 'user_roles#index'
|
25
|
+
|
26
|
+
resources :user_roles, except: [:new, :edit, :show, :destroy]
|
27
|
+
|
28
|
+
post '/assign_roles', to: 'user_roles#assign_roles'
|
29
|
+
|
30
|
+
get '/assigned_roles/:user_id', to: 'user_roles#get_assigned_roles'
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateMksAuthUsers < ActiveRecord::Migration[5.0]
|
2
|
+
def change
|
3
|
+
create_table :mks_auth_users do |t|
|
4
|
+
t.string :first_name, null: false
|
5
|
+
t.string :last_name, null: false
|
6
|
+
t.string :email, null: false
|
7
|
+
t.boolean :active, null: false, default: true
|
8
|
+
t.references :application_module, index: true
|
9
|
+
t.string :password_digest
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
add_foreign_key :mks_auth_users, :mks_auth_application_modules, :column => :application_module_id
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class CreateMksUsersUserRoles < ActiveRecord::Migration[5.0]
|
2
|
+
def change
|
3
|
+
create_table :mks_auth_users_user_roles, id: false do |t|
|
4
|
+
t.references :user, index: false
|
5
|
+
t.references :user_role, index: false
|
6
|
+
end
|
7
|
+
add_index :mks_auth_users_user_roles, [:user_id, :user_role_id]
|
8
|
+
add_foreign_key :mks_auth_users_user_roles, :mks_auth_users, :column => :user_id
|
9
|
+
add_foreign_key :mks_auth_users_user_roles, :mks_auth_user_roles, :column => :user_role_id
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateMksAuthMenus < ActiveRecord::Migration[5.0]
|
2
|
+
def change
|
3
|
+
create_table :mks_auth_menus do |t|
|
4
|
+
t.string :text, null: false
|
5
|
+
t.string :icon_cls
|
6
|
+
t.string :class_name
|
7
|
+
t.string :location
|
8
|
+
t.integer :parent_id, index: true
|
9
|
+
t.references :application_module, index: true
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
add_foreign_key :mks_auth_menus, :mks_auth_menus, :column => :parent_id
|
15
|
+
add_foreign_key :mks_auth_menus, :mks_auth_application_modules, :column => :application_module_id
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class CreateMksMenusUserRoles < ActiveRecord::Migration[5.0]
|
2
|
+
def change
|
3
|
+
create_table :mks_auth_menus_user_roles do |t|
|
4
|
+
t.references :menu, index: false
|
5
|
+
t.references :user_role, index: false
|
6
|
+
end
|
7
|
+
add_index :mks_auth_menus_user_roles, [:menu_id, :user_role_id]
|
8
|
+
add_foreign_key :mks_auth_menus_user_roles, :mks_auth_menus, :column => :menu_id
|
9
|
+
add_foreign_key :mks_auth_menus_user_roles, :mks_auth_user_roles, :column => :user_role_id
|
10
|
+
end
|
11
|
+
end
|
data/lib/mks/auth.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mks
|
2
|
+
module Auth
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
isolate_namespace Mks::Auth
|
5
|
+
|
6
|
+
initializer :append_migrations do |app|
|
7
|
+
unless app.root.to_s.match root.to_s
|
8
|
+
if app.config.app_code == 'PSH'
|
9
|
+
config.paths['db/migrate'].expanded.each do |expanded_path|
|
10
|
+
app.config.paths['db/migrate'] << expanded_path
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
initializer 'mks_auth.factories', :after => 'factory_girl.set_factory_paths' do
|
17
|
+
FactoryGirl.definition_file_paths << File.expand_path('../../../../spec/factories', __FILE__) if defined?(FactoryGirl)
|
18
|
+
end
|
19
|
+
|
20
|
+
config.generators do |g|
|
21
|
+
g.test_framework :rspec, :fixture => false
|
22
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
23
|
+
g.assets false
|
24
|
+
g.helper false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/mks_auth.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
module Mks
|
4
|
+
module Auth
|
5
|
+
RSpec.describe UsersController, type: :controller do
|
6
|
+
routes { Mks::Auth::Engine.routes }
|
7
|
+
|
8
|
+
describe 'GET #roles' do
|
9
|
+
it 'gets roles of a user' do
|
10
|
+
u = create(:user)
|
11
|
+
roles = [create(:user_role), create(:user_role)]
|
12
|
+
u.roles << roles
|
13
|
+
get :roles, session: { user_id: u.id }
|
14
|
+
result = JSON(response.body)
|
15
|
+
expect(result['data'].count).to eq 2
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/dummy/Rakefile
ADDED
@@ -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,13 @@
|
|
1
|
+
// Action Cable provides the framework to deal with WebSockets in Rails.
|
2
|
+
// You can generate new channels where WebSocket features live using the rails generate channel command.
|
3
|
+
//
|
4
|
+
//= require action_cable
|
5
|
+
//= require_self
|
6
|
+
//= require_tree ./channels
|
7
|
+
|
8
|
+
(function() {
|
9
|
+
this.App || (this.App = {});
|
10
|
+
|
11
|
+
App.cable = ActionCable.createConsumer();
|
12
|
+
|
13
|
+
}).call(this);
|
@@ -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,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
|
7
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
8
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
|
9
|
+
</head>
|
10
|
+
|
11
|
+
<body>
|
12
|
+
<%= yield %>
|
13
|
+
</body>
|
14
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
require 'fileutils'
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
# path to your application root.
|
7
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
8
|
+
|
9
|
+
def system!(*args)
|
10
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
11
|
+
end
|
12
|
+
|
13
|
+
chdir APP_ROOT do
|
14
|
+
# This script is a starting point to setup your application.
|
15
|
+
# Add necessary setup steps to this file.
|
16
|
+
|
17
|
+
puts '== Installing dependencies =='
|
18
|
+
system! 'gem install bundler --conservative'
|
19
|
+
system('bundle check') || system!('bundle install')
|
20
|
+
|
21
|
+
# puts "\n== Copying sample files =="
|
22
|
+
# unless File.exist?('config/database.yml')
|
23
|
+
# cp 'config/database.yml.sample', 'config/database.yml'
|
24
|
+
# end
|
25
|
+
|
26
|
+
puts "\n== Preparing database =="
|
27
|
+
system! 'bin/rails db:setup'
|
28
|
+
|
29
|
+
puts "\n== Removing old logs and tempfiles =="
|
30
|
+
system! 'bin/rails log:clear tmp:clear'
|
31
|
+
|
32
|
+
puts "\n== Restarting application server =="
|
33
|
+
system! 'bin/rails restart'
|
34
|
+
end
|