Reputation: 1976
I'm creating a simple application using Spring MVC and Security. I've created User and Authority tables in the db and mapped them in my application. Whenever a new user registers, I'd like to give him a "ROLE_USER" by default.
On what layer should I assign him a new role while registering?
In the controller, DAO, Service, or as a trigger in the database?
Regards, Tom.
Upvotes: 2
Views: 1529
Reputation: 34796
It should be on the service layer. If you have some sort of user service in your application (for instance an implementation of UserDetailsService
), put the method for creating a new user there and set the default role in that method.
DAO layer should be concerned with CRUD access to your database and as such it shouldn't deal with business logic such as assigning default roles when creating users.
Controller is also not a suitable place for this kind of logic, because it is a presentation layer component and user creation/role assignment is a core logic completely independent of the presentation layer.
I'm not sure about using a database trigger for this purpose, but personally I like to keep my logic in my application not scattered between database and the application.
Upvotes: 2