Last active
August 29, 2015 13:58
-
-
Save mutablestate/9999983 to your computer and use it in GitHub Desktop.
Simple authorization of controller actions with Rails 4 Concern
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
# app/controllers/posts_controller.rb | |
# authorize multiple roles or a singular role | |
class PostsController < ApplicationController | |
include Authorizable | |
# before_action :authorize_admin!, except: [:index] | |
before_action :authorize_roles!, except: [:index] | |
def index | |
@posts = Post.all | |
end | |
def new | |
@post = Post.new | |
end | |
private | |
def authorize_roles! | |
case | |
when current_user.admin? | |
authorize_admin! | |
when current_user.editor? | |
authorize_editor! | |
else | |
unauthorized! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment