fix: Audit log login did not record user#2691
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
|
||
| } | ||
| return { | ||
| "id": str(user.id), |
There was a problem hiding this comment.
The provided code does not appear to contain any significant errors or issues, but there are a few minor improvements that can be made:
Improvements and Suggestions
-
Consistency with
gettext: The current implementation uses'unknown', which should be consistent across the application. If you intended to use it for internationalization (i18n), make sure everything else in your project respects this practice. -
Default Parameters: It's good practice to include default parameters when defining functions, especially if some arguments might be optional. Although not strictly necessary here, adding them can improve readability and usage patterns.
-
Simplified Dictionary Assignment: You can simplify the dictionary assignment inside the function using f-strings:
def _get_user(request): user = request.user if user is None: return {"user_name": "unknown"} # Using string directly instead of gettext function return {"id": str(user.id)}
This streamlined version maintains the same functionality while being slightly more concise and adhering to common Python practices.
| @log(menu='User', operate='Log in', get_user=lambda r: {'username': r.data.get('username', None)}, | ||
| get_details=_get_details, | ||
| get_operation_object=lambda r, k: {'name': r.data.get('username')}) | ||
| def post(self, request: Request): |
There was a problem hiding this comment.
The code is largely correct, but there are a few minor improvements and clarifications that can be made:
- It's good practice to use the
@api_viewdecorator instead of inheriting fromAPIView. This allows you to specify which HTTP methods are accepted by the view more directly. - The
_get_detailsfunction is called at the end of each operation, regardless of whether an error occurs or not. Consider calling it only when no errors occurred to avoid unnecessary database access if an exception is raised.
Here's a revised version with these points addressed:
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['POST'])
def login(request: Request):
serializer = LoginSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
return login_successful(serializer.user)
@log(
menu='User',
operate='Log in',
get_user=lambda r: {'user_name': r.data.get('username', None)},
get_details=_get_details,
get_operation_object=lambda r, k: {'name': r.data.get('username')}
)
# Define other functions needed here...Make sure all necessary imports (like LoginSerializer, login_successful) are correctly imported for this solution to work.
fix: Audit log login did not record user