Merge pull request #2137 from SchrodingersGat/nameless-user-fix

Handle rare case where user instance has no name assigned
This commit is contained in:
Oliver 2021-10-12 14:39:53 +11:00 committed by GitHub
commit b9ad6bf9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 4 deletions

View File

@ -72,7 +72,10 @@ class InvenTreeMetadata(SimpleMetadata):
# Remove any HTTP methods that the user does not have permission for
for method, permission in rolemap.items():
if method in actions and not check(user, table, permission):
result = check(user, table, permission)
if method in actions and not result:
del actions[method]
# Add a 'DELETE' action if we are allowed to delete

View File

@ -296,9 +296,9 @@ class APITests(InvenTreeAPITestCase):
actions = self.getActions(url)
# 'add' permission does not apply here!
self.assertEqual(len(actions), 1)
self.assertEqual(len(actions), 2)
self.assertIn('PUT', actions.keys())
self.assertIn('GET', actions.keys())
# Add some other permissions
self.assignRole('part.change')

View File

@ -216,7 +216,10 @@ class RuleSet(models.Model):
return True
# Print message instead of throwing an error
logger.info(f"User '{user.name}' failed permission check for {table}.{permission}")
name = getattr(user, 'name', user.pk)
logger.info(f"User '{name}' failed permission check for {table}.{permission}")
return False
@staticmethod