Last active
August 29, 2015 14:03
-
-
Save Billz2me/f40a0d2189e6ec46fa49 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
class UsersController < ApplicationController | |
before_filter :authorize! | |
def edit | |
@user = User.find(params[:id]) | |
end | |
def update | |
@user = User.find(params[:id]) | |
if @user.update_attributes(user_params) | |
redirect_to :root, notice: "Profile successfully updated." | |
else | |
render :action => "edit" | |
end | |
end | |
def authorize! | |
case action | |
when :edit, :update | |
raise AccessDenied unless @current_user.id.eql?(params[:id]) | |
else | |
raise AccessDenied unless @current_user.is_admin? | |
end | |
end | |
end | |
class ApplicationController < ActionController::Base | |
rescue_from AccessDenied, with: unauthorized_access_attempt | |
def current_user | |
@current_user ||= User.find(session[:user_id]) if session[:user_id] | |
end | |
def user_signed_in? | |
current_user.present? | |
end | |
def unauthorized_access_attempt | |
redirect_to :root, alert: "You don't have permission to do that!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment