Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use objects instead of hash when returning permissions

  • Loading branch information...
commit eb9527fbe208db066f386976914f5dedebbebd1b 1 parent 3f30b2c
@mkristian authored
Showing with 25 additions and 27 deletions.
  1. +3 −2 ixtlan-guard.gemspec
  2. +22 −25 lib/ixtlan/guard/guard.rb
View
5 ixtlan-guard.gemspec
@@ -1,7 +1,7 @@
# -*- mode: ruby -*-
Gem::Specification.new do |s|
s.name = 'ixtlan-guard'
- s.version = '0.8.3'
+ s.version = '0.9.0'
s.summary = 'guard your controller actions'
s.description = 'simple authorization framework for rails controllers'
@@ -21,7 +21,8 @@ Gem::Specification.new do |s|
s.test_files += Dir['spec/**/*_spec.rb']
s.test_files += Dir['features/*.feature']
s.test_files += Dir['features/step_definitions/*.rb']
- s.add_runtime_dependency 'ixtlan-core', '~>0.7.0'
+ s.add_runtime_dependency 'ixtlan-core', '~>0.8.0'
+ s.add_runtime_dependency 'virtus', '~>0.5'
s.add_development_dependency 'rails', '3.0.9'
s.add_development_dependency 'rspec', '2.6.0'
s.add_development_dependency 'cucumber', '0.9.4'
View
47 lib/ixtlan/guard/guard.rb
@@ -1,5 +1,5 @@
require 'ixtlan/guard/guard_config'
-
+require 'virtus'
module Ixtlan
module Guard
class Guard
@@ -103,9 +103,9 @@ def permissions(current_groups, &block)
m = @config.map_of_all
m.each do |resource, actions|
nodes = []
- perm = Node.new(:permission)
- perm[:resource] = resource
- perm[:actions] = nodes
+ perm = Permission.new #Node.new(:permission)
+ perm.resource = resource
+ perm.actions = nodes
# setup default_groups
default_groups = actions.delete('defaults') || []
@@ -122,11 +122,11 @@ def permissions(current_groups, &block)
# deny = true : default_groups.member?('*')
default_groups.size != 0 || default_groups.member?('*')
end
- perm[:deny] = deny
+ perm.deny = deny
actions.each do |action, groups|
group_names = groups.collect { |g| g.is_a?(Hash) ? g.keys : g }.flatten if groups
- node = Node.new(:action)
+ node = Acton.new #Node.new(:action)
allowed_groups =
if groups && group_names.member?('*')
group_map.values
@@ -136,49 +136,46 @@ def permissions(current_groups, &block)
end
if (deny && allowed_groups.size == 0) || (!deny && allowed_groups.size > 0)
- node[:name] = action
+ node.name = action
if block
if allowed_groups.size > 0
assos = block.call(resource, allowed_groups)
- node[:associations] = assos if assos && assos.size > 0
+ node.associations = assos if assos && assos.size > 0
else
assos = block.call(resource, group_map.values)
- perm[:associations] = assos if assos && assos.size > 0
+ perm.associations = assos if assos && assos.size > 0
end
end
nodes << node
elsif deny && allowed_groups.size > 0 && block
assos = block.call(resource, group_map.values)
- perm[:associations] = assos if assos && assos.size > 0
+ perm.associations = assos if assos && assos.size > 0
end
end
# TODO is that right like this ?
# only default_groups, i.e. no actions !!!
if block && actions.size == 0 && deny
assos = block.call(resource, group_map.values)
- perm[:associations] = assos if assos && assos.size > 0
+ perm.associations = assos if assos && assos.size > 0
end
perms << perm
end
perms
end
end
- class Node < Hash
-
- attr_reader :content
+ class Action
+ include Virtus
- def initialize(name)
- map = super
- @content = {}
- merge!({ name => @content })
- end
+ attribute :name, String
+ attribute :associations, Array[String]
+ end
+ class Permission
+ include Virtus
- def []=(k,v)
- @content[k] = v
- end
- def [](k)
- @content[k]
- end
+ attribute :resource, String
+ attribute :actions, Array[Action]
+ attribute :deny, Boolean
+ attribute :associations, Array[String]
end
class GuardException < Exception; end
class PermissionDenied < GuardException; end
Please sign in to comment.
Something went wrong with that request. Please try again.