EpicEvents Journal

Managing Filtering Employees


The client_list_filter command provides functionality to list and filter clients (or other entities like contracts) based on the user's input. This command combines two key operations: listing and filtering. While the listing operation is open to all employees, the filtering operation is restricted based on the Role of the employee.

Command Overview

    • Listing: The list functionality is accessible to all employees, regardless of their role.
    • Filtering: The filter functionality is restricted to employees with the "SA" (Sales) role. This ensures that only employees in the Sales department can filter client data.

Permission Check

To enforce this permission logic, the user_choice() method checks whether the employee has the "SA" role before allowing access to the filtering functionality. If an employee attempts to filter without having the required role, they will receive a "permission denied" message and be redirected to the main command:

def user_choice(self, choice):
    if choice["filter"] == "Y" and self.user.employee_users.role == "SA":
        self.stdout.write()
        return
    elif choice["filter"] == "Y":
        create_permission_denied_message()
        call_command("client")
        sys.exit()
    elif choice["filter"] == "N":
        self.stdout.write()
        call_command("client")
        sys.exit()

In this example, if the user chooses the option to filter ("Y"), and they are not a Sales employee (role != "SA"), the system will display a permission denied message and exit. Otherwise, if filtering is allowed, the process proceeds.

Filtering Logic

Once the employee has been granted permission to filter (i.e., they have the appropriate role), the following sequence of operations is triggered:

1. Fetching Data: The list_filter() method retrieves the relevant data, such as the client database.
2. Field Selection: Employees can select which fields to filter by (e.g., name, location, etc.).
3. Filtering the Data: The filter_selected_fields() method applies the filter criteria based on the selected fields and order, narrowing down the results.
4. Displaying Results: The filtered data is then displayed for review.

Here's the relevant portion of the filtering logic:

def list_filter(self):
    """Methods when action='LIST_FILTER' in the child Command."""
    self.get_queryset()
    if not self.queryset:
        create_info_message("No data available!")
        self.go_back()
        sys.exit()
    self.get_instance_data()
    choice = self.get_data()
    self.user_choice(choice)
    if choice["filter"] == "Y":
        self.choose_attributes()
        selected_fields, order = self.request_field_selection()
        user_queryset = self.get_user_queryset()
        filter_queryset, order_by_fields = self.filter_selected_fields(
            selected_fields, order, user_queryset
        )
        self.display_result(filter_queryset, order_by_fields)
        self.go_back()
        sys.exit()

This method ensures that only employees with the correct permissions (Sales role) can perform filtering. If they are allowed, they can then specify the criteria for filtering and view the filtered list.
Conclusion

The client_list_filter command demonstrates a sophisticated approach to managing user permissions within a Django management command. By restricting access to certain functionalities based on roles, the system prevents unauthorized users from performing actions that they shouldn't have access to, ensuring better control and security over the application's data.

Designed by BootstrapMade and modified by DoriDoro