rbacan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 32a4086cc1604d5a527177b01562f7b5778cfc99
4
+ data.tar.gz: 28c63c5abd5efc4fda8c0819e00ccae64c75cdda
5
+ SHA512:
6
+ metadata.gz: 76a3ef0754d189322539b51cc6db41d6b8e1a5596d91b4f81876d4e4084d1b8d0061f85c900503fc5b3e1c7eaf2718b1e56ba59fdb758abd31bd0a913236ad9a
7
+ data.tar.gz: 4603cc7e51537b5c79fb704c6f5eac2860628765bbc0e5886e03d2754bf615bf8d781e514ff96336b5887d44fe9b82a6a8ae2d15b344b239549478699ca47caf
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.2
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hamdi_amiche@outlook.fr. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rbacan.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,148 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rbacan (0.1.0)
5
+ activesupport (~> 5.2, >= 5.2.3)
6
+ rails (>= 4.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.2.3)
12
+ actionpack (= 5.2.3)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailer (5.2.3)
16
+ actionpack (= 5.2.3)
17
+ actionview (= 5.2.3)
18
+ activejob (= 5.2.3)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.2.3)
22
+ actionview (= 5.2.3)
23
+ activesupport (= 5.2.3)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.2.3)
29
+ activesupport (= 5.2.3)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.2.3)
35
+ activesupport (= 5.2.3)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.2.3)
38
+ activesupport (= 5.2.3)
39
+ activerecord (5.2.3)
40
+ activemodel (= 5.2.3)
41
+ activesupport (= 5.2.3)
42
+ arel (>= 9.0)
43
+ activestorage (5.2.3)
44
+ actionpack (= 5.2.3)
45
+ activerecord (= 5.2.3)
46
+ marcel (~> 0.3.1)
47
+ activesupport (5.2.3)
48
+ concurrent-ruby (~> 1.0, >= 1.0.2)
49
+ i18n (>= 0.7, < 2)
50
+ minitest (~> 5.1)
51
+ tzinfo (~> 1.1)
52
+ arel (9.0.0)
53
+ builder (3.2.3)
54
+ concurrent-ruby (1.1.5)
55
+ crass (1.0.4)
56
+ diff-lcs (1.3)
57
+ erubi (1.8.0)
58
+ generator_spec (0.9.4)
59
+ activesupport (>= 3.0.0)
60
+ railties (>= 3.0.0)
61
+ globalid (0.4.2)
62
+ activesupport (>= 4.2.0)
63
+ i18n (1.6.0)
64
+ concurrent-ruby (~> 1.0)
65
+ loofah (2.2.3)
66
+ crass (~> 1.0.2)
67
+ nokogiri (>= 1.5.9)
68
+ mail (2.7.1)
69
+ mini_mime (>= 0.1.1)
70
+ marcel (0.3.3)
71
+ mimemagic (~> 0.3.2)
72
+ method_source (0.9.2)
73
+ mimemagic (0.3.3)
74
+ mini_mime (1.0.2)
75
+ mini_portile2 (2.4.0)
76
+ minitest (5.11.3)
77
+ nio4r (2.4.0)
78
+ nokogiri (1.10.3)
79
+ mini_portile2 (~> 2.4.0)
80
+ rack (2.0.7)
81
+ rack-test (1.1.0)
82
+ rack (>= 1.0, < 3)
83
+ rails (5.2.3)
84
+ actioncable (= 5.2.3)
85
+ actionmailer (= 5.2.3)
86
+ actionpack (= 5.2.3)
87
+ actionview (= 5.2.3)
88
+ activejob (= 5.2.3)
89
+ activemodel (= 5.2.3)
90
+ activerecord (= 5.2.3)
91
+ activestorage (= 5.2.3)
92
+ activesupport (= 5.2.3)
93
+ bundler (>= 1.3.0)
94
+ railties (= 5.2.3)
95
+ sprockets-rails (>= 2.0.0)
96
+ rails-dom-testing (2.0.3)
97
+ activesupport (>= 4.2.0)
98
+ nokogiri (>= 1.6)
99
+ rails-html-sanitizer (1.0.4)
100
+ loofah (~> 2.2, >= 2.2.2)
101
+ railties (5.2.3)
102
+ actionpack (= 5.2.3)
103
+ activesupport (= 5.2.3)
104
+ method_source
105
+ rake (>= 0.8.7)
106
+ thor (>= 0.19.0, < 2.0)
107
+ rake (10.5.0)
108
+ rspec (3.8.0)
109
+ rspec-core (~> 3.8.0)
110
+ rspec-expectations (~> 3.8.0)
111
+ rspec-mocks (~> 3.8.0)
112
+ rspec-core (3.8.2)
113
+ rspec-support (~> 3.8.0)
114
+ rspec-expectations (3.8.4)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.8.0)
117
+ rspec-mocks (3.8.1)
118
+ diff-lcs (>= 1.2.0, < 2.0)
119
+ rspec-support (~> 3.8.0)
120
+ rspec-support (3.8.2)
121
+ sprockets (3.7.2)
122
+ concurrent-ruby (~> 1.0)
123
+ rack (> 1, < 3)
124
+ sprockets-rails (3.2.1)
125
+ actionpack (>= 4.0)
126
+ activesupport (>= 4.0)
127
+ sprockets (>= 3.0.0)
128
+ thor (0.20.3)
129
+ thread_safe (0.3.6)
130
+ tzinfo (1.2.5)
131
+ thread_safe (~> 0.1)
132
+ websocket-driver (0.7.1)
133
+ websocket-extensions (>= 0.1.0)
134
+ websocket-extensions (0.1.4)
135
+
136
+ PLATFORMS
137
+ ruby
138
+
139
+ DEPENDENCIES
140
+ bundler (~> 2.0)
141
+ generator_spec (~> 0.9.4)
142
+ railties (~> 5.2, >= 5.2.3)
143
+ rake (~> 10.0)
144
+ rbacan!
145
+ rspec (~> 3.0)
146
+
147
+ BUNDLED WITH
148
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 hamdi
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,66 @@
1
+ # Rbacan
2
+
3
+ a Role-based access control tool to manipulate user access to the functionnalities of your application
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rbacan'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rbacan
20
+
21
+ ## Usage
22
+
23
+ run rails generate rbacan:install
24
+
25
+ copy the content in the generated file db/copy_to_seed.rb in your seeds.rb file
26
+ you have there all the tools you need to create you roles and permissions
27
+
28
+ if you want to assign a role to a user it is simple you just have to do so:
29
+
30
+ user = current_user
31
+
32
+ user.assign_role(role_name)
33
+
34
+ to remove a role from user do this:
35
+
36
+ user.remove_role(role_name)
37
+
38
+ now when you want to test if a user have access to a functionnality use this:
39
+
40
+ user.can?(permission_name)
41
+
42
+ add this line to your user model:
43
+
44
+ include Rbacan::Permittable
45
+
46
+ run:
47
+ rails db:migrate
48
+
49
+ enjoy :D
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hamdi777/rbacan. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+
64
+ ## Code of Conduct
65
+
66
+ Everyone interacting in the Rbacan project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rbacan/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ module Rbacan
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module Rbacan
2
+ class Permission < ApplicationRecord
3
+ self.table_name = Rbacan.permission_table
4
+
5
+ validates :name, presence: true, uniqueness: true
6
+
7
+ has_many :role_permissions, class_name: Rbacan.role_permission_class, dependent: :destroy
8
+ has_many :roles, class_name: Rbacan.role_class, through: :role_permissions
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module Rbacan
2
+ class Role < ApplicationRecord
3
+ self.table_name = Rbacan.role_table
4
+
5
+ validates :name, presence: true, uniqueness: true
6
+
7
+ has_many :role_permissions, class_name: Rbacan.role_permission_class, dependent: :destroy
8
+ has_many :permissions, class_name: Rbacan.permission_class, through: :role_permissions
9
+
10
+ has_many :user_roles, class_name: Rbacan.user_role_class, dependent: :destroy
11
+ has_many :users, through: :user_roles
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module Rbacan
2
+ class RolePermission < ApplicationRecord
3
+ self.table_name = Rbacan.role_permission_table
4
+ belongs_to :role, class_name: Rbacan.role_class
5
+ belongs_to :permission, class_name: Rbacan.permission_class
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Rbacan
2
+ class UserRole < ApplicationRecord
3
+ self.table_name = Rbacan.user_role_table
4
+ belongs_to :role, class_name: Rbacan.role_class
5
+ belongs_to :user, class_name: Rbacan.permittable_class
6
+ end
7
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rbacan"
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(__FILE__)
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
@@ -0,0 +1,26 @@
1
+ #Copy the content of this file into your seeds.rb and comment what you don't need
2
+
3
+
4
+ # define the roles you are going to use example: roles = ["support", "carrier", "mid lane :D", "bot lane :p"]
5
+ roles = []
6
+ # create roles
7
+ Rbacan::RolesAndPermissions.create_roles(roles)
8
+
9
+
10
+ # define the permissions you are going to use example: permissions = ["fire", "invoke", "fly"]
11
+ permissions = []
12
+ # create permissions
13
+ Rbacan::RolesAndPermissions.create_permissions(permissions)
14
+
15
+
16
+ # now assign some permissions to each role
17
+ # to do that you need to define an array of the permissions you want to assign example:
18
+ # role_permissions = ["fly", "fire"]
19
+ role_permissions = []
20
+ Rbacan::RolesAndPermissions.assign_permissions_to_role(role_name, role_permissions)
21
+ # example Rbacan::RolesAndPermissions.assign_permissions_to_role("mid lane :D", role_permissions)
22
+ # you can even define an array of many roles and then do :
23
+ # roles.each do |role|
24
+ # role_name = role.name
25
+ # Rbacan::RolesAndPermissions.assign_permissions_to_role(role_name, role_permissions)
26
+ # end
@@ -0,0 +1,9 @@
1
+ class CreatePermissions < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :permissions do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateRolePermissions < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :role_permissions do |t|
4
+ t.references :role, index: true, foreign_key: {on_delete: :cascade}
5
+ t.references :permission, index: true, foreign_key: {on_delete: :cascade}
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateRoles < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :roles do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class CreateUserRoles < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :user_roles do |t|
4
+ t.references :role, index: true, foreign_key: {on_delete: :cascade}
5
+ t.references :user, index: true, foreign_key: {on_delete: :cascade}
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Rbacan
5
+ module Generators
6
+ class InstallGenerator < ::Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../../install/templates', __FILE__)
9
+ desc "Add the migrations for roles and permissions"
10
+
11
+ def self.next_migration_number(path)
12
+ next_migration_number = current_migration_number(path) + 1
13
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
14
+ end
15
+
16
+ def copy_migrations
17
+ migration_template "create_permissions.rb", "db/migrate/create_permissions.rb"
18
+ migration_template "create_roles.rb", "db/migrate/create_roles.rb"
19
+ migration_template "create_role_permissions.rb", "db/migrate/create_role_permissions.rb"
20
+ migration_template "create_user_roles.rb", "db/migrate/create_user_roles.rb"
21
+ end
22
+
23
+ def copy_seed
24
+ copy_file 'copy_to_seeds.rb', "db/copy_to_seeds.rb"
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/rbacan.rb ADDED
@@ -0,0 +1,58 @@
1
+ require "rbacan/version"
2
+ require "rbacan/permittable"
3
+ require 'rbacan/engine'
4
+ require "rbacan/roles_and_permissions"
5
+
6
+ module Rbacan
7
+ mattr_accessor :permittable_class
8
+ # mattr_accessor :permittable_table
9
+ @@permittable_class = 'User'
10
+ # @@permittable_table = @@permittable_class.tableize
11
+
12
+ mattr_accessor :role_class
13
+ mattr_accessor :role_table
14
+ @@role_class = 'Rbacan::Role'
15
+ @@role_table = 'roles'
16
+
17
+ mattr_accessor :user_role_class
18
+ mattr_accessor :user_role_table
19
+ @@user_role_class = 'Rbacan::UserRole'
20
+ @@user_role_table = 'user_roles'
21
+
22
+ mattr_accessor :permission_class
23
+ mattr_accessor :permission_table
24
+ @@permission_class = 'Rbacan::Permission'
25
+ @@permission_table = 'permissions'
26
+
27
+ mattr_accessor :role_permission_class
28
+ mattr_accessor :role_permission_table
29
+ @@role_permission_class = 'Rbacan::RolePermission'
30
+ @@role_permission_table = 'role_permissions'
31
+
32
+ def create_role(role_name)
33
+ @@role_class.create(name: role_name)
34
+ end
35
+
36
+ def create_permission(permission_name)
37
+ @@permission_class.create(name: permission_name)
38
+ end
39
+
40
+ def assign_permission_to_role(role_name, permission_name)
41
+ chosen_role = @@role_class.find_by_name(role_name)
42
+ given_permission = @@permission_class.find_by_name(permission_name)
43
+ @@role_permission_class.create(role_id: chosen_role.id, perm_id: given_permission.id)
44
+ end
45
+
46
+ def assign_role_to_user(role_name)
47
+ assigned_role = Role.find_by_name(role_name)
48
+ @@user_role_class.create(user_id: self.id, role_id: assigned_role.id)
49
+ end
50
+
51
+ def remove_user_role(role_name)
52
+ removed_role = Role.find_by_name(role_name)
53
+ @@user_role_class.where(user_id: self.id, role_id: removed_role.id).destroy_all
54
+ end
55
+
56
+ class Error < StandardError; end
57
+ # Your code goes here...
58
+ end
@@ -0,0 +1,6 @@
1
+ module Rbacan
2
+ require "rails/all"
3
+ class Engine < ::Rails::Engine
4
+ engine_name 'rbacan'
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_support'
2
+
3
+ module Rbacan
4
+ module Permittable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+
9
+ has_many :user_roles, class_name: Rbacan.user_role_class, dependent: :destroy
10
+ has_many :roles, class_name: Rbacan.role_class, through: :user_roles
11
+
12
+ def assign_role_to_user(role_name)
13
+ assigned_role = Rbacan::Role.find_by_name(role_name)
14
+ Rbacan::UserRole.create(user_id: self.id, role_id: assigned_role.id)
15
+ end
16
+
17
+ def remove_role(role_name)
18
+ removed_role = Rbacan::Role.find_by_name(role_name)
19
+ Rbacan::UserRole.where(user_id: self.id, role_id: removed_role.id).destroy_all
20
+ end
21
+
22
+ def can?(permission)
23
+ @user_roles = self.roles
24
+ user_permission = Rbacan::Permission.find_by_name(permission)
25
+ if user_permission && @user_roles.joins(:role_permissions).where(role_permissions: {permission_id: user_permission.id}).count > 0
26
+ return true
27
+ else
28
+ return false
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ module Rbacan
2
+ module RolesAndPermissions
3
+
4
+ attr_reader :test_method
5
+
6
+ def self.create_roles(roles)
7
+ roles.each do |role|
8
+ Rbacan::Role.create(name: role)
9
+ end
10
+ end
11
+
12
+ def self.create_permissions(permissions)
13
+ permissions.each do |permission|
14
+ Rbacan::Permission.create(name: permission)
15
+ end
16
+ end
17
+
18
+ def self.assign_permissions_to_role(role_name, permissions)
19
+ chosen_role = Rbacan::Role.find_by_name(role_name)
20
+ permissions.each do |permission|
21
+ given_permission = Rbacan::Permission.find_by_name(permission)
22
+ Rbacan::RolePermission.create(role_id: chosen_role.id, permission_id: given_permission.id)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Rbacan
2
+ VERSION = "0.1.0"
3
+ end
data/rbacan.gemspec ADDED
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rbacan/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbacan"
8
+ spec.version = Rbacan::VERSION
9
+ spec.authors = ["hamdi"]
10
+ spec.email = ["hamdi_amiche@outlook.fr"]
11
+
12
+ spec.summary = %q{a gem to give permission access to users looknig back to their roles}
13
+ spec.description = %q{RBACan is a gem that provides security to your application functionnalities by a simple method can()}
14
+ spec.homepage = "https://github.com/hamdi777/RBACan"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/profiles/hamdi777"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "git@github.com:hamdi777/RBACan.git"
24
+ spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency 'rails', '>= 4.2'
40
+ spec.add_development_dependency "bundler", "~> 2.0"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ spec.add_development_dependency 'generator_spec', '~> 0.9.4'
44
+ spec.add_development_dependency 'railties', '~> 5.2', '>= 5.2.3'
45
+
46
+ spec.add_dependency 'activesupport', '~> 5.2', '>= 5.2.3'
47
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbacan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hamdi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: generator_spec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: railties
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 5.2.3
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '5.2'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 5.2.3
103
+ - !ruby/object:Gem::Dependency
104
+ name: activesupport
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '5.2'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 5.2.3
113
+ type: :runtime
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '5.2'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 5.2.3
123
+ description: RBACan is a gem that provides security to your application functionnalities
124
+ by a simple method can()
125
+ email:
126
+ - hamdi_amiche@outlook.fr
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".gitignore"
132
+ - ".rspec"
133
+ - ".travis.yml"
134
+ - CODE_OF_CONDUCT.md
135
+ - Gemfile
136
+ - Gemfile.lock
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - app/models/rbacan/application_record.rb
141
+ - app/models/rbacan/permission.rb
142
+ - app/models/rbacan/role.rb
143
+ - app/models/rbacan/role_permission.rb
144
+ - app/models/rbacan/user_role.rb
145
+ - bin/console
146
+ - bin/setup
147
+ - lib/generators/install/templates/copy_to_seeds.rb
148
+ - lib/generators/install/templates/create_permissions.rb
149
+ - lib/generators/install/templates/create_role_permissions.rb
150
+ - lib/generators/install/templates/create_roles.rb
151
+ - lib/generators/install/templates/create_user_roles.rb
152
+ - lib/generators/rbacan/install_generator.rb
153
+ - lib/rbacan.rb
154
+ - lib/rbacan/engine.rb
155
+ - lib/rbacan/permittable.rb
156
+ - lib/rbacan/roles_and_permissions.rb
157
+ - lib/rbacan/version.rb
158
+ - rbacan.gemspec
159
+ homepage: https://github.com/hamdi777/RBACan
160
+ licenses:
161
+ - MIT
162
+ metadata:
163
+ allowed_push_host: https://rubygems.org/profiles/hamdi777
164
+ homepage_uri: https://github.com/hamdi777/RBACan
165
+ source_code_uri: git@github.com:hamdi777/RBACan.git
166
+ changelog_uri: 'TODO: Put your gem''s CHANGELOG.md URL here.'
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.6.13
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: a gem to give permission access to users looknig back to their roles
187
+ test_files: []