I'm trying to use declarative auth to control access to my site. But when I use filter_resource_access I get this error . I was also trying to find out how to make the default role to be subscriber
undefined method `name' for "admin":String
user model
class User < ActiveRecord::Base
acts_as_authentic
ROLES = %w[admin moderator subscriber]
#Each user can subscribe to many channels
has_and_belongs_to_many :channels
#Each user who is a moderator can moderate many channels
#has_many :channel_mods
#has_many :channels, :through => :channel_mods
#Each user can receive many messages
has_and_belongs_to_many :messages
#Filter users by role(s)
named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }
def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def role_symbols
roles.map do |role|
role.name.underscore.to_sym
end
end
end
channel controller
class ChannelsController < ApplicationController
filter_resource_access
helper_method :require_user
def index
if current_user
@channels = Channel.find(:all)
else
flash[:notice] = "You must first login or register before accessing or site"
redirect_to :login
end
end
def show
if current_user
#@channel = Channel.find(params[:id])
@message = Message.new(:channel => @channel)
else
flash[:notice] = "You must first login or register before accessing or site"
redirect_to :login
end
end
def new
if current_user
#@channel = Channel.new
else
flash[:notice] = "You must first login or register before accessing or site"
redirect_to :login
end
end
def create
#@channel = Channel.new(params[:channel])
if @channel.save
flash[:notice] = "Successfully created channel."
redirect_to @channel
else
render :action => 'new'
end
end
def edit
if current_user
#@channel = Channel.find(params[:id])
else
flash[:notice] = "You must first login or register before accessing or site"
redirect_to :login
end
end
def update
#@channel = Channel.find(params[:id])
if @channel.update_attributes(params[:channel])
flash[:notice] = "Successfully updated channel."
redirect_to @channel
else
render :action => 'edit'
end
end
def destroy
#@channel = Channel.find(params[:id])
@channel.destroy
flash[:notice] = "Successfully destroyed channel."
redirect_to channels_url
end
end
authorization_rules.rb
authorization do
role :admin do
has_permission_on [:all], :to => [:index, :show, :new, :create, :edit, :update, :destroy]
end
role :subscriber do
includes :guest
has_permission_on :channels_users, :to => [:new, :create, :edit, :update, :destroy] do
if_attribute :user_id => is{user_id}
end
end
role :guest do
has_permission_on :channels, :to => [:index, :show]
has_permission_on :messages, :to => [:index, :show]
has_permission_on :users, :to => [:index, :show]
end
role :moderator do
includes :guest
has_permission_on [:channels] , :to=> [:edit, :update] do
if_attribute :moderator => is{user}
end
has_permission_on [:messages], :to=> [:edit, :update] do
if_attribute :moderator => is{user}
end
has_permission_on [:messages], :to =>[:create, :new]
end
end
webrick error
Permission denied: No matching rules found for index for #<User id: 1, login: "antarrbyrd", crypted_password: "2116af494
6914553db0589fe78e957122c9d5c017d5f99b4f0b...", password_salt: "9M9OIdBcQs11sF0ycn1b", persistence_token: "923c03ca2989b
0d7e862c6e6beb02ab09ec97b1675c27900142...", first_name: "Antarr", last_name: "Byrd", login_count: 13, last_request_at: "
2010-12-06 01:06:14", telephone: "8324051056", email: "antarr.byrd@gmail.com", last_login_at: "2010-12-05 09:10:26", cur
rent_login_at: "2010-12-06 01:02:22", last_login_ip: "127.0.0.1", current_login_ip: "127.0.0.1", carrier_name: nil, mode
rator: nil, created_at: "2010-12-04 05:47:16", updated_at: "2010-12-06 01:06:14", roles_mask: 1, perishable_token: "3ssc
XJhlfYE8tIKSRa0U"> (roles [:admin], privileges [:index], context :channels).
Here's one problem fixed:
def role_symbols
roles.map do |role|
role.underscore.to_sym # NOT role.name.underscore.to_sym (role is a string)
end
end
Try this and see if it works. Otherwise, please post any error messages.