简体   繁体   中英

How to set up Rails app that has different types of users?

If I want to build a Rails app that has two different types of users, let's say one type is called players and the other one is owners , what is the best and most efficient approach to modeling the app?

Things to take into account:

  • There should only be one Login, but different Registration forms that Owners/Players can use.
  • Owners can have access to a control panel but Players cannot.
  • Owners cannot share any of Players capabilities, but both need to be able to perform Login/Registration.
  • I am not using Devise, so please do not suggest it.

Different Approaches I've considered:

  • Using cancancan gem , but it does not really seem to meet my needs in the sense that I am not looking to create a user/admin hierarchical approach but rather a if you're a Player, then you can see these pages and perform these actions but Owners cannot and vice versa. Almost like splitting the app in two. cancancan seems that it would treat Owners as "Players with extra privileges", not different privileges entirely.

  • Creating separate models with separate login and registration forms , which seems like a disaster waiting to happen. One small mixup between a Players table and the Owners table, especially with the primary keys, and that will be a world of trouble where people could end up logging in to the wrong accounts.

  • Creating a polymorphic or has_one relation toward an Account model , which so far, seems like the best way to probably go about it. If I created a polymorphic Account model, I can store different types of Players / Owners , but how could I compare login credentials against all types?

I had been trying to find something on this matter regarding how to map this out and was surprised to not find an information on how to do this without using Devise . If anyone has any good links they can point me to that also address this matter ( without Devise), please leave them in your answer! Thanks.

I'd suggest one User class with a type attribute that determines whether the user is a Player or an Owner (single table inheritance). This way you keep the registration logic in one place but can customize the forms depending on the user's class.

There must be alternatives to cancancan that help with what you want to do, or you can implement helpers yourself:

def can_access_control_panel?
  current_user.is_a?(Owner)
end

You have to have a way to separate one user from another. One way is to add an attribute to the User table so you can call current_user.role and it will return "owner" or return "player" .

I have used Pundit gem in the past. It lets you define which controller actions the current user is allowed to access. So as you create resources for your application, you can add a policy that specifies who is allowed to that given resource. This is the repo to the application.

This answer might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM