Reputation: 1395
I have created user roles, however my app is not picking up that my account is a admin. It shows the role as admin but then admin is also nil.
From rails console:
2.0.0-p0 :001 > user = User.find(13)
User Load (17.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 13 LIMIT 1
=> #<User id: 13, admin: nil, role: "admin", roles_mask: nil>
2.0.0-p0 :002 > user.roles
=> []
2.0.0-p0 :003 > user.role?(:admin)
=> false
If it's understanding the users role is admin, how come it is not accepting it as admin and giving the account the proper permissions? I need this fixed as I am trying to allow only admins to modify all profiles, and then specifying regular users to only be able to modify and access their own profiles.
user.rb:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
validates_uniqueness_of :email
validates_presence_of :password, :on => :create
before_create { generate_token(:auth_token) }
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
end
Upvotes: 0
Views: 69
Reputation: 10769
As I said in my comment,
user.role
=> admin
So, you can add to your application_controller something like:
def admin
unless current_user.role == 'admin'
flash[:error] = "Authorisation is required to access this content."
redirect_to current_user
end
end
In this way, you can block users that are not admin to access some action in a controller:
before_filter :admin, :only => [:destroy]
It is just one example to give you some direction, and I am assuming you have current_user helper.
I hope it helps...
Upvotes: 1