Skip to content

Commit 77f1cd6

Browse files
committed
feat(router): implement role-based access control (RBAC)
- Add Role-Based Access Control to restrict user access based on their role - Integrate localization for unauthorized access messages - Implement redirect logic for unauthorized users - Update router to handle RBAC and localization
1 parent 72973ce commit 77f1cd6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/router/router.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import 'package:flutter_news_app_web_dashboard_full_source_code/content_manageme
2525
import 'package:flutter_news_app_web_dashboard_full_source_code/content_management/view/edit_topic_page.dart';
2626
import 'package:flutter_news_app_web_dashboard_full_source_code/content_management/widgets/filter_dialog/bloc/filter_dialog_bloc.dart';
2727
import 'package:flutter_news_app_web_dashboard_full_source_code/content_management/widgets/filter_dialog/filter_dialog.dart';
28+
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart';
2829
import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_management/bloc/filter_local_ads/filter_local_ads_bloc.dart';
2930
import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_management/view/create_local_banner_ad_page.dart';
3031
import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_management/view/create_local_interstitial_ad_page.dart';
@@ -38,6 +39,7 @@ import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_manage
3839
import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_management/widgets/local_ads_filter_dialog/bloc/local_ads_filter_dialog_bloc.dart';
3940
import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_management/widgets/local_ads_filter_dialog/local_ads_filter_dialog.dart';
4041
import 'package:flutter_news_app_web_dashboard_full_source_code/overview/view/overview_page.dart';
42+
import 'package:flutter_news_app_web_dashboard_full_source_code/router/route_permissions.dart';
4143
import 'package:flutter_news_app_web_dashboard_full_source_code/router/routes.dart';
4244
import 'package:flutter_news_app_web_dashboard_full_source_code/settings/view/settings_page.dart';
4345
import 'package:flutter_news_app_web_dashboard_full_source_code/shared/widgets/selection_page/searchable_selection_page.dart';
@@ -64,6 +66,7 @@ GoRouter createRouter({
6466
// --- Redirect Logic ---
6567
redirect: (BuildContext context, GoRouterState state) {
6668
final appStatus = context.read<AppBloc>().state.status;
69+
final l10n = AppLocalizationsX(context).l10n;
6770
final currentLocation = state.matchedLocation;
6871

6972
print(
@@ -95,6 +98,32 @@ GoRouter createRouter({
9598
if (appStatus == AppStatus.authenticated) {
9699
print(' Redirect Decision: User is $appStatus.');
97100

101+
// --- Role-Based Access Control (RBAC) ---
102+
final userRole = context.read<AppBloc>().state.user?.dashboardRole;
103+
final destinationRouteName = state.topRoute?.name;
104+
105+
// Allow navigation if role is not yet determined or route is unknown.
106+
if (userRole == null || destinationRouteName == null) {
107+
return null;
108+
}
109+
110+
final allowedRoutes = routePermissions[userRole];
111+
112+
// Check if the user is trying to access a route they are not
113+
// permitted to view.
114+
final isAuthorized = allowedRoutes?.contains(destinationRouteName) ??
115+
false;
116+
117+
// Universally allowed routes like 'settings' are exempt from this check.
118+
if (!isAuthorized && destinationRouteName != Routes.settingsName) {
119+
ScaffoldMessenger.of(context).showSnackBar(
120+
SnackBar(content: Text(l10n.unauthorizedAccessRedirect)),
121+
);
122+
// Redirect unauthorized users to the overview page.
123+
return Routes.overview;
124+
}
125+
// --- End of RBAC ---
126+
98127
// If an authenticated user is on any authentication-related path:
99128
if (isGoingToAuth) {
100129
print(

0 commit comments

Comments
 (0)