Hone logo
Hone
Problems

Implementing a usePermission Hook in React for Fine-Grained Access Control

This challenge focuses on creating a reusable React hook, usePermission, that simplifies the process of conditionally rendering components or executing actions based on user permissions. The hook will check if a user has a specific permission and return a boolean value indicating whether the permission is granted. This is a common requirement in applications with role-based access control (RBAC) or other permission systems.

Problem Description

You are tasked with implementing the usePermission hook in React using TypeScript. The hook should accept two arguments:

  1. requiredPermission: A string representing the permission required (e.g., "edit_posts", "delete_users").
  2. permissions: An array of strings representing the user's currently granted permissions.

The hook should return a boolean value: true if the user has the requiredPermission in their permissions array, and false otherwise. The hook should re-render whenever the permissions array changes.

Key Requirements:

  • The hook must be written in TypeScript.
  • The hook must use the useState hook to manage the permission status (although this isn't strictly necessary, it's good practice for re-renders).
  • The hook must correctly determine if the user has the required permission.
  • The hook must re-render when the permissions array changes.
  • The hook should handle cases where permissions is null or undefined gracefully (treat as no permissions granted).
  • The hook should handle cases where requiredPermission is null or undefined gracefully (treat as no permission required, effectively always granting).

Expected Behavior:

  • When the component using the hook initially renders, the hook should evaluate the permissions array and return true or false accordingly.
  • When the permissions array changes, the hook should re-evaluate and return the updated boolean value.
  • If permissions is null or undefined, the hook should return false.
  • If requiredPermission is null or undefined, the hook should return true.

Edge Cases to Consider:

  • Empty permissions array.
  • requiredPermission being an empty string.
  • permissions containing duplicate permission strings.
  • requiredPermission being null or undefined.
  • permissions being null or undefined.

Examples

Example 1:

Input: requiredPermission = "edit_posts", permissions = ["view_posts", "edit_posts", "delete_posts"]
Output: true
Explanation: The user has the "edit_posts" permission in their permissions array.

Example 2:

Input: requiredPermission = "delete_users", permissions = ["view_posts", "edit_posts"]
Output: false
Explanation: The user does not have the "delete_users" permission.

Example 3:

Input: requiredPermission = "admin", permissions = []
Output: false
Explanation: The user has no permissions, and therefore does not have the "admin" permission.

Example 4:

Input: requiredPermission = null, permissions = ["view_posts", "edit_posts"]
Output: true
Explanation: No permission is required, so the permission is effectively granted.

Example 5:

Input: requiredPermission = "edit_posts", permissions = null
Output: false
Explanation: The user has no permissions.

Constraints

  • requiredPermission and the elements of permissions are strings.
  • The permissions array can contain any number of strings (including zero).
  • The hook should be performant enough for typical React component usage. Avoid unnecessary iterations or complex logic.
  • The hook should not modify the original permissions array.

Notes

Consider using the useState hook to manage the permission status. The hook should be memoized to prevent unnecessary re-renders if the requiredPermission and permissions haven't changed. Think about how to handle the edge cases gracefully and ensure the hook behaves as expected in all scenarios. The core logic involves checking if the requiredPermission exists within the permissions array.

Loading editor...
typescript