Skip to content

[Bug]: Wildcard permissions (*) fail when user has empty permissions array #15

@iqbalhasandev

Description

@iqbalhasandev

What happened?

Bug Description:
When a user has no permissions (empty array) or when permissions are manually passed as an empty array, wildcard patterns like * don't work as expected. This breaks the intended behavior where certain components should be visible to all users regardless of their permission status.

Expected Behavior:

  • <Can permission="*">Dashboard</Can> should show the Dashboard for all users, even those with no permissions
  • Wildcard patterns should match when user has empty permissions array

Current Behavior:

  • <Can permission="*">Dashboard</Can> is hidden when user has no permissions
  • Wildcard patterns return false for users with empty permissions

Steps to Reproduce:

  1. Set user permissions to empty array: []
  2. Use <Can permission="*">Content</Can>
  3. Content is not displayed

Environment:

  • Package version: 1.1.1
  • React/Inertia.js application

How to reproduce the bug

// ...existing code...
/**

  • Check if user has a simple permission pattern (no logical operators)
    */
    const checkSimplePermissionPattern = (pattern: string): boolean => {
    // Handle boolean literals
    if (pattern.trim() === 'true') return true;
    if (pattern.trim() === 'false') return false;
// Special case: if pattern is just '*', it should match for all users
// including those with empty permissions (universal access)
if (pattern.trim() === '*') return true;

// Convert pattern to regex for simple patterns
let regexPattern = pattern
  .replace(/\./g, '\\.') // Escape dots
  .replace(/\*/g, '.*') // * becomes .*
  .replace(/\?/g, '.'); // ? becomes .

// Ensure exact match (anchor start and end)
regexPattern = `^${regexPattern}$`;

const regex = new RegExp(regexPattern);
return userPermissions.some(permission => regex.test(permission));

};
// ...existing code...

Package Version

v1.1.1

Node.js Version

latest

React Version

latest

Inertia.js Version

latest

TypeScript Version (if applicable)

latest

Which operating systems does this happen with?

Linux

Notes

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions