guachiman 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 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
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in guachiman.gemspec
4
+ gemspec
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
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |task|
6
+ task.libs << 'test'
7
+ task.pattern = 'test/**/*_test.rb'
8
+ task.warning = true
9
+ task.verbose = true
10
+ end
11
+
12
+ task default: :test
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
@@ -0,0 +1,4 @@
1
+ class Permission
2
+ include Guachiman::Permissions
3
+ # include Guachiman::Params
4
+ end
data/lib/guachiman.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'guachiman/version'
2
+ require 'guachiman/permissions'
3
+ require 'guachiman/params'
4
+
5
+ if defined? Rails
6
+ require 'guachiman/rails/railtie'
7
+ require 'guachiman/rails/permissible'
8
+ end
@@ -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,8 @@
1
+ require 'rails'
2
+
3
+ module Guachiman
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Guachiman
2
+ VERSION = '0.0.2'
3
+ 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
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+ require 'minitest/focus'
5
+ require 'guachiman'
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