diff --git a/README.md b/README.md index 4a3daba..0c2376d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -82,9 +74,11 @@ 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 @@ -92,6 +86,9 @@ guest_authorization.allow?(:sessions, :new) user_authorization.allow?(:users, :show) # => false +admin_authorization.allow?(:users, :show) +# => true + user_authorization.allow?(:users, :show, user.id) # => true ``` @@ -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 diff --git a/lib/guachiman.rb b/lib/guachiman.rb index 2ca3e1b..4c47fe8 100644 --- a/lib/guachiman.rb +++ b/lib/guachiman.rb @@ -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 diff --git a/lib/guachiman/version.rb b/lib/guachiman/version.rb index bb2bdab..a518cff 100644 --- a/lib/guachiman/version.rb +++ b/lib/guachiman/version.rb @@ -1,3 +1,3 @@ module Guachiman - VERSION = "1.0.2" + VERSION = "1.1.0" end diff --git a/test/guachiman_test.rb b/test/guachiman_test.rb index b246981..a468203 100644 --- a/test/guachiman_test.rb +++ b/test/guachiman_test.rb @@ -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 @@ -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