Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padrino::Admin::AccessControl detect the model name of accounts from settings #729

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion padrino-admin/lib/padrino-admin/access_control.rb
Expand Up @@ -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
Expand All @@ -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 }

Expand Down
Expand Up @@ -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
Expand Down