Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Commit

Permalink
Add allow_all back
Browse files Browse the repository at this point in the history
  • Loading branch information
beathyate committed Oct 21, 2014
1 parent 582b1a1 commit 45f7aa3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,16 @@ class Authorization
include Guachiman

def initialize(user = nil)
if @current_user = user
user_authorization
else
guest_authorization
end
end

private

def guest_authorization
allow :sessions, [:new]
end

def user_authorization
guest_authorization

allow :users, [:show, :edit, :update] do |user_id|
@current_user.id == user_id
allow :sessions, [:new, :create]

if user
if user.admin?
@allow_all = true
else
allow :users, [:show, :edit, :update] do |user_id|
user.id == user_id
end
end
end
end
end
Expand All @@ -82,16 +74,21 @@ So that you can use them like this:

```ruby
user = User.find(user_id)
admin = User.find(admin_id)

guest_authorization = Authorization.new
user_authorization = Authorization.new(user)
guest_authorization = Authorization.new
user_authorization = Authorization.new(user)
admin_authorization = Authorization.new(admin)

guest_authorization.allow?(:sessions, :new)
# => true

user_authorization.allow?(:users, :show)
# => false

admin_authorization.allow?(:users, :show)
# => true

user_authorization.allow?(:users, :show, user.id)
# => true
```
Expand All @@ -103,7 +100,7 @@ This is what you use to set permissions. It takes two parameters, `group` and `p
### `#allow?`

This is what you use to check permissions. It takes a `group` param, a `permission` param, and an optional `object`
param to evaluate in the block.
param to evaluate in the block. **If the instance variable `@allow_all` is set to `true` it will always return `true`.**


License
Expand Down
2 changes: 2 additions & 0 deletions lib/guachiman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def allow(group, permissions, &block)
end

def allow?(group, permission, object = nil)
return true if instance_variable_defined?(:@allow_all) && @allow_all

if rule = rules[group] && rules[group][permission]
rule == true || object && rule.call(object)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/guachiman/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Guachiman
VERSION = "1.0.2"
VERSION = "1.1.0"
end
14 changes: 12 additions & 2 deletions test/guachiman_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ def setup
@authorization = Class.new do
include Guachiman

def initialize
def initialize(user = 1)
allow :group, [:permission1, :permission2]

allow :group, [:permission3, :permission4] do |object|
object == 1
object == user
end
end
end.new

p @authorization
end

def test_basic_rules
Expand All @@ -37,4 +39,12 @@ def test_block_rules_with_good_object
assert @authorization.allow?(:group, :permission3, 1)
assert @authorization.allow?(:group, :permission4, 1)
end

def test_allow_all
refute @authorization.allow?(:group, :permission0)

@authorization.instance_variable_set(:@allow_all, true)

assert @authorization.allow?(:group, :permission0)
end
end

0 comments on commit 45f7aa3

Please sign in to comment.