Sha256: ba18c89ea7ab7a0018bcbdd1758c942a295e4d5bd93f23f21011821b369f6a12

Contents?: true

Size: 1 KB

Versions: 7

Compression:

Stored size: 1 KB

Contents

class UsersController < ApplicationController
  before_action :authenticate_user!
  before_action :admin_only, :except => :show

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    unless current_user.admin?
      unless @user == current_user
        redirect_to :back, :alert => "Access denied."
      end
    end
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(secure_params)
      redirect_to :back, :notice => "User updated."
    else
      redirect_to :back, :alert => "Unable to update user."
    end
  end

  def destroy
    user = User.find(params[:id])
    user.destroy
    redirect_to users_path, :notice => "User deleted."
  end

  private

  def admin_only
    unless current_user.admin?
      redirect_to :back, :alert => "Access denied."
    end
  end

  def secure_params
    if @user == current_user
      params.require(:user).permit(:email)
    elsif current_user.admin?
      params.require(:user).permit(:role)
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails_apps_pages-0.6.6 lib/generators/pages/roles/templates/omniauth/users_controller.rb
rails_apps_pages-0.6.5 lib/generators/pages/roles/templates/omniauth/users_controller.rb
rails_apps_pages-0.6.4 lib/generators/pages/roles/templates/omniauth/users_controller.rb
rails_apps_pages-0.6.3 lib/generators/pages/roles/templates/omniauth/users_controller.rb
rails_apps_pages-0.6.2 lib/generators/pages/roles/templates/omniauth/users_controller.rb
rails_apps_pages-0.6.0 lib/generators/pages/roles/templates/omniauth/users_controller.rb
rails_apps_pages-0.5.16 lib/generators/pages/roles/templates/omniauth/users_controller.rb