Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

moved export to proper models and fixed specs

  • Loading branch information...
commit a0ead8e35b4c7828cdbd122602d9772d435831af 1 parent eb9527f
Christian Meier authored
28 Rakefile
... ... @@ -0,0 +1,28 @@
  1 +# -*- mode: ruby -*-
  2 +
  3 +require 'rspec/core/rake_task'
  4 +
  5 +RSpec::Core::RakeTask.new(:spec)
  6 +
  7 +task :default => [ :spec ]
  8 +
  9 +task :headers do
  10 + require 'rubygems'
  11 + require 'copyright_header'
  12 +
  13 + s = Gem::Specification.load( Dir["*gemspec"].first )
  14 +
  15 + args = {
  16 + :license => s.license,
  17 + :copyright_software => s.name,
  18 + :copyright_software_description => s.description,
  19 + :copyright_holders => s.authors,
  20 + :copyright_years => [Time.now.year],
  21 + :add_path => 'lib',
  22 + :output_dir => './'
  23 + }
  24 +
  25 + command_line = CopyrightHeader::CommandLine.new( args )
  26 + command_line.execute
  27 +end
  28 +# vim: syntax=Ruby
2  ixtlan-guard.gemspec
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21 21 s.test_files += Dir['spec/**/*_spec.rb']
22 22 s.test_files += Dir['features/*.feature']
23 23 s.test_files += Dir['features/step_definitions/*.rb']
24   - s.add_runtime_dependency 'ixtlan-core', '~>0.8.0'
  24 +# s.add_runtime_dependency 'ixtlan-core', '~>0.8.0'
25 25 s.add_runtime_dependency 'virtus', '~>0.5'
26 26 s.add_development_dependency 'rails', '3.0.9'
27 27 s.add_development_dependency 'rspec', '2.6.0'
7 lib/ixtlan/guard/guard.rb
@@ -105,7 +105,7 @@ def permissions(current_groups, &block)
105 105 nodes = []
106 106 perm = Permission.new #Node.new(:permission)
107 107 perm.resource = resource
108   - perm.actions = nodes
  108 + perm.actions = []#nodes
109 109
110 110 # setup default_groups
111 111 default_groups = actions.delete('defaults') || []
@@ -126,7 +126,7 @@ def permissions(current_groups, &block)
126 126
127 127 actions.each do |action, groups|
128 128 group_names = groups.collect { |g| g.is_a?(Hash) ? g.keys : g }.flatten if groups
129   - node = Acton.new #Node.new(:action)
  129 + node = Action.new #Node.new(:action)
130 130 allowed_groups =
131 131 if groups && group_names.member?('*')
132 132 group_map.values
@@ -134,7 +134,6 @@ def permissions(current_groups, &block)
134 134 names = group_map.keys & ((group_names || []) + @superuser)
135 135 names.collect { |name| group_map[name] }
136 136 end
137   -
138 137 if (deny && allowed_groups.size == 0) || (!deny && allowed_groups.size > 0)
139 138 node.name = action
140 139 if block
@@ -146,7 +145,7 @@ def permissions(current_groups, &block)
146 145 perm.associations = assos if assos && assos.size > 0
147 146 end
148 147 end
149   - nodes << node
  148 + perm.actions << node
150 149 elsif deny && allowed_groups.size > 0 && block
151 150 assos = block.call(resource, group_map.values)
152 151 perm.associations = assos if assos && assos.size > 0
2  lib/ixtlan/guard/guard_rails.rb
@@ -116,7 +116,7 @@ def self.guard_filters
116 116 end
117 117
118 118 def self.guard
119   - Rails.application.config.guard
  119 + ::Rails.application.config.guard
120 120 end
121 121
122 122 def self.allowed?(action, current_groups, reference = nil)
22 lib/ixtlan/guard/models.rb
... ... @@ -0,0 +1,22 @@
  1 +require 'virtus'
  2 +module Ixtlan
  3 + module Guard
  4 + class Action
  5 + include Virtus
  6 +
  7 + attribute :name, String
  8 + attribute :associations, Array[String]
  9 + end
  10 + class Permission
  11 + include Virtus
  12 +
  13 + attribute :resource, String
  14 + attribute :actions, Array[Action], :default => []
  15 + attribute :deny, Boolean, :default => false
  16 + attribute :associations, Array[String]
  17 + end
  18 + #TODO
  19 + class GuardException < Exception; end
  20 + class PermissionDenied < GuardException; end
  21 + end
  22 +end
2  lib/ixtlan/guard/railtie.rb
... ... @@ -1,4 +1,4 @@
1   -require 'rails'
  1 +require 'rails/railtie'
2 2 require 'ixtlan/guard/guard'
3 3 require 'ixtlan/guard/guard_rails'
4 4 require 'logger'
23 spec/guard_export_spec.rb
@@ -7,16 +7,21 @@
7 7 def assert(expected, perms)
8 8 map = {}
9 9 expected.each do |e|
10   - map[e[:permission][:resource]] = e
11   - if e[:permission][:actions]
12   - e[:permission][:actions].sort!{ |n,m| n[:action][:name] <=> m[:action][:name] }
  10 + map[(e[:permission] || e)[:resource]] = e
  11 + if (e[:permission] || e)[:actions]
  12 + (e[:permission] || e)[:actions].sort!{ |n,m| n[:action][:name] <=> m[:action][:name] }
13 13 end
14 14 end
15 15 perms.each do |perm|
16   - if perm[:actions]
17   - perm[:actions].sort!{ |n,m| n.content[:name] <=> m.content[:name] }
  16 + attr = perm.attributes
  17 + attr[ :actions ] = perm.actions.collect do |a|
  18 + aa = a.attributes
  19 + aa.delete( :associations ) if aa[ :associations ].nil?
  20 + {:action => aa}
18 21 end
19   - map[perm[:resource].to_s].should == perm
  22 + attr[:actions].sort!{ |n,m| n[:action][:name] <=> m[:action][:name] }
  23 + attr.delete( :associations ) if attr[ :associations ].nil?
  24 + map[perm[:resource]][:permission].should == attr
20 25 end
21 26 end
22 27
@@ -184,7 +189,7 @@ def group.name
184 189 end
185 190 perm = subject.permissions([group])do |resource, groups|
186 191 if resource == 'regions'
187   - [:europe, :asia]
  192 + ["europe", "asia"]
188 193 end
189 194 end
190 195 expected = [
@@ -219,8 +224,8 @@ def group.name
219 224 {:permission=>
220 225 {:resource=>"regions",
221 226 :actions=>
222   - [{:action=>{:name=>"show", :associations=>[:europe, :asia]}},
223   - {:action=>{:name=>"create", :associations=>[:europe, :asia]}}],
  227 + [{:action=>{:name=>"show", :associations=>["europe", "asia"]}},
  228 + {:action=>{:name=>"create", :associations=>["europe", "asia"]}}],
224 229 :deny=>false}},
225 230 #allow nothing
226 231 {:permission=>{:resource=>"users", :actions=>[], :deny=>false}}]
33 spec/guard_with_associations_spec.rb
@@ -47,7 +47,7 @@ def logger.debug(&block)
47 47 end
48 48
49 49 it 'should add associations to node' do
50   - perms = subject.permissions([Group.new('admin', [:german, :french])]) do |resource, groups|
  50 + perms = subject.permissions([Group.new('admin', ["german", "french"])]) do |resource, groups|
51 51 if groups && groups.first && groups.first.name == 'admin'
52 52 groups.first.domains
53 53 else
@@ -61,7 +61,7 @@ def logger.debug(&block)
61 61 :resource=>"accounts",
62 62 :actions=>[{:action=>{
63 63 :name=>"destroy",
64   - :associations=>[:german, :french]}}],
  64 + :associations=>["german", "french"]}}],
65 65 :deny=>false}
66 66 }
67 67 expected[:allow_all_defaults] = {
@@ -69,14 +69,14 @@ def logger.debug(&block)
69 69 :resource=>"allow_all_defaults",
70 70 :actions=>[{:action=>{:name=>"index"}}],
71 71 :deny=>true,
72   - :associations=>[:german, :french]}
  72 + :associations=>["german", "french"]}
73 73 }
74 74 expected[:defaults] = {
75 75 :permission=>{
76 76 :resource=>"defaults",
77 77 :actions=>[{:action=>{
78 78 :name=>"index",
79   - :associations=>[:german, :french]}}],
  79 + :associations=>["german", "french"]}}],
80 80 :deny=>false}
81 81 }
82 82 expected[:no_defaults] = {
@@ -84,14 +84,14 @@ def logger.debug(&block)
84 84 :resource=>"no_defaults",
85 85 :actions=>[{:action=>{
86 86 :name=>"index",
87   - :associations=>[:german, :french]}}],
  87 + :associations=>["german", "french"]}}],
88 88 :deny=>false}
89 89 }
90 90 expected[:only_defaults] = {
91 91 :permission=>{
92 92 :resource=>"only_defaults",
93 93 :actions=>[],
94   - :associations=>[:german, :french],
  94 + :associations=>["german", "french"],
95 95 :deny=>true}
96 96 }
97 97 expected[:person]= {
@@ -99,18 +99,18 @@ def logger.debug(&block)
99 99 :resource=>"person",
100 100 :actions=> [{:action=>{
101 101 :name=>"destroy",
102   - :associations=>[:german, :french]}},
  102 + :associations=>["german", "french"]}},
103 103 {:action=>{
104 104 :name=>"index",
105   - :associations=>[:german, :french]}}],
  105 + :associations=>["german", "french"]}}],
106 106 :deny=>false}
107 107 }
108 108 expected[:regions] = {
109 109 :permission=>{
110 110 :resource=>"regions",
111 111 :actions=>[
112   - {:action=>{:name=>"create", :associations=>[:german, :french]}},
113   - {:action=>{:name=>"show", :associations=>[:german, :french]}}
  112 + {:action=>{:name=>"create", :associations=>["german", "french"]}},
  113 + {:action=>{:name=>"show", :associations=>["german", "french"]}}
114 114 ],
115 115 :deny=>false}
116 116 }
@@ -119,12 +119,17 @@ def logger.debug(&block)
119 119 :resource=>"users",
120 120 :actions=>[],
121 121 :deny=>false}
122   - }
  122 + }
123 123 perms.each do |perm|
124   - if perm[:actions]
125   - perm[:actions].sort!{ |n,m| n.content[:name] <=> m.content[:name] }
  124 + attr = perm.attributes
  125 + attr[ :actions ] = perm.actions.collect do |a|
  126 + aa = a.attributes
  127 + aa.delete( :associations ) if aa[ :associations ].nil?
  128 + {:action => aa}
126 129 end
127   - expected[perm[:resource].to_sym].should == perm
  130 + attr[:actions].sort!{ |n,m| n[:action][:name] <=> m[:action][:name] }
  131 + attr.delete( :associations ) if attr[ :associations ].nil?
  132 + expected[perm[:resource].to_sym][:permission].should == attr
128 133 end
129 134 end
130 135 end

0 comments on commit a0ead8e

Please sign in to comment.
Something went wrong with that request. Please try again.