A powerful and flexible permission management system for Flutter applications with device-level role-based access control (RBAC), hierarchical permissions, and customizable UI components.
Features
Type-safe models - Permission and Role with full serialization support
Hierarchical permissions - Permissions can inherit from other permissions
Role inheritance - Roles can inherit permissions from other roles
Device-level roles - Assign roles directly to the device — no user identity required
Detailed results - PermissionResult includes the reason, contributing roles, and permissions
Pluggable storage - Implement PermissionStorage for any backend (default uses Nylo local storage)
Performance caching - Resolved permissions are cached and automatically invalidated on changes
UI components - PermissionGuard, PermissionBuilder, and RoleSelector
A Permission represents a specific action or access right. Permissions can inherit from other permissions using inheritsFrom - granting a permission automatically includes its parents.
A Role groups permissions together and can inherit from other roles. Use isAssignable to control whether a role appears in selectors, and isSystem to mark roles that shouldn't be deleted.
Permissions are resolved purely through roles. The device has a list of role IDs, and the system resolves all permissions by walking the role and permission inheritance chains.
API Reference
Checking Permissions
final policy = PermissionPolicy.instance;
// Simple boolean checkbool canEdit = await policy.hasPermission('edit_posts');
// Check any of multiple permissionsbool canManage = await policy.hasAnyPermission(['edit_posts', 'delete_posts']);
// Check all permissions are grantedbool fullAccess = await policy.hasAllPermissions(['read_posts', 'write_posts', 'delete_posts']);
Detailed Permission Results
Use checkPermission to get a PermissionResult with the reason and contributing factors:
PermissionResult result = await policy.checkPermission('edit_posts');
print(result.granted); // true or falseprint(result.reason); // e.g. 'Permission granted through roles'print(result.contributingRoles); // e.g. ['editor']print(result.contributingPermissions); // e.g. ['edit_posts']// Also available for multiple checksPermissionResult anyResult = await policy.checkAnyPermission(['edit_posts', 'delete_posts']);
PermissionResult allResult = await policy.checkAllPermissions(['read_posts', 'write_posts']);
Managing Roles
// Give a role to the deviceawait policy.giveRole('editor');
// Remove a roleawait policy.removeRole('editor');
// Check rolesbool isAdmin = await policy.hasRole('admin');
bool hasEditAccess = await policy.hasAnyRole(['editor', 'admin']);
bool isSuperMod = await policy.hasAllRoles(['moderator', 'admin']);
// Get the device's rolesList<String> roleIds = await policy.getDeviceRoles();
// Get all available rolesList<Role> allRoles = await policy.getRoles();
// Get only assignable roles (isAssignable == true)List<Role> assignable = await policy.getAssignableRoles();
// Get all resolved permissions for the deviceSet<String> allPermissions = await policy.getDevicePermissions();
// Clear all device rolesawait policy.clearRoles();
// Clear everything (permissions, roles, and device roles)await policy.clearAll();
Widgets
PermissionGuard
Conditionally renders a widget based on permissions or roles. Shows the child when granted and the fallback when denied.
RoleSelectorStyle supports customizing spacing, selectedColor, backgroundColor, titleStyle, subtitleStyle, scrollPhysics, and for cards: crossAxisCount and childAspectRatio.
Advanced Usage
Permission Inheritance
Build hierarchical permission structures where higher-level permissions automatically include lower-level ones: