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'],
      "Sales Manager": ['view_revenue', 'view_apps'],
      "Developer Manager": ['view_apps'],
      "Marketing": ['view_media'],
      "Project Manager": ["edit_projects"]
    };
PermissionPolicy.instance.addRoles(roleAndPermissions);
// Check if a user has a role
await PermissionPolicy.instance.hasRole("Admin");
// Check if a user has a permission
await PermissionPolicy.instance.hasPermission("view_revenue");
// Give the user a role
await PermissionPolicy.giveRole("Admin");
// Remove a role from the user
await PermissionPolicy.removeRole();

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: ^0.1.0

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


// 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);
  

You can then check if a user has a role or permission.

// Check if a user has a role in your Widget
import 'package:flutter/material.dart';
import 'package:nylo_framework/nylo_framework.dart';
import 'package:permission_policy/permission_policy.dart';

// Check if a user has a role in your Widget

class PermissionPage extends NyPage {

  static String path = '/permission';

  @override
  Widget build(BuildContext context) {
    print('object');
    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(),
                  UserRole(), // This widget will show the users current role

                  Text("Your Permissions").fontWeightBold(),
                  UserPermissions(), // This widget will show the users current permissions
                ],
              ),

              RoleSelector(onUpdate: () {
                refreshPage(); // Refresh the page when the user selects a role
              }),

              RoleView(widgetMap: () => {
                "Admin": Text("The Admin UI"),
                "Subscriber": Text("The Subscriber UI"),
                "User": Text("The User 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();
                  refreshPage();
                },
                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.