Created
October 24, 2014 07:58
-
-
Save pirj/6e7afad5bbcf354f3e5b to your computer and use it in GitHub Desktop.
Padrino::CanCan example that is known to have worked years ago
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class App < Padrino::Application | |
| register Padrino::Mailer | |
| register Padrino::Helpers | |
| register Padrino::Admin::AccessControl | |
| register Padrino::Rendering | |
| set :session_secret, "blah" | |
| set :sessions, true | |
| [403, 404, 405, 500].each do |code| | |
| error code do | |
| render "errors/#{code}", :layout => 'errors/layout' | |
| end | |
| end | |
| error CanCan::AccessDenied do | |
| 403 | |
| end | |
| error do | |
| 'Sorry there was a nasty error - ' + env['sinatra.error'].name | |
| end | |
| set :login_page, "/sessions/new" | |
| # enable :store_location | |
| enable :authentication | |
| [:admin, :manager, :manufacturer, :reports, :external].each do |role| | |
| access_control.roles_for role do |void| end | |
| end | |
| end | |
| module CanCan | |
| module ControllerAdditions | |
| def current_user | |
| current_account | |
| end | |
| def self.included(base) | |
| base.extend ClassMethods | |
| # base.helper_method :can?, :cannot?, :current_ability | |
| end | |
| end | |
| end | |
| class Ability | |
| include CanCan::Ability | |
| def initialize account | |
| @abilities ||= {} | |
| allow [:any, :external, :manager, :manufacturer, :admin] do | |
| can :index, :base | |
| can [:index, :view, :find, :search], Product | |
| end | |
| allow [:external, :manager, :manufacturer, :admin] do | |
| can [:edit, :update], :account => account | |
| end | |
| allow [:manager, :reports, :admin] do | |
| can [:index, :view, :find, :search], Manufacturer | |
| can :index, [Manufacturer, Factory, Brand] | |
| end | |
| allow [:manager, :admin] do | |
| can [:new, :create, :edit, :update], Manufacturer | |
| can [:create, :destroy], [Brand, Factory] | |
| can :manage, Product | |
| end | |
| allow [:manager, :admin, :reports] do | |
| can [:index, :new, :create, :report], Statistic | |
| end | |
| allow :admin do | |
| can :destroy, Manufacturer | |
| can :manage, Account | |
| end | |
| role = account.role.to_sym rescue :any | |
| (@abilities[role] || []).each do |block| | |
| block.call | |
| end | |
| end | |
| def allow roles, &block | |
| if roles.is_a? Array | |
| roles.each do |role| allow_role role, &block end | |
| else | |
| allow_role roles, &block | |
| end | |
| end | |
| def allow_role role, &block | |
| @abilities[role] ||= [] | |
| @abilities[role] << block | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment