guachiman 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +25 -0
- data/Rakefile +12 -0
- data/guachiman.gemspec +25 -0
- data/lib/generators/guachiman/install/install_generator.rb +12 -0
- data/lib/generators/guachiman/install/templates/permission.rb +4 -0
- data/lib/guachiman.rb +8 -0
- data/lib/guachiman/params.rb +40 -0
- data/lib/guachiman/permissions.rb +32 -0
- data/lib/guachiman/rails/permissible.rb +31 -0
- data/lib/guachiman/rails/railtie.rb +8 -0
- data/lib/guachiman/version.rb +3 -0
- data/test/generators/install_generator_test.rb +24 -0
- data/test/test_helper.rb +5 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb461ee01dde450aef11c476682464abbadc3bea
|
4
|
+
data.tar.gz: 946f2fa5afb0ac8c04ca70bea40a555d8ad59c9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c595ce58306613c4716d44a905e8b604313be6d8d8761bcb9b717df24099520aac0ae0d2c0666354518fb555fd43b5a86e626a2dff5052d97d720bda8277a20d
|
7
|
+
data.tar.gz: 819f6bb872bec38d9e14d174496a1ba6fb0316a4f201fd3274a53bb6005b0044debe191f8d686d843add2fd5e343384286d5919961ea13738e89232af924a1a9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Guachiman
|
2
|
+
=========
|
3
|
+
|
4
|
+
Basic Authorization gem. Based on [RailsCast #385 Authorization from Scratch](http://railscasts.com/episodes/385-authorization-from-scratch-part-1)
|
5
|
+
from Ryan Bates.
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'guachiman'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install guachiman
|
21
|
+
|
22
|
+
Usage
|
23
|
+
-----
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
data/Rakefile
ADDED
data/guachiman.gemspec
ADDED
@@ -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 'guachiman/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'guachiman'
|
8
|
+
spec.version = Guachiman::VERSION
|
9
|
+
spec.authors = ['Francesco Rodriguez', 'Gustavo Beathyate']
|
10
|
+
spec.email = ['lrodriguezsanc@gmail.com', 'gustavo@epiclabs.pe']
|
11
|
+
spec.summary = 'Basic authorization library'
|
12
|
+
spec.description = spec.summary << ' inspired in Ryan Bates code'
|
13
|
+
spec.homepage = 'https://github.com/epiclabs/guachiman'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_dependency 'railties', '>= 3.2'
|
21
|
+
spec.add_development_dependency 'minitest'
|
22
|
+
spec.add_development_dependency 'minitest-focus'
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Guachiman
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
desc 'Install guachiman'
|
5
|
+
source_root File.expand_path '../templates', __FILE__
|
6
|
+
|
7
|
+
def copy_permission_model
|
8
|
+
template 'permission.rb', 'app/models/permission.rb'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/guachiman.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Guachiman
|
2
|
+
module Params
|
3
|
+
attr_reader :read_allowed_params, :write_allowed_params
|
4
|
+
|
5
|
+
[:read, :write].each do |action|
|
6
|
+
ivar = "@#{action}_allowed_params"
|
7
|
+
|
8
|
+
define_method "allow_#{action}_param" do |resources, attributes|
|
9
|
+
instance_variable_set(ivar, {}) unless instance_variable_get ivar
|
10
|
+
Array(resources).each do |resource|
|
11
|
+
instance_variable_get(ivar)[resource] ||= []
|
12
|
+
instance_variable_get(ivar)[resource] += Array(attributes)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
define_method "allow_#{action}_param?" do |resource, attribute|
|
17
|
+
if instance_variable_get :@allow_all
|
18
|
+
true
|
19
|
+
elsif instance_variable_get(ivar) && instance_variable_get(ivar)[resource]
|
20
|
+
instance_variable_get(ivar)[resource].include? attribute
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def allow_param resources, attributes
|
26
|
+
allow_read_param resources, attributes
|
27
|
+
allow_write_param resources, attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
def permit_params! params
|
31
|
+
if @allow_all
|
32
|
+
params.permit!
|
33
|
+
elsif write_allowed_params
|
34
|
+
write_allowed_params.each do |resource, attributes|
|
35
|
+
params[resource] = params[resource].permit(*attributes) if params[resource].respond_to? :permit
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Guachiman
|
2
|
+
module Permissions
|
3
|
+
attr_reader :allowed_actions, :allow_all
|
4
|
+
|
5
|
+
def allow controllers, actions, &block
|
6
|
+
@allowed_actions ||= Hash.new
|
7
|
+
Array(controllers).each do |controller|
|
8
|
+
Array(actions).each do |action|
|
9
|
+
allowed_actions[controller] ||= {}
|
10
|
+
allowed_actions[controller].merge!({
|
11
|
+
action => (block || true)
|
12
|
+
})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def allow? controller, action, resource = nil
|
18
|
+
allowed = allow_all || check_allowed_action(controller, action)
|
19
|
+
!!allowed && (allowed == true || resource && allowed.call(resource))
|
20
|
+
end
|
21
|
+
|
22
|
+
def allow_all!
|
23
|
+
@allow_all = true
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def check_allowed_action controller, action
|
29
|
+
allowed_actions && allowed_actions[controller.to_sym] && allowed_actions[controller.to_sym][action.to_sym]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Guachiman
|
2
|
+
module Permissible
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_filter :authorize
|
7
|
+
end
|
8
|
+
|
9
|
+
def current_permission
|
10
|
+
@current_permission ||= Permission.new current_user
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_resource
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def current_user
|
18
|
+
raise 'This method must be implemented'
|
19
|
+
end
|
20
|
+
|
21
|
+
def not_authorized
|
22
|
+
raise 'This method must be implemented'
|
23
|
+
end
|
24
|
+
|
25
|
+
def authorize
|
26
|
+
if !current_permission.allow?(params[:controller], params[:action], current_resource)
|
27
|
+
not_authorized
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails/generators/test_case'
|
3
|
+
require 'generators/guachiman/install/install_generator'
|
4
|
+
|
5
|
+
class InstallGeneratorTest < Rails::Generators::TestCase
|
6
|
+
DESTINATION = File.expand_path File.join(File.dirname(__FILE__), '..', '..', 'tmp')
|
7
|
+
destination DESTINATION
|
8
|
+
|
9
|
+
tests Guachiman::Generators::InstallGenerator
|
10
|
+
setup :prepare_destination
|
11
|
+
|
12
|
+
def prepare_destination
|
13
|
+
FileUtils.mkdir_p "#{DESTINATION}/app"
|
14
|
+
FileUtils.mkdir_p "#{DESTINATION}/app/models"
|
15
|
+
end
|
16
|
+
|
17
|
+
test 'create permission' do
|
18
|
+
run_generator
|
19
|
+
|
20
|
+
assert_file 'app/models/permission.rb', /Permission/
|
21
|
+
assert_file 'app/models/permission.rb', /Guachiman::Permissions/
|
22
|
+
assert_file 'app/models/permission.rb', /Guachiman::Params/
|
23
|
+
end
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: guachiman
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Rodriguez
|
8
|
+
- Gustavo Beathyate
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '3.2'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '3.2'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: minitest
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest-focus
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: bundler
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '1.3'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: Basic authorization library inspired in Ryan Bates code
|
85
|
+
email:
|
86
|
+
- lrodriguezsanc@gmail.com
|
87
|
+
- gustavo@epiclabs.pe
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- guachiman.gemspec
|
98
|
+
- lib/generators/guachiman/install/install_generator.rb
|
99
|
+
- lib/generators/guachiman/install/templates/permission.rb
|
100
|
+
- lib/guachiman.rb
|
101
|
+
- lib/guachiman/params.rb
|
102
|
+
- lib/guachiman/permissions.rb
|
103
|
+
- lib/guachiman/rails/permissible.rb
|
104
|
+
- lib/guachiman/rails/railtie.rb
|
105
|
+
- lib/guachiman/version.rb
|
106
|
+
- test/generators/install_generator_test.rb
|
107
|
+
- test/test_helper.rb
|
108
|
+
homepage: https://github.com/epiclabs/guachiman
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.0.0
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Basic authorization library inspired in Ryan Bates code
|
132
|
+
test_files:
|
133
|
+
- test/generators/install_generator_test.rb
|
134
|
+
- test/test_helper.rb
|