-
-
Save RafeHatfield/6440441 to your computer and use it in GitHub Desktop.
This file contains 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
# app/models/ability.rb | |
# All front end users are authorized using this class | |
class Ability | |
include CanCan::Ability | |
def initialize(user) | |
user ||= User.new | |
can :read, :all | |
end | |
end |
This file contains 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
# config/initializers/active_admin.rb | |
ActiveAdmin.setup do |config| | |
# You don't need to modify any settings here to get CanCan to work | |
# Just remember to add CanCan to your Gemfile | |
# ... | |
config.authorization_adapter = ActiveAdmin::CanCanAdapter | |
config.cancan_ability_class = "AdminAbility" | |
end |
This file contains 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
# app/models/admin_ability.rb | |
# All back end users (i.e. Active Admin users) are authorized using this class | |
class AdminAbility | |
include CanCan::Ability | |
def initialize(user) | |
user ||= User.new | |
# We operate with three role levels: | |
# - Editor | |
# - Moderator | |
# - Manager | |
# An editor can do the following: | |
can :manage, Foobar | |
can :read, SomeOtherModel | |
# A moderator can do the following: | |
if user.has_role?('moderator') | |
can :manage, SomeOtherModel | |
end | |
# A manager can do the following: | |
if user.has_role?('manager') | |
can :manage, SomeThirdModel | |
end | |
end | |
end |
This file contains 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
# app/admin/foobars.rb | |
ActiveAdmin.register Foobar do | |
end |
This file contains 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
# app/models/user.rb | |
class User < ActiveRecord::Base | |
# The order of the ROLES array is important! | |
# All privileges are inherited from left to right | |
ROLES = %w(editor moderator manager) | |
# Privileges are inherited between roles in the order specified in the ROLES | |
# array. E.g. A moderator can do the same as an editor + more. | |
# | |
# This method understands that and will therefore return true for moderator | |
# users even if you call `role?('editor')`. | |
def role?(base_role) | |
return false unless role # A user have a role attribute. If not set, the user does not have any roles. | |
ROLES.index(base_role.to_s) <= ROLES.index(role) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment