rbac 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/CODE_OF_CONDUCT.md +49 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +85 -0
  7. data/Rakefile +2 -0
  8. data/app/helpers/rbac/application_helper.rb +20 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/config/routes.rb +7 -0
  12. data/images/user_form.png +0 -0
  13. data/lib/generators/rbac/create/create_generator.rb +33 -0
  14. data/lib/generators/rbac/create/templates/controllers/rbac/groups_controller.rb +81 -0
  15. data/lib/generators/rbac/create/templates/controllers/rbac/privileges_controller.rb +76 -0
  16. data/lib/generators/rbac/create/templates/controllers/rbac/roles_controller.rb +81 -0
  17. data/lib/generators/rbac/create/templates/models/rbac/group.rb +11 -0
  18. data/lib/generators/rbac/create/templates/models/rbac/group_privilege.rb +6 -0
  19. data/lib/generators/rbac/create/templates/models/rbac/group_role.rb +6 -0
  20. data/lib/generators/rbac/create/templates/models/rbac/privilege.rb +7 -0
  21. data/lib/generators/rbac/create/templates/models/rbac/role.rb +11 -0
  22. data/lib/generators/rbac/create/templates/models/rbac/role_user.rb +6 -0
  23. data/lib/generators/rbac/create/templates/views/rbac/groups/_form.html.erb +35 -0
  24. data/lib/generators/rbac/create/templates/views/rbac/groups/_privilege_fields.html.erb +27 -0
  25. data/lib/generators/rbac/create/templates/views/rbac/groups/edit.html.erb +6 -0
  26. data/lib/generators/rbac/create/templates/views/rbac/groups/index.html.erb +31 -0
  27. data/lib/generators/rbac/create/templates/views/rbac/groups/index.json.jbuilder +4 -0
  28. data/lib/generators/rbac/create/templates/views/rbac/groups/new.html.erb +6 -0
  29. data/lib/generators/rbac/create/templates/views/rbac/groups/show.html.erb +19 -0
  30. data/lib/generators/rbac/create/templates/views/rbac/groups/show.json.jbuilder +1 -0
  31. data/lib/generators/rbac/create/templates/views/rbac/privileges/_form.html.erb +39 -0
  32. data/lib/generators/rbac/create/templates/views/rbac/privileges/edit.html.erb +6 -0
  33. data/lib/generators/rbac/create/templates/views/rbac/privileges/index.html.erb +35 -0
  34. data/lib/generators/rbac/create/templates/views/rbac/privileges/index.json.jbuilder +4 -0
  35. data/lib/generators/rbac/create/templates/views/rbac/privileges/new.html.erb +6 -0
  36. data/lib/generators/rbac/create/templates/views/rbac/privileges/show.html.erb +29 -0
  37. data/lib/generators/rbac/create/templates/views/rbac/privileges/show.json.jbuilder +1 -0
  38. data/lib/generators/rbac/create/templates/views/rbac/roles/_form.html.erb +35 -0
  39. data/lib/generators/rbac/create/templates/views/rbac/roles/_group_fields.html.erb +29 -0
  40. data/lib/generators/rbac/create/templates/views/rbac/roles/edit.html.erb +6 -0
  41. data/lib/generators/rbac/create/templates/views/rbac/roles/index.html.erb +31 -0
  42. data/lib/generators/rbac/create/templates/views/rbac/roles/index.json.jbuilder +4 -0
  43. data/lib/generators/rbac/create/templates/views/rbac/roles/new.html.erb +6 -0
  44. data/lib/generators/rbac/create/templates/views/rbac/roles/show.html.erb +19 -0
  45. data/lib/generators/rbac/create/templates/views/rbac/roles/show.json.jbuilder +1 -0
  46. data/lib/generators/rbac/migration/migration_generator.rb +32 -0
  47. data/lib/generators/rbac/migration/templates/create_group_privileges.rb +8 -0
  48. data/lib/generators/rbac/migration/templates/create_group_roles.rb +8 -0
  49. data/lib/generators/rbac/migration/templates/create_groups.rb +11 -0
  50. data/lib/generators/rbac/migration/templates/create_privileges.rb +13 -0
  51. data/lib/generators/rbac/migration/templates/create_role_users.rb +8 -0
  52. data/lib/generators/rbac/migration/templates/create_roles.rb +11 -0
  53. data/lib/generators/rbac/migration/templates/create_users.rb +12 -0
  54. data/lib/rbac.rb +7 -0
  55. data/lib/rbac/engine.rb +4 -0
  56. data/lib/rbac/version.rb +3 -0
  57. data/rbac.gemspec +24 -0
  58. metadata +129 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec393998b95d9b00fb71159f190f84b57fab9646
