Role and Permissions for Flutter

pub package License: MIT

Permission policy helps you manage role and permissions in your Flutter application. It works on Android, iOS, macOS, linux, windows and web.

Usage

Simple to use

// Add roles and permissions to the permission policy
RoleAndPermissions roleAndPermissions = {
    "Admin": ['admin'],
    "Subscriber": ['can_unsubscribe', 'view_exclusive_content'],
    "User": ['can_subscribe', 'view_content'],
    "Sales Manager": ['view_revenue', 'view_apps'],
  };
PermissionPolicy.instance.addRoles(roleAndPermissions);
// Get the user's current roles
await PermissionPolicy.getRoles();
// Check if a user has a role
await PermissionPolicy.hasRole('Admin');
// Check if a user has a permission
await PermissionPolicy.hasPermission('view_revenue');
// Give the user a role
await PermissionPolicy.giveRole("Admin");
// Remove a role from the user
await PermissionPolicy.removeRole("Subscriber");
// Clear all roles from the user
await PermissionPolicy.clearRoles();

Widgets

// [UserRoles] This widget will show the user's current roles
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: Text("Permission Policy")),
      body: SafeArea(
        child: UserRoles() // This widget will show the user's current roles
      )
  );
}
// [UserPermissions] This widget will show the users current permissions
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: Text("Permission Policy")),
      body: SafeArea(
        child: UserPermissions() // This widget will show the users current permissions
      )
  );
}
// [RoleSelector] This widget will allow the user to select a role
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: Text("Permission Policy")),
      body: SafeArea(
        child: RoleSelector(onUpdate: () {
          // onUpdate is called after the user selects a role
        }),
      )
  );
}
// [RoleView] This widget will display a widget based on the users current role
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: Text("Permission Policy")),
      body: SafeArea(
        child: RoleView(
            roles: ['user', 'subscriber'], // if the user has the role of user or subscriber, the child will be shown
          child: Text("A user or subscriber"),
        ),
      )
  );
}
// [PermissionView] This widget will show a widget if the user has the correct permissions
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(title: Text("Permission Policy")),
      body: SafeArea(
        child: PermissionView(
          child: Text("Join the Pro plan"),
          permissions: ['can_subscribe']
        ),
      )
  );
}

Features

  • x Add roles and permissions to your Flutter application
  • x Check if a user has a role
  • x Check if a user has a permission
  • x Give a user a role
  • x Remove a role from a user
  • x Widgets to show a users current role and permissions

Getting started

Installation

Add the following to your pubspec.yaml file:

dependencies:
  permission_policy: ^2.0.2

or with Dart:

dart pub add permission_policy

How to use

The package is very simple to use. You can add roles and permissions to the permission policy and then check if a user has a role or permission.

Add roles and permissions

import 'package:flutter/material.dart';
import 'package:nylo_support/helpers/extensions.dart';
import 'package:permission_policy/permission_policy.dart';

void main() {
  // Add roles and permissions to the permission policy
  RoleAndPermissions roleAndPermissions = {
    "Admin": ['admin'],
    "Subscriber": ['can_unsubscribe', 'view_exclusive_content'],
    "User": ['can_subcribe', 'view_content'],
  };
  PermissionPolicy.instance.addRoles(roleAndPermissions);

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Permission Policy")),
      body: SafeArea(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 8),
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              ListView(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                children: [
                  Text("Your role").fontWeightBold(),
                  UserRoles(), // This widget will show the user's current roles

                  Text("Your Permissions").fontWeightBold(),
                  UserPermissions(), // This widget will show the user's current permissions
                ],
              ),
              Expanded(
                child: RoleSelector(onUpdate: () {
                  setState(() {});
                }),
              ),
              RoleView(
                  roles: ['user', 'subscriber'],
                  child: Text("The user and subscriber UI"),
              ),
              PermissionView(
                  child: Text("Join the Pro plan"),
                  permissions: ['can_subscribe']),
              PermissionView(
                  child: Text("Unsubscribe from the Pro plan"),
                  permissions: ['can_unsubscribe']),
              MaterialButton(
                onPressed: () async {
                  await PermissionPolicy.removeRole("subscriber");
                  
                  setState(() {});
                },
                child: Text("Clear Roles"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

If the user has the role of Admin, they will be able to see any PermissionView widgets.

Try the example app to see how it works.

Changelog

Please see CHANGELOG for more information what has changed recently.

Social

Licence

The MIT License (MIT). Please view the License File for more information.