Skip to content

Instantly share code, notes, and snippets.

@bvsatyaram
Created January 24, 2015 06:31
Show Gist options
  • Save bvsatyaram/9a94199e7e6587ea4f3b to your computer and use it in GitHub Desktop.
Save bvsatyaram/9a94199e7e6587ea4f3b to your computer and use it in GitHub Desktop.
Authorize Admin for certain actions
$('.flash_messages').html('<%= j flash_tag("Access Denied!", "alert") %>')
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
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("&times;"), "aria-hidden" => true) +
content_tag(:span, "Close", class: "sr-only")
end + msg
end
end
end
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