开发者

multi level user groups

开发者 https://www.devze.com 2022-12-08 19:14 出处:网络
I\'m trying to determine the best structure to approach multi level user groups. Thus far I\'ve created one object called \"User\" which i assumed could pote开发者_JAVA百科ntially be broken into diffe

I'm trying to determine the best structure to approach multi level user groups. Thus far I've created one object called "User" which i assumed could pote开发者_JAVA百科ntially be broken into different levels. Or should I simply create different tables for each user group?


Have a look into Single Table Inheritance..

The short version is that you add a type(string) column to your table and subclass all other models that will use that table from User

Eg:

class SuperUser < User
  ...
end


I assume you are talking about differnt roles for your users. I am currently using RoleRequirement. It gets the job done fairly easily. http://code.google.com/p/rolerequirement/


As EmFi suggested, single table inheritance is your best bet. You would need to add the type field to the users table in the database and subclass your User model as below:

class Admin < User
  # You probably don't need any methods here.
end

but you would also need to create a before filter. Quite similar to the one which makes sure that the user is logged in, it simply checks the class of your user. This should go in your ApplicationController:

def check_admin
  current_user.is_a? Admin
end

There you go, Bob's your uncle, you have rudimentary authorisation.

To promote a user to Admin within rails, just change the type field. Of course, in this way, the user can only hold one access level (which is fine for a lot of applications). If you should want a more complex system, acl9 is quite well equipped for the job. I personally make a habit of using it along with authlogic to form quite a powerful system.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号