permission_policy

Last updated:

0 purchases

permission_policy Image
permission_policy Images
Add to Cart

Description:

permission policy

Role and Permissions for Flutter #


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);
copied to clipboard
// Get the user's current roles
await PermissionPolicy.getRoles();
copied to clipboard
// Check if a user has a role
await PermissionPolicy.hasRole('Admin');
copied to clipboard
// Check if a user has a permission
await PermissionPolicy.hasPermission('view_revenue');
copied to clipboard
// Give the user a role
await PermissionPolicy.giveRole("Admin");
copied to clipboard
// Remove a role from the user
await PermissionPolicy.removeRole("Subscriber");
copied to clipboard
// Clear all roles from the user
await PermissionPolicy.clearRoles();
copied to clipboard
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
)
);
}
copied to clipboard
// [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
)
);
}
copied to clipboard
// [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
}),
)
);
}
copied to clipboard
// [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"),
),
)
);
}
copied to clipboard
// [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']
),
)
);
}
copied to clipboard
Features #

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

Getting started #
Installation #
Add the following to your pubspec.yaml file:
dependencies:
permission_policy: ^1.2.5
copied to clipboard
or with Dart:
dart pub add permission_policy
copied to clipboard
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"),
)
],
),
),
),
);
}
}
copied to clipboard
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 #

Twitter

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

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.