lockdown 1.6.5 → 2.0.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/.gitignore +1 -0
  2. data/README.txt +8 -5
  3. data/Rakefile +43 -26
  4. data/lib/lockdown/access.rb +108 -0
  5. data/lib/lockdown/configuration.rb +201 -0
  6. data/lib/lockdown/database.rb +31 -36
  7. data/lib/lockdown/delivery.rb +26 -0
  8. data/lib/lockdown/errors.rb +3 -7
  9. data/lib/lockdown/frameworks/rails/controller.rb +21 -59
  10. data/lib/lockdown/frameworks/rails/view.rb +1 -1
  11. data/lib/lockdown/frameworks/rails.rb +7 -43
  12. data/lib/lockdown/helper.rb +14 -85
  13. data/lib/lockdown/orms/active_record.rb +7 -9
  14. data/lib/lockdown/permission.rb +37 -203
  15. data/lib/lockdown/resource.rb +54 -0
  16. data/lib/lockdown/session.rb +16 -25
  17. data/lib/lockdown/user_group.rb +16 -0
  18. data/lib/lockdown.rb +15 -60
  19. data/lockdown.gemspec +29 -69
  20. data/test/helper.rb +9 -0
  21. data/test/lockdown/test_access.rb +80 -0
  22. data/test/lockdown/test_configuration.rb +194 -0
  23. data/test/lockdown/test_delivery.rb +163 -0
  24. data/test/lockdown/test_helper.rb +33 -0
  25. data/test/lockdown/test_permission.rb +73 -0
  26. data/test/lockdown/test_resource.rb +47 -0
  27. data/test/lockdown/test_session.rb +31 -0
  28. data/test/lockdown/test_user_group.rb +17 -0
  29. data/test/test_lockdown.rb +11 -0
  30. metadata +41 -78
  31. data/lib/lockdown/context.rb +0 -41
  32. data/lib/lockdown/references.rb +0 -19
  33. data/lib/lockdown/rspec_helper.rb +0 -118
  34. data/lib/lockdown/rules.rb +0 -372
  35. data/lib/lockdown/system.rb +0 -58
  36. data/rails_generators/lockdown/lockdown_generator.rb +0 -274
  37. data/rails_generators/lockdown/templates/app/controllers/permissions_controller.rb +0 -22
  38. data/rails_generators/lockdown/templates/app/controllers/sessions_controller.rb +0 -39
  39. data/rails_generators/lockdown/templates/app/controllers/user_groups_controller.rb +0 -122
  40. data/rails_generators/lockdown/templates/app/controllers/users_controller.rb +0 -117
  41. data/rails_generators/lockdown/templates/app/helpers/permissions_helper.rb +0 -2
  42. data/rails_generators/lockdown/templates/app/helpers/user_groups_helper.rb +0 -2
  43. data/rails_generators/lockdown/templates/app/helpers/users_helper.rb +0 -2
  44. data/rails_generators/lockdown/templates/app/models/permission.rb +0 -13
  45. data/rails_generators/lockdown/templates/app/models/profile.rb +0 -10
  46. data/rails_generators/lockdown/templates/app/models/user.rb +0 -95
  47. data/rails_generators/lockdown/templates/app/models/user_group.rb +0 -15
  48. data/rails_generators/lockdown/templates/app/views/permissions/index.html.erb +0 -16
  49. data/rails_generators/lockdown/templates/app/views/permissions/show.html.erb +0 -26
  50. data/rails_generators/lockdown/templates/app/views/sessions/new.html.erb +0 -12
  51. data/rails_generators/lockdown/templates/app/views/user_groups/edit.html.erb +0 -33
  52. data/rails_generators/lockdown/templates/app/views/user_groups/index.html.erb +0 -20
  53. data/rails_generators/lockdown/templates/app/views/user_groups/new.html.erb +0 -31
  54. data/rails_generators/lockdown/templates/app/views/user_groups/show.html.erb +0 -29
  55. data/rails_generators/lockdown/templates/app/views/users/edit.html.erb +0 -51
  56. data/rails_generators/lockdown/templates/app/views/users/index.html.erb +0 -22
  57. data/rails_generators/lockdown/templates/app/views/users/new.html.erb +0 -50
  58. data/rails_generators/lockdown/templates/app/views/users/show.html.erb +0 -33
  59. data/rails_generators/lockdown/templates/config/initializers/lockit.rb +0 -1
  60. data/rails_generators/lockdown/templates/db/migrate/create_admin_user.rb +0 -17
  61. data/rails_generators/lockdown/templates/db/migrate/create_permissions.rb +0 -19
  62. data/rails_generators/lockdown/templates/db/migrate/create_profiles.rb +0 -26
  63. data/rails_generators/lockdown/templates/db/migrate/create_user_groups.rb +0 -19
  64. data/rails_generators/lockdown/templates/db/migrate/create_users.rb +0 -17
  65. data/rails_generators/lockdown/templates/lib/lockdown/README +0 -42
  66. data/rails_generators/lockdown/templates/lib/lockdown/init.rb +0 -131
  67. data/spec/lockdown/context_spec.rb +0 -191
  68. data/spec/lockdown/database_spec.rb +0 -162
  69. data/spec/lockdown/frameworks/rails/controller_spec.rb +0 -215
  70. data/spec/lockdown/frameworks/rails/view_spec.rb +0 -96
  71. data/spec/lockdown/frameworks/rails_spec.rb +0 -163
  72. data/spec/lockdown/permission_spec.rb +0 -156
  73. data/spec/lockdown/rspec_helper_spec.rb +0 -40
  74. data/spec/lockdown/rules_spec.rb +0 -245
  75. data/spec/lockdown/session_spec.rb +0 -112
  76. data/spec/lockdown/system_spec.rb +0 -51
  77. data/spec/lockdown_spec.rb +0 -19
  78. data/spec/rcov.opts +0 -5
  79. data/spec/spec.opts +0 -3
  80. data/spec/spec_helper.rb +0 -8
@@ -2,31 +2,19 @@ module Lockdown
2
2
  module Frameworks
3
3
  module Rails
4
4
  module Controller
5
-
6
- def available_actions(klass)
7
- klass.action_methods
8
- end
9
-
10
- def controller_name(klass)
11
- klass.controller_name
12
- end
13
-
14
5
  # Locking methods
15
6
  module Lock
16
7
 
17
8
  def configure_lockdown
18
- Lockdown.maybe_parse_init
19
- check_session_expiry
20
9
  store_location
21
10
  end
22
11
 
23
12
  # Basic auth functionality needs to be reworked as
24
13
  # Lockdown doesn't provide authentication functionality.
25
14
  def set_current_user
26
- #login_from_basic_auth? unless logged_in?
27
15
  if logged_in?
28
- Thread.current[:who_did_it] = Lockdown::System.
29
- call(self, :who_did_it)
16
+ whodat = send(Lockdown::Configuration.who_did_it)
17
+ Thread.current[:who_did_it] = whodat
30
18
  end
31
19
  end
32
20
 
@@ -39,19 +27,6 @@ module Lockdown
39
27
 
40
28
  protected
41
29
 
42
- def path_allowed?(url)
43
- session[:access_rights] ||= Lockdown::System.public_access
44
- session[:access_rights].include?(url)
45
- end
46
-
47
- def check_session_expiry
48
- if session[:expiry_time] && session[:expiry_time] < Time.now
49
- nil_lockdown_values
50
- Lockdown::System.call(self, :session_timeout_method)
51
- end
52
- session[:expiry_time] = Time.now + Lockdown::System.fetch(:session_timeout)
53
- end
54
-
55
30
  def store_location
56
31
  if (request.method == :get) && (session[:thispage] != sent_from_uri)
57
32
  session[:prevpage] = session[:thispage] || ''
@@ -69,25 +44,30 @@ module Lockdown
69
44
 
70
45
  return false unless url
71
46
 
72
- return true if current_user_is_admin?
73
-
74
47
  method ||= (params[:method] || request.method)
75
48
 
76
49
  url_parts = URI::split(url.strip)
77
50
 
78
51
  path = url_parts[5]
79
52
 
80
- subdir = Lockdown::System.fetch(:subdirectory)
81
- if subdir && subdir == path[1,subdir.length]
82
- path = path[(subdir.length+1)..-1]
53
+ if Lockdown::Delivery.allowed?(path, session[:access_rights])
54
+ return true
83
55
  end
84
56
 
85
- return true if path_allowed?(path)
86
-
87
57
  begin
88
- hash = ActionController::Routing::Routes.recognize_path(path, :method => method)
89
- return path_allowed?(path_from_hash(hash)) if hash
90
- rescue Exception => e
58
+ if ::Rails.respond_to?(:application)
59
+ router = ::Rails.application.routes
60
+ else
61
+ router = ActionController::Routing::Routes
62
+ end
63
+
64
+ hash = router.recognize_path(path, :method => method)
65
+
66
+ if hash
67
+ return Lockdown::Delivery.allowed?(path_from_hash(hash),
68
+ session[:access_rights])
69
+ end
70
+ rescue ActionController::RoutingError
91
71
  # continue on
92
72
  end
93
73
 
@@ -95,7 +75,7 @@ module Lockdown
95
75
  return true if url =~ /^mailto:/
96
76
 
97
77
  # Public file
98
- file = File.join(RAILS_ROOT, 'public', url)
78
+ file = File.join(::Rails.root, 'public', url)
99
79
  return true if File.exists?(file)
100
80
 
101
81
  # Passing in different domain
@@ -106,13 +86,13 @@ module Lockdown
106
86
 
107
87
  Lockdown.logger.info "Access denied: #{e}"
108
88
 
109
- if Lockdown::System.fetch(:logout_on_access_violation)
89
+ if Lockdown::Configuration.logout_on_access_violation
110
90
  reset_session
111
91
  end
112
92
  respond_to do |format|
113
93
  format.html do
114
94
  store_location
115
- redirect_to Lockdown::System.fetch(:access_denied_path)
95
+ redirect_to Lockdown::Configuration.access_denied_path
116
96
  return
117
97
  end
118
98
  format.xml do
@@ -125,8 +105,7 @@ module Lockdown
125
105
  end
126
106
 
127
107
  def path_from_hash(hash)
128
- subdir = Lockdown::System.fetch(:subdirectory)
129
- (subdir ? subdir + "/" : "") + hash[:controller].to_s + "/" + hash[:action].to_s
108
+ hash[:controller].to_s + "/" + hash[:action].to_s
130
109
  end
131
110
 
132
111
  def remote_url?(domain = nil)
@@ -141,23 +120,6 @@ module Lockdown
141
120
  redirect_to(session[:prevpage])
142
121
  end
143
122
  end
144
-
145
- # Called from current_user. Now, attempt to login by
146
- # basic authentication information.
147
- def login_from_basic_auth?
148
- username, passwd = get_auth_data
149
- if username && passwd
150
- set_session_user ::User.authenticate(username, passwd)
151
- end
152
- end
153
-
154
- @@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization)
155
- # gets BASIC auth info
156
- def get_auth_data
157
- auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
158
- auth_data = request.env[auth_key].to_s.split unless auth_key.blank?
159
- return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil]
160
- end
161
123
  end # Lock
162
124
  end # Controller
163
125
  end # Rails
@@ -42,7 +42,7 @@ module Lockdown
42
42
  def links(*lis)
43
43
  rvalue = []
44
44
  lis.each{|link| rvalue << link if link.length > 0 }
45
- rvalue.join( Lockdown::System.fetch(:link_separator) )
45
+ rvalue.join( Lockdown::Configuration.link_separator )
46
46
  end
47
47
  end # View
48
48
  end # Rails
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.join(File.dirname(__FILE__), "rails", "controller")
2
4
  require File.join(File.dirname(__FILE__), "rails", "view")
3
5
 
@@ -5,10 +7,6 @@ module Lockdown
5
7
  module Frameworks
6
8
  module Rails
7
9
  class << self
8
- def use_me?
9
- Object.const_defined?("ActionController") && ActionController.const_defined?("Base")
10
- end
11
-
12
10
  def included(mod)
13
11
  mod.extend Lockdown::Frameworks::Rails::Environment
14
12
  mixin
@@ -21,8 +19,10 @@ module Lockdown
21
19
  include Lockdown::Frameworks::Rails::View
22
20
  end
23
21
 
24
- Lockdown.system.class_eval do
25
- extend Lockdown::Frameworks::Rails::System
22
+ Lockdown::Configuration.class_eval do
23
+ def self.skip_sync?
24
+ skip_db_sync_in.include?(::Rails.env)
25
+ end
26
26
  end
27
27
  end
28
28
 
@@ -34,13 +34,12 @@ module Lockdown
34
34
 
35
35
  klass.helper_method :authorized?
36
36
 
37
- klass.hide_action(:set_current_user, :configure_lockdown, :check_request_authorization, :check_model_authorization)
37
+ klass.hide_action(:set_current_user, :configure_lockdown, :check_request_authorization)
38
38
 
39
39
  klass.before_filter do |c|
40
40
  c.set_current_user
41
41
  c.configure_lockdown
42
42
  c.check_request_authorization
43
- c.check_model_authorization
44
43
  end
45
44
 
46
45
  klass.filter_parameter_logging :password, :password_confirmation
@@ -55,10 +54,6 @@ module Lockdown
55
54
  ::RAILS_ROOT
56
55
  end
57
56
 
58
- def init_file
59
- "#{project_root}/lib/lockdown/init.rb"
60
- end
61
-
62
57
  def view_helper
63
58
  ::ActionView::Base
64
59
  end
@@ -76,38 +71,7 @@ module Lockdown
76
71
  def caching?
77
72
  ::Rails.configuration.cache_classes
78
73
  end
79
-
80
- # cache_classes is true in production and testing, need to
81
- # do an instance eval instead
82
- def add_controller_method(code)
83
- Lockdown.controller_parent.class_eval code, __FILE__,__LINE__ +1
84
- end
85
-
86
- def controller_class_name(str)
87
- str = "#{str}Controller"
88
- if str.include?("__")
89
- str.split("__").collect{|p| Lockdown.camelize(p)}.join("::")
90
- else
91
- Lockdown.camelize(str)
92
- end
93
- end
94
-
95
- def fetch_controller_class(str)
96
- eval("::#{controller_class_name(str)}")
97
- end
98
74
  end
99
-
100
- module System
101
- include Lockdown::Frameworks::Rails::Controller
102
-
103
- def skip_sync?
104
- Lockdown.system.fetch(:skip_db_sync_in).include?(framework_environment)
105
- end
106
-
107
- def framework_environment
108
- ::Rails.env
109
- end
110
- end # System
111
75
  end # Rails
112
76
  end # Frameworks
113
77
  end # Lockdown
@@ -1,111 +1,40 @@
1
- require 'active_support'
1
+ # encoding: utf-8
2
+
3
+ require 'active_support/core_ext'
2
4
 
3
5
  module Lockdown
4
6
  module Helper
5
- def class_name_from_file(str)
6
- str.split(".")[0].split("/").collect{|s| camelize(s) }.join("::")
7
+ # @return [Regexp] with \A \z boundaries
8
+ def regex(string)
9
+ Regexp.new(/\A#{string}\z/)
7
10
  end
8
11
 
9
- # If str_sym is a Symbol (:users), return "Users"
10
- # If str_sym is a String ("Users"), return :users
11
- def convert_reference_name(str_sym)
12
- if str_sym.is_a?(Symbol)
13
- titleize(str_sym)
14
- else
15
- str_sym.underscore.tr(' ','_').to_sym
16
- end
12
+ def administrator_group_name
13
+ 'Administrators'
17
14
  end
18
15
 
19
16
  def user_group_class
20
- eval(user_group_model_string)
17
+ eval("::#{Lockdown::Configuration.user_group_model}")
21
18
  end
22
19
 
23
20
  def user_groups_hbtm_reference
24
- user_group_model_string.underscore.pluralize.to_sym
21
+ Lockdown::Configuration.user_group_model.underscore.pluralize.to_sym
25
22
  end
26
23
 
27
24
  def user_group_id_reference
28
- user_group_model_string.underscore + "_id"
25
+ Lockdown::Configuration.user_group_model.underscore + "_id"
29
26
  end
30
27
 
31
28
  def user_class
32
- eval(user_model_string)
29
+ eval("::#{Lockdown::Configuration.user_model}")
33
30
  end
34
31
 
35
32
  def users_hbtm_reference
36
- user_model_string.underscore.pluralize.to_sym
33
+ Lockdown::Configuration.user_model.underscore.pluralize.to_sym
37
34
  end
38
35
 
39
36
  def user_id_reference
40
- user_model_string.underscore + "_id"
41
- end
42
-
43
- def user_group_model_string
44
- Lockdown.system.fetch(:user_group_model) || "UserGroup"
45
- end
46
-
47
- def user_model_string
48
- Lockdown.system.fetch(:user_model) || "User"
49
- end
50
-
51
- def get_string(value)
52
- if value.respond_to?(:name)
53
- string_name(value.name)
54
- else
55
- string_name(value)
56
- end
57
- end
58
-
59
- def get_symbol(value)
60
- if value.respond_to?(:name)
61
- symbol_name(value.name)
62
- elsif value.is_a?(String)
63
- symbol_name(value)
64
- else
65
- value
66
- end
67
- end
68
-
69
- def camelize(str)
70
- str.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
71
- end
72
-
73
- def random_string(len = 10)
74
- chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
75
- Array.new(len){||chars[rand(chars.size)]}.join
76
- end
77
-
78
- def administrator_group_string
79
- string_name(administrator_group_symbol)
80
- end
81
-
82
- def administrator_group_symbol
83
- :administrators
84
- end
85
-
86
- private
87
-
88
- def string_name(str_sym)
89
- str_sym.is_a?(Symbol) ? convert_reference_name(str_sym) : str_sym
90
- end
91
-
92
- def symbol_name(str_sym)
93
- str_sym.is_a?(String) ? convert_reference_name(str_sym) : str_sym
94
- end
95
-
96
- def titleize(str)
97
- humanize(underscore(str)).gsub(/\b([a-z])/) { $1.capitalize }
98
- end
99
-
100
- def humanize(str)
101
- str.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
102
- end
103
-
104
- def underscore(str)
105
- str.to_s.gsub(/::/, '/').
106
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
107
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
108
- tr("-", "_").downcase
37
+ Lockdown::Configuration.user_model.underscore + "_id"
109
38
  end
110
39
  end
111
40
  end
@@ -1,11 +1,9 @@
1
+ # encoding: utf-8
2
+
1
3
  module Lockdown
2
4
  module Orms
3
5
  module ActiveRecord
4
6
  class << self
5
- def use_me?
6
- Object.const_defined?("ActiveRecord") && ::ActiveRecord.const_defined?("Base")
7
- end
8
-
9
7
  def included(mod)
10
8
  mod.extend Lockdown::Orms::ActiveRecord::Helper
11
9
  mixin
@@ -51,15 +49,15 @@ module Lockdown
51
49
  end
52
50
 
53
51
  def create_with_stamps
54
- pid = current_who_did_it || Lockdown::System.fetch(:default_who_did_it)
55
- self[:created_by] = pid if self.respond_to?(:created_by)
56
- self[:updated_by] = pid if self.respond_to?(:updated_by)
52
+ pid = current_who_did_it || Lockdown::Configuration.default_who_did_it
53
+ self[:created_by] = pid if respond_to?(:created_by)
54
+ self[:updated_by] = pid if respond_to?(:updated_by)
57
55
  create_without_stamps
58
56
  end
59
57
 
60
58
  def update_with_stamps
61
- pid = current_who_did_it || Lockdown::System.fetch(:default_who_did_it)
62
- self[:updated_by] = pid if self.respond_to?(:updated_by)
59
+ pid = current_who_did_it || Lockdown::Configuration.default_who_did_it
60
+ self[:updated_by] = pid if respond_to?(:updated_by)
63
61
  update_without_stamps
64
62
  end
65
63
  end
@@ -1,222 +1,56 @@
1
- module Lockdown
2
- class Controller
3
- attr_accessor :name, :access_methods, :only_methods, :except_methods
4
-
5
- def initialize(name)
6
- @name = name
7
- @except_methods = []
8
- end
9
-
10
- def set_access_methods
11
- if @only_methods
12
- @access_methods = paths_for(@name, *@only_methods)
13
- else
14
- @access_methods = paths_for(@name)
15
- end
16
-
17
- apply_exceptions if @except_methods.length > 0
18
- end
19
-
20
- private
21
-
22
- def apply_exceptions
23
- exceptions = paths_for(@name, *@except_methods)
24
- @access_methods = @access_methods - exceptions
25
- end
26
-
27
- def paths_for(str_sym, *methods)
28
- Lockdown::System.paths_for(str_sym, *methods)
29
- end
30
- end
1
+ # encoding: utf-8
31
2
 
32
- class Model
33
- attr_accessor :name, :controller_method, :model_method, :association, :param
34
-
35
- def initialize(name, param = :id)
36
- @name = name
37
- @param = param
38
- end
39
-
40
- def class_name
41
- self.name.to_s.camelize
42
- end
43
-
44
- end
45
-
3
+ module Lockdown
46
4
  class Permission
47
- attr_reader :name, :controllers, :models
48
-
49
- # A Permission is a set of rules that are, through UserGroups, assigned
50
- # to users to allow access to system resources.
51
- #
52
- # ==== Summary of controller oriented methods:
53
- #
54
- # # defines which controller we're talking about
55
- # .with_controller(:controller_name) #all_methods is the default
56
- #
57
- # # only these methods on the controller
58
- # .only_methods(:meth1, :meth2)
59
- #
60
- # # all controller methods except these
61
- # .except_methods(:meth1, :meth2)
62
- #
63
- # ==== Summary of model oriented methods:
64
- #
65
- # # defines which model we're talking about
66
- # .to_model(:model)
67
- #
68
- # # model_method is simply a public method on :model
69
- # .where(:model_method)
70
- #
71
- # # controller_method must equal model_method
72
- # .equals(:controller_method)
73
- #
74
- # # controller_method.include?(model_method)
75
- # .is_in(:controller_method)
76
- #
77
- #
78
- # ==== Example:
79
- #
80
- # # Define a permission called 'Manage Users' that allows users access
81
- # # all methods on the users_controller
82
- #
83
- # set_permission(:manage_users).
84
- # with_controller(:users)
85
- #
86
- # # Define a permission called "My Account" that only allows a user access
87
- # # to methods show and update and the current_user_id must match the id
88
- # # of the user being modified
89
- #
90
- # set_permission(:my_account).
91
- # with_controller(:users).
92
- # only_methods(:show, :update).
93
- # to_model(:user).
94
- # where(:current_user_id).
95
- # equals(:id)
96
- #
97
- def initialize(name_symbol)
98
- @name = name_symbol
99
- @controllers = {}
100
- @models = {}
101
- @current_context = Lockdown::RootContext.new(name_symbol)
102
- @public_access = false
103
- @protected_access = false
104
- end
105
-
106
- def with_controller(name_symbol)
107
- validate_context
108
-
109
- controller = Controller.new(name_symbol)
110
- @controllers[name_symbol] = controller
111
- @current_context = Lockdown::ControllerContext.new(name_symbol)
112
- self
113
- end
114
-
115
- alias_method :and_controller, :with_controller
116
-
117
- def only_methods(*methods)
118
- validate_context
5
+ # Name of permission
6
+ attr_accessor :name
7
+ # Array of resource objects that define the access rights for this permission
8
+ attr_reader :resources
119
9
 
120
- current_controller.only_methods = methods
121
- @current_context = Lockdown::RootContext.new(@name)
122
- self
123
- end
124
-
125
- def except_methods(*methods)
126
- validate_context
127
-
128
- current_controller.except_methods = methods
129
-
130
- @current_context = Lockdown::RootContext.new(@name)
131
- self
132
- end
133
-
134
- def to_model(name_symbol, param = :id)
135
- validate_context
136
-
137
- @models[name_symbol] = Model.new(name_symbol, param)
138
- @current_context = Lockdown::ModelContext.new(name_symbol)
139
- self
140
- end
141
-
142
- def where(model_method)
143
- validate_context
144
-
145
- current_model.model_method = model_method
146
- @current_context = Lockdown::ModelWhereContext.new(current_context.name)
147
- self
148
- end
149
-
150
- def equals(controller_method)
151
- validate_context
152
-
153
- associate_controller_method(controller_method, :==)
154
- @current_context = Lockdown::RootContext.new(@name)
155
- self
156
- end
157
-
158
- def is_in(controller_method)
159
- validate_context
160
-
161
- associate_controller_method(controller_method, :include?)
162
- @current_context = Lockdown::RootContext.new(@name)
163
- self
164
- end
165
-
166
- alias_method :includes, :is_in
167
-
168
- def public_access?
169
- @public_access
170
- end
171
-
172
- def protected_access?
173
- @protected_access
10
+ # @param [String,Symbol] name permission reference.
11
+ def initialize(name)
12
+ @name = name.to_s
13
+ @resources = []
14
+ @ispublic = false
15
+ @isprotected = false
174
16
  end
175
17
 
176
- def set_as_public_access
177
- if protected_access?
178
- raise Lockdown::PermissionScopeCollision, "Permission: #{name} already marked as protected and trying to set as public."
179
- end
180
- @public_access = true
18
+ # @param [String,Symbol] name resource reference.
19
+ # @return new resource
20
+ def resource(name, &block)
21
+ resource = Lockdown::Resource.new(name)
22
+ resource.instance_eval(&block) if block_given?
23
+ @resources << resource
24
+ resource
181
25
  end
182
26
 
183
- def set_as_protected_access
184
- if public_access?
185
- raise Lockdown::PermissionScopeCollision, "Permission: #{name} already marked as public and trying to set as protected."
186
- end
187
- @protected_access = true
188
- end
27
+ alias_method :controller, :resource
189
28
 
190
- def current_context
191
- @current_context
29
+ def controllers
30
+ @resources
192
31
  end
193
32
 
194
- def current_controller
195
- @controllers[current_context.name]
33
+ def is_public
34
+ @ispublic = true
35
+ @isprotected = false
196
36
  end
197
37
 
198
- def current_model
199
- @models[current_context.name]
38
+ def public?
39
+ @ispublic
200
40
  end
201
41
 
202
- def ==(other)
203
- name == other.name
42
+ def is_protected
43
+ @isprotected = true
44
+ @ispublic = false
204
45
  end
205
46
 
206
- private
207
-
208
- def associate_controller_method(controller_method, association)
209
- current_model.controller_method = controller_method
210
- current_model.association = association
211
- @current_context = Lockdown::RootContext.new(@name)
47
+ def protected?
48
+ @isprotected
212
49
  end
213
50
 
214
- def validate_context
215
- method_trace = caller.first;
216
- calling_method = caller.first[/#{__FILE__}:(\d+):in `(.*)'/,2]
217
- unless current_context.allows?(calling_method)
218
- raise Lockdown::InvalidRuleContext, "Method: #{calling_method} was called on wrong context #{current_context}. Allowed methods are: #{current_context.allowed_methods.join(',')}."
219
- end
51
+ # @return String representing all resources defining this permission
52
+ def regex_pattern
53
+ resources.collect{|r| "(#{r.regex_pattern})"}.join("|")
220
54
  end
221
- end
222
- end
55
+ end # Permission
56
+ end # Lockdown