zuul 0.1.1 → 0.2.0

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 (80) hide show
  1. data/lib/generators/zuul/orm_helpers.rb +21 -0
  2. data/lib/generators/zuul/permission_generator.rb +57 -0
  3. data/lib/generators/zuul/permission_role_generator.rb +40 -0
  4. data/lib/generators/zuul/permission_subject_generator.rb +40 -0
  5. data/lib/generators/zuul/role_generator.rb +58 -0
  6. data/lib/generators/zuul/role_subject_generator.rb +40 -0
  7. data/lib/generators/zuul/subject_generator.rb +39 -0
  8. data/lib/generators/zuul/templates/permission.rb +18 -0
  9. data/lib/generators/zuul/templates/permission_existing.rb +25 -0
  10. data/lib/generators/zuul/templates/permission_role.rb +17 -0
  11. data/lib/generators/zuul/templates/permission_role_existing.rb +24 -0
  12. data/lib/generators/zuul/templates/permission_subject.rb +17 -0
  13. data/lib/generators/zuul/templates/permission_subject_existing.rb +24 -0
  14. data/lib/generators/zuul/templates/role.rb +20 -0
  15. data/lib/generators/zuul/templates/role_existing.rb +27 -0
  16. data/lib/generators/zuul/templates/role_subject.rb +17 -0
  17. data/lib/generators/zuul/templates/role_subject_existing.rb +24 -0
  18. data/lib/tasks/zuul.rake +56 -0
  19. data/lib/zuul.rb +14 -5
  20. data/lib/zuul/action_controller.rb +108 -0
  21. data/lib/zuul/action_controller/dsl.rb +384 -0
  22. data/lib/zuul/action_controller/evaluators.rb +60 -0
  23. data/lib/zuul/active_record.rb +338 -0
  24. data/lib/zuul/active_record/context.rb +38 -0
  25. data/lib/zuul/active_record/permission.rb +31 -0
  26. data/lib/zuul/active_record/permission_role.rb +29 -0
  27. data/lib/zuul/active_record/permission_subject.rb +29 -0
  28. data/lib/zuul/active_record/role.rb +117 -0
  29. data/lib/zuul/active_record/role_subject.rb +29 -0
  30. data/lib/zuul/active_record/scope.rb +71 -0
  31. data/lib/zuul/active_record/subject.rb +239 -0
  32. data/lib/zuul/configuration.rb +149 -0
  33. data/lib/zuul/context.rb +53 -0
  34. data/lib/zuul/exceptions.rb +3 -0
  35. data/lib/zuul/exceptions/access_denied.rb +9 -0
  36. data/lib/zuul/exceptions/invalid_context.rb +9 -0
  37. data/lib/zuul/exceptions/undefined_scope.rb +9 -0
  38. data/lib/zuul/railtie.rb +5 -0
  39. data/lib/zuul/version.rb +3 -0
  40. data/lib/zuul_viz.rb +195 -0
  41. data/spec/db/schema.rb +172 -0
  42. data/spec/spec_helper.rb +25 -0
  43. data/spec/support/capture_stdout.rb +12 -0
  44. data/spec/support/models.rb +167 -0
  45. data/spec/zuul/active_record/context_spec.rb +55 -0
  46. data/spec/zuul/active_record/permission_role_spec.rb +84 -0
  47. data/spec/zuul/active_record/permission_spec.rb +174 -0
  48. data/spec/zuul/active_record/permission_subject_spec.rb +84 -0
  49. data/spec/zuul/active_record/role_spec.rb +694 -0
  50. data/spec/zuul/active_record/role_subject_spec.rb +84 -0
  51. data/spec/zuul/active_record/scope_spec.rb +75 -0
  52. data/spec/zuul/active_record/subject_spec.rb +1186 -0
  53. data/spec/zuul/active_record_spec.rb +624 -0
  54. data/spec/zuul/configuration_spec.rb +254 -0
  55. data/spec/zuul/context_spec.rb +128 -0
  56. data/spec/zuul_spec.rb +15 -0
  57. metadata +181 -70
  58. data/.document +0 -5
  59. data/.gitignore +0 -23
  60. data/LICENSE +0 -20
  61. data/README.rdoc +0 -65
  62. data/Rakefile +0 -54
  63. data/VERSION +0 -1
  64. data/lib/zuul/restrict_access.rb +0 -104
  65. data/lib/zuul/valid_roles.rb +0 -37
  66. data/spec/rails_root/app/controllers/application_controller.rb +0 -2
  67. data/spec/rails_root/app/models/user.rb +0 -8
  68. data/spec/rails_root/config/boot.rb +0 -110
  69. data/spec/rails_root/config/database.yml +0 -5
  70. data/spec/rails_root/config/environment.rb +0 -7
  71. data/spec/rails_root/config/environments/test.rb +0 -7
  72. data/spec/rails_root/config/initializers/session_store.rb +0 -15
  73. data/spec/rails_root/config/routes.rb +0 -4
  74. data/spec/rails_root/db/test.sqlite3 +0 -0
  75. data/spec/rails_root/log/test.log +0 -5388
  76. data/spec/rails_root/spec/controllers/require_user_spec.rb +0 -138
  77. data/spec/rails_root/spec/controllers/restrict_access_spec.rb +0 -64
  78. data/spec/rails_root/spec/models/user_spec.rb +0 -37
  79. data/spec/rails_root/spec/spec_helper.rb +0 -34
  80. data/zuul.gemspec +0 -78
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,23 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
22
- spec/rails_root/db/test.sqlite3
23
- spec/rails_root/log/*
data/LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2009 Wes Gibbs
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc DELETED
@@ -1,65 +0,0 @@
1
- =Zuul
2
-
3
- Zuul provides a simple role-based authorization framework for Rails apps.
4
-
5
- ==Quick Start
6
-
7
- Zuul expects that you have a +current_user+ method available.
8
-
9
- Add a +role+ to your +users+ table.
10
-
11
- add_column :users, :role, :string
12
-
13
- In your +User+ model, specify the valid roles.
14
-
15
- valid_roles :guest, :member, :admin
16
-
17
- In your +ApplicationController+, enable access restrictions.
18
-
19
- include Zuul::RestrictAccess
20
- restrict_access
21
-
22
- In your controllers, specify which roles are allowed for which actions.
23
-
24
- require_user :guest, :admin, :only => :index, :show
25
-
26
- ==Examples and Options
27
-
28
- You can pass +restrict_access+ some options
29
- * +access_denied_message+ - The string that will be added to the flash[:notice] if the user has been denied access to an action. Defaults to "You must be logged in to access this page".
30
- * +require_no_user_message+ - The string that will be added to the flash[:notice] if the requested action requires there be NO user signed in and there is one. Defaults to "You must be logged out to access this page".
31
- * +unauthorized_redirect_path+ - The name of a method, as a symbol, that will be called to determine where to redirect someone when they have been denied access. The method is expected to return a string. The default is :unauthorized_path which returns "/".
32
-
33
- You can pass +require_user+ a list of roles and also indicate which actions to apply the restriction to using <code>:only</code> and <code>:except</code>. Some examples:
34
- * Restrict access to all actions for a specific role.
35
-
36
- <code>require_user :admin</code>
37
-
38
- * Restrict access to specific actions for specific roles.
39
-
40
- <code>require_user :guest, :admin, :only => :index, :show</code>
41
-
42
- * Require a user but don't care about the role.
43
-
44
- <code>require_user :only => :show</code>
45
-
46
- * Don't allow access to edit or update if there is a user.
47
-
48
- <code>require_no_user :only => :edit, :update</code>
49
-
50
- == Credits
51
-
52
- Thanks to {Les Hill}[http://github.com/leshill] for help testing the +ApplicationController+ mixins.
53
-
54
- == Note on Patches/Pull Requests
55
-
56
- * Fork the project.
57
- * Make your feature addition or bug fix.
58
- * Add tests for it. This is important so I don't break it in a
59
- future version unintentionally.
60
- * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
61
- * Send me a pull request. Bonus points for topic branches.
62
-
63
- == Copyright
64
-
65
- Copyright (c) 2009 Wes Gibbs. See LICENSE for details.
data/Rakefile DELETED
@@ -1,54 +0,0 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "zuul"
8
- gem.summary = %Q{Simple Rails Authorization}
9
- gem.description = %Q{A simple authorization solution for Rails apps.}
10
- gem.email = "wes@hashrocket.com"
11
- gem.homepage = "http://github.com/wgibbs/zuul"
12
- gem.authors = ["Wes Gibbs"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
- gem.test_files = ["spec/**/*"]
15
- gem.files.exclude "spec/rails_root/**/*"
16
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
- end
18
- Jeweler::GemcutterTasks.new
19
- rescue LoadError
20
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
- end
22
-
23
- require 'rake/testtask'
24
- Rake::TestTask.new(:test) do |test|
25
- test.libs << 'lib' << 'test'
26
- test.pattern = 'teset/rails_root/test/**/test_*.rb'
27
- test.verbose = true
28
- end
29
-
30
- require 'spec/rake/spectask'
31
- Spec::Rake::SpecTask.new(:spec) do |spec|
32
- spec.libs << 'lib' << 'spec'
33
- spec.spec_files = FileList['spec/rails_root/spec/**/*_spec.rb']
34
- end
35
-
36
- Spec::Rake::SpecTask.new(:rcov) do |spec|
37
- spec.libs << 'lib' << 'spec'
38
- spec.pattern = 'spec/**/*_spec.rb'
39
- spec.rcov = true
40
- end
41
-
42
- task :spec => :check_dependencies
43
-
44
- task :default => :spec
45
-
46
- require 'rake/rdoctask'
47
- Rake::RDocTask.new do |rdoc|
48
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
-
50
- rdoc.rdoc_dir = 'rdoc'
51
- rdoc.title = "zuul #{version}"
52
- rdoc.rdoc_files.include('README*')
53
- rdoc.rdoc_files.include('lib/**/*.rb')
54
- end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.1
@@ -1,104 +0,0 @@
1
- module Zuul
2
- module RestrictAccess
3
- def self.included(controller)
4
- controller.extend(ClassMethods)
5
- end
6
-
7
- module ClassMethods
8
- def self.extended(klass)
9
- klass.cattr_accessor :access_denied_message
10
- klass.cattr_accessor :require_no_user_message
11
- klass.cattr_accessor :unauthorized_redirect_path
12
- end
13
-
14
- # Meant to be called from your controllers. This is
15
- # where you define which roles have access to which actions in the
16
- # controller. Examples:
17
- # * <code>require_user :admin</code>: Restrict access to all actions for a specific role.
18
- # * <code>require_user :guest, :admin, :only => :index, :show</code>: Restrict access to specific actions for specific roles.
19
- # * <code>require_user :only => :show</code>: Require a user but don't care about the role.
20
- def require_user(*roles)
21
- options = roles.extract_options!
22
- self.before_filter options do |controller|
23
- controller.send(:require_user, roles)
24
- end
25
- end
26
-
27
- # Tells its controller to check that there is no user
28
- # before allowing someone into an action. For example:
29
- # * <code>require_no_user :only => :edit, :update</code>: Don't allow access to the edit action
30
- # if there is a user.
31
- def require_no_user(options = {})
32
- self.before_filter options do |controller|
33
- controller.send(:require_no_user)
34
- end
35
- end
36
-
37
- # Intended to be called by ApplicationController. It mixes
38
- # in a set of instance methods that manage conferring or denying access to actions.
39
- # You can customize the behavior when a user is denied access with these
40
- # options:
41
- # * +access_denied_message+: The string that will be added to the
42
- # flash[:notice] if the user has been denied access to an action.
43
- # Defaults to "You must be logged in to access this page".
44
- # * +require_no_user_message+: The string that will be added to the
45
- # flash[:notice] if the requested action requires there be NO user signed
46
- # in and there is one. Defaults to "You must be logged out to access this
47
- # page.".
48
- # * +unauthorized_redirect_path+: The name of a method, as a symbol, that
49
- # will be called to determine where to redirect someone when they have
50
- # been denied access. The method is expected to return a string. The
51
- # default is :unauthorized_path which returns "/".
52
- def restrict_access(options = {})
53
- self.access_denied_message = options[:access_denied_message] || "You must be logged in to access this page"
54
- self.require_no_user_message = options[:require_no_user_message] || "You must be logged out to access this page"
55
- self.unauthorized_redirect_path = options[:unauthorized_redirect_path] || :unauthorized_path
56
- include InstanceMethods
57
- end
58
- end
59
-
60
- module InstanceMethods
61
- def require_user(*roles)
62
- roles.flatten!
63
- return true if current_user && roles.empty?
64
- deny_access unless roles.any? do |role|
65
- method = (role.to_s + "?").to_sym
66
- if current_user && current_user.respond_to?(method)
67
- current_user.send(method)
68
- else
69
- false
70
- end
71
- end
72
- end
73
- private :require_user
74
-
75
- def require_no_user
76
- if current_user
77
- store_location
78
- flash[:notice] = self.class.require_no_user_message
79
- redirect_to send(self.class.unauthorized_redirect_path)
80
- return false
81
- end
82
- end
83
- private :require_no_user
84
-
85
- def deny_access
86
- store_location
87
- flash[:notice] = self.class.access_denied_message
88
- redirect_to send(self.class.unauthorized_redirect_path)
89
- return false
90
- end
91
- private :deny_access
92
-
93
- def store_location
94
- session[:return_to] = request.request_uri
95
- end
96
- private :store_location
97
-
98
- def unauthorized_path
99
- "/"
100
- end
101
- private :unauthorized_path
102
- end
103
- end
104
- end
@@ -1,37 +0,0 @@
1
- module Zuul
2
- module ValidRoles
3
- def self.included(base)
4
- base.extend ClassMethods
5
- end
6
-
7
- module ClassMethods
8
- def valid_roles(*roles)
9
- attr_protected :role
10
- write_inheritable_attribute(:roles, roles)
11
- include InstanceMethods
12
- end
13
- end
14
-
15
- module InstanceMethods
16
- def self.included(base)
17
- base.read_inheritable_attribute(:roles).each do |role|
18
- class_eval <<-CODE
19
- def #{role}?
20
- self.role.to_sym == :#{role}
21
- end
22
- CODE
23
- end
24
- end
25
-
26
- def role
27
- role_name = read_attribute(:role)
28
- role_name && role_name.to_sym
29
- end
30
-
31
- def role=(role)
32
- return unless self.class.read_inheritable_attribute(:roles).include?(role.to_sym)
33
- write_attribute(:role, role.to_s)
34
- end
35
- end
36
- end
37
- end
@@ -1,2 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- end
@@ -1,8 +0,0 @@
1
- require 'zuul'
2
- class User < ActiveRecord::Base
3
- include Zuul::ValidRoles
4
-
5
- validates_presence_of :first_name, :last_name, :email
6
-
7
- valid_roles :guest, :member, :admin
8
- end
@@ -1,110 +0,0 @@
1
- # Don't change this file!
2
- # Configure your app in config/environment.rb and config/environments/*.rb
3
-
4
- RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
-
6
- module Rails
7
- class << self
8
- def boot!
9
- unless booted?
10
- preinitialize
11
- pick_boot.run
12
- end
13
- end
14
-
15
- def booted?
16
- defined? Rails::Initializer
17
- end
18
-
19
- def pick_boot
20
- (vendor_rails? ? VendorBoot : GemBoot).new
21
- end
22
-
23
- def vendor_rails?
24
- File.exist?("#{RAILS_ROOT}/vendor/rails")
25
- end
26
-
27
- def preinitialize
28
- load(preinitializer_path) if File.exist?(preinitializer_path)
29
- end
30
-
31
- def preinitializer_path
32
- "#{RAILS_ROOT}/config/preinitializer.rb"
33
- end
34
- end
35
-
36
- class Boot
37
- def run
38
- load_initializer
39
- Rails::Initializer.run(:set_load_path)
40
- end
41
- end
42
-
43
- class VendorBoot < Boot
44
- def load_initializer
45
- require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
- Rails::Initializer.run(:install_gem_spec_stubs)
47
- Rails::GemDependency.add_frozen_gem_path
48
- end
49
- end
50
-
51
- class GemBoot < Boot
52
- def load_initializer
53
- self.class.load_rubygems
54
- load_rails_gem
55
- require 'initializer'
56
- end
57
-
58
- def load_rails_gem
59
- if version = self.class.gem_version
60
- gem 'rails', version
61
- else
62
- gem 'rails'
63
- end
64
- rescue Gem::LoadError => load_error
65
- $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
- exit 1
67
- end
68
-
69
- class << self
70
- def rubygems_version
71
- Gem::RubyGemsVersion rescue nil
72
- end
73
-
74
- def gem_version
75
- if defined? RAILS_GEM_VERSION
76
- RAILS_GEM_VERSION
77
- elsif ENV.include?('RAILS_GEM_VERSION')
78
- ENV['RAILS_GEM_VERSION']
79
- else
80
- parse_gem_version(read_environment_rb)
81
- end
82
- end
83
-
84
- def load_rubygems
85
- require 'rubygems'
86
- min_version = '1.3.1'
87
- unless rubygems_version >= min_version
88
- $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
- exit 1
90
- end
91
-
92
- rescue LoadError
93
- $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
- exit 1
95
- end
96
-
97
- def parse_gem_version(text)
98
- $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
- end
100
-
101
- private
102
- def read_environment_rb
103
- File.read("#{RAILS_ROOT}/config/environment.rb")
104
- end
105
- end
106
- end
107
- end
108
-
109
- # All that for this:
110
- Rails.boot!