guachiman-rails 1.0.0.pre
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 +20 -0
- data/README.md +128 -0
- data/Rakefile +12 -0
- data/guachiman-rails.gemspec +26 -0
- data/lib/generators/guachiman/rails/install/install_generator.rb +14 -0
- data/lib/generators/guachiman/rails/install/templates/authorization.rb +25 -0
- data/lib/guachiman/rails/authorizable.rb +41 -0
- data/lib/guachiman/rails/railtie.rb +9 -0
- data/lib/guachiman/rails/version.rb +5 -0
- data/lib/guachiman-rails.rb +2 -0
- data/test/generators/install_generator_test.rb +29 -0
- data/test/test_helper.rb +4 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2ff0bc3ea49cd25fa5316275c91f4294ecd2a204
|
4
|
+
data.tar.gz: 587e98a3eebbb0bc1c0bc3e5053e83a93e77626b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a45afbcae201def6c8e3292fa4d16d65d6e42179b2550041d191af5757ca62ae34e8a2572020efd2800c146c081b7c9ec203598635dd20b082c71b80e97a9fb
|
7
|
+
data.tar.gz: c66a6871dfbabf18fff46c89b4dca4fd3150b2e7b7c91c1a19ccbc9db82ed77183bb4f2541861c5f723142f3904b37df5d4a62226b2d5f132d3a49c09b95756a
|
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,128 @@
|
|
1
|
+
Guachiman for Rails
|
2
|
+
===================
|
3
|
+
|
4
|
+
Basic Authorization gem for rails based on [RailsCast #385 Authorization from Scratch][1] by Ryan Bates.
|
5
|
+
Built on top of [guachiman][2].
|
6
|
+
|
7
|
+
[![Codeship Status for goddamnhippie/guachiman-rails][3]][4]
|
8
|
+
|
9
|
+
[1]: http://railscasts.com/episodes/385-authorization-from-scratch-part-1
|
10
|
+
[2]: https://github.com/goddamnhippie/guachiman
|
11
|
+
[3]: https://www.codeship.io/projects/06034ef0-f456-0131-65bd-5a054a318c0e/status
|
12
|
+
[4]: https://www.codeship.io/projects/28084
|
13
|
+
|
14
|
+
Installation
|
15
|
+
------------
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'guachiman-rails'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$ bundle
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it directly:
|
30
|
+
|
31
|
+
```bash
|
32
|
+
$ gem install guachiman-rails
|
33
|
+
```
|
34
|
+
|
35
|
+
Usage
|
36
|
+
-----
|
37
|
+
|
38
|
+
Run `rails g guachiman:install`
|
39
|
+
|
40
|
+
This will generate a `authorization.rb` file in `app/models`.
|
41
|
+
|
42
|
+
Include `Guachiman::Authorizable` in `ApplicationController` and implement a `current_user` method there.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# app/controllers/application_controller.rb
|
46
|
+
|
47
|
+
include Guachiman::Authorizable
|
48
|
+
|
49
|
+
def current_user
|
50
|
+
@current_user ||= User.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
You can also override these methods to change the behaviour, for example:
|
55
|
+
|
56
|
+
### To skip authorization for admins
|
57
|
+
|
58
|
+
Defaults to `false`.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
def skip_authorization?
|
62
|
+
current_user.admin?
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
### To handle what happens after the authorization takes place
|
67
|
+
|
68
|
+
This is the default implementation.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
def after_authorization(authorized)
|
72
|
+
return true if authorized
|
73
|
+
|
74
|
+
if request.get? && !request.xhr?
|
75
|
+
redirect_to root_path, alert: t(:not_authorized)
|
76
|
+
else
|
77
|
+
render nothing: true, status: :unauthorized
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
That's it, now you can describe your authorization object in this way:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class Authorization
|
86
|
+
include Guachiman
|
87
|
+
|
88
|
+
def initialize(user)
|
89
|
+
if @current_user = user
|
90
|
+
user_authorization
|
91
|
+
else
|
92
|
+
guest_authorization
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def guest_authorization
|
99
|
+
allow :sessions, [:new, :create]
|
100
|
+
allow :users, [:new, :create]
|
101
|
+
end
|
102
|
+
|
103
|
+
def user_authorization
|
104
|
+
guest_authorization
|
105
|
+
|
106
|
+
allow :users, [:show, :edit, :update] do |user|
|
107
|
+
@current_user.id == user.id
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
The method `#current_resource` will default to nil but you can override in the controllers:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
class UsersController < ApplicationController
|
117
|
+
# ...
|
118
|
+
|
119
|
+
def current_resource
|
120
|
+
@user ||= params[:id].present? ? User.find(params[:id]) : User.new
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
License
|
126
|
+
-------
|
127
|
+
|
128
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'guachiman/rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'guachiman-rails'
|
8
|
+
spec.version = Guachiman::Rails::VERSION
|
9
|
+
spec.authors = ['Francesco Rodriguez', 'Gustavo Beathyate']
|
10
|
+
spec.email = ['lrodriguezsanc@gmail.com', 'gustavo.bt@me.com']
|
11
|
+
spec.summary = 'Rails specific implementation of the Guachiman gem'
|
12
|
+
spec.description = "#{ spec.summary } for authorization in ActionController"
|
13
|
+
spec.homepage = 'https://github.com/goddamnhippie/guachiman-rails'
|
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 'guachiman', '~> 1.0', '>= 1.0.2'
|
21
|
+
spec.add_dependency 'railties', '~> 4.0', '>= 4.0.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.3', '>= 10.3.0'
|
24
|
+
spec.add_development_dependency 'minitest', '~> 5.3', '>= 5.3.3'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.6', '>= 1.6.0'
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Guachiman
|
2
|
+
module Rails
|
3
|
+
module Generators
|
4
|
+
class InstallGenerator < ::Rails::Generators::Base
|
5
|
+
desc 'Create Authorization model'
|
6
|
+
source_root File.expand_path '../templates', __FILE__
|
7
|
+
|
8
|
+
def copy_authorization_model
|
9
|
+
template 'authorization.rb', 'app/models/authorization.rb'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Authorization
|
2
|
+
include Guachiman
|
3
|
+
|
4
|
+
def initialize(user)
|
5
|
+
if @current_user = user
|
6
|
+
user_authorization
|
7
|
+
else
|
8
|
+
guest_authorization
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def guest_authorization
|
15
|
+
# allow :sessions, [:new, :create]
|
16
|
+
end
|
17
|
+
|
18
|
+
def user_authorization
|
19
|
+
guest_authorization
|
20
|
+
|
21
|
+
# allow :users, [:show, :edit, :update] do |user_id|
|
22
|
+
# @current_user.id == user_id
|
23
|
+
# end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Guachiman
|
2
|
+
module Authorizable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_action :authorize, unless: :skip_authorization?
|
7
|
+
end
|
8
|
+
|
9
|
+
def authorization
|
10
|
+
@authorization ||= Authorization.new(current_user)
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_user
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
|
17
|
+
def current_resource
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def skip_authorization?
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def authorize
|
26
|
+
authorized = authorization.allow?(controller_name, action_name, current_resource)
|
27
|
+
|
28
|
+
after_authorization(authorized)
|
29
|
+
end
|
30
|
+
|
31
|
+
def after_authorization(authorized)
|
32
|
+
return true if authorized
|
33
|
+
|
34
|
+
if request.get? && !request.xhr?
|
35
|
+
redirect_to root_path, alert: t(:not_authorized)
|
36
|
+
else
|
37
|
+
render nothing: true, status: :unauthorized
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rails/generators/test_case'
|
3
|
+
require 'generators/guachiman/rails/install/install_generator'
|
4
|
+
|
5
|
+
class InstallGeneratorTest < Rails::Generators::TestCase
|
6
|
+
DESTINATION = File.expand_path File.join(File.dirname(__FILE__), '..', '..', 'tmp')
|
7
|
+
FileUtils.mkdir_p DESTINATION unless Dir.exist? DESTINATION
|
8
|
+
|
9
|
+
destination DESTINATION
|
10
|
+
|
11
|
+
tests Guachiman::Rails::Generators::InstallGenerator
|
12
|
+
setup :prepare_destination
|
13
|
+
|
14
|
+
def prepare_destination
|
15
|
+
if Dir.exists? "#{ DESTINATION }/app"
|
16
|
+
FileUtils.rm_r "#{ DESTINATION }/app"
|
17
|
+
end
|
18
|
+
|
19
|
+
FileUtils.mkdir_p "#{ DESTINATION }/app/models"
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'create permission' do
|
23
|
+
run_generator
|
24
|
+
|
25
|
+
assert_file 'app/models/authorization.rb' do |f|
|
26
|
+
assert_match /include Guachiman/, f
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: guachiman-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Rodriguez
|
8
|
+
- Gustavo Beathyate
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: guachiman
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.0'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.2
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.0'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.2
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: railties
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 4.0.0
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - "~>"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '4.0'
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 4.0.0
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rake
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.3'
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 10.3.0
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '10.3'
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 10.3.0
|
74
|
+
- !ruby/object:Gem::Dependency
|
75
|
+
name: minitest
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '5.3'
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 5.3.3
|
84
|
+
type: :development
|
85
|
+
prerelease: false
|
86
|
+
version_requirements: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '5.3'
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 5.3.3
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: bundler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - "~>"
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '1.6'
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.6.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.6'
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 1.6.0
|
114
|
+
description: Rails specific implementation of the Guachiman gem for authorization
|
115
|
+
in ActionController
|
116
|
+
email:
|
117
|
+
- lrodriguezsanc@gmail.com
|
118
|
+
- gustavo.bt@me.com
|
119
|
+
executables: []
|
120
|
+
extensions: []
|
121
|
+
extra_rdoc_files: []
|
122
|
+
files:
|
123
|
+
- ".gitignore"
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- guachiman-rails.gemspec
|
129
|
+
- lib/generators/guachiman/rails/install/install_generator.rb
|
130
|
+
- lib/generators/guachiman/rails/install/templates/authorization.rb
|
131
|
+
- lib/guachiman-rails.rb
|
132
|
+
- lib/guachiman/rails/authorizable.rb
|
133
|
+
- lib/guachiman/rails/railtie.rb
|
134
|
+
- lib/guachiman/rails/version.rb
|
135
|
+
- test/generators/install_generator_test.rb
|
136
|
+
- test/test_helper.rb
|
137
|
+
homepage: https://github.com/goddamnhippie/guachiman-rails
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">"
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: 1.3.1
|
155
|
+
requirements: []
|
156
|
+
rubyforge_project:
|
157
|
+
rubygems_version: 2.4.1
|
158
|
+
signing_key:
|
159
|
+
specification_version: 4
|
160
|
+
summary: Rails specific implementation of the Guachiman gem
|
161
|
+
test_files:
|
162
|
+
- test/generators/install_generator_test.rb
|
163
|
+
- test/test_helper.rb
|