bcgov / lcfs

An online application for fuel suppliers to manage their compliance obligations under the Low Carbon Fuels Act
Apache License 2.0
5 stars 3 forks source link

LCFS - Implement Role-Based Access Control HOC for React Components #196

Closed AlexZorkin closed 8 months ago

AlexZorkin commented 10 months ago

Describe the task:
Develop a Higher-Order Component (HOC) in React that controls the rendering of components based on user roles. This HOC should accept a component and an array of allowed roles, rendering the component only if the user's role matches one of the specified roles.

Purpose:
The purpose of this task is to enhance our application's security and user experience by ensuring that users only have access to components relevant to their role. This will prevent unauthorized access and simplify the user interface for different types of users.

Acceptance Criteria:

Additional context:

Example Implementation

import React from 'react';

// Mock function to get the current user's role.
// Replace this with the actual logic to retrieve the user's role.
const getCurrentUserRole = () => {
    return 'admin'; // example role
};

// Higher-Order Component
const withRole = (WrappedComponent, allowedRoles) => {
    return class extends React.Component {
        render() {
            const userRole = getCurrentUserRole();

            if (allowedRoles.includes(userRole)) {
                return <WrappedComponent {...this.props} />;
            } else {
                // Render nothing or some fallback UI
                return null;
            }
        }
    };
};

// Example usage
class MyComponent extends React.Component {
    render() {
        // Your component logic
        return <div>Protected Content for Multiple Roles</div>;
    }
}

// Wrap and export your component with allowed roles
export default withRole(MyComponent, ['admin', 'analyst']);
AlexZorkin commented 10 months ago

Hey team! Please add your planning poker estimate with Zenhub @hamed-valiollahi @jig-patel @kevin-hashimoto @prv-proton

justin-lepitzki commented 8 months ago

Nothing for PO to test