Skip to content

Instantly share code, notes, and snippets.

@simi
Created May 14, 2012 13:20
Show Gist options
  • Select an option

  • Save simi/2693934 to your computer and use it in GitHub Desktop.

Select an option

Save simi/2693934 to your computer and use it in GitHub Desktop.
ActiveController roles
class Admin::AdminController < Admin::ApplicationController
role 'Admin'
end
class Admin::ApplicationController < ApplicationController
include Roleable
# Devise
before_filter :authenticate_user!
end
# encoding: UTF-8
module Roleable
extend ActiveSupport::Concern
included do
class_eval do
cattr_accessor :admin_role
before_filter :check_for_role
end
end
def check_for_role
if current_user.in_role?(self.class.admin_role)
return true
else
redirect_to admin_dashboard_path, :notice => 'Nemáte oprávnění'
end
end
module ClassMethods
def role(role)
self.admin_role = role
end
end
end
class User
def in_role?(role)
return true if self.role == 'Admin'
return true if role == nil
return self.role == role
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment