Hey,
Developping a first full-scale django app. Going pretty well so far, but there’s one design decision I’m not so sure about - user rights. My situation is this:
- This is pretty much an agile development project, as the customer wasn’t necessarily 100% on the idea of a webapp, didn’t want to invest too much. So having a prototype that is relatively quick to implement is a factor.
- I have ~6 different “user profiles”, e.g. user who would ideally not have access to the full app, but some sections only, varying from one profile to the other.
- The project will have 5 or 6 apps (for now)
- I don’t need object-level access management and I don’t foresee this being necessary anytime soon. The access issues are really more at the model/conceptual level, not on specific rows of data which are all considered “equal” in the business logic.
For starter, I’m going to use a few booleans in class User(AbstractUser). Some user may have more than one role, so that gives me that option, bools are easy to check too. That seems like a sensible approach.
However, I’m less sure WHERE I should ultimately check & decide what to return the user. The mainlines of my thinking right now:
Put it in the template
The good thing about this is that I could get rather specific with what I want to check. Potentially some front-end guy could also tweak things without having to touch the django proper part of the app.
However that will litter my html will clunky {% if …%}. I’m not sure if it is “django-ic” either.
As an example - I have a sidebar, that display most of the places that a user can visit. The sidebar is divided into subsections (accordion-style using bootstrap). Some subsection wouldn’t be rendered at all, if the user has no business there. Other example, user X may have access to page A, and page A may contain links to page B. But that user may not be allowed to visit page B. In that case I simply wouldn’t display (or just display as text) the link to page B.
To be clear - in this approach, I still have a single template for a given page for all users. ANother approach would be to make entirely different templates for different users (and choose which template to render in the views). If I’m told that’s much smarter I may do that, but this is not my choosen approach at the moment.
Put it in the views
To some extent, part of the logic will be in the views (e.g. some users may have access to a DetailVIew, but not an UpdateView for instance, then the view is an easy place to check for that). I also know that technically, if I don’t check for rights in the view, a user could manually enter a URL, so simply “not showing” a link isn’t enough in some cases.
My questions:
- I am currently not necessarily considering using django-authority or django groupes & permissions at this point. Basically the users will be managed through the admin page by a superuser and he/she woud be setting the attributes on each user to determine what they can access. Is that a case of misguided lazyness on my part? For my current requirements, seems this is overkill, but is it a case where down the road I may realize I would have been way, way smarter to just bit the bullet now? As oppose to refactor my entier user-rights management?
- Am I doing anything stupid?
- When both approaches work, is there a rule of thumb as to whether it’s prefereable to check user rights in the templates, or in the views? Or is it really just a matter of style?
- Would it be preferrable to make entierly different templates for different users? As opposed to just tweak bits of templates for different users?