permissable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/permissable.rb CHANGED
@@ -52,11 +52,14 @@ module Permissable
52
52
 
53
53
  write_inheritable_attribute :permissable_by_association, (options[:with] || false)
54
54
  class_inheritable_reader :permissable_by_association
55
-
55
+
56
+ write_inheritable_attribute :has_permissions_for, []
57
+ class_inheritable_accessor :has_permissions_for
58
+
56
59
  # The class that called permissable becomes a member.
57
60
  include Permissable::Member
58
- has_many(:permissions, :as => :member, :conditions => { :member_type => "#{self.to_s}" }) unless permissable_by_association
59
61
 
62
+ # Yield the permissions block to add permissions.
60
63
  yield self
61
64
 
62
65
  end
@@ -64,29 +67,20 @@ module Permissable
64
67
  # +permission_for+ is called via the permissable block
65
68
  def permission_for(methods, *resources)
66
69
 
67
- methods = [methods] unless methods.is_a?(Array)
68
-
69
- resources.each do |resource|
70
-
71
- klass = resource.to_s.classify
72
- klass.constantize.class_eval do
73
-
74
- write_inheritable_attribute :permissable_methods, methods
75
- class_inheritable_reader :permissable_methods
76
- has_many(:permissions, :as => :resource, :conditions => { :resource_type => "#{self.to_s}" })
77
-
78
- include Permissable::Resource
79
-
80
- end
81
-
82
- end
83
-
84
- end
85
-
86
- end
87
-
70
+ methods = [methods].flatten
71
+
72
+ # Add each resource to the member class' permissions list.
73
+ has_permissions_for.concat(resources).flatten.uniq
74
+
75
+ resources.uniq.each do |resource|
76
+ #Include the Resource module to each resource class.
77
+ resource.to_s.classify.constantize.class_eval{ include Permissable::Resource }
78
+ resource.to_s.classify.constantize.send :set_permissions!, methods
79
+ end
80
+
81
+ end
82
+
83
+ end
88
84
  end
89
85
 
90
- ActiveSupport.on_load :active_record do
91
- ActiveRecord::Base.send :include, Permissable
92
- end
86
+ ActiveRecord::Base.send :include, Permissable
@@ -2,24 +2,22 @@ module Permissable
2
2
 
3
3
  module Member
4
4
 
5
- def self.included(base)
6
-
5
+ def self.included(base)
7
6
  base.send :include, InstanceMethods
8
-
9
- if base.permissable_by_association
10
- base.send :include, AssociatedMethods
11
- # Add a has many to our association so it can find permissions.
12
- base.permissable_by_association.to_s.classify.constantize.class_eval do
13
- has_many :permissions, :as => :member, :conditions => { :member_type => "#{self.to_s}" }
14
- end
15
- else
16
- base.send :include, MemberMethods
7
+ member_klass = (base.permissable_by_association) ? base.permissable_by_association.to_s.classify.constantize : base
8
+ member_klass.class_eval do
9
+ has_many :permissions, :as => :member, :conditions => { :member_type => "#{self.to_s}" }
17
10
  end
11
+ base.send :include, ((base.permissable_by_association) ? AssociatedMethods : MemberMethods)
18
12
  end
19
13
 
20
14
  # This module includes methods that should exist on ALL members.
21
15
  module InstanceMethods
22
16
 
17
+ # When a member is initialized, its definitions are eager-loaded to cut down on database queries.
18
+ # Once loaded all of the permission definitions are cached into the @permission_definitions variable.
19
+ attr_accessor :permission_definitions
20
+
23
21
  # Can this member perform the requested action?
24
22
  def can?(method, resource)
25
23
  method = method.to_s
@@ -27,40 +25,38 @@ module Permissable
27
25
  end
28
26
 
29
27
  # Set a permission
