Skip to content

Instantly share code, notes, and snippets.

@TigerWolf
Created April 1, 2014 03:41
Show Gist options
  • Save TigerWolf/9907304 to your computer and use it in GitHub Desktop.
Save TigerWolf/9907304 to your computer and use it in GitHub Desktop.
# Ability Class
class AdminAbility
include CanCan::Ability
def initialize(user)
if user && user.admin?
can :access, :rails_admin
can :manage, :all
end
end
end
# User Model
class User < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email, :on => :create
validates_uniqueness_of :email
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
def create
User.create(user_params)
end
def admin?
return true
end
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
# Application Controller
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
helper_method :current_user
def current_user
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end
end
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
### Popular gems integration
## == Devise ==
# config.authenticate_with do
# warden.authenticate! scope: :user
# end
# config.current_user_method(&:current_user)
## == Cancan ==
config.authorize_with :cancan, AdminAbility
## == PaperTrail ==
# config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0
### More at https://github.com/sferik/rails_admin/wiki/Base-configuration
config.actions do
dashboard # mandatory
index # mandatory
new
export
bulk_delete
show
edit
delete
show_in_app
## With an audit adapter, you can add:
# history_index
# history_show
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment