Created
January 24, 2015 06:31
-
-
Save bvsatyaram/9a94199e7e6587ea4f3b to your computer and use it in GitHub Desktop.
Authorize Admin for certain actions
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
$('.flash_messages').html('<%= j flash_tag("Access Denied!", "alert") %>') |
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
class ApplicationController < ActionController::Base | |
# Prevent CSRF attacks by raising an exception. | |
# For APIs, you may want to use :null_session instead. | |
protect_from_forgery with: :exception | |
protected | |
def deny_access! | |
if request.xhr? | |
render 'common/access_denied' | |
else | |
redirect_to root_path, alert: "Access Denied!" | |
end | |
end | |
def authorize_admin! | |
deny_access! unless current_user.admin? | |
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
module ApplicationHelper | |
def flash_tag(msg, msg_type) | |
return unless msg.present? | |
if msg_type == "notice" | |
msg_type = "info" | |
elsif msg_type == "alert" | |
msg_type = "danger" | |
end | |
content_tag(:div, class: "alert alert-#{msg_type} alert-dismissible", role: "alert") do | |
button_tag(class: "close", "data-dismiss" => "alert") do | |
content_tag(:span, raw("×"), "aria-hidden" => true) + | |
content_tag(:span, "Close", class: "sr-only") | |
end + msg | |
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
class ItemssController < ApplicationController | |
# This is provided by `devise` gem | |
before_action :authenticate_user! | |
# We define this in `ApplicationController` | |
before_action :authorize_admin!, except: [:index, :show] | |
def index | |
... | |
end | |
def show | |
... | |
end | |
def new | |
... | |
end | |
def create | |
... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment