A single Administrator without boolean column in User's table?

Hi, I’m using Ryan Bate’s CanCan gem for my authorization (coupled
with Authlogic for my authentication). In his railscast, he uses a
boolean column in the database to define roles. However, my app is
super simple and only needs one Administrator, and everyone else has
the same permissions. How would I go about doing this?

I thought of something like:
def initialize(user)
user ||= User.new

if admin?
  can :manage, :all
else
  can :read, :all
  can :create, Comment
end

end

and then defining admin? as True if the current_user’s username was
admin, but I cant figure out where to put this, much less how to
accomplish this correctly. Any ideas?

Or, maybe there’s even a better way?

AlwaysCharging wrote:

Hi, I’m using Ryan Bate’s CanCan gem for my authorization (coupled
with Authlogic for my authentication). In his railscast, he uses a
boolean column in the database to define roles. However, my app is
super simple and only needs one Administrator, and everyone else has
the same permissions. How would I go about doing this?

The way Ryan did – with a boolean column.

I thought of something like:
def initialize(user)
user ||= User.new

if admin?
  can :manage, :all
else
  can :read, :all
  can :create, Comment
end

end

and then defining admin? as True if the current_user’s username was
admin, but I cant figure out where to put this, much less how to
accomplish this correctly. Any ideas?

Don’t try to be clever with your usernames. There’s no reason to
restrict the administrator name to being “admin”, and no reason to
restrict yourself to only having one administrator.

Or, maybe there’s even a better way?

There is. The way Ryan did it.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs