| 1 |
""" |
| 2 |
Creates permissions for all installed apps that need permissions. |
| 3 |
""" |
| 4 |
|
| 5 |
import getpass |
| 6 |
import unicodedata |
| 7 |
|
| 8 |
from django.apps import apps as global_apps |
| 9 |
from django.contrib.auth import get_permission_codename |
| 10 |
from django.contrib.contenttypes.management import create_contenttypes |
| 11 |
from django.core import exceptions |
| 12 |
from django.db import DEFAULT_DB_ALIAS, router |
| 13 |
|
| 14 |
|
| 15 |
def _get_all_permissions(opts): |
| 16 |
""" |
| 17 |
Return (codename, name) for all permissions in the given opts. |
| 18 |
""" |
| 19 |
return [*_get_builtin_permissions(opts), *opts.permissions] |
| 20 |
|
| 21 |
|
| 22 |
def _get_builtin_permissions(opts): |
| 23 |
""" |
| 24 |
Return (codename, name) for all autogenerated permissions. |
| 25 |
By default, this is ('add', 'change', 'delete', 'view') |
| 26 |
""" |
| 27 |
perms = [] |
| 28 |
for action in opts.default_permissions: |
| 29 |
perms.append( |
| 30 |
( |
| 31 |
get_permission_codename(action, opts), |
| 32 |
"Can %s %s" % (action, opts.verbose_name_raw), |
| 33 |
) |
| 34 |
) |
| 35 |
return perms |
| 36 |
|
| 37 |
|
| 38 |
def create_permissions( |
| 39 |
app_config, |
| 40 |
verbosity=2, |
| 41 |
interactive=True, |
| 42 |
using=DEFAULT_DB_ALIAS, |
| 43 |
apps=global_apps, |
| 44 |
**kwargs, |
| 45 |
): |
| 46 |
if not app_config.models_module: |
| 47 |
return |
| 48 |
|
| 49 |
try: |
| 50 |
Permission = apps.get_model("auth", "Permission") |
| 51 |
except LookupError: |
| 52 |
return |
| 53 |
if not router.allow_migrate_model(using, Permission): |
| 54 |
return |
| 55 |
|
| 56 |
# Ensure that contenttypes are created for this app. Needed if |
| 57 |
# 'django.contrib.auth' is in INSTALLED_APPS before |
| 58 |
# 'django.contrib.contenttypes'. |
| 59 |
create_contenttypes( |
| 60 |
app_config, |
| 61 |
verbosity=verbosity, |
| 62 |
interactive=interactive, |
| 63 |
using=using, |
| 64 |
apps=apps, |
| 65 |
**kwargs, |
| 66 |
) |
| 67 |
|
| 68 |
app_label = app_config.label |
| 69 |
try: |
| 70 |
app_config = apps.get_app_config(app_label) |
| 71 |
ContentType = apps.get_model("contenttypes", "ContentType") |
| 72 |
except LookupError: |
| 73 |
return |
| 74 |
|
| 75 |
models = list(app_config.get_models()) |
| 76 |
|
| 77 |
# Grab all the ContentTypes. |
| 78 |
ctypes = ContentType.objects.db_manager(using).get_for_models( |
| 79 |
*models, for_concrete_models=False |
| 80 |
) |
| 81 |
|
| 82 |
# Find all the Permissions that have a content_type for a model we're |
| 83 |
# looking for. We don't need to check for codenames since we already have |
| 84 |
# a list of the ones we're going to create. |
| 85 |
all_perms = set( |
| 86 |
Permission.objects.using(using) |
| 87 |
.filter( |
| 88 |
content_type__in=set(ctypes.values()), |
| 89 |
) |
| 90 |
.values_list("content_type", "codename") |
| 91 |
) |
| 92 |
|
| 93 |
perms = [] |
| 94 |
for model in models: |
| 95 |
ctype = ctypes[model] |
| 96 |
for codename, name in _get_all_permissions(model._meta): |
| 97 |
if (ctype.pk, codename) not in all_perms: |
| 98 |
permission = Permission() |
| 99 |
permission._state.db = using |
| 100 |
permission.codename = codename |
| 101 |
permission.name = name |
| 102 |
permission.content_type = ctype |
| 103 |
perms.append(permission) |
| 104 |
|
| 105 |
Permission.objects.using(using).bulk_create(perms) |
| 106 |
if verbosity >= 2: |
| 107 |
for perm in perms: |
| 108 |
print("Adding permission '%s'" % perm) |
| 109 |
|
| 110 |
|
| 111 |
def get_system_username(): |
| 112 |
""" |
| 113 |
Return the current system user's username, or an empty string if the |
| 114 |
username could not be determined. |
| 115 |
""" |
| 116 |
try: |
| 117 |
result = getpass.getuser() |
| 118 |
except (ImportError, KeyError, OSError): |
| 119 |
# TODO: Drop ImportError and KeyError when dropping support for PY312. |
| 120 |
# KeyError (Python <3.13) or OSError (Python 3.13+) will be raised by |
| 121 |
# os.getpwuid() (called by getuser()) if there is no corresponding |
| 122 |
# entry in the /etc/passwd file (for example, in a very restricted |
| 123 |
# chroot environment). |
| 124 |
return "" |
| 125 |
return result |
| 126 |
|
| 127 |
|
| 128 |
def get_default_username(check_db=True, database=DEFAULT_DB_ALIAS): |
| 129 |
""" |
| 130 |
Try to determine the current system user's username to use as a default. |
| 131 |
|
| 132 |
:param check_db: If ``True``, requires that the username does not match an |
| 133 |
existing ``auth.User`` (otherwise returns an empty string). |
| 134 |
:param database: The database where the unique check will be performed. |
| 135 |
:returns: The username, or an empty string if no username can be |
| 136 |
determined or the suggested username is already taken. |
| 137 |
""" |
| 138 |
# This file is used in apps.py, it should not trigger models import. |
| 139 |
from django.contrib.auth import models as auth_app |
| 140 |
|
| 141 |
# If the User model has been swapped out, we can't make any assumptions |
| 142 |
# about the default user name. |
| 143 |
if auth_app.User._meta.swapped: |
| 144 |
return "" |
| 145 |
|
| 146 |
default_username = get_system_username() |
| 147 |
try: |
| 148 |
default_username = ( |
| 149 |
unicodedata.normalize("NFKD", default_username) |
| 150 |
.encode("ascii", "ignore") |
| 151 |
.decode("ascii") |
| 152 |
.replace(" ", "") |
| 153 |
.lower() |
| 154 |
) |
| 155 |
except UnicodeDecodeError: |
| 156 |
return "" |
| 157 |
|
| 158 |
# Run the username validator |
| 159 |
try: |
| 160 |
auth_app.User._meta.get_field("username").run_validators(default_username) |
| 161 |
except exceptions.ValidationError: |
| 162 |
return "" |
| 163 |
|
| 164 |
# Don't return the default username if it is already taken. |
| 165 |
if check_db and default_username: |
| 166 |
try: |
| 167 |
auth_app.User._default_manager.db_manager(database).get( |
| 168 |
username=default_username, |
| 169 |
) |
| 170 |
except auth_app.User.DoesNotExist: |
| 171 |
pass |
| 172 |
else: |
| 173 |
return "" |
| 174 |
return default_username |