restricted_access 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +178 -0
- data/Rakefile +1 -0
- data/lib/generators/restricted_access/USAGE +9 -0
- data/lib/generators/restricted_access/install_generator.rb +24 -0
- data/lib/generators/restricted_access/templates/restricted_access.erb +18 -0
- data/lib/restricted_access.rb +55 -0
- data/lib/restricted_access/access.rb +17 -0
- data/lib/restricted_access/configuration.rb +7 -0
- data/lib/restricted_access/controller.rb +6 -0
- data/lib/restricted_access/helper.rb +8 -0
- data/lib/restricted_access/model.rb +19 -0
- data/lib/restricted_access/version.rb +3 -0
- data/restricted_access.gemspec +25 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 57423d579e160bde70cb930f1afec19fd06254d0
|
4
|
+
data.tar.gz: af663d4ad4acf2af0484898788688e426bc1b76e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f05f7df578367cfeed90cff6f96be561a2547a6ab43ff283e7624239b9bdbb7c0b3697e2a1cfa854c6aa33b05b1ecf079bf00de24215671d000a6bef8e62172
|
7
|
+
data.tar.gz: 1d8e02118ae4acf5f2f431f431fbadc402e852547982d739777ab823eabc012b77cfa86deeb3cfb94d5754b42a62b57432ea16c90ba495e24c304f0be65c192f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 4nt1
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
# RestrictedAccess
|
2
|
+
|
3
|
+
An access rights management tool.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'restricted_access', git: 'https://github.com/4nt1/restricted_access'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install restricted_access
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The gem is currently working only with Mongoid.
|
22
|
+
|
23
|
+
It depends on [Devise](https://github.com/plataformatec/devise) & [mongoid-enum](https://github.com/thetron/mongoid-enum).
|
24
|
+
|
25
|
+
|
26
|
+
Generate this initializer with
|
27
|
+
```
|
28
|
+
rails g restricted_access:install admin --levels=mini normal super --controller_scope=backoffice
|
29
|
+
```
|
30
|
+
|
31
|
+
model_name is the name of the model concerned with the access restriction.
|
32
|
+
|
33
|
+
Give the available levels of access to the --levels options
|
34
|
+
|
35
|
+
Give your controllers scope name to the --controller_scope options (default: nil)
|
36
|
+
|
37
|
+
This will generate the restricted_access.rb initializer
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
RestrictedAccess.configure do |config|
|
41
|
+
|
42
|
+
config.accesses = [ { level: :mini,
|
43
|
+
label: 'Some description for this access level',
|
44
|
+
power: 0 },
|
45
|
+
{ level: :normal,
|
46
|
+
label: 'Some description for this access level',
|
47
|
+
power: 1 },
|
48
|
+
{ level: :super,
|
49
|
+
label: 'Some description for this access level',
|
50
|
+
power: 2}
|
51
|
+
]
|
52
|
+
config.resource = :admin
|
53
|
+
config.controller_scope = :backoffice
|
54
|
+
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
You can customize the accesses with a label (optional) and define different power (the higher has more rights).
|
59
|
+
|
60
|
+
The `config.resource` and `config.controller_scope` are useful only in Rails, defining some methods in controllers and helpers (see below).
|
61
|
+
|
62
|
+
### RestrictedAccess::Model
|
63
|
+
|
64
|
+
Include the RestrictedAccess::Model module in your related model
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class Admin
|
68
|
+
include Mongoid::Document
|
69
|
+
include RestrictedAccess::Model
|
70
|
+
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
The module enhances the model with some methods and attributes.
|
75
|
+
|
76
|
+
Every model has now a :level attribute (Symbol type), by default the first defined in your initializer. You can set it like any attributes.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
admin = Admin.first
|
80
|
+
admin.update(level: :super)
|
81
|
+
|
82
|
+
admin2 = Admin.last
|
83
|
+
admin.update(level: :mini)
|
84
|
+
```
|
85
|
+
|
86
|
+
The level defines its access rights.
|
87
|
+
|
88
|
+
Each instance has a `:access` method, returning a `RestrictedAccess::Access` instance.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
admin.access
|
92
|
+
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @label="", @power=2>
|
93
|
+
|
94
|
+
```
|
95
|
+
|
96
|
+
The `RestrictedAccess::Access` class include comparable, so you can do such things :
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
admin.access > admin2.access
|
100
|
+
=> true
|
101
|
+
|
102
|
+
RestrictedAccess.accesses.max
|
103
|
+
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @label="", @power=2>
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
Thanks to the [mongoid-enum](https://github.com/thetron/mongoid-enum) gem, some methods to check rights.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
admin.mini?
|
111
|
+
=> false
|
112
|
+
|
113
|
+
admin.super?
|
114
|
+
=> true
|
115
|
+
|
116
|
+
Admin::LEVEL
|
117
|
+
=> [:mini, :normal, :super]
|
118
|
+
|
119
|
+
# scopes
|
120
|
+
Admin.mini # => Mongoid::Criteria
|
121
|
+
Admin.super # => Mongoid::Criteria
|
122
|
+
```
|
123
|
+
|
124
|
+
### RestrictedAccess::Controller
|
125
|
+
|
126
|
+
If you provided a `config.resource` and `config.controller_scope` in the initializer you can include the `RestrictedAccess::Controller` in your controller.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class Backoffice::BaseController < ApplicationController
|
130
|
+
include RestrictedAccess::Controller
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
Every inherited controller has now a few more methods:
|
135
|
+
|
136
|
+
* `:restrict_access`, which redirect to the `#{controller_scope}_root_path`. Set controller_scope to nil if you just want to redirect to root_path.
|
137
|
+
|
138
|
+
* `:prevent_#{level}_access`, which calls `:restrict_access` if the `:current_#{resource_name}` doesn't have enough access right. If you use Devise, you already have a `:current_#{resource_name}` method, if you don't use Devise, just implement it.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
class Backoffice::AdminsController < Backoffice::BaseController
|
142
|
+
before_action :prevent_normal_access, except: [:index]
|
143
|
+
# mini & normal admins will only be able to access index view
|
144
|
+
end
|
145
|
+
```
|
146
|
+
|
147
|
+
### RestrictedAccess::Helper
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
module Backoffice::AdminHelper
|
151
|
+
include RestrictedAccess::Helper
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
If you provided a `config.resource` option, you can include the `RestrictedAccess::Helper` in one of your helpers.
|
157
|
+
|
158
|
+
It provides a `:available_for` method in the views, allowing you to hide some part of the view.
|
159
|
+
|
160
|
+
```html
|
161
|
+
<!-- this div won't be seen be admins lower than super -->
|
162
|
+
<%= available_for :super do %>
|
163
|
+
<div>
|
164
|
+
I have something to hide here.
|
165
|
+
</div>
|
166
|
+
<%- end %>
|
167
|
+
```
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
## Contributing
|
173
|
+
|
174
|
+
1. Fork it ( http://github.com/<my-github-username>/restricted_access/fork )
|
175
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
176
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
177
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
178
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RestrictedAccess
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::NamedBase
|
4
|
+
include Rails::Generators::ResourceHelpers
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
argument :resource_name, type: :string, default: 'user'
|
7
|
+
class_option :levels, type: :array, default: ['normal', 'super'], desc: "List of the differents access levels"
|
8
|
+
class_option :controller_scope, type: :string, desc: "Scope of the concerned controllers"
|
9
|
+
|
10
|
+
desc "Creates a RestrictedAccess initializer."
|
11
|
+
|
12
|
+
def set_variable
|
13
|
+
@levels = options.levels
|
14
|
+
@resource_name = resource_name
|
15
|
+
@controller_scope = options.controller_scope
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_initializer
|
19
|
+
template "restricted_access.erb", "config/initializers/restricted_access.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
RestrictedAccess.configure do |config|
|
2
|
+
|
3
|
+
config.accesses = [<% @levels.each_with_index do |level, index| %>
|
4
|
+
{ level: :<%= level %>,
|
5
|
+
label: '',
|
6
|
+
power: <%= index %> }<% if index + 1 < @levels.count %>,<%- end %>
|
7
|
+
<%- end %>
|
8
|
+
]
|
9
|
+
|
10
|
+
config.resource = :<%= @resource_name %>
|
11
|
+
|
12
|
+
<% if @controller_scope %>
|
13
|
+
config.controller_scope = :<%= @controller_scope %>
|
14
|
+
<% else %>
|
15
|
+
config.controller_scope = nil
|
16
|
+
<%- end %>
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "mongoid/enum"
|
2
|
+
require "restricted_access/version"
|
3
|
+
require 'restricted_access/configuration'
|
4
|
+
require 'restricted_access/access'
|
5
|
+
require 'restricted_access/model'
|
6
|
+
require 'restricted_access/controller'
|
7
|
+
require 'restricted_access/helper'
|
8
|
+
|
9
|
+
module RestrictedAccess
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def configuration
|
13
|
+
@configuration ||= Configuration.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield(configuration)
|
18
|
+
define_dynamic_methods
|
19
|
+
end
|
20
|
+
|
21
|
+
def accesses
|
22
|
+
@accesses ||= configuration.accesses.map do |a|
|
23
|
+
Access.new(a[:level], a[:label], a[:power])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def resource
|
28
|
+
@resource ||= configuration.resource
|
29
|
+
end
|
30
|
+
|
31
|
+
def controller_scope
|
32
|
+
@controller_scope ||= configuration.controller_scope
|
33
|
+
end
|
34
|
+
|
35
|
+
def define_dynamic_methods
|
36
|
+
# on Access class
|
37
|
+
accesses.map(&:level).each do |level|
|
38
|
+
Access.define_singleton_method level do
|
39
|
+
RestrictedAccess.accesses.find {|a| a.level == level}
|
40
|
+
end
|
41
|
+
|
42
|
+
RestrictedAccess::Controller.class_eval do
|
43
|
+
define_method "prevent_#{level}_access" do
|
44
|
+
restrict_access if send("current_#{RestrictedAccess.resource}").access <= RestrictedAccess::Access.send(level)
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method :restrict_access do
|
48
|
+
_scope = RestrictedAccess.controller_scope.present? ? "#{RestrictedAccess.controller_scope}_" : nil
|
49
|
+
redirect_to send("#{_scope}root_path"), notice: 'You do not have access to this page' and return
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RestrictedAccess
|
2
|
+
class Access
|
3
|
+
include Comparable
|
4
|
+
attr_accessor :level, :label, :power
|
5
|
+
|
6
|
+
def <=>(access)
|
7
|
+
power <=> access.power
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(level, label, power)
|
11
|
+
@level = level
|
12
|
+
@label = label
|
13
|
+
@power = power
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module RestrictedAccess
|
2
|
+
module Helper
|
3
|
+
def available_for(level, &block)
|
4
|
+
access = RestrictedAccess::Access.send(level)
|
5
|
+
capture(&block) if access && send("current_#{RestrictedAccess.resource}") && send("current_#{RestrictedAccess.resource}").access >= access
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RestrictedAccess
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do |base|
|
6
|
+
include Mongoid::Enum
|
7
|
+
enum :level, RestrictedAccess.accesses.map(&:level)
|
8
|
+
end
|
9
|
+
|
10
|
+
def access
|
11
|
+
RestrictedAccess.accesses.find {|a| a.level == level}
|
12
|
+
end
|
13
|
+
|
14
|
+
def authorized_accesses
|
15
|
+
RestrictedAccess.accesses.select {|a| a <= access}
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'restricted_access/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "restricted_access"
|
8
|
+
spec.version = RestrictedAccess::VERSION
|
9
|
+
spec.authors = ["4nt1"]
|
10
|
+
spec.email = ["antoinemary@hotmail.fr"]
|
11
|
+
spec.summary = %q{An access rights management tool intended to work with Devise}
|
12
|
+
spec.description = %q{An access rights management tool intended to work with Devise}
|
13
|
+
spec.homepage = "https://github.com/4nt1/restricted_access"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency "mongoid"
|
24
|
+
spec.add_dependency "mongoid-enum"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restricted_access
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- 4nt1
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mongoid
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mongoid-enum
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: An access rights management tool intended to work with Devise
|
70
|
+
email:
|
71
|
+
- antoinemary@hotmail.fr
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/generators/restricted_access/USAGE
|
82
|
+
- lib/generators/restricted_access/install_generator.rb
|
83
|
+
- lib/generators/restricted_access/templates/restricted_access.erb
|
84
|
+
- lib/restricted_access.rb
|
85
|
+
- lib/restricted_access/access.rb
|
86
|
+
- lib/restricted_access/configuration.rb
|
87
|
+
- lib/restricted_access/controller.rb
|
88
|
+
- lib/restricted_access/helper.rb
|
89
|
+
- lib/restricted_access/model.rb
|
90
|
+
- lib/restricted_access/version.rb
|
91
|
+
- restricted_access.gemspec
|
92
|
+
homepage: https://github.com/4nt1/restricted_access
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: An access rights management tool intended to work with Devise
|
116
|
+
test_files: []
|