6

I want to know if all the logic related to permissions and access control belongs to controllers (Application logic) or models (Business logic).

Currently I have a controller that does something like that:

function delete (resource) {
   if (resource.belongsTo (currentUser) {
       resource.delete ();
   }
   else {
       throw Exception ("you can't do this");
   }
}

Is this kind of logic application logic or business logic?

PS. I already read Where to put business logic in MVC design?, but I still have doubts

8

It is perfectly acceptable to put security/permissions logic in the controller method.

The purpose of the controller method is to coordinate service calls to the service layer or business logic layer or repository. Technically, security is an orthogonal, but very important concern to the business logic methods. It is orthogonal because it essentially boils down to a "gate" that you must pass through: either the resulting business logic method gets executed, or it does not. But the essential logic itself does not change: assuming you have permissions, the outcome will always be the same.

The permissions function may be provided by another library or service, but that further underscores the role of the controller method as a coordinator, since now the controller method won't even be providing security functionality (it defers that functionality to some other mechanism).

3

It is helpful to distinguish two types of access control:

  • Vertical - functions that some users can access and some users cannot. For example, anyone can view the home page, but only admin can ban a user.
  • Horizonal - functions that multiple users can access, but the data is segregated. For example, everyone can access "inbox" - but they see only their own messages, not other people's.

Vertical access control is almost always enforced at the controller layer.

Most applications also enforce horizontal access control at the controller layer. This works, but there is a disadvantage: access checks need to be repeated in multiple places, which invites mistakes. Complex applications need a lot of checks to properly enforce horizontal access control.

But there is an alternative: perform horizontal access control in the model. This tends to reduce the number of checks needed - e.g. you have an access control check on the Email object, rather that 15 separate checks in controller methods that access the object. One criticism of this approach is that is requires the model to be identity-aware, which could be unwanted coupling between model and controller. However, I think it's worth it. I've written more about this topic on my website.

1

absolutely awesome practice. also try having some sort of base controller that have all other of your controller extended to it to avoid repetitive access control check. let's say i want to limit people from visiting the profile controller, i can have a parent controller called user and right in the constructor i would do a check to see if user has access else i will redirect. if you decide developing with java, you should probably try the spring security (http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html) which is awesome and eliminates much need for the struggle for access controls from the controllers.

  • 1
    Constructive criticism: your answer would be better (easier to read) if you broke it up into a few paragraphs. – DougM Nov 11 '14 at 15:48
  • thanks @DougM . didn't know i went that poor constructing my answer. – Kevin Constantine Nov 11 '14 at 16:47
  • 1
    Using the shift key properly helps also. If you're posting from a cell phone, there's usually an auto-capitalization function that is available. – Robert Harvey Nov 12 '14 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.