can_self_do_it 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in can_do_it.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,203 @@
1
+ = can_self_do_it
2
+
3
+ * http://github.com/mbuceta/can_self_o_it
4
+
5
+ == Description:
6
+
7
+ Generate a simple interface to works with users CRUD rights.
8
+
9
+
10
+ == Features:
11
+
12
+ * Allows check permissions for CRUD actions can_create?, can_see?, can_edit? and can_delete?
13
+ * Allows check permissions for general actions responding to can_.+? regular expresion
14
+ * Add an interface for known users with a default implementation of the CRUD action permissions.
15
+ * Add an interface for unknown users with a default implementation of the CRUD action permissions.
16
+ * Allows add custom behaviors for especific objects instead of the default implementation
17
+
18
+ == Synopsis:
19
+
20
+ Despite the fact that this gems hasn't any dependency with others gems, it arises from the double
21
+ permission checking in rails. We have to check action rights in controllers, and in the other hand
22
+ we have to check rights to show actions in views.
23
+
24
+ This gem allows you to add the permission logic in one place and use it in both, views and controllers.
25
+ Also, this gem add default behaviors for common cases. And overwrite the default behavior using custom
26
+ implementations.
27
+
28
+ == Basic usage in a Rails app:
29
+
30
+ === Model:
31
+
32
+ class Post
33
+ ...
34
+ end
35
+
36
+ # Represent an identified user of the application
37
+ class User
38
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Known)
39
+ ...
40
+ end
41
+
42
+ === View:
43
+
44
+ link_to('edit', @post) if session_user.user.can_edit?(@post)
45
+ link_to('delete', @post) if session_user.user.can_delete?(@post)
46
+
47
+ === Controller:
48
+
49
+ before_filter :check_post_edition_rights, :only => :edit
50
+ ....
51
+ private
52
+
53
+ def check_post_edition_rights
54
+ render(:status => :unauthorized) unless session_user.can_edit?(@post)
55
+ end
56
+
57
+
58
+ == Complete usage example:
59
+
60
+ === Application permissions management
61
+
62
+ Module for custom permissions for Guest (unknown user)
63
+
64
+ module GuestCustomPermissions
65
+ # Ovewrite default CanSelfDoIt::Unknown implementation.
66
+ # Guest only can see admin comments.
67
+ # This method overrides
68
+ # can_see? method for objects of Comment class.
69
+ # i.e. this method is called when can_see?(comment) is
70
+ # called and comment is a Comment
71
+ def can_see_comment?(comment); comment.user.admin?; end
72
+ end
73
+
74
+ Module for custom permissions for User (known user)
75
+
76
+ module UserCustomPermissions
77
+ # CanSelfDoIt::Known check this method for default implementation.
78
+ def admin?; false;end
79
+
80
+ # Ovewrite default CanSelfDoIt::Known implementation
81
+ # Users can comment any post
82
+ # This method overrides
83
+ # can_create? method for objects of Comment class.
84
+ # i.e. this method is called when can_create?(Comment, post)
85
+ # The post param is the post in which the comment will be written
86
+ def can_create_comment?(post); true; end
87
+ end
88
+
89
+ Module for custom permissions for Admin (admin like user)
90
+
91
+ module AdminCustomPermissions
92
+ # CanSelfDoIt::Known check this method for default implementation.
93
+ def admin?; true;end
94
+ end
95
+
96
+ === Application classes
97
+
98
+ # Represent an unidentified user of the application
99
+ class Guest
100
+ acts_as_can_self_do_it(:as => [CanSelfDoIt::Unknown, GuestCustomPermissions])
101
+ ...
102
+ end
103
+
104
+ # Represent an identified user of the application
105
+ class User
106
+ acts_as_can_self_do_it(:as => [CanSelfDoIt::Known, UserCustomPermissions])
107
+ attr_accessor :blogs
108
+ ...
109
+ end
110
+
111
+ # Represent the application admin
112
+ class Admin
113
+ acts_as_can_self_do_it(:as => [CanSelfDoIt::Known, AdminCustomPermissions])
114
+ attr_accessor :blogs
115
+ ...
116
+ end
117
+
118
+ # A simple Blog + Post + Comment app
119
+ class Blog
120
+ attr_accessor :user, :posts
121
+ ...
122
+ end
123
+
124
+ class Post
125
+ attr_accessor :blog, :comments, :user
126
+ ...
127
+ end
128
+
129
+ class Comment
130
+ attr_accessor :post, :user
131
+ ...
132
+ end
133
+
134
+ === CanSelfDoIt working
135
+
136
+
137
+ Working for an instance of Admin
138
+
139
+ an_admin.can_see?(admin_blog).should_be true
140
+ an_admin.can_see?(other_user_blog).should_be true
141
+ an_admin.can_edit?(admin_blog).should_be true
142
+ an_admin.can_edit?(other_user_blog).should_be true
143
+ an_admin.can_create?(Post, admin_blog).should_be true
144
+ an_admin.can_create?(Post, other_user_blog).should_be true
145
+
146
+ Working for an instance of User
147
+
148
+ an_user.can_see?(user_blog).should_be true
149
+ an_user.can_see?(other_user_blog).should_be true
150
+ an_user.can_edit?(user_blog).should_be true
151
+ an_user.can_edit?(other_user_blog).should_be false
152
+ an_user.can_create?(Post, user_blog).should_be true
153
+ an_user.can_create?(Post, other_user_blog).should_be false
154
+ # Custom
155
+ an_user.can_create?(Comment, user_post).should_be true
156
+ an_user.can_create?(Comment, other_user_post).should_be true
157
+
158
+ Working for an instance of Guest
159
+ a_guest.can_see?(user_blog).should_be true
160
+ a_guest.can_see?(user_post).should_be true
161
+ a_guest.can_edit?(user_blog).should_be false
162
+ a_guest.can_edit?(user_post).should_be false
163
+ a_guest.can_create?(Post, user_blog).should_be false
164
+ a_guest.can_create?(Comment, user_post).should_be false
165
+ # Custom
166
+ a_guest.can_see?(user_comment).should_be false
167
+ a_guest.can_see?(admin_comment).should_be true
168
+
169
+
170
+
171
+ == Requirements:
172
+
173
+ This gems hasn't dependencies
174
+
175
+
176
+ == Install:
177
+
178
+ sudo gem install can_self_do_it
179
+
180
+ == License:
181
+
182
+ (The MIT License)
183
+
184
+ Copyright (c) 2013 Juan Martin Buceta
185
+
186
+ Permission is hereby granted, free of charge, to any person obtaining
187
+ a copy of this software and associated documentation files (the
188
+ 'Software'), to deal in the Software without restriction, including
189
+ without limitation the rights to use, copy, modify, merge, publish,
190
+ distribute, sublicense, and/or sell copies of the Software, and to
191
+ permit persons to whom the Software is furnished to do so, subject to
192
+ the following conditions:
193
+
194
+ The above copyright notice and this permission notice shall be
195
+ included in all copies or substantial portions of the Software.
196
+
197
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
198
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
199
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
200
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
201
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
202
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
203
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ desc "Open an irb session preloaded with this library"
7
+ task :console do
8
+ sh "irb -rubygems -I lib -r can_do_it.rb"
9
+ end
10
+
11
+ task :default => :spec
12
+ task :test => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "can_self_do_it/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "can_self_do_it"
7
+ s.version = CanSelfDoIt::VERSION
8
+ s.authors = ["Juan Martín Buceta"]
9
+ s.email = ["mbuceta@grantaire.com.ar"]
10
+ s.homepage = "http://martinbuceta.com.ar"
11
+ s.summary = %q{Simple can_*? like method to manage permissions}
12
+ s.description = %q{Provide modules to work with sereveral permission policies}
13
+
14
+ s.rubyforge_project = "can_self_do_it"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "rspec"
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
@@ -0,0 +1,39 @@
1
+ require 'can_self_do_it/helper'
2
+ module CanSelfDoIt
3
+ module Auto
4
+
5
+ # Automatic method generation.
6
+ # Everything defined as /can_.+\?/ that does not exists get name from param type
7
+ # i.e methods like can_do_something?(project) => can_do_something_project
8
+ def method_missing(symbol, *args, &block)
9
+ if symbol.to_s == 'can_modify_default?'
10
+ raise NotImplementedError.new("You must implement can_modify_default?.")
11
+ elsif can_self_do_it_method?(symbol)
12
+ raise ArgumentError, "wrong number of arguments(#{args.size}1 for 1)" unless args.size == 1
13
+ obj = args[0]
14
+ new_symbol = "#{symbol.to_s[0..-2]}_#{CanSelfDoIt::Helper.class_2_method_sub_str(obj.class)}?"
15
+ respond_to_without_can_self_do_it_method?(new_symbol) ? send(new_symbol, obj) : can_modify_default?(obj)
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+
22
+ def respond_to_with_can_self_do_it_method?(*args)
23
+ can_self_do_it_method?(*args) || respond_to_without_can_self_do_it_method?(*args)
24
+ end
25
+
26
+ alias_method :respond_to?, :respond_to_with_can_self_do_it_method?
27
+
28
+ protected
29
+
30
+ def can_modify_default?(obj); raise NotImplementedError.new("You must implement can_modify_default?.") ; end
31
+
32
+ private
33
+
34
+ def can_self_do_it_method?(symbol)
35
+ !!(symbol.to_s =~ /^can_.+\?/ )
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'can_self_do_it/helper'
2
+ module CanSelfDoIt
3
+ module Base
4
+
5
+ def can_see?(obj)
6
+ method = "can_see_#{CanSelfDoIt::Helper.class_2_method_sub_str(obj.class)}?"
7
+ respond_to_without_can_self_do_it_method?(method) ? self.send(method,obj) : can_see_default?(obj)
8
+ end
9
+
10
+ def can_edit?(obj)
11
+ method = "can_edit_#{CanSelfDoIt::Helper.class_2_method_sub_str(obj.class)}?"
12
+ respond_to_without_can_self_do_it_method?(method) ? send(method,obj) : can_edit_default?(obj)
13
+ end
14
+
15
+ # parent: parent of the object created
16
+ # Examples:
17
+ # - session_user.can_create?(Proposal, project)
18
+ # - session_user.can_create?(Project)
19
+ def can_create?(obj_class, parent = self)
20
+ method = "can_create_#{CanSelfDoIt::Helper.class_2_method_sub_str(obj_class)}?"
21
+ respond_to_without_can_self_do_it_method?(method) ? send(method,parent) : can_create_default?(parent)
22
+ end
23
+
24
+ def can_delete?(obj)
25
+ method = "can_delete_#{CanSelfDoIt::Helper.class_2_method_sub_str(obj.class)}?"
26
+ respond_to_without_can_self_do_it_method?(method) ? send(method,obj) : can_delete_default?(obj)
27
+ end
28
+
29
+ alias_method :respond_to_without_can_self_do_it_method?, :respond_to?
30
+
31
+ protected
32
+
33
+ def can_see_default?(obj); raise NotImplementedError.new("You must implement can_see_default?.") ; end
34
+ def can_edit_default?(obj); raise NotImplementedError.new("You must implement can_edit_default?.") ; end
35
+ def can_create_default?(parent); raise NotImplementedError.new("You must implement can_create_default?.") ; end
36
+ def can_delete_default?(obj); raise NotImplementedError.new("You must implement can_delete_default?.") ; end
37
+
38
+
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ module CanSelfDoIt
2
+ module Helper
3
+
4
+ def self.class_2_method_sub_str(a_class)
5
+ str = a_class.to_s
6
+ str.respond_to?(:underscore) ? str.underscore.sub('/','__') : self.underscore(str).sub('/','__')
7
+ end
8
+
9
+ def self.underscore(camel_cased_word)
10
+ word = camel_cased_word.to_s.dup
11
+ word.gsub!(/::/, '/')
12
+ word.gsub!(/(?:([A-Za-z\d])|^)(#{/(?=a)b/})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
13
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
14
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
15
+ word.tr!("-", "_")
16
+ word.downcase!
17
+ word
18
+ end
19
+
20
+ def self.camelize(string)
21
+ string.sub(/^[a-z\d]*/){$&.capitalize}.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}"}.gsub('/', '::')
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+
@@ -0,0 +1,44 @@
1
+ module CanSelfDoIt
2
+ module Known
3
+
4
+ protected
5
+
6
+ def can_see_default?(obj); true; end
7
+
8
+ # Check for can_modify_default
9
+ def can_edit_default?(obj); can_modify_default?(obj); end
10
+
11
+ # Check the parent owner of the object to create
12
+ def can_create_default?(parent); can_modify_default?(parent); end
13
+
14
+ # Check for can_modify_default
15
+ def can_delete_default?(obj); can_modify_default?(obj); end
16
+
17
+ # Check if self is the administration or the obj owner
18
+ def can_modify_default?(obj); administrator_or_object_owner?(obj); end
19
+
20
+
21
+ def owner_method_names
22
+ [:owner, :author, :user]
23
+ end
24
+
25
+ def admin_method_name
26
+ :admin?
27
+ end
28
+
29
+
30
+ private
31
+
32
+ def administrator_or_object_owner?(obj)
33
+ !! (
34
+ (self.admin_method_name && self.respond_to?(admin_method_name) && self.send(admin_method_name)) || #Administrator
35
+ self == owner_for(obj))
36
+ end
37
+
38
+ #we are finding object owner
39
+ def owner_for(obj)
40
+ (obj.is_a?(self.class) && obj) || (owner_method = owner_method_names.detect{|n| obj.respond_to?(n)}) && obj.send(owner_method)
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,18 @@
1
+ module CanSelfDoIt
2
+ module Unknown
3
+
4
+ protected
5
+
6
+ def can_see_default?(obj); true; end
7
+
8
+ def can_edit_default?(obj); can_modify_default?(obj); end
9
+
10
+ def can_create_default?(parent); can_modify_default?(parent); end
11
+
12
+ def can_delete_default?(obj); can_modify_default?(obj); end
13
+
14
+ def can_modify_default?(obj); false; end
15
+
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module CanSelfDoIt
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require "can_self_do_it/version"
5
+ require "can_self_do_it/base"
6
+ require "can_self_do_it/auto"
7
+ require "can_self_do_it/known"
8
+ require "can_self_do_it/unknown"
9
+
10
+ module CanSelfDoIt
11
+ # Your code goes here...
12
+ end
13
+
14
+ Module.class_eval do
15
+ def acts_as_can_self_do_it(options={})
16
+ self.send(:include, CanSelfDoIt::Base)
17
+ self.send(:include, CanSelfDoIt::Auto) if options[:auto]
18
+ as = options[:as] || []
19
+ as = [as] unless as.respond_to?(:each)
20
+ as.each{|m| self.send(:include, m)}
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Admin do
4
+
5
+ before(:all) do
6
+ # Admin Blogs and Posts
7
+ class AdminActsAsCanSelfDoIt < Admin
8
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Known, :auto => false)
9
+ end
10
+ @admin = AdminActsAsCanSelfDoIt.new
11
+ @blog_admin = Blog.new(@admin)
12
+ @post_admin = Post.new(@blog_admin)
13
+ # User Blogs and Posts
14
+ @user = User.new
15
+ @blog_user = Blog.new(@user)
16
+ @post_user = Post.new(@blog_user)
17
+ end
18
+
19
+ it "can see any Blog" do
20
+ @admin.can_see?(@blog_admin).should == true
21
+ @admin.can_see?(@blog_user).should == true
22
+ end
23
+
24
+ it "can see any Post" do
25
+ @admin.can_see?(@post_admin).should == true
26
+ @admin.can_see?(@post_user).should == true
27
+ end
28
+
29
+
30
+ it "can edit any Blog" do
31
+ @admin.can_edit?(@blog_admin).should == true
32
+ @admin.can_edit?(@blog_user).should == true
33
+ end
34
+
35
+ it "can edit any Post" do
36
+ @admin.can_edit?(@post_admin).should == true
37
+ @admin.can_edit?(@post_user).should == true
38
+ end
39
+
40
+ it "can comment any Post" do
41
+ @admin.can_create?(Comment, @post_admin).should == true
42
+ @admin.can_create?(Comment, @post_user).should == true
43
+ end
44
+
45
+ it "can delete any Blog" do
46
+ @admin.can_delete?(@blog_admin).should == true
47
+ @admin.can_delete?(@blog_user).should == true
48
+ end
49
+
50
+ it "can delete any Post" do
51
+ @admin.can_delete?(@post_admin).should == true
52
+ @admin.can_delete?(@post_user).should == true
53
+ end
54
+
55
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Admin do
4
+
5
+ before(:all) do
6
+ # Admin Blogs and Posts
7
+ class AutoAdminActsAsCanSelfDoIt < Admin
8
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Known, :auto => true)
9
+ end
10
+
11
+ @admin = AutoAdminActsAsCanSelfDoIt.new
12
+ @blog_admin = Blog.new(@admin)
13
+ @post_admin = Post.new(@blog_admin)
14
+ # User Blogs and Posts
15
+ @user = User.new
16
+ @blog_user = Blog.new(@user)
17
+ @post_user = Post.new(@blog_user)
18
+ end
19
+
20
+ it "can see any Blog" do
21
+ @admin.can_see?(@blog_admin).should == true
22
+ @admin.can_see?(@blog_user).should == true
23
+ end
24
+
25
+ it "can see any Post" do
26
+ @admin.can_see?(@post_admin).should == true
27
+ @admin.can_see?(@post_user).should == true
28
+ end
29
+
30
+
31
+ it "can edit any Blog" do
32
+ @admin.can_edit?(@blog_admin).should == true
33
+ @admin.can_edit?(@blog_user).should == true
34
+ end
35
+
36
+ it "can edit any Post" do
37
+ @admin.can_edit?(@post_admin).should == true
38
+ @admin.can_edit?(@post_user).should == true
39
+ end
40
+
41
+ it "can comment any Post" do
42
+ @admin.can_create?(Comment, @post_admin).should == true
43
+ @admin.can_create?(Comment, @post_user).should == true
44
+ end
45
+
46
+ it "can delete any Blog" do
47
+ @admin.can_delete?(@blog_admin).should == true
48
+ @admin.can_delete?(@blog_user).should == true
49
+ end
50
+
51
+ it "can delete any Post" do
52
+ @admin.can_delete?(@post_admin).should == true
53
+ @admin.can_delete?(@post_user).should == true
54
+ end
55
+
56
+ it "can share any Blog" do
57
+ @admin.can_share?(@blog_admin).should == true
58
+ @admin.can_share?(@blog_user).should == true
59
+ end
60
+
61
+ it "can join any Post" do
62
+ @admin.can_share?(@post_admin).should == true
63
+ @admin.can_share?(@post_user).should == true
64
+ end
65
+
66
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Guest do
4
+
5
+ before(:all) do
6
+
7
+ class GuestActsAsCanSelfDoIt < Guest
8
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Unknown, :auto => true)
9
+ end
10
+
11
+ @guest = GuestActsAsCanSelfDoIt.instance
12
+
13
+ # Admin Blogs and Posts
14
+ @admin = Admin.new
15
+ @blog_admin = Blog.new(@admin)
16
+ @post_admin = Post.new(@blog_admin)
17
+ # User Blogs and Posts
18
+ @user = User.new
19
+ @blog_user = Blog.new(@user)
20
+ @post_user = Post.new(@blog_user)
21
+
22
+ @comment_user = Comment.new(@post_admin, @user)
23
+ @comment_admin = Comment.new(@post_user, @admin)
24
+ end
25
+
26
+ it "can see any Blog" do
27
+ @guest.can_see?(@blog_admin).should == true
28
+ @guest.can_see?(@blog_user).should == true
29
+ end
30
+
31
+ it "can see any Post" do
32
+ @guest.can_see?(@post_admin).should == true
33
+ @guest.can_see?(@post_user).should == true
34
+ end
35
+
36
+ it "can't edit Blogs" do
37
+ @guest.can_edit?(@blog_admin).should == false
38
+ @guest.can_edit?(@blog_user).should == false
39
+ end
40
+
41
+ it "can't edit Posts" do
42
+ @guest.can_edit?(@post_admin).should == false
43
+ @guest.can_edit?(@post_user).should == false
44
+ end
45
+
46
+ it "can't delete Blogs" do
47
+ @guest.can_delete?(@blog_admin).should == false
48
+ @guest.can_delete?(@blog_user).should == false
49
+ end
50
+
51
+ it "can't delete Posts" do
52
+ @guest.can_delete?(@post_admin).should == false
53
+ @guest.can_delete?(@post_user).should == false
54
+ end
55
+
56
+ it "can't see user comments" do
57
+ @guest.can_see?(@comment_user).should == false
58
+ end
59
+
60
+ it "can see admin comments" do
61
+ @guest.can_see?(@comment_admin).should == true
62
+ end
63
+
64
+ it "can't share Posts" do
65
+ @guest.can_share?(@post_admin).should == false
66
+ @guest.can_share?(@post_user).should == false
67
+ end
68
+
69
+ it "can't join to Blogs" do
70
+ @guest.can_join?(@blog_admin).should == false
71
+ @guest.can_join?(@blog_user).should == false
72
+ end
73
+
74
+ end
data/spec/auto_spec.rb ADDED
@@ -0,0 +1,57 @@
1
+ describe CanSelfDoIt::Auto do
2
+
3
+ before(:all) do
4
+ class CanSelfDoItImpWithAuto; acts_as_can_self_do_it(:auto => true); end
5
+ end
6
+
7
+ it "must respond to can_see?" do
8
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
9
+ can_self_do_it_impl.respond_to?(:can_see?).should == true
10
+ end
11
+
12
+ it "must raise NotImplementedError when call can_see?" do
13
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
14
+ expect{can_self_do_it_impl.can_see?(nil)}.to raise_error(NotImplementedError)
15
+ end
16
+
17
+ it "must respond to can_view?" do
18
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
19
+ can_self_do_it_impl.respond_to?(:can_view?).should == true
20
+ end
21
+
22
+ it "must raise NotImplementedError when call can_view?" do
23
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
24
+ expect{can_self_do_it_impl.can_view?(nil)}.to raise_error(NotImplementedError)
25
+ end
26
+
27
+ it "must respond to can_eliminate?" do
28
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
29
+ can_self_do_it_impl.respond_to?(:can_eliminate?).should == true
30
+ end
31
+
32
+ it "must raise NotImplementedError when call can_eliminate?" do
33
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
34
+ expect{can_self_do_it_impl.can_eliminate?(nil)}.to raise_error(NotImplementedError)
35
+ end
36
+
37
+ it "mustn't respond to eliminate?" do
38
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
39
+ can_self_do_it_impl.respond_to?(:eliminate?).should == false
40
+ end
41
+
42
+ it "must raise NoMethodError when call eliminate?" do
43
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
44
+ expect{can_self_do_it_impl.eliminate?(nil)}.to raise_error(NoMethodError)
45
+ end
46
+
47
+ it "mustn't respond to can_eliminate" do
48
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
49
+ can_self_do_it_impl.respond_to?(:can_eliminate).should == false
50
+ end
51
+
52
+ it "must raise NoMethodError when call can_eliminate" do
53
+ can_self_do_it_impl = CanSelfDoItImpWithAuto.new
54
+ expect{can_self_do_it_impl.can_eliminate(nil)}.to raise_error(NoMethodError)
55
+ end
56
+
57
+ end
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe User do
4
+
5
+ before(:all) do
6
+ # User
7
+ class AutoUserActsAsCanSelfDoIt < User
8
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Known, :auto => true)
9
+ end
10
+
11
+ @user = AutoUserActsAsCanSelfDoIt.new
12
+
13
+ # User Blogs and Posts
14
+ @blog_user = Blog.new(@user)
15
+ @post_user = Post.new(@blog_user)
16
+
17
+ # Other User Blogs and Posts
18
+ @blog_other_user = Blog.new(User.new)
19
+ @post_other_user = Post.new(@blog_other_user)
20
+
21
+ # Admin Blogs and Posts
22
+ @blog_admin = Blog.new(Admin.new)
23
+ @post_admin = Post.new(@blog_admin)
24
+ end
25
+
26
+ it "can see any Blog" do
27
+ @user.can_see?(@blog_admin).should == true
28
+ @user.can_see?(@blog_user).should == true
29
+ @user.can_see?(@blog_other_user).should == true
30
+ end
31
+
32
+ it "can see any Post" do
33
+ @user.can_see?(@post_admin).should == true
34
+ @user.can_see?(@post_user).should == true
35
+ @user.can_see?(@post_other_user).should == true
36
+ end
37
+
38
+
39
+ it "can edit his Blogs" do
40
+ @user.can_edit?(@blog_user).should == true
41
+ end
42
+
43
+ it "can edit his Posts" do
44
+ @user.can_edit?(@post_user).should == true
45
+ end
46
+
47
+
48
+ it "can't edit other's Blogs" do
49
+ @user.can_edit?(@blog_admin).should == false
50
+ @user.can_edit?(@blog_other_user).should == false
51
+ end
52
+
53
+ it "can't edit other's Posts" do
54
+ @user.can_edit?(@post_admin).should == false
55
+ @user.can_edit?(@post_other_user).should == false
56
+ end
57
+
58
+ it "can comment his own posts" do
59
+ @user.can_create?(Comment, @user_post).should == true
60
+ end
61
+
62
+ it "can comment other user posts" do
63
+ @user.can_create?(Comment, @other_user_post).should == true
64
+ end
65
+
66
+ it "can comment admin posts" do
67
+ @user.can_create?(Comment, @other_user_post).should == true
68
+ end
69
+
70
+ it "can delete his Blogs" do
71
+ @user.can_delete?(@blog_user).should == true
72
+ end
73
+
74
+ it "can delete his Posts" do
75
+ @user.can_delete?(@post_user).should == true
76
+ end
77
+
78
+ it "can't delete other's Blogs" do
79
+ @user.can_delete?(@blog_admin).should == false
80
+ @user.can_delete?(@blog_other_user).should == false
81
+ end
82
+
83
+ it "can't delete other's Posts" do
84
+ @user.can_delete?(@post_admin).should == false
85
+ @user.can_delete?(@post_other_user).should == false
86
+ end
87
+
88
+ it "can share his Posts" do
89
+ @user.can_share?(@post_user).should == true
90
+ end
91
+
92
+ it "can't share other's Blogs" do
93
+ @user.can_share?(@blog_admin).should == false
94
+ @user.can_share?(@blog_other_user).should == false
95
+ end
96
+
97
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,67 @@
1
+ describe CanSelfDoIt::Base do
2
+
3
+ before(:all) do
4
+ class CanSelfDoItImp; acts_as_can_self_do_it; end
5
+ end
6
+
7
+ it "must respond to can_see?" do
8
+ can_self_do_it_impl = CanSelfDoItImp.new
9
+ can_self_do_it_impl.respond_to?(:can_see?).should == true
10
+ end
11
+
12
+ it "must respond to can_edit?" do
13
+ can_self_do_it_impl = CanSelfDoItImp.new
14
+ can_self_do_it_impl.respond_to?(:can_edit?).should == true
15
+ end
16
+
17
+ it "must respond to can_delete?" do
18
+ can_self_do_it_impl = CanSelfDoItImp.new
19
+ can_self_do_it_impl.respond_to?(:can_delete?).should == true
20
+ end
21
+
22
+ it "must respond to can_create?" do
23
+ can_self_do_it_impl = CanSelfDoItImp.new
24
+ can_self_do_it_impl.respond_to?(:can_create?).should == true
25
+ end
26
+
27
+ it "must raise NotImplementedError when call can_see?" do
28
+ can_self_do_it_impl = CanSelfDoItImp.new
29
+ expect{can_self_do_it_impl.can_see?(nil)}.to raise_error(NotImplementedError)
30
+ end
31
+
32
+ it "must raise NotImplementedError when call can_edit?" do
33
+ can_self_do_it_impl = CanSelfDoItImp.new
34
+ expect{can_self_do_it_impl.can_edit?(nil)}.to raise_error(NotImplementedError)
35
+ end
36
+
37
+ it "must raise NotImplementedError when call can_delete?" do
38
+ can_self_do_it_impl = CanSelfDoItImp.new
39
+ expect{can_self_do_it_impl.can_delete?(nil)}.to raise_error(NotImplementedError)
40
+ end
41
+
42
+ it "must raise NotImplementedError when call can_create?" do
43
+ can_self_do_it_impl = CanSelfDoItImp.new
44
+ expect{can_self_do_it_impl.can_create?(nil,nil)}.to raise_error(NotImplementedError)
45
+ end
46
+
47
+ it "mustn't respond to can_view?" do
48
+ can_self_do_it_impl = CanSelfDoItImp.new
49
+ can_self_do_it_impl.respond_to?(:can_view?).should == false
50
+ end
51
+
52
+ it "must raise NoMethodError when call can_view?" do
53
+ can_self_do_it_impl = CanSelfDoItImp.new
54
+ expect{can_self_do_it_impl.can_view?(nil)}.to raise_error(NoMethodError)
55
+ end
56
+
57
+ it "mustn't respond to can_eliminate?" do
58
+ can_self_do_it_impl = CanSelfDoItImp.new
59
+ can_self_do_it_impl.respond_to?(:can_eliminate?).should == false
60
+ end
61
+
62
+ it "must raise NoMethodError when call can_eliminate?" do
63
+ can_self_do_it_impl = CanSelfDoItImp.new
64
+ expect{can_self_do_it_impl.can_eliminate?(nil)}.to raise_error(NoMethodError)
65
+ end
66
+
67
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Guest do
4
+
5
+ before(:all) do
6
+
7
+ class GuestActsAsCanSelfDoIt < Guest
8
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Unknown, :auto => false)
9
+ end
10
+
11
+ @guest = GuestActsAsCanSelfDoIt.instance
12
+
13
+ # Admin Blogs and Posts
14
+ @admin = Admin.new
15
+ @blog_admin = Blog.new(@admin)
16
+ @post_admin = Post.new(@blog_admin)
17
+ # User Blogs and Posts
18
+ @user = User.new
19
+ @blog_user = Blog.new(@user)
20
+ @post_user = Post.new(@blog_user)
21
+
22
+ @comment_user = Comment.new(@post_admin, @user)
23
+ @comment_admin = Comment.new(@post_user, @admin)
24
+ end
25
+
26
+ it "can see any Blog" do
27
+ @guest.can_see?(@blog_admin).should == true
28
+ @guest.can_see?(@blog_user).should == true
29
+ end
30
+
31
+ it "can see any Post" do
32
+ @guest.can_see?(@post_admin).should == true
33
+ @guest.can_see?(@post_user).should == true
34
+ end
35
+
36
+ it "can't edit Blogs" do
37
+ @guest.can_edit?(@blog_admin).should == false
38
+ @guest.can_edit?(@blog_user).should == false
39
+ end
40
+
41
+ it "can't edit Posts" do
42
+ @guest.can_edit?(@post_admin).should == false
43
+ @guest.can_edit?(@post_user).should == false
44
+ end
45
+
46
+ it "can't delete Blogs" do
47
+ @guest.can_delete?(@blog_admin).should == false
48
+ @guest.can_delete?(@blog_user).should == false
49
+ end
50
+
51
+ it "can't delete Posts" do
52
+ @guest.can_delete?(@post_admin).should == false
53
+ @guest.can_delete?(@post_user).should == false
54
+ end
55
+
56
+ it "can't see user comments" do
57
+ @guest.can_see?(@comment_user).should == false
58
+ end
59
+
60
+ it "can see admin comments" do
61
+ @guest.can_see?(@comment_admin).should == true
62
+ end
63
+
64
+
65
+
66
+ end
@@ -0,0 +1,31 @@
1
+ describe CanSelfDoIt::Known do
2
+
3
+ it "must implement can_see_default?" do
4
+ CanSelfDoIt::Known.protected_method_defined?(:can_see_default?).should == true
5
+ end
6
+
7
+ it "must implement can_edit_default?" do
8
+ CanSelfDoIt::Known.protected_method_defined?(:can_edit_default?).should == true
9
+ end
10
+
11
+ it "must implement can_create_default?" do
12
+ CanSelfDoIt::Known.protected_method_defined?(:can_create_default?).should == true
13
+ end
14
+
15
+ it "must implement can_delete_default?" do
16
+ CanSelfDoIt::Known.protected_method_defined?(:can_delete_default?).should == true
17
+ end
18
+
19
+ it "must implement can_modify_default?" do
20
+ CanSelfDoIt::Known.protected_method_defined?(:can_modify_default?).should == true
21
+ end
22
+
23
+ it "must implement owner_method_names" do
24
+ CanSelfDoIt::Known.protected_method_defined?(:owner_method_names).should == true
25
+ end
26
+
27
+ it "must implement admin_method_name" do
28
+ CanSelfDoIt::Known.protected_method_defined?(:admin_method_name).should == true
29
+ end
30
+
31
+ end
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require 'singleton'
3
+ require 'rspec'
4
+ require 'can_self_do_it'
5
+ $LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__))
6
+
7
+ RSpec.configure do |config|
8
+ config.before :all do
9
+ end
10
+ end
11
+
12
+ class Guest
13
+ include Singleton
14
+ # Custom
15
+ # Guest only can see admin comments
16
+ def can_see_comment?(comment); comment.user.admin?; end
17
+ end
18
+
19
+ class User
20
+ attr_accessor :blogs
21
+ def admin?; false;end
22
+ def initialize
23
+ @blogs = []
24
+ self
25
+ end
26
+
27
+ # Custom
28
+ # Users can comment any post
29
+ def can_create_comment?(post); true; end
30
+
31
+ end
32
+
33
+ class Admin
34
+ attr_accessor :blogs
35
+ def admin?; true;end
36
+ def initialize
37
+ @blogs = []
38
+ self
39
+ end
40
+ end
41
+
42
+
43
+ class Blog
44
+ attr_accessor :user, :posts
45
+ def initialize(user)
46
+ @user = user
47
+ @posts = []
48
+ @user.blogs << self
49
+ self
50
+ end
51
+ end
52
+
53
+ class Post
54
+ attr_accessor :blog, :comments
55
+
56
+ def owner
57
+ @blog.user
58
+ end
59
+
60
+ def initialize(blog)
61
+ @comments = []
62
+ @blog = blog
63
+ @blog.posts << self
64
+ self
65
+ end
66
+ end
67
+
68
+ class Comment
69
+ attr_accessor :post, :user
70
+
71
+ def initialize(post, user)
72
+ @post = post
73
+ @user = user
74
+ @post.comments << self
75
+ self
76
+ end
77
+ end
@@ -0,0 +1,24 @@
1
+ describe CanSelfDoIt::Unknown do
2
+
3
+ it "must implement can_see_default?" do
4
+ CanSelfDoIt::Unknown.protected_method_defined?(:can_see_default?).should == true
5
+ end
6
+
7
+ it "must implement can_edit_default?" do
8
+ CanSelfDoIt::Unknown.protected_method_defined?(:can_edit_default?).should == true
9
+ end
10
+
11
+ it "must implement can_create_default?" do
12
+ CanSelfDoIt::Unknown.protected_method_defined?(:can_create_default?).should == true
13
+ end
14
+
15
+ it "must implement can_delete_default?" do
16
+ CanSelfDoIt::Unknown.protected_method_defined?(:can_delete_default?).should == true
17
+ end
18
+
19
+ it "must implement can_modify_default?" do
20
+ CanSelfDoIt::Unknown.protected_method_defined?(:can_modify_default?).should == true
21
+ end
22
+
23
+
24
+ end
data/spec/user_spec.rb ADDED
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe User do
4
+
5
+ before(:all) do
6
+ # User
7
+ class UserActsAsCanSelfDoIt < User
8
+ acts_as_can_self_do_it(:as => CanSelfDoIt::Known, :auto => false)
9
+ end
10
+
11
+ @user = UserActsAsCanSelfDoIt.new
12
+
13
+ # User Blogs and Posts
14
+ @blog_user = Blog.new(@user)
15
+ @post_user = Post.new(@blog_user)
16
+
17
+ # Other User Blogs and Posts
18
+ @blog_other_user = Blog.new(User.new)
19
+ @post_other_user = Post.new(@blog_other_user)
20
+
21
+ # Admin Blogs and Posts
22
+ @blog_admin = Blog.new(Admin.new)
23
+ @post_admin = Post.new(@blog_admin)
24
+ end
25
+
26
+ it "can see any Blog" do
27
+ @user.can_see?(@blog_admin).should == true
28
+ @user.can_see?(@blog_user).should == true
29
+ @user.can_see?(@blog_other_user).should == true
30
+ end
31
+
32
+ it "can see any Post" do
33
+ @user.can_see?(@post_admin).should == true
34
+ @user.can_see?(@post_user).should == true
35
+ @user.can_see?(@post_other_user).should == true
36
+ end
37
+
38
+
39
+ it "can edit his Blogs" do
40
+ @user.can_edit?(@blog_user).should == true
41
+ end
42
+
43
+ it "can edit his Posts" do
44
+ @user.can_edit?(@post_user).should == true
45
+ end
46
+
47
+
48
+ it "can't edit other's Blogs" do
49
+ @user.can_edit?(@blog_admin).should == false
50
+ @user.can_edit?(@blog_other_user).should == false
51
+ end
52
+
53
+ it "can't edit other's Posts" do
54
+ @user.can_edit?(@post_admin).should == false
55
+ @user.can_edit?(@post_other_user).should == false
56
+ end
57
+
58
+ it "can comment his own posts" do
59
+ @user.can_create?(Comment, @user_post).should == true
60
+ end
61
+
62
+ it "can comment other user posts" do
63
+ @user.can_create?(Comment, @other_user_post).should == true
64
+ end
65
+
66
+ it "can comment admin posts" do
67
+ @user.can_create?(Comment, @other_user_post).should == true
68
+ end
69
+
70
+ it "can delete his Blogs" do
71
+ @user.can_delete?(@blog_user).should == true
72
+ end
73
+
74
+ it "can delete his Posts" do
75
+ @user.can_delete?(@post_user).should == true
76
+ end
77
+
78
+ it "can't delete other's Blogs" do
79
+ @user.can_delete?(@blog_admin).should == false
80
+ @user.can_delete?(@blog_other_user).should == false
81
+ end
82
+
83
+ it "can't delete other's Posts" do
84
+ @user.can_delete?(@post_admin).should == false
85
+ @user.can_delete?(@post_other_user).should == false
86
+ end
87
+
88
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: can_self_do_it
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Juan Martín Buceta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &8556920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *8556920
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &8556500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *8556500
36
+ description: Provide modules to work with sereveral permission policies
37
+ email:
38
+ - mbuceta@grantaire.com.ar
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.rdoc
46
+ - Rakefile
47
+ - can_self_do_it.gemspec
48
+ - lib/can_self_do_it.rb
49
+ - lib/can_self_do_it/auto.rb
50
+ - lib/can_self_do_it/base.rb
51
+ - lib/can_self_do_it/helper.rb
52
+ - lib/can_self_do_it/known.rb
53
+ - lib/can_self_do_it/unknown.rb
54
+ - lib/can_self_do_it/version.rb
55
+ - spec/admin_spec.rb
56
+ - spec/auto_admin_spec.rb
57
+ - spec/auto_guest_spec.rb
58
+ - spec/auto_spec.rb
59
+ - spec/auto_user_spec.rb
60
+ - spec/base_spec.rb
61
+ - spec/guest_spec.rb
62
+ - spec/known_spec.rb
63
+ - spec/spec_helper.rb
64
+ - spec/unknown_spec.rb
65
+ - spec/user_spec.rb
66
+ homepage: http://martinbuceta.com.ar
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: can_self_do_it
86
+ rubygems_version: 1.8.11
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Simple can_*? like method to manage permissions
90
+ test_files: []