Question

I have only recently started working with the MVC approach, so I suppose this is an easy one for you gurus here:

Where do I put access control?

  1. In a view? I don't want to have any logic besides switches and flags in my templates, so that sounds like the least viable option
  2. In the model? Should each business object decide what data it will reveal about itself based on who's asking?
  3. In the controller? That's where I have it now but it makes it hard to keep business rules consistent

Or is there another option?

Was it helpful?

Solution

This will depend on what framework you're using as that and the language will dictate a lot of the tools you have available to you.

From a high level, you should have access security configured at points-of-entry. And you should double-check access security at every level that could be considered autonomous or reused from multiple parts of your application (who knows if security was checked by your co-worker's portal that uses your logic layer? etc.). The other thing to worry about is data security, and that belongs as close to your data as possible (so, yes to your #2 above, but understand that it's separate).

This is akin to the difference between application logic and domain logic, which I'm fond of talking about. If there is any logic that is specific to one particular application (web app compared to a windows service, or whatever) then that logic should be defined in that application only. If some logic crosses the boundary between applications (is reusable between applications) then it qualifies as domain logic and should be defined in your model. Your applications can make use of domain logic, but they should not own it.

OTHER TIPS

For Model (aka data) security, the Model would "control" the access and the Controller would "facilitate" the access. This provides for the reuse of the Model independently of the Controller and minimizes if not negates the general code replication necessary across dissimilar Controllers which use the Model.

For example a Car, a Driver, and a Key. (Model, Controller, API respectively). By virtue of a very small interface (key == API), the Model permits or denies Controller access per API (key fob). Different types of access are permitted (Valet key, Owner Key, Owner FOB). Using the Valet key interface, the Controller will not have access to some of the data/function of the Model such as the glove compartment, the trunk and the gas tank. This is essentially role based access implemented by the Model through identifying and categorizing the Controller with a very small API/command surface area.

This means that the Model can be used by other controllers (car with different drivers) which only need the basic authentication to access the data of the model (functions and compartments of the car).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top