2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-16 20:15:44 +00:00

Refactor login state management (#7158)

* Refactor login state management

- Previously relied only on presence of cookie
- Cookie may not actually be *valid*
- Inspect actual login state by looking at userState values
- Ensures better sequencing of global state API requests
- Login state is now correctly preseed across browsers

* Ignore errors for user/me/ API endpoint in playwright test

* Do not request notifications unless logged in

* Prevent duplicate licenses

* Update src/frontend/src/views/DesktopAppView.tsx

Co-authored-by: Matthias Mair <code@mjmair.com>

* Simplify checkLoginState

* Fix bug in return types

* Update playwright tests

* linting

* Remove error msg

* Use token auth for API calls

- Will (hopefully) allow us to bypass csrfmiddle request handling?

* Refetch token if not available

* Use cache for DISPLAY_FULL_NAMES setting

* Update src/frontend/tests/baseFixtures.ts

Co-authored-by: Matthias Mair <code@mjmair.com>

* PUI test updates

* Tweak doLogout function

* Revert change to baseFixtures.ts

* Cleanup

* Fix highlighted property

* Test cleanup

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
Oliver
2024-05-07 23:11:38 +10:00
committed by GitHub
parent 6c944c73dd
commit 289af4e924
24 changed files with 225 additions and 109 deletions

View File

@ -73,8 +73,24 @@ class LicenseView(APIView):
logger.exception("Exception while reading license file '%s': %s", path, e)
return []
# Ensure consistent string between backend and frontend licenses
return [{key.lower(): value for key, value in entry.items()} for entry in data]
output = []
names = set()
# Ensure we do not have any duplicate 'name' values in the list
for entry in data:
name = None
for key in entry.keys():
if key.lower() == 'name':
name = entry[key]
break
if name is None or name in names:
continue
names.add(name)
output.append({key.lower(): value for key, value in entry.items()})
return output
@extend_schema(responses={200: OpenApiResponse(response=LicenseViewSerializer)})
def get(self, request, *args, **kwargs):

View File

@ -70,7 +70,8 @@ class AuthRequiredMiddleware(object):
# API requests are handled by the DRF library
if request.path_info.startswith('/api/'):
return self.get_response(request)
response = self.get_response(request)
return response
# Is the function exempt from auth requirements?
path_func = resolve(request.path).func

View File

@ -34,7 +34,7 @@ logger = logging.getLogger('inventree')
# string representation of a user
def user_model_str(self):
"""Function to override the default Django User __str__."""
if common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES'):
if common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES', cache=True):
if self.first_name or self.last_name:
return f'{self.first_name} {self.last_name}'
return self.username
@ -831,7 +831,9 @@ class Owner(models.Model):
"""Defines the owner string representation."""
if (
self.owner_type.name == 'user'
and common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES')
and common_models.InvenTreeSetting.get_setting(
'DISPLAY_FULL_NAMES', cache=True
)
):
display_name = self.owner.get_full_name()
else:
@ -842,7 +844,9 @@ class Owner(models.Model):
"""Return the 'name' of this owner."""
if (
self.owner_type.name == 'user'
and common_models.InvenTreeSetting.get_setting('DISPLAY_FULL_NAMES')
and common_models.InvenTreeSetting.get_setting(
'DISPLAY_FULL_NAMES', cache=True
)
):
return self.owner.get_full_name() or str(self.owner)
return str(self.owner)