Created
April 12, 2013 15:54
-
-
Save jgautsch/5373034 to your computer and use it in GitHub Desktop.
LOGAN Center 2/3 Code example
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
############################################ | |
# Application Controller - CanCan | |
############################################ | |
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
rescue_from CanCan::AccessDenied do |exception| | |
redirect_to root_path, :alert => exception.message | |
end | |
def after_sign_in_path_for(resource) | |
case current_user.roles.first.name | |
when 'admin' | |
users_path | |
when 'applicant' | |
content_applicant_path | |
when 'employee' | |
content_employee_path | |
when 'client' | |
content_client_path | |
when 'HRadmin' | |
content_hradmin_path | |
else | |
root_path | |
end | |
end | |
end | |
############################################ | |
# Content Controller - Rolify and Devise | |
############################################ | |
class ContentController < ApplicationController | |
before_filter :authenticate_user! | |
def applicant | |
authorize! :view, :applicant, :message => 'Access limited to applicants.' | |
end | |
def employee | |
authorize! :view, :employee, :message => 'Access limited to current employees.' | |
end | |
def client | |
authorize! :view, :client, :message => 'Access limited to current clients.' | |
end | |
end | |
############################################ | |
# Abilities/Roles - CanCan | |
############################################ | |
class Ability | |
include CanCan::Ability | |
def initialize(user) | |
user ||= User.new # guest user (not logged in) | |
if user.has_role? :admin | |
can :manage, :all | |
else | |
can :view, :employee if user.has_role? :employee | |
can :view, :applicant if user.has_role? :applicant | |
can :view, :client if user.has_role? :client | |
can :view, :hradmin if user.has_role? :hradmin | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment