Skip to content

fix: Audit log login did not record user#2691

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_audit_log
Mar 26, 2025
Merged

fix: Audit log login did not record user#2691
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_audit_log

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

fix: Audit log login did not record user

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Mar 26, 2025

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.

Details

Instructions 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.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Mar 26, 2025

[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.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment


}
return {
"id": str(user.id),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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.

  2. 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.

  3. 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):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is largely correct, but there are a few minor improvements and clarifications that can be made:

  1. It's good practice to use the @api_view decorator instead of inheriting from APIView. This allows you to specify which HTTP methods are accepted by the view more directly.
  2. The _get_details function 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.

@shaohuzhang1 shaohuzhang1 merged commit a0ee5c9 into main Mar 26, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_audit_log branch March 26, 2025 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant