Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should authorization be part of the model or controller?

I'm writing a web application with some ACL requirements: a user can make changes to some items, some items may be editable by several users, administrator can edit anything and a manager can edit everything within her organization etc.

I'm using the Play! framework, and by the looks of the Secure module, it seems that the place to put authorization concerns is in the Controllers. However, it seems to me that the authorization issues are part of the business logic, and therefore should be in the model. Furthermore, I'm starting to see duplicated logic in the controllers that I need to refactor out.

On the other hand, adding authorization to the model means that I'd have to have some way of getting the current user from within the model, which doesn't seem right. Alternatively, I could add a "current_user" parameter to every model method, but that seems even worse.

So what is the common practice? Can/should I put authorization code in the model, or keep it in the controller?

like image 803
itsadok Avatar asked Aug 31 '11 18:08

itsadok


People also ask

Why should you not implement API authorization checks?

If you implement authorization completely outside of your application and it still gets exposed, then your data is gone, your integrity is gone. Of course, your reputation will probably follow course. So no, not such a great idea. Of course there are ways to make that better.

What does Authorize attribute do?

The Authorize attribute is inheritable. This means that you can add it to a base controller class of yours and thereby ensure that any methods of any derived controllers are subject to authentication. NOTE: In general, any public method on a Controller class can be invoked via a valid URL.

What is authorization management?

Authorization management can be considered to be the approach and practices that are applied to ensure people have the correct level of access to different applications, so that they can carry out their role while also maintaining the integrity of organisational and IT policies relating to security, data privacy and ...


2 Answers

I think this is a grey area. One could argue that the user access is part of the mapping between the HTTP world and the Object-Oriented world. This is what the controller is intended for (hence the heavy use of statics), to transform the incoming request, ready to process the business rules on the domain model.

I would suggest that the controller logic is absolutely the right place for controlling the access to the model, especially as this is managed largely at an annotation level, and the authentication is abstracted off to a Security class.

like image 133
Codemwnci Avatar answered Sep 19 '22 14:09

Codemwnci


Authorization should neither be part of controller or domain model.

Instead it should be in the service layer.

Controller should just act as dispatcher and delegate between HTTP and application service. It's the application service where the orchestration takes place. This is the best place for placing authorization.

Suppose user A is authorized to access data from domain X, but not authorized for even a read access for data from domain Y. If authorization is placed in the controller, then user A gets authorized in the controller X, and via the service calls can access data from domain Y, which is not what we expected.

Since domain models communicate with each other on service layer, hence it best to place the authorization on the same level.

like image 25
user3802960 Avatar answered Sep 19 '22 14:09

user3802960