Comments (3)
I'm not sure if I understand the issue correctly. Could you please share the relevant parts of you roles.py
and what behavior you expect?
Isn't this a case where your new CustomUser model doesn't have the right permissions, as the old one?
Check https://docs.djangoproject.com/en/3.2/topics/auth/default/#default-permissions
from django-role-permissions.
To answer your second question first, yes, my new CustomUser model doesn't have the right permissions. The issue is I cannot create the new permissions that I need using django-role-permissions.
Here is my original / current roles.py:
class Admin(AbstractUserRole):
available_permissions = {
'run_task': True,
'run_scheduled_task': True,
'view_cred': True,
'create_cred': True,
'delete_cred': True,
'create_user': True,
'delete_user': True,
'view_user': True,
'change_user': True,
'add_user': True,
}
This creates permissions named Add User, Change User, View User, Delete User, and this works fine with the default django user model. These permission names are created as Title Case by django-role-permissions, as the documentation describes.
Now I move the user model to CustomUser. The symptom appears, which is I can no longer see the users when I log into the django admin as "Admin" role. I use the template permission tags, with has_role in the template logic. We can come back to the template discussion, but I don't believe this is the heart of the issue.
With the CustomUser model implemented, I now log into the django admin as superuser. I now see eight permissions for the superuser associated with users: Add User, Change User, View User, Delete User (the four original ones), and four new ones Can add user, Can change user, Can view user, Can delete user. Notice the case of these last four permissions, it's like a sentence with just the first letter in the first word capitalized. In the django admin, I add the Can view user permission to my "Admin" user. Now I login again to the django admin as the "Admin" user. I can now view the users. Great! I think, I just need to create these new permissions with django-role-permissions.
I go to the roles file above and add:
'Can_view_user':True,
The permission django-role-permissions then creates is Can View User (note the Title Case) which does not match the case of Can view user. I login to the django admin after this change to the roles file, and I am still unable to view the users in the django admin.
Desired behavior from django-role-permission:
snake case in ===> Title Case out (no change, this is the current default behavior)
"sentence case" in ===> Sentence case out (not Title Case)
example:
'view_user':True, in roles.py ===> View User is the permission name created
'Can_view_user': True in roles.py ===> Can view user is the permission name created
from django-role-permissions.
Something like this in rolepermissions/utils.py
import re
def Sentence_case(s):
return s[0].isupper() and sum([int(x[0].isupper()) for x in s.split('_')]) == 1
def camelToSnake(s):
"""
https://gist.github.com/jaytaylor/3660565
Is it ironic that this function is written in camel case, yet it
converts to snake case? hmm..
"""
if Sentence_case(s):
return s
else:
_underscorer1 = re.compile(r'(.)([A-Z][a-z]+)')
_underscorer2 = re.compile('([a-z0-9])([A-Z])')
subbed = _underscorer1.sub(r'\1_\2', s)
return _underscorer2.sub(r'\1_\2', subbed).lower()
def snake_to_title(s):
if Sentence_case(s):
return ' '.join(s.split('_'))
else:
return ' '.join(x.capitalize() for x in s.split('_'))
def camel_or_snake_to_title(s):
return snake_to_title(camelToSnake(s))
tests = ['camelCase', 'snake_case', 'Can_add_user']
for t in tests:
print(camel_or_snake_to_title(t))
from django-role-permissions.
Related Issues (20)
- Queryset of all objects which a particular user may access? HOT 1
- Create Dynamically roles HOT 3
- sync_roles --reset_user_permissions taking too much time HOT 2
- permissions added to role are not assigned automatically to user HOT 1
- Does not work with transactions? HOT 2
- Creating roles dynamically HOT 1
- Different permission for DRF CRUD HOT 3
- User hierarchy and role inheritance HOT 1
- HasPermissionsMixin no multiple permission support in required_permission HOT 1
- Can I do this with this git project? HOT 4
- Can role classes be created dynamically? HOT 1
- Updating roles question HOT 1
- Giving user a role from the admin pages doesn't update permissions? HOT 3
- sync_roles not updating permissions for each role. HOT 1
- Unnecessary dependency: django-discover-runner HOT 2
- Migrate from Travis to GitHub Actions
- Valid for Django 4.2 HOT 5
- Any plans on implementing `HasObjectPermissionsMixin`?
- sync_roles across multitenant app HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-role-permissions.