cancan-permits 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.rspec +1 -0
- data/LICENSE +20 -0
- data/README.markdown +103 -0
- data/Rakefile +19 -0
- data/VERSION +1 -0
- data/lib/cancan-permits.rb +3 -0
- data/lib/cancan-permits/namespaces.rb +6 -0
- data/lib/cancan-permits/permit/base_permit.rb +44 -0
- data/lib/cancan-permits/permits/abiity.rb +40 -0
- data/lib/cancan-permits/permits/configuration.rb +7 -0
- data/lib/cancan-permits/permits/roles.rb +9 -0
- data/lib/cancan-permits/rspec/config.rb +0 -0
- data/lib/cancan-permits/rspec/matchers/have_permits.rb +0 -0
- data/lib/generators/permits/permits_generator.rb +71 -0
- data/lib/generators/permits/templates/permit.rb +12 -0
- data/spec/cancan-permits/fixtures/ability.rb +19 -0
- data/spec/cancan-permits/fixtures/permits/admin_permit.rb +14 -0
- data/spec/cancan-permits/fixtures/permits/editor_permit.rb +26 -0
- data/spec/cancan-permits/fixtures/permits/guest_permit.rb +25 -0
- data/spec/cancan-permits/permits/fixtures/models.rb +23 -0
- data/spec/cancan-permits/permits/owner_permits_spec.rb +33 -0
- data/spec/cancan-permits/permits/permits_spec.rb +79 -0
- data/spec/generators/permit_generator_spec.rb +35 -0
- data/spec/spec_helper.rb +36 -0
- metadata +154 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format nested --color
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
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.markdown
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# CanCan Permits
|
2
|
+
|
3
|
+
Role specific Permits for use with CanCan permission system.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
<code>gem install cancan-permits</code>
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
* Define Roles that Users can have
|
12
|
+
* Define which Roles are available
|
13
|
+
* Define a Permit for each Role.
|
14
|
+
* For each Permit, define what that Role can do
|
15
|
+
|
16
|
+
To add Roles to your app, you might consider using a *roles* gem such as *roles_generic*
|
17
|
+
|
18
|
+
### Define which Roles are available
|
19
|
+
|
20
|
+
You can override the default configuration here:
|
21
|
+
|
22
|
+
<pre>
|
23
|
+
module Permits::Roles
|
24
|
+
def self.available
|
25
|
+
# return symbols array of Roles available to users
|
26
|
+
end
|
27
|
+
end
|
28
|
+
</pre>
|
29
|
+
|
30
|
+
By default it returns User.roles if such exists, otherwise it returns [:guest, :admin] by default.
|
31
|
+
|
32
|
+
### Define a Permit for each Role.
|
33
|
+
|
34
|
+
_Note:_ You might consider using the Permits generator in order to generate your permits for you (see below)
|
35
|
+
|
36
|
+
<pre>
|
37
|
+
module RolePermit
|
38
|
+
class Admin < Base
|
39
|
+
def initialize(ability)
|
40
|
+
super
|
41
|
+
end
|
42
|
+
|
43
|
+
def permit?(user, request=nil)
|
44
|
+
super
|
45
|
+
return if !role_match? user
|
46
|
+
|
47
|
+
can :manage, :all
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
</pre>
|
52
|
+
|
53
|
+
## Special Permits
|
54
|
+
|
55
|
+
The Permits generator always generates the special permits *Any* and *System*.
|
56
|
+
|
57
|
+
### Any permit
|
58
|
+
|
59
|
+
The Any permit, can be used to set permissions that should hold true for a user in any role.
|
60
|
+
F.ex, maybe in your app, any user should be able to read comments, articles and posts:
|
61
|
+
|
62
|
+
For this to hold true, put the following permit logic in your Any permit.
|
63
|
+
<pre>
|
64
|
+
can :read, [Comment, Article, Post]
|
65
|
+
</pre>
|
66
|
+
|
67
|
+
### System permit
|
68
|
+
|
69
|
+
The System permit is run before any of the other permits. This gives you a chance to control the permission flow.
|
70
|
+
By returning a value of :break you force a break-out from the permission flow, ensuring none of the other permits are run.
|
71
|
+
|
72
|
+
Example:
|
73
|
+
The system permit can be used to allow management of all resources given the request is from localhost (which usually means "in development mode"). By default this logic is setup and ready to go.
|
74
|
+
|
75
|
+
You can be enable this simply by setting the following class instance variable:
|
76
|
+
|
77
|
+
<code>Permits::Configuration.localhost_manager = true</code>
|
78
|
+
|
79
|
+
## Permits Generator
|
80
|
+
|
81
|
+
Options
|
82
|
+
* --orm : The ORM to use (active_record, data_mapper, mongoid, mongo_mapper)
|
83
|
+
* --roles : The roles for which to generate permits ; default Guest (read all) and Admin (manage all)
|
84
|
+
|
85
|
+
Note, by default the Permits generator will attempt to discover which roles are currently defined as available to the system
|
86
|
+
and generate permits for those roles (using some conventions - TODO). Any roles specified in the --roles option are merged
|
87
|
+
with the roles found to be available in the app.
|
88
|
+
|
89
|
+
<code>$ rails g permits --orm active_record --roles guest author admin</code>
|
90
|
+
|
91
|
+
## Note on Patches/Pull Requests
|
92
|
+
|
93
|
+
* Fork the project.
|
94
|
+
* Make your feature addition or bug fix.
|
95
|
+
* Add tests for it. This is important so I don't break it in a
|
96
|
+
future version unintentionally.
|
97
|
+
* Commit, do not mess with rakefile, version, or history.
|
98
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
99
|
+
* Send me a pull request. Bonus points for topic branches.
|
100
|
+
|
101
|
+
## Copyright
|
102
|
+
|
103
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gem|
|
4
|
+
gem.name = "cancan-permits"
|
5
|
+
gem.summary = %Q{Permits for use with CanCan}
|
6
|
+
gem.description = %Q{Role specific Permits for use with CanCan permission system}
|
7
|
+
gem.email = "kmandrup@gmail.com"
|
8
|
+
gem.homepage = "http://github.com/kristianmandrup/cancan-permits"
|
9
|
+
gem.authors = ["Kristian Mandrup"]
|
10
|
+
gem.add_development_dependency "rspec", "~> 2.0.0"
|
11
|
+
gem.add_dependency 'cancan', "~> 1.3"
|
12
|
+
gem.add_dependency 'require_all', "~> 1.1"
|
13
|
+
gem.add_dependency 'sugar-high', "~> 0.1"
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Permit
|
2
|
+
class Base
|
3
|
+
attr_reader :ability
|
4
|
+
|
5
|
+
def initialize(ability)
|
6
|
+
@ability = ability
|
7
|
+
end
|
8
|
+
|
9
|
+
def permit?(user, request=nil)
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
13
|
+
def can(action, subject, conditions = nil, &block)
|
14
|
+
can_definitions << CanCan::CanDefinition.new(true, action, subject, conditions, block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def cannot(action, subject, conditions = nil, &block)
|
18
|
+
can_definitions << CanCan::CanDefinition.new(false, action, subject, conditions, block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id)
|
22
|
+
begin
|
23
|
+
user_id = user.send :"#{user_id_attribute}"
|
24
|
+
rescue
|
25
|
+
raise ArgumentError, "ERROR (owns) - The user of class #{user.class} does not respond to ##{user_id_attribute}"
|
26
|
+
end
|
27
|
+
can :manage, clazz, ownership_relation => user_id
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def localhost_manager?
|
33
|
+
Permits::Configuration.localhost_manager
|
34
|
+
end
|
35
|
+
|
36
|
+
def role_match? user
|
37
|
+
user.has_role? self.class.last_name.downcase.to_sym
|
38
|
+
end
|
39
|
+
|
40
|
+
def can_definitions
|
41
|
+
ability.send :can_definitions
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Permits
|
2
|
+
class Ability
|
3
|
+
include CanCan::Ability
|
4
|
+
|
5
|
+
# set up each RolePermit instance to share this same Ability
|
6
|
+
# so that the can and cannot operations work on the same permission collection!
|
7
|
+
def self.permits ability
|
8
|
+
special_permits << [:system, :any].map{|name| make_permit(role, ability)}
|
9
|
+
role_permits = Permits::Roles.available.inject([]) do |permits, role|
|
10
|
+
permits << make_permit role, ability
|
11
|
+
end
|
12
|
+
special_permits + role_permits
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(user, request=nil)
|
16
|
+
# put ability logic here!
|
17
|
+
user ||= Guest.new
|
18
|
+
|
19
|
+
Ability.permits(self).each do |permit|
|
20
|
+
# get role name of permit
|
21
|
+
permit_role = permit.class.demodulize.to_sym
|
22
|
+
|
23
|
+
if permit_role == :system
|
24
|
+
# always execute system permit
|
25
|
+
result = role_permit.permit?(user, request)
|
26
|
+
break if result == :break
|
27
|
+
else
|
28
|
+
# only execute the permit if the user has the role of the permit or is for any role
|
29
|
+
role_permit.permit?(user, request) if user.has_role?(permit_role) || permit_role == :any
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def self.make_permit role, ability
|
37
|
+
"Permit::#{role.to_s.camelize}".constantize.new(ability)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'sugar-high/array'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
class PermitsGenerator < Rails::Generators::Base
|
5
|
+
desc "Creates a Permit for each role in 'app/permits' and ensures that the permit folder is added to Rails load path."
|
6
|
+
|
7
|
+
class_option :roles, :type => :array, :default => [], :desc => "Roles to create permits for"
|
8
|
+
# ORM to use
|
9
|
+
class_option :orm, :type => :string, :desc => "ORM to use", :default => 'active_record'
|
10
|
+
|
11
|
+
source_root File.dirname(__FILE__) + '/templates'
|
12
|
+
|
13
|
+
def main_flow
|
14
|
+
template_permit :admin, :admin_permit
|
15
|
+
template_permit :any, :any_permit
|
16
|
+
template_permit :system, :barebones_permit
|
17
|
+
|
18
|
+
permit_logic = base_logic
|
19
|
+
roles.each do |role|
|
20
|
+
template_permit role if !role == :admin
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
attr_accessor :permit_name, :permit_logic
|
27
|
+
|
28
|
+
# TODO: merge with any registered roles in application
|
29
|
+
def roles
|
30
|
+
options[:roles].uniq.to_symbols
|
31
|
+
end
|
32
|
+
|
33
|
+
def template_permit name, template_name=nil
|
34
|
+
permit_logic = send "#{name}_logic" if [:admin, :system, :any].include?(name)
|
35
|
+
self.permit_name = name
|
36
|
+
|
37
|
+
template "permit.rb", "app/permits/#{name}_permit.rb"
|
38
|
+
end
|
39
|
+
|
40
|
+
def any_logic
|
41
|
+
""
|
42
|
+
end
|
43
|
+
|
44
|
+
def system_logic
|
45
|
+
%{
|
46
|
+
# allow to manage all and return :break to
|
47
|
+
# abort calling any other permissions
|
48
|
+
|
49
|
+
if request.host.localhost? && localhost_manager?
|
50
|
+
can(:manage, :all)
|
51
|
+
return :break
|
52
|
+
end
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def base_logic
|
57
|
+
%{
|
58
|
+
return if !role_match? user
|
59
|
+
|
60
|
+
# can :create, Comment
|
61
|
+
# owns(user, Comment)
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def admin_logic
|
66
|
+
%{
|
67
|
+
return if !role_match? user
|
68
|
+
can :manage, :all
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module AuthAssistant
|
2
|
+
class Ability
|
3
|
+
include CanCan::Ability
|
4
|
+
|
5
|
+
# set up each RolePermit instance to share this same Ability
|
6
|
+
# so that the can and cannot operations work on the same permission collection!
|
7
|
+
def self.role_permits ability
|
8
|
+
@role_permits = AuthAssistant::Roles.available.inject([]) do |permits, role|
|
9
|
+
permits << "RolePermit::#{role.to_s.camelize}".constantize.new(ability)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(user, request=nil)
|
14
|
+
# put ability logic here!
|
15
|
+
user ||= Guest.new
|
16
|
+
Ability.role_permits(self).each{|role_permit| role_permit.permit?(user, request) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RolePermit
|
2
|
+
class Editor < Base
|
3
|
+
def initialize(ability)
|
4
|
+
super
|
5
|
+
end
|
6
|
+
|
7
|
+
def permit?(user, request=nil)
|
8
|
+
super
|
9
|
+
return if !role_match? user
|
10
|
+
|
11
|
+
# uses default user_id
|
12
|
+
owns(user, Comment)
|
13
|
+
#
|
14
|
+
owns(user, Post, :writer)
|
15
|
+
#
|
16
|
+
owns(user, Article, :author, :name)
|
17
|
+
|
18
|
+
# a user can manage comments he/she created
|
19
|
+
# can :manage, Comment do |comment|
|
20
|
+
# comment.try(:user) == user
|
21
|
+
# end
|
22
|
+
|
23
|
+
# can :create, Comment
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RolePermit
|
2
|
+
class Guest < Base
|
3
|
+
def initialize(ability)
|
4
|
+
super
|
5
|
+
end
|
6
|
+
|
7
|
+
def permit?(user, request=nil)
|
8
|
+
super
|
9
|
+
return if !role_match? user
|
10
|
+
|
11
|
+
can :read, [Comment, Post]
|
12
|
+
can [:update, :destroy], [Comment]
|
13
|
+
can :create, Article
|
14
|
+
|
15
|
+
# owns(user, Comment)
|
16
|
+
|
17
|
+
# a user can manage comments he/she created
|
18
|
+
# can :manage, Comment do |comment|
|
19
|
+
# comment.try(:user) == user
|
20
|
+
# end
|
21
|
+
|
22
|
+
# can :create, Comment
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Comment
|
2
|
+
attr_accessor :user_id
|
3
|
+
|
4
|
+
def initialize user_id
|
5
|
+
self.user_id = user_id
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Post
|
10
|
+
attr_accessor :writer
|
11
|
+
|
12
|
+
def initialize user_id
|
13
|
+
self.writer = user_id
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Article
|
18
|
+
attr_accessor :author
|
19
|
+
|
20
|
+
def initialize name
|
21
|
+
self.author = name
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# can :read, [Comment, Post]
|
4
|
+
# can [:update, :destroy], [Comment]
|
5
|
+
# can :create, Article
|
6
|
+
|
7
|
+
|
8
|
+
describe AuthAssistant::Ability do
|
9
|
+
context "Editor user" do
|
10
|
+
before :each do
|
11
|
+
@editor = User.new(1, :editor, 'kristian')
|
12
|
+
@ability = AuthAssistant::Ability.new @editor
|
13
|
+
@comment = Comment.new(1)
|
14
|
+
@post = Post.new(1)
|
15
|
+
@article = Article.new('kristian')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be able to :read Comment he owns, using default :user_id relation - foreign key to User.id" do
|
19
|
+
@ability.should be_able_to(:read, Comment)
|
20
|
+
@ability.should be_able_to(:read, @comment)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be able to :read Post he owns, using :owner relation - foreign key to User.id" do
|
24
|
+
@ability.should be_able_to(:read, Post)
|
25
|
+
@ability.should be_able_to(:read, @post)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be able to :read Article he owns, using :author relation - foreign key to User.name" do
|
29
|
+
@ability.should be_able_to(:read, Article)
|
30
|
+
@ability.should be_able_to(:read, @article)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Comment
|
4
|
+
attr_accessor :owner
|
5
|
+
end
|
6
|
+
|
7
|
+
class Post
|
8
|
+
attr_accessor :writer
|
9
|
+
end
|
10
|
+
|
11
|
+
class Article
|
12
|
+
attr_accessor :author
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
describe AuthAssistant::Ability do
|
17
|
+
context "Guest user" do
|
18
|
+
before :each do
|
19
|
+
@guest = User.new(1, :guest)
|
20
|
+
@ability = AuthAssistant::Ability.new @guest
|
21
|
+
@comment = Comment.new(1)
|
22
|
+
@post = Post.new(1)
|
23
|
+
end
|
24
|
+
|
25
|
+
# can :read, [Comment, Post]
|
26
|
+
# can [:update, :destroy], [Comment]
|
27
|
+
# can :create, Article
|
28
|
+
|
29
|
+
it "should be able to :read Comment and Post but NOT Article" do
|
30
|
+
@ability.can?(:read, Comment).should be_true
|
31
|
+
@ability.can?(:read, @comment).should be_true
|
32
|
+
|
33
|
+
@ability.can?(:read, Post).should be_true
|
34
|
+
@ability.can?(:read, @post).should be_true
|
35
|
+
|
36
|
+
@ability.can?(:read, Article).should be_false
|
37
|
+
@ability.can?(:read, @article).should be_false
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be not able to :update only Comment" do
|
41
|
+
@ability.can?(:update, Comment).should be_true
|
42
|
+
@ability.can?(:update, @comment).should be_true
|
43
|
+
|
44
|
+
@ability.can?(:update, Post).should be_false
|
45
|
+
@ability.can?(:update, @post).should be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context "Admin user" do
|
51
|
+
before do
|
52
|
+
admin = User.new(2, :admin)
|
53
|
+
@ability = AuthAssistant::Ability.new admin
|
54
|
+
end
|
55
|
+
#
|
56
|
+
# # can :manage, :all
|
57
|
+
#
|
58
|
+
it "should be able to :read anything" do
|
59
|
+
@ability.can?(:read, Comment).should be_true
|
60
|
+
@ability.can?(:read, Post).should be_true
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be not able to :update everything" do
|
64
|
+
@ability.can?(:update, Comment).should be_true
|
65
|
+
@ability.can?(:update, Post).should be_true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be not able to :create everything" do
|
69
|
+
@ability.can?(:create, Comment).should be_true
|
70
|
+
@ability.can?(:create, Post).should be_true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should be not able to :update everything" do
|
74
|
+
@ability.can?(:destroy, Comment).should be_true
|
75
|
+
@ability.can?(:destroy, Post).should be_true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'generator-spec'
|
3
|
+
|
4
|
+
describe 'Permits generator' do
|
5
|
+
GeneratorSpec.with_generator do
|
6
|
+
tests PermitsGenerator
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'result of running generator with default profile' do
|
10
|
+
before :each do
|
11
|
+
GeneratorSpec.with_generator do |g, check|
|
12
|
+
g.run_generator
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should create Admin permit" do
|
17
|
+
g.should have_permit :admin
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'result of running generator with option to create permit for each registered role' do
|
22
|
+
context "Registered roles :guest, :admin"
|
23
|
+
before :each do
|
24
|
+
GeneratorSpec.with_generator do |g, check|
|
25
|
+
g.run_generator "--roles admin guest"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have created Guest and Admin permits" do
|
30
|
+
# Find at: 'app/permits/admin_permit.rb'
|
31
|
+
g.should have_permits :guest, :admin
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'cancan/matchers'
|
4
|
+
require 'cancan-permits'
|
5
|
+
|
6
|
+
require_all File.dirname(__FILE__) + 'cancan-permits/fixtures/permits'
|
7
|
+
require 'cancan-permits/fixtures/ability'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.mock_with :mocha
|
11
|
+
end
|
12
|
+
|
13
|
+
module AuthAssistant::Roles
|
14
|
+
def self.available
|
15
|
+
User.roles
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class User
|
20
|
+
attr_accessor :id, :role, :name
|
21
|
+
|
22
|
+
def self.roles
|
23
|
+
[:guest, :admin, :editor]
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize id, role, name
|
27
|
+
self.id = id
|
28
|
+
raise ArgumentError, "Role #{role} is not in list of available roles: #{self.class.roles}" if !self.class.roles.include? role
|
29
|
+
self.role = role
|
30
|
+
self.name = name
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_role? role
|
34
|
+
self.role == role
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cancan-permits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-17 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 2.0.0
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: cancan
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 3
|
46
|
+
version: "1.3"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: require_all
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 1
|
59
|
+
- 1
|
60
|
+
version: "1.1"
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: sugar-high
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
- 1
|
74
|
+
version: "0.1"
|
75
|
+
type: :runtime
|
76
|
+
version_requirements: *id004
|
77
|
+
description: Role specific Permits for use with CanCan permission system
|
78
|
+
email: kmandrup@gmail.com
|
79
|
+
executables: []
|
80
|
+
|
81
|
+
extensions: []
|
82
|
+
|
83
|
+
extra_rdoc_files:
|
84
|
+
- LICENSE
|
85
|
+
- README.markdown
|
86
|
+
files:
|
87
|
+
- .document
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- LICENSE
|
91
|
+
- README.markdown
|
92
|
+
- Rakefile
|
93
|
+
- VERSION
|
94
|
+
- lib/cancan-permits.rb
|
95
|
+
- lib/cancan-permits/namespaces.rb
|
96
|
+
- lib/cancan-permits/permit/base_permit.rb
|
97
|
+
- lib/cancan-permits/permits/abiity.rb
|
98
|
+
- lib/cancan-permits/permits/configuration.rb
|
99
|
+
- lib/cancan-permits/permits/roles.rb
|
100
|
+
- lib/cancan-permits/rspec/config.rb
|
101
|
+
- lib/cancan-permits/rspec/matchers/have_permits.rb
|
102
|
+
- lib/generators/permits/permits_generator.rb
|
103
|
+
- lib/generators/permits/templates/permit.rb
|
104
|
+
- spec/cancan-permits/fixtures/ability.rb
|
105
|
+
- spec/cancan-permits/fixtures/permits/admin_permit.rb
|
106
|
+
- spec/cancan-permits/fixtures/permits/editor_permit.rb
|
107
|
+
- spec/cancan-permits/fixtures/permits/guest_permit.rb
|
108
|
+
- spec/cancan-permits/permits/fixtures/models.rb
|
109
|
+
- spec/cancan-permits/permits/owner_permits_spec.rb
|
110
|
+
- spec/cancan-permits/permits/permits_spec.rb
|
111
|
+
- spec/generators/permit_generator_spec.rb
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: http://github.com/kristianmandrup/cancan-permits
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options:
|
119
|
+
- --charset=UTF-8
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
version: "0"
|
138
|
+
requirements: []
|
139
|
+
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.3.7
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: Permits for use with CanCan
|
145
|
+
test_files:
|
146
|
+
- spec/cancan-permits/fixtures/ability.rb
|
147
|
+
- spec/cancan-permits/fixtures/permits/admin_permit.rb
|
148
|
+
- spec/cancan-permits/fixtures/permits/editor_permit.rb
|
149
|
+
- spec/cancan-permits/fixtures/permits/guest_permit.rb
|
150
|
+
- spec/cancan-permits/permits/fixtures/models.rb
|
151
|
+
- spec/cancan-permits/permits/owner_permits_spec.rb
|
152
|
+
- spec/cancan-permits/permits/permits_spec.rb
|
153
|
+
- spec/generators/permit_generator_spec.rb
|
154
|
+
- spec/spec_helper.rb
|