sushil bansal
sushil bansal

Reputation: 2013

grails spring security multiple custom (domain) classes

I have two types of users for my website: Seller and Buyer. Both of these domain classes are different (Seller has auctions and Buyer has bids). How can I create two domain classes using Spring security plugin?

  1. Extend the User class to both Seller and Buyer - it will not work as I will have to register one class for spring security

  2. Have the user object as an attribute in both Seller and Buyer. Will that work? Has anyone tried that before?

I could not find an example on internet. All I found was how to customise the plugin either by modifying User class or by extending it. If you can point me to some direction that will be good. I am new to Grails. Thanks.

Upvotes: 0

Views: 190

Answers (1)

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9885

It really depends on what Seller and Buyer mean.

If it's similar to Amazon.com; how they allow one account (aka. User) to have a seller profile and a customer (buyer) profile, then you can add Seller and Buyer properties to User.

class User { 
    Seller seller
    Buyer buyer
}

Then you would select the appropriate profile as needed.

springSecurityService.currentUser.seller
// or
springSecurityService.currentUser.buyer

If a User can be only either a Buyer or Seller, then a one-to-one association would be best, but it would require a common superclass for Buyer and Seller.

class User { 
    Static hasOne = [profile: UserProfile]
}

class UserProfile { }

class Seller extends UserProfile {
    User user
}

class Buyer extends UserProfile {
    User user
}

But with this approach you won't know which type of profile you've got. Because

springSecurityService.currentUser.profile

could return either type. If the type matters, then that's a warning sign that a one-to-one is not the right choice. An alternative might be a user type property in the User domain.

Upvotes: 1

Related Questions