Skip to content

Instantly share code, notes, and snippets.

@Spaceghost
Forked from joshrendek/Role.rb
Created December 25, 2010 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spaceghost/754961 to your computer and use it in GitHub Desktop.
Save Spaceghost/754961 to your computer and use it in GitHub Desktop.
protected
def check_perms
#logger.info "------------------\n"
#logger.info controller_name
#logger.info action_name
#logger.info current_user.role.permissions.fetch(controller_name).index(action_name)
#logger.info "------------------\n"
if current_user.role.permissions.fetch(controller_name).index(action_name).nil?
flash[:warning] = "You do not have permission to do that."
redirect_to current_user
end
end
before_filter :check_perms, :only => [:edit, :update, :new]
<% if current_user.can?("foobar", "edit") %>
some stuff here
<% end %>
class CreateRoles < ActiveRecord::Migration
def self.up
create_table :roles do |t|
t.string :name
t.text :permissions
t.timestamps
end
end
def self.down
drop_table :roles
end
end
class Role < ActiveRecord::Base
serialize :permissions
has_many :users
end
belongs_to :role
# permissions
def can?(controller, action)
begin
self.role.permissions.fetch(controller).index(action).nil? ? false : true
rescue
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment