Django - make admin fields invisible to some users - python

TL;DR I'd like to be able to disable certain models per-user in /admin view.
Specifically: I'm looking to make admin models invisible to some staff users, so that they can have a sort of customized dashboard. There's all sorts of fields that change how to present, search, query, etc. models based on whatever you want, but I can't find anything to allow me to determine whether or not to even show models on the /admin page without resorting to blacklisting individual permissions (of which there are hundreds), and I'd like to be able to make some models only available to superusers and not staff.
Any thoughts?
Thanks!

Just don't give them superuser rights. Superuser means they have all rights automatically, which isn't what you want.
Then add edit, add, delete rights for the models they are allowed to edit, add and delete. You can create a group that you give these rights to, then add the users to that group.
If a user doesn't have add, edit or delete rights to a model, the model isn't shown in the admin.

Related

Multiple admins in Django is it possible

Is it possible to add multiple admins in Django's admin panel and how I will register them(they need to have the same rights)? And also is it possible to add another 2 roles to the panel with different rights?
I read a lot about it and can't find the answer.
I will really appreciate your help!
You can just create/modify a user and while creating a user you can select their role if they are admin, staff, etc. You can also customize user's permissions when you create their account or if the accounts are already created you can modify it.
To avoid having to assign permission's again and again, i would suggest you create a new group and add the permission that the user in that group should have and then assign the desired user in that group.

Django group permission activate

If you have django.contrib.auth in your INSTALLED_APPS django will automatically create add, change, delete and view permissions to every model in your system (or any one you add later). These are stored in auth_permission.
In django doc, here is what we can read under Groups section:
django.contrib.auth.models.Group models are a generic way of categorizing users so you can apply permissions, or some other label, to those users. A user can belong to any number of groups.
A user in a group automatically has the permissions granted to that group. For example, if the group 'Site editors' has the permission can_edit_home_page, any user in that group will have that permission.
I've a group with no permission at all (call it NADA) and I've assign that group to a specific user (let's call him Pierre). Pierre can still connect and create, update, delete or view anything on my web interface.
How can I make it working? There's few or no doc on the web for native Django Permission.
I've read this nice publication
django-permission-apps-comparison.
I know I could install django-guardian, django-role-permissions or
django-rules...
I know we can manage access via middleware or decorator But since django IS creating these tables for us (user, groups, permissions and group_permissions)
I thought it was extremely simple to implement CRUD access to any model class!
Wrong?
Do I miss something?
Note: Working with Python3.6 and Django 2.1.3
Django permissions are simple. As far as I understand your question, you are trying to create a user with no permission and he should not see any entries on the Django admin.
First thing is to make sure the user is not marked as "superuser", the superuser sees everything no matter which group they are added in.
If he is not a superuser and is still able to see the model then you should make sure he is not part of multiple groups. If a user is in multiple groups then a union of all permissions is what is applied to them. This link will give you more details on different flags for a user https://djangobook.com/users-groups-permissions/. Let me know if this helps.

Django create a custom form permission

I'm developing a management software. And I need create a module for manage the permissions and groups using the auth of django. I dont want use the admin django because this just allow log in for super users.
I want override the admin route and create a form with the same features from the admin site. If is possible, I want use the widget for the assignment of permission and group.
I need all this built into an app because I need this to work for this and other projects.
I have already written a custom form to add, edit and view users extending the class UserCreationForm, I need something similar to that.
I hope you can help me...
First things first: don't do this!
Creating your own Django admin site is a load of work, and likely to be insecure etc. Your'e opening a giant can of worms here.
If you need members of your app to edit permissions, they do not have to be superusers! Users with is_staff = True can all access the admin site. Once you've set this for the users you want, go ahead and configure the exact permissions for this type of user.
Start with the official docs on user permissions.

Django User Custom Permissions without using Django Admin & Groups?

I'm using a custom user model and trying to add permission to users that will be able to manage (only) their business profile.
I have two models that I need to connect.
Business & User
I will have four levels of user permissions.
1. No permission
2. Employee
3. Manager
4. Owner (can own multiple businesses)
I will need to have control, not to only control the permissions in my views.py but also in templates where each user may see slightly different content, links etc
So the million dollar question is:
Option 1.
Do I just simply add the is_employee, is_manager and is_owner fields directly to the user model as M2M fields and just check when the user is logged in for the permission?
Option 2.
Technically, I can also add those three permission fields is_employee, is_manager and is_owner to the business model as M2M fields and check whether the user is authorized to access the business data.
Option 3.
Create some kind of middle service layer (manager) functionality that will connect my views and models to handle this?
Or perhaps there is some use of Django Permissions for something like this?

Adding permissions to django users which have add-on models

I will be creating an intranet site with multiple roles (client-employee, client-admin, staff team member). Each role will have a model that attaches (via One-to-One or ForeignKey field) to a user with custom fields. I want each role to have it's own set of permissions (like a group).
How can I store this permissions set inside my application. Groups seem to be defined as part of the contrib.admin app rather than in code. I couldn't find anything in documentation on how to define a group.
What is the best way to handle model level permissions. Maybe I could do a check in the model if see if the user has the right role-model.
Access control lists are tricky (some say dead), but Django comes with a good default implementation in contrib.auth equipped with:
Users
Permissions: Binary (yes/no) flags designating whether a user may perform a certain task.
Groups: A generic way of applying labels and permissions to more than one user.
A more detailed introduction can be found here:
http://parand.com/say/index.php/2010/02/19/django-using-the-permission-system/

Categories