rbac 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/app/helpers/rbac/application_helper.rb +20 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/routes.rb +7 -0
- data/images/user_form.png +0 -0
- data/lib/generators/rbac/create/create_generator.rb +33 -0
- data/lib/generators/rbac/create/templates/controllers/rbac/groups_controller.rb +81 -0
- data/lib/generators/rbac/create/templates/controllers/rbac/privileges_controller.rb +76 -0
- data/lib/generators/rbac/create/templates/controllers/rbac/roles_controller.rb +81 -0
- data/lib/generators/rbac/create/templates/models/rbac/group.rb +11 -0
- data/lib/generators/rbac/create/templates/models/rbac/group_privilege.rb +6 -0
- data/lib/generators/rbac/create/templates/models/rbac/group_role.rb +6 -0
- data/lib/generators/rbac/create/templates/models/rbac/privilege.rb +7 -0
- data/lib/generators/rbac/create/templates/models/rbac/role.rb +11 -0
- data/lib/generators/rbac/create/templates/models/rbac/role_user.rb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/_form.html.erb +35 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/_privilege_fields.html.erb +27 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/edit.html.erb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/index.html.erb +31 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/index.json.jbuilder +4 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/new.html.erb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/show.html.erb +19 -0
- data/lib/generators/rbac/create/templates/views/rbac/groups/show.json.jbuilder +1 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/_form.html.erb +39 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/edit.html.erb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/index.html.erb +35 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/index.json.jbuilder +4 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/new.html.erb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/show.html.erb +29 -0
- data/lib/generators/rbac/create/templates/views/rbac/privileges/show.json.jbuilder +1 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/_form.html.erb +35 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/_group_fields.html.erb +29 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/edit.html.erb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/index.html.erb +31 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/index.json.jbuilder +4 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/new.html.erb +6 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/show.html.erb +19 -0
- data/lib/generators/rbac/create/templates/views/rbac/roles/show.json.jbuilder +1 -0
- data/lib/generators/rbac/migration/migration_generator.rb +32 -0
- data/lib/generators/rbac/migration/templates/create_group_privileges.rb +8 -0
- data/lib/generators/rbac/migration/templates/create_group_roles.rb +8 -0
- data/lib/generators/rbac/migration/templates/create_groups.rb +11 -0
- data/lib/generators/rbac/migration/templates/create_privileges.rb +13 -0
- data/lib/generators/rbac/migration/templates/create_role_users.rb +8 -0
- data/lib/generators/rbac/migration/templates/create_roles.rb +11 -0
- data/lib/generators/rbac/migration/templates/create_users.rb +12 -0
- data/lib/rbac.rb +7 -0
- data/lib/rbac/engine.rb +4 -0
- data/lib/rbac/version.rb +3 -0
- data/rbac.gemspec +24 -0
- 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
data/CODE_OF_CONDUCT.md
ADDED
@@ -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
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,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
data/config/routes.rb
ADDED
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
|