Permalink
Browse files

Padrino::Admin::AccessControl refers app.settings.account_model_name

  • Loading branch information...
1 parent 0affca0 commit 4f770f095211778772b907a40f700387337d8517 AOKI Hanae committed Dec 4, 2011
@@ -14,11 +14,13 @@ class << self
#
def registered(app)
app.set :session_id, "_padrino_#{File.basename(Padrino.root)}_#{app.app_name}".to_sym
+ app.set :account_model_name, 'Account' unless app.respond_to?(:account_model_name)
app.helpers Padrino::Admin::Helpers::AuthenticationHelpers
app.helpers Padrino::Admin::Helpers::ViewHelpers
app.before { login_required }
app.send(:cattr_accessor, :access_control)
app.send(:access_control=, Padrino::Admin::AccessControl::Base.new)
+ app.send(:access_control).account_model_name = app.settings.account_model_name
end
alias :included :registered
end
@@ -27,15 +29,23 @@ def registered(app)
# This base access control class where roles are defined as are authorizations.
#
class Base
+ attr_accessor :account_model_name
+
def initialize # @private
@roles, @authorizations, @project_modules = [], [], []
end
+ def account_model
+ account_model_name.constantize
+ rescue NameError => e
+ raise Padrino::Admin::AccessControlError, "You must define an #{account_model_name} Model!"
+ end
+
##
# We map project modules for a given role or roles
#
def roles_for(*roles, &block)
- raise Padrino::Admin::AccessControlError, "You must define an Account Model!" unless defined?(Account)
+ account_model
raise Padrino::Admin::AccessControlError, "Role #{role} must be present and must be a symbol!" if roles.any? { |r| !r.kind_of?(Symbol) } || roles.empty?
raise Padrino::Admin::AccessControlError, "You can't merge :any with other roles" if roles.size > 1 && roles.any? { |r| r == :any }
@@ -96,7 +96,11 @@ def store_location
end
def login_from_session
- Account.find_by_id(session[settings.session_id]) if defined?(Account)
+ account_model.find_by_id(session[settings.session_id]) if account_model
+ end
+
+ def account_model
+ settings.account_model_name.constantize rescue nil
end
end # AuthenticationHelpers
end # Helpers

0 comments on commit 4f770f0

Please sign in to comment.