logical_authz 0.1.6

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 (58) hide show
  1. data/app/controllers/groups_controller.rb +77 -0
  2. data/app/controllers/groups_users_controller.rb +27 -0
  3. data/app/controllers/permissions_controller.rb +63 -0
  4. data/app/helpers/logical_authz_helper.rb +158 -0
  5. data/app/views/groups/_controls.html.haml +18 -0
  6. data/app/views/groups/_form.html.haml +4 -0
  7. data/app/views/groups/create.rjs +1 -0
  8. data/app/views/groups/edit.html.haml +1 -0
  9. data/app/views/groups/index.html.haml +14 -0
  10. data/app/views/groups/new.html.haml +2 -0
  11. data/app/views/groups/show.html.haml +6 -0
  12. data/app/views/permissions/_controls.html.haml +18 -0
  13. data/app/views/permissions/_form.html.haml +8 -0
  14. data/app/views/permissions/create.rjs +1 -0
  15. data/app/views/permissions/edit.html.haml +1 -0
  16. data/app/views/permissions/index.html.haml +20 -0
  17. data/app/views/permissions/new.html.haml +2 -0
  18. data/config/initializers/activate.rb +1 -0
  19. data/generators/logical_authz/logical_authz_generator.rb +13 -0
  20. data/generators/logical_authz/templates/README +11 -0
  21. data/generators/logical_authz/templates/app/controllers/authz_controller.rb.erb +4 -0
  22. data/generators/logical_authz/templates/app/views/layouts/_explain_authz.html.haml.erb +21 -0
  23. data/generators/logical_authz_models/logical_authz_models_generator.rb +22 -0
  24. data/generators/logical_authz_routes/logical_authz_routes_generator.rb +12 -0
  25. data/generators/logical_authz_specs/logical_authz_specs_generator.rb +26 -0
  26. data/lib/logical_authz/access_control.rb +343 -0
  27. data/lib/logical_authz/application.rb +350 -0
  28. data/lib/logical_authz/authn_facade/authlogic.rb +13 -0
  29. data/lib/logical_authz/configuration.rb +64 -0
  30. data/lib/logical_authz/engine.rb +18 -0
  31. data/lib/logical_authz/generator.rb +22 -0
  32. data/lib/logical_authz/generators/controllers/generator.rb +15 -0
  33. data/lib/logical_authz/generators/controllers/templates/app/controllers/authz_controller.rb +6 -0
  34. data/lib/logical_authz/generators/models/generator.rb +109 -0
  35. data/lib/logical_authz/generators/models/templates/app/models/group.rb +33 -0
  36. data/lib/logical_authz/generators/models/templates/app/models/permission.rb +3 -0
  37. data/lib/logical_authz/generators/models/templates/config/initializers/logical_authz.rb +20 -0
  38. data/lib/logical_authz/generators/models/templates/db/seeds_logical_authz.rb +21 -0
  39. data/lib/logical_authz/generators/models/templates/migrations/create_groups.rb +12 -0
  40. data/lib/logical_authz/generators/models/templates/migrations/create_permissions.rb +15 -0
  41. data/lib/logical_authz/generators/models/templates/migrations/create_users_groups.rb +13 -0
  42. data/lib/logical_authz/generators/routes/generator.rb +21 -0
  43. data/lib/logical_authz/generators/specs/generator.rb +57 -0
  44. data/lib/logical_authz/generators/specs/templates/spec/controllers/groups_controller_spec.rb +102 -0
  45. data/lib/logical_authz/generators/specs/templates/spec/controllers/groups_users_controller_spec.rb +47 -0
  46. data/lib/logical_authz/generators/specs/templates/spec/controllers/permissions_controller_spec.rb +24 -0
  47. data/lib/logical_authz/generators/specs/templates/spec/factories/az_accounts.rb +7 -0
  48. data/lib/logical_authz/generators/specs/templates/spec/factories/az_groups.rb +7 -0
  49. data/lib/logical_authz/generators/specs/templates/spec/factories/permissions.rb +2 -0
  50. data/lib/logical_authz/generators/specs/templates/spec/helpers/logical_authz_helper_spec.rb +90 -0
  51. data/lib/logical_authz/generators/specs/templates/spec/support/logical_authz.rb +1 -0
  52. data/lib/logical_authz/generators/specs/templates/spec/support/mock_auth.rb +30 -0
  53. data/lib/logical_authz/spec_helper.rb +75 -0
  54. data/lib/logical_authz.rb +110 -0
  55. data/lib/tasks/rspec.rake +15 -0
  56. data/spec/gem_test_suite.rb +17 -0
  57. data/spec/spec_helper.rb +43 -0
  58. metadata +127 -0
@@ -0,0 +1,90 @@
1
+ require 'spec/spec_helper'
2
+
3
+ class FooController < AuthzController
4
+ end
5
+
6
+ class BarController < AuthzController
7
+ end
8
+
9
+ class WireController < AuthzController
10
+ end
11
+
12
+ describe LogicalAuthz::Helper do
13
+ include LogicalAuthz::MockAuth
14
+
15
+ before do
16
+ @group = Factory(:group)
17
+ Factory(:permission, :group => @group, :controller => "foo")
18
+ Factory(:permission, :group => @group, :controller => "bar", :action => "baz")
19
+ Factory(:permission, :group => @group, :controller => "wire", :action => "vinyl", :subject_id => 1)
20
+ end
21
+
22
+ it "should refuse authorization to guests" do
23
+ logout
24
+
25
+ helper.authorized?(:controller => "foo").should == false
26
+ helper.authorized?(:controller => "bar", :action => "baz").should == false
27
+ helper.authorized?(:controller => "wire", :action => "vinyl", :id => 1).should == false
28
+ end
29
+
30
+ describe "should recognize authorized users" do
31
+ before do
32
+ user = Factory(:authz_account, :groups => [@group])
33
+ login_as(user)
34
+ end
35
+
36
+ it "on a controller level" do
37
+ helper.authorized?(:controller => "foo",
38
+ :action => "nerf",
39
+ :id => 7).should == true
40
+ end
41
+
42
+ it "on an action level" do
43
+ helper.authorized?(:controller => "bar",
44
+ :action => "baz",
45
+ :id => 23).should == true
46
+ end
47
+
48
+ it "not on the wrong action level" do
49
+ helper.authorized?(:controller => "bar",
50
+ :action => "bat",
51
+ :id => 23).should == false
52
+ end
53
+
54
+ it "on a record level" do
55
+ helper.authorized?(:controller => "wire",
56
+ :action => "vinyl",
57
+ :id => 1).should == true
58
+ end
59
+
60
+ it "not on the wrong record level" do
61
+ helper.authorized?(:controller => "wire",
62
+ :action => "vinyl",
63
+ :id => 2).should == false
64
+ end
65
+ end
66
+
67
+ describe "should refuse unauthorized users" do
68
+ before do
69
+ login_as(:authz_account)
70
+ end
71
+
72
+ it "on a controller level" do
73
+ helper.authorized?(:controller => "foo",
74
+ :action => "nerf",
75
+ :id => 7).should == false
76
+ end
77
+
78
+ it "on an action level" do
79
+ helper.authorized?(:controller => "bar",
80
+ :action => "baz",
81
+ :id => 23).should == false
82
+ end
83
+
84
+ it "on a record level" do
85
+ helper.authorized?(:controller => "wire",
86
+ :action => "vinyl",
87
+ :id => 1).should == false
88
+ end
89
+ end
90
+ end
@@ -0,0 +1 @@
1
+ require 'logical_authz/spec_helper'
@@ -0,0 +1,30 @@
1
+ module LogicalAuthz
2
+ if defined?(:AuthnFacade)
3
+ remove_const(:AuthnFacade)
4
+ end
5
+ module AuthnFacade
6
+ @@current_user = nil
7
+
8
+ def self.current_user(controller)
9
+ @@current_user
10
+ end
11
+
12
+ def self.current_user=(user)
13
+ @@current_user = user
14
+ end
15
+ end
16
+
17
+ module MockAuth
18
+ def logout
19
+ AuthnFacade.current_user = nil
20
+ end
21
+
22
+ def login_as(user)
23
+ if <%= user_class %> === user
24
+ AuthnFacade.current_user = user
25
+ else
26
+ AuthnFacade.current_user = Factory(user)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,75 @@
1
+ module LogicalAuthz
2
+ module Matcher
3
+ class Authorized
4
+ def initialize
5
+ @controller = nil
6
+ end
7
+
8
+ def match_state
9
+ "authorized"
10
+ end
11
+
12
+ def check_authorization_flag
13
+ return false unless @flash.has_key? :logical_authz_record
14
+ return true if @flash[:logical_authz_record][:result] == true
15
+ return false
16
+ end
17
+
18
+ def matches?(controller)
19
+ @controller = controller
20
+ @flash = controller.__send__(:flash)
21
+ #controller should be a controller
22
+ return check_authorization_flag
23
+ end
24
+
25
+ def failure_message(match_text)
26
+ if @flash.has_key? :logical_authz_record
27
+ laz_rec = @flash[:logical_authz_record]
28
+ "Expected #{@controller.class.name}(#{@controller.params.inspect})" +
29
+ " #{match_text} #{match_state}, but flash[:logical_authz_record][:result] " +
30
+ "is <#{laz_rec[:result].inspect}> (reason: #{laz_rec[:reason].inspect}, " +
31
+ "rule: #{laz_rec[:determining_rule].try(:name)})"
32
+ else
33
+ "Expected #{@controller.class.name}(#{@controller.params.inspect}) #{match_text} #{match_state}, but flash did not have key :logical_authz_record"
34
+ end
35
+ end
36
+
37
+ def failure_message_for_should
38
+ failure_message("to be")
39
+ end
40
+
41
+ def failure_message_for_should_not
42
+ failure_message("not to be")
43
+ end
44
+ end
45
+
46
+ class Forbidden < Authorized
47
+ def match_state
48
+ "forbidden"
49
+ end
50
+
51
+ def check_authorization_flag
52
+ return false unless @flash.has_key? :logical_authz_record
53
+ return true if @flash[:logical_authz_record][:result] == false
54
+ return false
55
+ end
56
+ end
57
+ end
58
+
59
+
60
+ module ControllerExampleGroupMixin
61
+ def be_authorized
62
+ return Matcher::Authorized.new
63
+ end
64
+
65
+ def be_forbidden
66
+ return Matcher::Forbidden.new
67
+ end
68
+ end
69
+ end
70
+
71
+ module RSpec::Rails
72
+ module ControllerExampleGroup
73
+ include LogicalAuthz::ControllerExampleGroupMixin
74
+ end
75
+ end
@@ -0,0 +1,110 @@
1
+ require 'logical_authz/access_control'
2
+ require 'logical_authz/application'
3
+ require 'logical_authz/configuration'
4
+
5
+ module LogicalAuthz
6
+ PermissionSelect = "controller = :controller AND " +
7
+ "group_id IN (:group_ids) AND " +
8
+ "((action IS NULL AND subject_id IS NULL) OR " +
9
+ "(action IN (:action_names) AND " +
10
+ "(subject_id IS NULL OR subject_id = :subject_id)))"
11
+
12
+ class << self
13
+ def inspect_criteria(criteria)
14
+ criteria.inject({}) do |hash, name_value|
15
+ name, value = *name_value
16
+ case value
17
+ when ActiveRecord::Base
18
+ hash[name] = {value.class.name => value.id}
19
+ when ActionController::Base
20
+ hash[name] = value.class
21
+ else
22
+ hash[name] = value
23
+ end
24
+
25
+ hash
26
+ end.inspect
27
+ end
28
+
29
+ def find_controller(reference)
30
+ klass = nil
31
+
32
+ case reference
33
+ when Class
34
+ if LogicalAuthz::Application > reference
35
+ klass = reference
36
+ end
37
+ when LogicalAuthz::Application
38
+ klass = reference.class
39
+ when String, Symbol
40
+ klass_name = reference.to_s.camelize + "Controller"
41
+ begin
42
+ klass = klass_name.constantize
43
+ rescue NameError
44
+ end
45
+ end
46
+
47
+ return klass
48
+ end
49
+
50
+ def check_controller(klass, from_criteria)
51
+ if klass.nil?
52
+ raise "Could not determine controller class - criteria[:controller] => #{from_criteria}"
53
+ end
54
+ end
55
+
56
+ def check_permitted(criteria)
57
+ select_on = {
58
+ :group_ids => criteria[:group].map {|grp| grp.id},
59
+ :controller => criteria[:controller_path],
60
+ :action_names => criteria[:action_aliases].map {|a| a.to_s},
61
+ :subject_id => criteria[:id]
62
+ }
63
+
64
+ laz_debug{ "LogicalAuthz: checking permissions: #{select_on.inspect}" }
65
+ allowed = LogicalAuthz::Configuration::permission_model.exists?([PermissionSelect, select_on])
66
+ unless allowed
67
+ laz_debug{ "Denied: #{select_on.inspect}"}
68
+ else
69
+ laz_debug{ "Allowed: #{select_on.inspect}"}
70
+ end
71
+ return allowed
72
+ end
73
+
74
+
75
+ def is_authorized?(criteria=nil, authz_record=nil)
76
+ criteria ||= {}
77
+ authz_record ||= {}
78
+ authz_record.merge! :criteria => criteria, :result => nil, :reason => nil
79
+
80
+ laz_debug{"LogicalAuthz: asked to authorize #{inspect_criteria(criteria)}"}
81
+
82
+ controller_class = find_controller(criteria[:controller])
83
+
84
+ laz_debug{"LogicalAuthz: determined controller: #{controller_class.name}"}
85
+
86
+ check_controller(controller_class, criteria[:controller])
87
+
88
+ unless controller_class.authorization_needed?(criteria[:action])
89
+ laz_debug{"LogicalAuthz: controller says no authz needed."}
90
+ authz_record.merge! :reason => :no_authorization_needed, :result => true
91
+ else
92
+ laz_debug{"LogicalAuthz: checking authorization"}
93
+
94
+ controller_class.normalize_criteria(criteria)
95
+
96
+ #TODO Fail if group unspecified and user unspecified?
97
+
98
+ unless (acl_result = controller_class.check_acls(criteria, authz_record)).nil?
99
+ authz_record[:result] = acl_result
100
+ else
101
+ authz_record.merge! :reason => :default, :result => controller_class.default_authorization
102
+ end
103
+ end
104
+
105
+ laz_debug{authz_record}
106
+
107
+ return authz_record[:result]
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,15 @@
1
+ require 'rake'
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+
5
+ namespace :logical_authz do
6
+ desc 'Run the specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |t|
8
+ t.pattern = File::expand_path("../../../spec/**/*_spec.rb", __FILE__)
9
+ end
10
+ end
11
+
12
+ task :spec => 'logical_authz:spec'
13
+ rescue LoadError
14
+ warn "Not defining logical_authz:spec"
15
+ end
@@ -0,0 +1,17 @@
1
+ puts Dir::pwd
2
+ require 'test/unit'
3
+ begin
4
+ require 'spec'
5
+ rescue LoadError
6
+ false
7
+ end
8
+
9
+ class RSpecTest < Test::Unit::TestCase
10
+ def test_that_rspec_is_available
11
+ assert_nothing_raised("\n\n * RSpec isn't available - please run: gem install rspec *\n\n"){ ::Spec }
12
+ end
13
+
14
+ def test_that_specs_pass
15
+ assert(system(*%w{spec -f e -p **/*.rb spec}),"\n\n * Specs failed *\n\n")
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ $" << File.expand_path(File.join(File.dirname(__FILE__), '..','..','..','..','app','controllers','authz_controller.rb'))
4
+
5
+
6
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','..','config','environment'))
7
+ require 'spec/rails'
8
+ require 'logical_authz/spec_helper'
9
+
10
+ # Requires supporting files with custom matchers and macros, etc,
11
+ # in ./support/ and its subdirectories.
12
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
13
+
14
+ plugin_spec_dir = File.dirname(__FILE__)
15
+ $: << File::join(plugin_spec_dir, "spec_helper", "models")
16
+ RSpec::Runner.configure do |config|
17
+ # If you're not using ActiveRecord you should remove these
18
+ # lines, delete config/database.yml and disable :active_record
19
+ # in your config/boot.rb
20
+ # config.use_transactional_fixtures = true
21
+ config.use_instantiated_fixtures = true
22
+ config.fixture_path = File::join(File.dirname(__FILE__), 'fixtures')
23
+ config.global_fixtures = [
24
+ :az_accounts, :groups, :permissions
25
+ ]
26
+ end
27
+
28
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
29
+
30
+ databases = YAML::load(IO.read(plugin_spec_dir + "/db/database.yml"))
31
+ ActiveRecord::Base.establish_connection(databases[ENV["DB"] || "sqlite3"])
32
+ load(File.join(plugin_spec_dir, "db", "schema.rb"))
33
+
34
+ require File::join(plugin_spec_dir, "mock_auth")
35
+ require File::join(plugin_spec_dir, "routes")
36
+
37
+ Dir.glob(File::join(plugin_spec_dir, "factories", "*.rb")) do |path|
38
+ require path
39
+ end
40
+
41
+
42
+ Group::member_class = AzAccount
43
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logical_authz
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 6
10
+ version: 0.1.6
11
+ platform: ruby
12
+ authors:
13
+ - Judson Lester
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-30 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: " LogicalAuthorization allows authorization in a finely grained framework, including\n ACLs and database based permissions, designed to slide into your project seamlessly.\n\n You should be able to add logical_authz to your Gemfile and add needs_authorization to\n your base controller class and be done.\n"
23
+ email: judson@lrdesign.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/tasks/rspec.rake
32
+ - lib/logical_authz.rb
33
+ - lib/logical_authz/configuration.rb
34
+ - lib/logical_authz/spec_helper.rb
35
+ - lib/logical_authz/generator.rb
36
+ - lib/logical_authz/authn_facade/authlogic.rb
37
+ - lib/logical_authz/generators/specs/generator.rb
38
+ - lib/logical_authz/generators/specs/templates/spec/factories/az_groups.rb
39
+ - lib/logical_authz/generators/specs/templates/spec/factories/az_accounts.rb
40
+ - lib/logical_authz/generators/specs/templates/spec/factories/permissions.rb
41
+ - lib/logical_authz/generators/specs/templates/spec/support/logical_authz.rb
42
+ - lib/logical_authz/generators/specs/templates/spec/support/mock_auth.rb
43
+ - lib/logical_authz/generators/specs/templates/spec/controllers/permissions_controller_spec.rb
44
+ - lib/logical_authz/generators/specs/templates/spec/controllers/groups_controller_spec.rb
45
+ - lib/logical_authz/generators/specs/templates/spec/controllers/groups_users_controller_spec.rb
46
+ - lib/logical_authz/generators/specs/templates/spec/helpers/logical_authz_helper_spec.rb
47
+ - lib/logical_authz/generators/controllers/generator.rb
48
+ - lib/logical_authz/generators/controllers/templates/app/controllers/authz_controller.rb
49
+ - lib/logical_authz/generators/models/generator.rb
50
+ - lib/logical_authz/generators/models/templates/db/seeds_logical_authz.rb
51
+ - lib/logical_authz/generators/models/templates/app/models/group.rb
52
+ - lib/logical_authz/generators/models/templates/app/models/permission.rb
53
+ - lib/logical_authz/generators/models/templates/config/initializers/logical_authz.rb
54
+ - lib/logical_authz/generators/models/templates/migrations/create_users_groups.rb
55
+ - lib/logical_authz/generators/models/templates/migrations/create_groups.rb
56
+ - lib/logical_authz/generators/models/templates/migrations/create_permissions.rb
57
+ - lib/logical_authz/generators/routes/generator.rb
58
+ - lib/logical_authz/engine.rb
59
+ - lib/logical_authz/access_control.rb
60
+ - lib/logical_authz/application.rb
61
+ - app/views/permissions/index.html.haml
62
+ - app/views/permissions/create.rjs
63
+ - app/views/permissions/new.html.haml
64
+ - app/views/permissions/_controls.html.haml
65
+ - app/views/permissions/_form.html.haml
66
+ - app/views/permissions/edit.html.haml
67
+ - app/views/groups/index.html.haml
68
+ - app/views/groups/create.rjs
69
+ - app/views/groups/new.html.haml
70
+ - app/views/groups/_controls.html.haml
71
+ - app/views/groups/_form.html.haml
72
+ - app/views/groups/edit.html.haml
73
+ - app/views/groups/show.html.haml
74
+ - app/controllers/groups_controller.rb
75
+ - app/controllers/permissions_controller.rb
76
+ - app/controllers/groups_users_controller.rb
77
+ - app/helpers/logical_authz_helper.rb
78
+ - config/initializers/activate.rb
79
+ - generators/logical_authz_specs/logical_authz_specs_generator.rb
80
+ - generators/logical_authz/logical_authz_generator.rb
81
+ - generators/logical_authz/templates/app/views/layouts/_explain_authz.html.haml.erb
82
+ - generators/logical_authz/templates/app/controllers/authz_controller.rb.erb
83
+ - generators/logical_authz/templates/README
84
+ - generators/logical_authz_models/logical_authz_models_generator.rb
85
+ - generators/logical_authz_routes/logical_authz_routes_generator.rb
86
+ - spec/spec_helper.rb
87
+ - spec/gem_test_suite.rb
88
+ has_rdoc: true
89
+ homepage: http://lrdesign.com/tools
90
+ licenses: []
91
+
92
+ post_install_message: Another tidy package brought to you by Logical Reality Design
93
+ rdoc_options:
94
+ - --inline-source
95
+ - --main
96
+ - doc/README
97
+ - --title
98
+ - logical_authz-0.1.6 RDoc
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: logical_authz
122
+ rubygems_version: 1.3.7
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Full fledged authorization, starting from one line
126
+ test_files:
127
+ - spec/gem_test_suite.rb