Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/contrib/auth/management/__init__.py: 55%

66 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Creates permissions for all installed apps that need permissions. 

3""" 

4import getpass 

5import unicodedata 

6 

7from django.apps import apps as global_apps 

8from django.contrib.auth import get_permission_codename 

9from django.contrib.contenttypes.management import create_contenttypes 

10from django.core import exceptions 

11from django.db import DEFAULT_DB_ALIAS, router 

12 

13 

14def _get_all_permissions(opts): 

15 """ 

16 Return (codename, name) for all permissions in the given opts. 

17 """ 

18 return [*_get_builtin_permissions(opts), *opts.permissions] 

19 

20 

21def _get_builtin_permissions(opts): 

22 """ 

23 Return (codename, name) for all autogenerated permissions. 

24 By default, this is ('add', 'change', 'delete', 'view') 

25 """ 

26 perms = [] 

27 for action in opts.default_permissions: 

28 perms.append( 

29 ( 

30 get_permission_codename(action, opts), 

31 "Can %s %s" % (action, opts.verbose_name_raw), 

32 ) 

33 ) 

34 return perms 

35 

36 

37def create_permissions( 

38 app_config, 

39 verbosity=2, 

40 interactive=True, 

41 using=DEFAULT_DB_ALIAS, 

42 apps=global_apps, 

43 **kwargs, 

44): 

45 if not app_config.models_module: 45 ↛ 46line 45 didn't jump to line 46, because the condition on line 45 was never true

46 return 

47 

48 # Ensure that contenttypes are created for this app. Needed if 

49 # 'django.contrib.auth' is in INSTALLED_APPS before 

50 # 'django.contrib.contenttypes'. 

51 create_contenttypes( 

52 app_config, 

53 verbosity=verbosity, 

54 interactive=interactive, 

55 using=using, 

56 apps=apps, 

57 **kwargs, 

58 ) 

59 

60 app_label = app_config.label 

61 try: 

62 app_config = apps.get_app_config(app_label) 

63 ContentType = apps.get_model("contenttypes", "ContentType") 

64 Permission = apps.get_model("auth", "Permission") 

65 except LookupError: 

66 return 

67 

68 if not router.allow_migrate_model(using, Permission): 68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never true

69 return 

70 

71 # This will hold the permissions we're looking for as 

72 # (content_type, (codename, name)) 

73 searched_perms = [] 

74 # The codenames and ctypes that should exist. 

75 ctypes = set() 

76 for klass in app_config.get_models(): 

77 # Force looking up the content types in the current database 

78 # before creating foreign keys to them. 

79 ctype = ContentType.objects.db_manager(using).get_for_model( 

80 klass, for_concrete_model=False 

81 ) 

82 

83 ctypes.add(ctype) 

84 for perm in _get_all_permissions(klass._meta): 

85 searched_perms.append((ctype, perm)) 

86 

87 # Find all the Permissions that have a content_type for a model we're 

88 # looking for. We don't need to check for codenames since we already have 

89 # a list of the ones we're going to create. 

90 all_perms = set( 

91 Permission.objects.using(using) 

92 .filter( 

93 content_type__in=ctypes, 

94 ) 

95 .values_list("content_type", "codename") 

96 ) 

97 

98 perms = [ 

99 Permission(codename=codename, name=name, content_type=ct) 

100 for ct, (codename, name) in searched_perms 

101 if (ct.pk, codename) not in all_perms 

102 ] 

103 Permission.objects.using(using).bulk_create(perms) 

104 if verbosity >= 2: 104 ↛ 105line 104 didn't jump to line 105, because the condition on line 104 was never true

105 for perm in perms: 

106 print("Adding permission '%s'" % perm) 

107 

108 

109def get_system_username(): 

110 """ 

111 Return the current system user's username, or an empty string if the 

112 username could not be determined. 

113 """ 

114 try: 

115 result = getpass.getuser() 

116 except (ImportError, KeyError): 

117 # KeyError will be raised by os.getpwuid() (called by getuser()) 

118 # if there is no corresponding entry in the /etc/passwd file 

119 # (a very restricted chroot environment, for example). 

120 return "" 

121 return result 

122 

123 

124def get_default_username(check_db=True, database=DEFAULT_DB_ALIAS): 

125 """ 

126 Try to determine the current system user's username to use as a default. 

127 

128 :param check_db: If ``True``, requires that the username does not match an 

129 existing ``auth.User`` (otherwise returns an empty string). 

130 :param database: The database where the unique check will be performed. 

131 :returns: The username, or an empty string if no username can be 

132 determined or the suggested username is already taken. 

133 """ 

134 # This file is used in apps.py, it should not trigger models import. 

135 from django.contrib.auth import models as auth_app 

136 

137 # If the User model has been swapped out, we can't make any assumptions 

138 # about the default user name. 

139 if auth_app.User._meta.swapped: 

140 return "" 

141 

142 default_username = get_system_username() 

143 try: 

144 default_username = ( 

145 unicodedata.normalize("NFKD", default_username) 

146 .encode("ascii", "ignore") 

147 .decode("ascii") 

148 .replace(" ", "") 

149 .lower() 

150 ) 

151 except UnicodeDecodeError: 

152 return "" 

153 

154 # Run the username validator 

155 try: 

156 auth_app.User._meta.get_field("username").run_validators(default_username) 

157 except exceptions.ValidationError: 

158 return "" 

159 

160 # Don't return the default username if it is already taken. 

161 if check_db and default_username: 

162 try: 

163 auth_app.User._default_manager.db_manager(database).get( 

164 username=default_username, 

165 ) 

166 except auth_app.User.DoesNotExist: 

167 pass 

168 else: 

169 return "" 

170 return default_username