30
- def can!(method, resource)
31
- method = method.to_s.downcase
32
- klassname = permissable_by_association ? permissable_by_association.to_s.classify : self.class.to_s
33
-
34
- throw Permissable::ResourceNotPermissable unless resource.respond_to? :permissable_methods
35
- throw Permissable::PermissionNotDefined unless resource.permissable_methods.include?(method)
36
-
37
- members.each do |m|
38
- next if resource.permissions.for_member_and_resource(m,resource).exists?
39
- new_permission = Permission.new()
40
- new_permission.member = m
41
- new_permission.resource = resource
42
- new_permission.permission_type = method
43
- new_permission.save
44
- end
28
+ def can!(methods, resource)
29
+ methods = [methods].flatten.uniq
45
30
 
31
+ members.each do |member|
32
+ methods.each do |perm|
33
+ next if resource.permissions.for_member(member).with_permission_to(perm).exists?
34
+ new_permission = Permission.new({ :permission_type => method.to_s })
35
+ new_permission.member = m
36
+ new_permission.resource = resource
37
+ new_permission.save
38
+ end
39
+ end
40
+ end
41
+
42
+ def cannot?(methods, resource)
43
+ !can?(methods, resource)
44
+ end
45
+
46
+ def cannot!(methods, resources)
47
+ methods = [methods].flatten.uniq
48
+ existing = permissions.for_resource(resource).with_permission_to(methods).all.collect{ |perm| perm.id }
49
+ Permission.destroy(existing)
46
50
  end
47
51
 
48
52
  def permissions_for(resource)
49
- if resource.is_a?(Class)
50
- permissions_scope[resource.to_s]
51
- else
52
- permissions.for_resource(resource).all.collect{ |p| p.permission_type.to_sym }.uniq
53
- end
53
+ permissions.for_resource(resource).all.collect{ |p| p.permission_type.to_sym }.uniq
54
54
  end
55
55
 
56
56
  def permissions_for?(resource)
57
57
  !permissions_for(resource).empty?
58
58
  end
59
59
 
60
- def permissions_for!(resources, *perms)
61
-
62
- end
63
-
64
60
  private
65
61
 
66
62
  def get_const(resource)
@@ -4,11 +4,13 @@ class Permission < ActiveRecord::Base
4
4
 
5
5
  class << self
6
6
  def for_member(member)
7
- where(:member_id => member.id, :member_type => member.class.to_s)
7
+ member = flatten(member)
8
+ where(:member_id => member[:ids], :member_type => member[:types])
8
9
  end
9
10
 
10
11
  def for_resource(resource)
11
- where(:resource_id => resource.id, :resource_type => resource.class.to_s)
12
+ resource = flatten(resource)
13
+ where(:resource_id => resource[:ids], :resource_type => resource[:types])
12
14
  end
13
15
 
14
16
  def for_member_and_resource(member, resource)
@@ -16,8 +18,14 @@ class Permission < ActiveRecord::Base
16
18
  end
17
19
 
18
20
  def with_permission_to(perm)
19
- where(:permission_type => perm.to_s)
21
+ perm = perm.is_a?(Array) ? perm.collect{ |p| p.to_s } : perm.to_s
22
+ where(:permission_type => perm)
20
23
  end
21
24
  end
22
25
 
26
+ def flatten(obj)
27
+ return { :ids => obj, :types => obj.class.to_s } unless obj.is_a?(Array)
28
+ { :ids => obj.collect{ |o| o.id }, :types => obj.collect{ |o| o.class.to_s } }
29
+ end
30
+
23
31
  end
@@ -2,10 +2,18 @@ module Permissable
2
2
 
3
3
  module Resource
4
4
 
5
- private
5
+ def self.included(base)
6
+ base.class_eval{ has_many(:permissions, :as => :resource, :conditions => { :resource_type => "#{self.to_s}" }) }
7
+ base.extend(ClassMethods)
8
+ end
6
9
 
7
- def self.set_permissions!
8
- puts permissable_methods.inspect
10
+ module ClassMethods
11
+ private
12
+ def set_permissions!(permission_list)
13
+ class_inheritable_accessor :permissable_methods
14
+ write_inheritable_attribute(:permissable_methods, []) if permissable_methods.nil?
15
+ permissable_methods.concat permission_list.uniq.flatten
16
+ end
9
17
  end
10
18
 
11
19
  end
data/permissable.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{permissable}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brent Kirby"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: permissable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brent Kirby