4
+ data.tar.gz: 7e8bb1a6437c6a49c5b23832695127c5c3b1cb71
5
+ SHA512:
6
+ metadata.gz: 0db6944f1484a4e0d1333b929bc0e1455dacdde2c6830a4af897ff0715d5819dd962819f36314eeca1fc7040dd59e94730e567352aa031d64bdc739d891704f7
7
+ data.tar.gz: da238ba74a480a87f0c9c1475d0b739e5f79833a466a6970903b9c11dc2ab02ee8474c3dc972362b1c6533ccb9b327c45193928164241edb6769223532194a7b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at sandip.karanjekar@globallogic.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rbac.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sandip Karanjekar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Rbac
2
+
3
+ RBAC (Role Based Access Control) gem useful to enable your Rails application with the power of RBAC.
4
+ This gem is helpful to maintain roles, groups and privileges at database table level. You have user interface to manage the roles, groups and privileges.This gem is applicable in the area where you need a multiple roles and privileges. It will fulfil all your needs.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rbac'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rbac
21
+
22
+ As this gem is database based access control system, so need to run:
23
+
24
+ $ rails g rbac:migration
25
+
26
+ after this -
27
+
28
+ $ rake db:migrate
29
+
30
+ To setup user interface and backend for RBAC you need to run following generator. It will create necessary controllers, views, models, routes, helper method and associations.
31
+
32
+ *Note - Before this your Rails application have User model*
33
+
34
+ $ rails g rbac:create
35
+
36
+ ## Pre-requisites
37
+ This gem work with User model only. Generator of this gem add association for User model. In further development we will integrate this with any authentication gem.
38
+
39
+ ## Concept
40
+ In this there is strong relationship between user, role, group and privilege.
41
+ * User and Role model have many-to-many association
42
+ User can have multiple roles and Role can have multiple users associated. It's as per your requirements, for this you need to associate roles with user in your add/update users controller/view.
43
+
44
+ For example -
45
+
46
+ ![alt text](https://raw.githubusercontent.com/sandipkaranjekar/rbac/master/images/user_form.png "User new/edit form")
47
+
48
+ * Role and Group model have many-to-many association
49
+ Here group is set of privileges group together while creating role, user need to assign groups to the role. So automatically privileges associated with this group get assigned to role (indirectly to user).
50
+ You can access Role and Group UI -
51
+ * http://{host_url}/rbac/roles
52
+ * http://{host_url}/rbac/groups
53
+
54
+ * Group and Privilege model have many-to-many association
55
+ Here Group can have multiple privileges and privilege belongs to many groups. You need to first set privileges with controller and action. If you select privilege under group it will be accessable to role.
56
+ All privileges are controller and action based.You should have controller and action to set privilege.
57
+ You can access Privilege UI -
58
+ * http://{host_url}/rbac/privileges
59
+
60
+ ## Usage
61
+ Here you will have one helper method to check role have access to that resource.
62
+ ```ruby
63
+ has_permission(user, controller, action)
64
+ ```
65
+ user = Logged in user active record object
66
+ controller = For which controller that resource belong
67
+ action = Action of controller which we need to check for having permission
68
+
69
+ This method return *true* and *false*. Accordingly you can make a decision.
70
+
71
+ For example -
72
+ ```ruby
73
+ # In view
74
+ <% if has_permission(user, "users", "show") %>
75
+ <%= link_to 'Show', user %>
76
+ <% end %>
77
+ ```
78
+ ## TODO
79
+ * Support API application means has_permission should work with api permission granting.
80
+ * Integrate with authentication gem.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
85
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ module Rbac
2
+ module ApplicationHelper
3
+ def has_permission(user, controller, action)
4
+ role_ids_arr = user.role_ids
5
+ role_ids_arr.each do |role_id|
6
+ group = Rbac::Role.find(role_id).group_ids
7
+ group.each do |group_id|
8
+ privilage_data = Rbac::Group.find(group_id).privileges
9
+ if privilage_data.detect{|privilage|
10
+ privilage.controller == controller && privilage.action == action
11
+ }
12
+ return true
13
+ else
14
+ return false
15
+ end
16
+ end #end of group id loop
17
+ end #end of role id loop
18
+ end
19
+ end
20
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rbac"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+ namespace :rbac do
3
+ resources :privileges
4
+ resources :groups
5
+ resources :roles
6
+ end
7
+ end
Binary file
@@ -0,0 +1,33 @@
1
+ require 'rails/generators'
2
+
3
+ module Rbac
4
+ module Generators
5
+ class CreateGenerator < Rails::Generators::Base
6
+
7
+ def self.source_root
8
+ source_root ||= File.join(File.dirname(__FILE__), 'templates/')
9
+ end
10
+
11
+ def create_controllers
12
+ directory "controllers/rbac", "app/controllers/rbac"
13
+ end
14
+
15
+ def create_models
16
+ directory "models/rbac", "app/models/rbac"
17
+ end
18
+
19
+ def create_views
20
+ directory "views/rbac", "app/views/rbac"
21
+ end
22
+
23
+ def add_association
24
+ line = "class User < ActiveRecord::Base"
25
+ gsub_file 'app/models/user.rb', /(#{Regexp.escape(line)})/mi do |match|
26
+ "#{match}\n # User - Role many-to-many relationship\n
27
+ has_many :roles, :through => :role_user, class_name: 'Rbac::Role'\n
28
+ has_many :role_user, dependent: :delete_all, class_name: 'Rbac::RoleUser'\n"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,81 @@
1
+ module Rbac
2
+ class GroupsController < ApplicationController
3
+ before_action :set_group, only: [:show, :edit, :update, :destroy]
4
+
5
+ # GET /groups
6
+ # GET /groups.json
7
+ def index
8
+ @groups = Rbac::Group.all
9
+ end
10
+
11
+ # GET /groups/1
12
+ # GET /groups/1.json
13
+ def show
14
+ end
15
+
16
+ # GET /groups/new
17
+ def new
18
+ @group = Rbac::Group.new
19
+ @privileges = Rbac::Privilege.all
20
+ end
21
+
22
+ # GET /groups/1/edit
23
+ def edit
24
+ @group_privileges = @group.privileges
25
+ @privileges = Rbac::Privilege.all
26
+ end
27
+
28
+ # POST /groups
29
+ # POST /groups.json
30
+ def create
31
+ @group = Rbac::Group.new(group_params)
32
+
33
+ respond_to do |format|
34
+ if @group.save
35
+ @group.privileges = Rbac::Privilege.where(id: params[:privilege_ids])
36
+ format.html { redirect_to rbac_groups_path, notice: 'Group was successfully created.' }
37
+ format.json { render :show, status: :created, location: @group }
38
+ else
39
+ format.html { redirect_to new_rbac_group_path }
40
+ format.json { render json: @group.errors, status: :unprocessable_entity }
41
+ end
42
+ end
43
+ end
44
+
45
+ # PATCH/PUT /groups/1
46
+ # PATCH/PUT /groups/1.json
47
+ def update
48
+ respond_to do |format|
49
+ if @group.update(group_params)
50
+ @group.privileges = Rbac::Privilege.where(id: params[:privilege_ids])
51
+ format.html { redirect_to rbac_groups_path, notice: 'Group was successfully updated.' }
52
+ format.json { render :show, status: :ok, location: @group }
53
+ else
54
+ format.html { redirect_to edit_rbac_group_path }
55
+ format.json { render json: @group.errors, status: :unprocessable_entity }
56
+ end
57
+ end
58
+ end
59
+
60
+ # DELETE /groups/1
61
+ # DELETE /groups/1.json
62
+ def destroy
63
+ @group.destroy
64
+ respond_to do |format|
65
+ format.html { redirect_to rbac_groups_url, notice: 'Group was successfully destroyed.' }
66
+ format.json { head :no_content }
67
+ end
68
+ end
69
+
70
+ private
71
+ # Use callbacks to share common setup or constraints between actions.
72
+ def set_group
73
+ @group = Rbac::Group.find(params[:id])
74
+ end
75
+
76
+ # Never trust parameters from the scary internet, only allow the white list through.
77
+ def group_params
78
+ params.require(:rbac_group).permit(:name, :description, :privilege_ids, :is_active)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,76 @@
1
+ module Rbac
2
+ class PrivilegesController < ApplicationController
3
+ before_action :set_privilege, only: [:show, :edit, :update, :destroy]
4
+
5
+ # GET /privileges
6
+ # GET /privileges.json
7
+ def index
8
+ @privileges = Rbac::Privilege.all
9
+ end
10
+
11
+ # GET /privileges/1
12
+ # GET /privileges/1.json
13
+ def show
14
+ end
15
+
16
+ # GET /privileges/new
17
+ def new
18
+ @privilege = Rbac::Privilege.new
19
+ end
20
+
21
+ # GET /privileges/1/edit
22
+ def edit
23
+ end
24
+
25
+ # POST /privileges
26
+ # POST /privileges.json
27
+ def create
28
+ @privilege = Rbac::Privilege.new(privilege_params)
29
+
30
+ respond_to do |format|
31
+ if @privilege.save
32
+ format.html { redirect_to rbac_privileges_path, notice: 'Privilege was successfully created.' }
33
+ format.json { render :show, status: :created, location: @privilege }
34
+ else
35
+ format.html { redirect_to new_rbac_privilege_path }
36
+ format.json { render json: @privilege.errors, status: :unprocessable_entity }
37
+ end
38
+ end
39
+ end
40
+
41
+ # PATCH/PUT /privileges/1
42
+ # PATCH/PUT /privileges/1.json
43
+ def update
44
+ respond_to do |format|
45
+ if @privilege.update(privilege_params)
46
+ format.html { redirect_to rbac_privileges_path, notice: 'Privilege was successfully updated.' }
47
+ format.json { render :show, status: :ok, location: @privilege }
48
+ else
49
+ format.html { redirect_to edit_rbac_privilege_path }
50
+ format.json { render json: @privilege.errors, status: :unprocessable_entity }
51
+ end
52
+ end
53
+ end
54
+
55
+ # DELETE /privileges/1
56
+ # DELETE /privileges/1.json
57
+ def destroy
58
+ @privilege.destroy
59
+ respond_to do |format|
60
+ format.html { redirect_to rbac_privileges_url, notice: 'Privilege was successfully destroyed.' }
61
+ format.json { head :no_content }
62
+ end
63
+ end
64
+
65
+ private
66
+ # Use callbacks to share common setup or constraints between actions.
67
+ def set_privilege
68
+ @privilege = Rbac::Privilege.find(params[:id])
69
+ end
70
+
71
+ # Never trust parameters from the scary internet, only allow the white list through.
72
+ def privilege_params
73
+ params.require(:rbac_privilege).permit(:name, :description, :controller, :action, :is_active)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,81 @@
1
+ module Rbac
2
+ class RolesController < ApplicationController
3
+ before_action :set_role, only: [:show, :edit, :update, :destroy]
4
+
5
+ # GET /roles
6
+ # GET /roles.json
7
+ def index
8
+ @roles = Rbac::Role.all
9
+ end
10
+
11
+ # GET /roles/1
12
+ # GET /roles/1.json
13
+ def show
14
+ end
15
+
16
+ # GET /roles/new
17
+ def new
18
+ @role = Rbac::Role.new
19
+ @groups = Rbac::Group.where("is_active = ?", true)
20
+ end
21
+
22
+ # GET /roles/1/edit
23
+ def edit
24
+ @groups = Rbac::Group.where("is_active = ?", true)
25
+ @role_groups = @role.groups
26
+ end
27
+
28
+ # POST /roles
29
+ # POST /roles.json
30
+ def create
31
+ @role = Rbac::Role.new(role_params)
32
+
33
+ respond_to do |format|
34
+ if @role.save
35
+ @role.groups = Rbac::Group.where(id: params[:group_ids])
36
+ format.html { redirect_to rbac_roles_path, notice: 'Role was successfully created.' }
37
+ format.json { render :show, status: :created, location: @role }
38
+ else
39
+ format.html { redirect_to new_rbac_role_path }
40
+ format.json { render json: @role.errors, status: :unprocessable_entity }
41
+ end
42
+ end
43
+ end
44
+
45
+ # PATCH/PUT /roles/1
46
+ # PATCH/PUT /roles/1.json
47
+ def update
48
+ respond_to do |format|
49
+ if @role.update(role_params)
50
+ @role.groups = Rbac::Group.where(id: params[:group_ids])
51
+ format.html { redirect_to rbac_roles_path, notice: 'Role was successfully updated.' }
52
+ format.json { render :show, status: :ok, location: @role }
53
+ else
54
+ format.html { redirect_to edit_rbac_role_path }
55
+ format.json { render json: @role.errors, status: :unprocessable_entity }
56
+ end
57
+ end
58
+ end
59
+
60
+ # DELETE /roles/1
61
+ # DELETE /roles/1.json
62
+ def destroy
63
+ @role.destroy
64
+ respond_to do |format|
65
+ format.html { redirect_to rbac_roles_url, notice: 'Role was successfully destroyed.' }
66
+ format.json { head :no_content }
67
+ end
68
+ end
69
+
70
+ private
71
+ # Use callbacks to share common setup or constraints between actions.
72
+ def set_role
73
+ @role = Rbac::Role.find(params[:id])
74
+ end
75
+
76
+ # Never trust parameters from the scary internet, only allow the white list through.
77
+ def role_params
78
+ params.require(:rbac_role).permit(:role_name, :role_description, :is_active, :group_ids)
79
+ end
80
+ end
81
+ end