Added server dashboard button to menu

The button is only shown if the user has
any of the management permissions.
This commit is contained in:
Jakob Meier 2023-03-23 10:51:34 +01:00
parent 5c8c108818
commit aee9c9f82e
No known key found for this signature in database
GPG key ID: 66BDC7E6A01A6152
6 changed files with 141 additions and 63 deletions

View file

@ -12,34 +12,48 @@ class ServerPermission {
static int get none {
return oB("0000000000000000");
}
// default permission set
static int get def {
return oB("0000000000000011");
}
static int get canUseAPI {
return oB("0000000000000001");
}
static int get provideCert {
return oB("0000000000000010");
}
static int get manageOTA {
return oB("0000010000000000");
}
static int get manageServerProductList {
return oB("0000100000000000");
}
static int get viewUsersAndLists {
return oB("0001000000000000");
}
static int get editSettings {
return oB("0010000000000000");
}
static int get editPermissions {
return oB("0100000000000000");
}
static int get editUsers {
return oB("1000000000000000");
}
static int get allManagement {
return oB("1111110000000000");
}
static int get all {
return oB("1111111111111111");
}

View file

@ -36,3 +36,31 @@ class User {
stream.listen(cb);
}
}
class AccountMeta {
final int permissions;
final String username;
final bool discvoverable;
final int maxRoomCount;
final int maxRoomSize;
final int maxRoomMemberCount;
const AccountMeta(
{required this.permissions,
required this.username,
required this.maxRoomSize,
required this.maxRoomCount,
required this.maxRoomMemberCount,
required this.discvoverable});
factory AccountMeta.fromJSON(dynamic json) {
return AccountMeta(
permissions: json['rights'],
username: json['name'],
maxRoomSize: json['maxRoomSize'],
maxRoomCount: json['maxRooms'],
maxRoomMemberCount: json['maxUsersPerRoom'],
discvoverable: json['viewable'] == 1
);
}
}

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:outbag_app/backend/user.dart';
import 'package:outbag_app/screens/room/join.dart';
import 'package:outbag_app/screens/room/new.dart';
import 'package:provider/provider.dart';
import './screens/home.dart';
import './screens/welcome.dart';
import './screens/room/main.dart';
@ -16,7 +17,7 @@ final routesUnauthorized = RouteMap(routes: {
'/signupOTA': (_) =>
const MaterialPage(child: AuthPage(mode: Mode.signupOTA)),
'/signin': (_) => const MaterialPage(child: AuthPage(mode: Mode.signin)),
}, onUnknownRoute: (_) => const MaterialPage(child: WelcomePage()));
}, onUnknownRoute: (_) => const MaterialPage(child: WelcomePage()));
// routes when user is logged in
final routesLoggedIn = RouteMap(routes: {
@ -29,7 +30,7 @@ final routesLoggedIn = RouteMap(routes: {
return MaterialPage(child: RoomPage(server, tag));
},
}, onUnknownRoute: (_) => const Redirect('/'));
}, onUnknownRoute: (_) => const Redirect('/'));
void main() {
WidgetsFlutterBinding.ensureInitialized();
@ -48,6 +49,7 @@ class _OutbagAppState extends State {
// unless not userdata is found
// or the userdata turns out to be wrong
bool isAuthorized = true;
AccountMeta? info;
@override
void initState() {
@ -75,15 +77,16 @@ class _OutbagAppState extends State {
credentials: credentials,
body: {});
if (resp.res == Result.ok) {
print(resp.body);
final info = AccountMeta.fromJSON(resp.body['data']);
setState(() {
isAuthorized = true;
this.info = info;
});
} else {
// credentials are wrong
// log out
setState(() {
isAuthorized = true;
isAuthorized = false;
});
}
} catch (_) {
@ -110,7 +113,11 @@ class _OutbagAppState extends State {
@override
Widget build(BuildContext context) {
return MaterialApp.router(
return MultiProvider(
providers: [
Provider<AccountMeta?>.value(value: info,),
],
child: MaterialApp.router(
title: "Outbag",
// TODO: change back to system (or load from disk)
//themeMode: ThemeMode.system,
@ -121,6 +128,6 @@ class _OutbagAppState extends State {
routesBuilder: (context) =>
isAuthorized ? routesLoggedIn : routesUnauthorized),
routeInformationParser: const RoutemasterParser(),
);
));
}
}

View file

@ -2,8 +2,10 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:outbag_app/backend/permissions.dart';
import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/backend/user.dart';
import 'package:provider/provider.dart';
import 'package:routemaster/routemaster.dart';
import '../backend/room.dart';
@ -103,6 +105,16 @@ class _HomePageState extends State<HomePage> {
// show settings screen
Routemaster.of(context).push("/settings");
}),
...(context.watch<AccountMeta?>() != null &&
(context.watch<AccountMeta?>()?.permissions)! & ServerPermission.allManagement != 0)?[
MenuItemButton(
leadingIcon: const Icon(Icons.dns),
child: const Text('Server Dashboard'),
onPressed: () {
// show settings screen
Routemaster.of(context).push("/server");
}),
]:[],
MenuItemButton(
leadingIcon: const Icon(Icons.info_rounded),
child: const Text('About'),

View file

@ -184,6 +184,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.0"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
path:
dependency: transitive
description:
@ -280,6 +288,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.2.4"
provider:
dependency: "direct main"
description:
name: provider
sha256: cdbe7530b12ecd9eb455bdaa2fcb8d4dad22e80b8afb4798b41479d5ce26847f
url: "https://pub.dev"
source: hosted
version: "6.0.5"
routemaster:
dependency: "direct main"
description:

View file

@ -40,6 +40,7 @@ dependencies:
http: ^0.13.5
localstore: ^1.3.5
crypto: ^3.0.2
provider: ^6.0.5
dev_dependencies:
flutter_test: