six 0.0.2 → 0.0.3

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.
Files changed (2) hide show
  1. data/lib/six.rb +113 -7
  2. metadata +2 -2
data/lib/six.rb CHANGED
@@ -1,4 +1,16 @@
1
1
  class Six
2
+ class NoPackError < StandardError
3
+ def message
4
+ "No such pack"
5
+ end
6
+ end
7
+
8
+ class InvalidPackPassed < StandardError
9
+ def message
10
+ "Wrong Rule Pack. You must provide correct 'allowed' method"
11
+ end
12
+ end
13
+
2
14
  class << self
3
15
  attr_accessor :rules_packs
4
16
  attr_accessor :current_rule_pack
@@ -7,15 +19,56 @@ class Six
7
19
  @rules_packs ||= {}
8
20
  end
9
21
 
22
+ # Set current pack from stored packs by key
23
+ #
24
+ # == Parameters:
25
+ # name::
26
+ # A Symbol declaring the key name of stored pack
27
+ #
28
+ # == Returns:
29
+ # self or false
30
+ #
10
31
  def use(name)
11
- @current_rule_pack = rules_packs[name.to_sym] if pack_exist?(name)
12
- self
32
+ if pack_exist?(name)
33
+ @current_rule_pack = name.to_sym
34
+ self
35
+ end
36
+ end
37
+
38
+ # Same as use but raise exception if no pack found
39
+ def use!(name)
40
+ use(name) ? self : raise_no_such_pack
13
41
  end
14
42
 
43
+ # Add pack to authorization class
44
+ #
45
+ # == Parameters:
46
+ # name::
47
+ # A Symbol declaring the key name of stored pack
48
+ # pack::
49
+ # Any kind of object responding to allowed method
50
+ #
51
+ # == Returns:
52
+ # true or false
53
+ #
15
54
  def add_pack(name, pack)
16
55
  rules_packs[name.to_sym] = pack if valid_rules_object?(pack)
17
56
  end
18
57
 
58
+ # Same as add_pack but raise exception if pack is invalid
59
+ def add_pack!(name, pack)
60
+ add_pack(name, pack) || raise_incorrect_pack_object
61
+ end
62
+
63
+ # Remove pack from authorization class
64
+ #
65
+ # == Parameters:
66
+ # name::
67
+ # A Symbol declaring the key name of stored pack
68
+ #
69
+ # == Returns:
70
+ # true or false
71
+ #
19
72
  def remove_pack(name)
20
73
  if pack_exist?(name)
21
74
  @current_rule_pack = nil if rules_packs[name.to_sym] == @current_rule_pack
@@ -23,26 +76,79 @@ class Six
23
76
  end
24
77
  end
25
78
 
79
+ # Same as remove_pack but raise exception if pack wasnt found
80
+ def remove_pack!(name)
81
+ remove_pack(name) || raise_no_such_pack
82
+ end
83
+
84
+ # Check if object for rule pack is valid
85
+ #
86
+ # == Parameters:
87
+ # pack::
88
+ # Any kind of object responding to allowed method
89
+ #
90
+ # == Returns:
91
+ # true or false
92
+ #
26
93
  def valid_rules_object?(object)
27
- object.respond_to?(:allowed) ||
28
- raise("Wrong Rule Pack. You must provide 'allowed' method")
94
+ object.respond_to?(:allowed) &&
95
+ object.send(:allowed, nil, nil).kind_of?(Array)
96
+ rescue
97
+ false
29
98
  end
30
99
 
100
+ # Check if authorization class has pack with such name
101
+ #
102
+ # == Parameters:
103
+ # name::
104
+ # A Symbol declaring the key name of stored pack
105
+ #
106
+ # == Returns:
107
+ # true or false
108
+ #
31
109
  def pack_exist?(name)
32
- rules_packs.has_key?(name.to_sym) ||
33
- raise("No such pack")
110
+ rules_packs.has_key?(name.to_sym)
34
111
  end
35
112
 
113
+ # Check if authorization class allow access for object to subject
114
+ # using selected pack or all stored.
115
+ # Basically this method
116
+ # 1. send :allowed for every stored object in packs and pass object & subject
117
+ # 2. check if any of results include allowed action
118
+ #
119
+ # == Parameters:
120
+ # action::
121
+ # Action name to check for access
122
+ # object::
123
+ # object trying to access resource
124
+ # subject::
125
+ # resource
126
+ #
127
+ # == Returns:
128
+ # true or false
129
+ #
36
130
  def allowed?(action, object, subject)
37
131
  if current_rule_pack
38
- current_rule_pack.allowed(object, subject).include?(action)
132
+ rules_packs[current_rule_pack].allowed(object, subject).include?(action)
39
133
  else
40
134
  rules_packs.values.map { |rp| rp.allowed(object, subject) }.flatten.include?(action)
41
135
  end
42
136
  end
43
137
 
138
+ # Reset current used rule pack so auth class use
139
+ # global allowed? for new request
44
140
  def reset_use
45
141
  @current_rule_pack = nil
46
142
  end
143
+
144
+ protected
145
+
146
+ def raise_no_such_pack
147
+ raise Six::NoPackError.new
148
+ end
149
+
150
+ def raise_incorrect_pack_object
151
+ raise Six::InvalidPackPassed.new
152
+ end
47
153
  end
48
154
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: six
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-13 00:00:00.000000000Z
12
+ date: 2011-08-17 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Very simple authorization gem
15
15
  email: dmitriy.zaporozhets@gmail.com