minican 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea52e4d136e6fc32c74452ad561ea64e6d832a84
4
+ data.tar.gz: 961382bb4426291699b14e0074a8aaf2f417377e
5
+ SHA512:
6
+ metadata.gz: cc3f9fd5ee74bad3261013c7b4540a73c10f379f16c6e70c00c5437cda5d5c4547195da9ac09500acf708e1e30d47803bbcf748a6ad1e582193761a7583e6c4b
7
+ data.tar.gz: 94e5140f784369993a333bd3c2e93d31630ca426177dfe93b2933aacf2050c7d89e0f9142328fb207dd859843eb1c211acd6e3e26cf898ada9c5824942c3e1d6
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Blake Williams
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.
@@ -0,0 +1,12 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rspec/core/rake_task'
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ task :default => :spec
11
+
12
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,7 @@
1
+ require 'minican/helpers'
2
+ require 'minican/access_denied'
3
+ require 'minican/controller_additions'
4
+ require 'minican/policy'
5
+
6
+ module Minican
7
+ end
@@ -0,0 +1,17 @@
1
+ module Minican
2
+ class AccessDenied < StandardError
3
+
4
+ # This is raised when a policy fails in a controller via
5
+ # {Minican::ControllerAdditions}
6
+ #
7
+ # @param [policy] The policy that authorized it
8
+ def initialize(policy)
9
+ @policy = policy
10
+ end
11
+
12
+ # Human readable error message
13
+ def to_s
14
+ "You are not authorized to access this page"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,58 @@
1
+ module Minican
2
+ module ControllerAdditions
3
+ include Minican::Helpers
4
+
5
+ private
6
+
7
+ # Controller helper method to verify call method on
8
+ # policy. Raises {Minican::AccessDenied} if the policy
9
+ # method fails
10
+ #
11
+ # @param method [Symbol] method to be called on the policy
12
+ # @param object [Object] The object to apply the policy to
13
+ # @param user [User] The user object to pass to the method
14
+ #
15
+ # @visibility public
16
+ def authorize!(method, object, user = current_user)
17
+ policy = policy_for(object)
18
+
19
+ if policy.cannot?(method, current_user)
20
+ raise Minican::AccessDenied.new(policy)
21
+ end
22
+ end
23
+
24
+ # Helper method available in controllers and views
25
+ # that returns the value of the policy method
26
+ #
27
+ # @param (see #authorize!)
28
+ # @return (Boolean)
29
+ #
30
+ # @visibility public
31
+ def can?(method, object, user = current_user)
32
+ policy = policy_for(object)
33
+ policy.can?(method, user)
34
+ end
35
+
36
+ # Helper method available in controllers and views
37
+ # that returns the negated value of the policy method
38
+ #
39
+ # @param (see #can?)
40
+ # @return (see #can?)
41
+ #
42
+ # @visibility public
43
+ def cannot?(method, object, user = current_user)
44
+ !can?(method, object, user)
45
+ end
46
+
47
+ def self.included(base)
48
+ base.helper_method :can?
49
+ base.helper_method :cannot?
50
+ end
51
+ end
52
+ end
53
+
54
+ if defined? ActionController::Base
55
+ ActionController::Base.class_eval do
56
+ include Minican::ControllerAdditions
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ module Minican
2
+ module Helpers
3
+
4
+ private
5
+
6
+ # Returns a new policy for passed in object
7
+ #
8
+ # @param [object] The object to create a policy for
9
+ #
10
+ # @visibility public
11
+ def policy_for(object)
12
+ policy_class(object).new(object)
13
+ end
14
+
15
+ # Returns a policy class based on the object
16
+ # passed in. If object is a class, it will
17
+ # return itself, if it's an instance it will
18
+ # return a class based on its name.
19
+ #
20
+ # @param [object] The object to classify
21
+ #
22
+ # @visibility public
23
+ def policy_class(object)
24
+ if object.class == Class
25
+ class_name = object
26
+ else
27
+ class_name = object.class
28
+ end
29
+
30
+ "#{class_name.to_s}Policy".constantize
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,49 @@
1
+ module Minican
2
+ class Policy
3
+ include Minican::Helpers
4
+
5
+ # Creates a new instance of policy with object
6
+ # that is accessible via {object} or after the
7
+ # name of the policy class
8
+ #
9
+ # @param [object] The object the policy tests against
10
+ def initialize(object)
11
+ instance_variable_set(instance_name, object)
12
+ end
13
+
14
+ # Convenience method to call predicate method
15
+ # and return the value
16
+ #
17
+ # @param [method] Method to call
18
+ # @param [user] User to send to the method
19
+ #
20
+ # @return [Boolean]
21
+ def can?(method, user)
22
+ self.send("#{method}?", user)
23
+ end
24
+
25
+ # Convenience method to call predicate method
26
+ # and return the negated value
27
+ #
28
+ # @param [method] Method to call
29
+ # @param [user] User to send to the method
30
+ #
31
+ # @return [Boolean]
32
+ def cannot?(method, user)
33
+ !can?(method, user)
34
+ end
35
+
36
+ # Conveneince method to get the current object
37
+ # without having to get the class name
38
+ def object
39
+ instance_variable_get(instance_name)
40
+ end
41
+
42
+ private
43
+
44
+ def instance_name
45
+ class_name = self.class.to_s.gsub(/Policy$/, '')
46
+ "@#{class_name.underscore}"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Minican
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :minican do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minican
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Blake Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.beta1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.beta1
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.7.3
69
+ description: Miniature authorization for Rails.
70
+ email:
71
+ - blake@blakewilliams.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/minican/access_denied.rb
77
+ - lib/minican/controller_additions.rb
78
+ - lib/minican/helpers.rb
79
+ - lib/minican/policy.rb
80
+ - lib/minican/version.rb
81
+ - lib/minican.rb
82
+ - lib/tasks/minican_tasks.rake
83
+ - MIT-LICENSE
84
+ - Rakefile
85
+ homepage: http://github.com/BlakeWilliams/Minican
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.1.11
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Miniature authorization for Rails.
109
+ test_files: []
110
+ has_rdoc: