Skip to content

Commit

Permalink
feat: Enhance user permissions handling when no users on server
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Dec 25, 2024
1 parent 69c52ee commit 93b8cc8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
21 changes: 13 additions & 8 deletions lib/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AuthInfo {
ServerStatus? get status => _status;
Token? _token;
Token? get token => _token;
bool? get noUser => _status?.noUser;
SharedToken? _sharedToken;
SharedToken? get sharedToken => _sharedToken;
bool get isAuthed => (_user != null);
Expand All @@ -26,14 +27,18 @@ class AuthInfo {
bool? get isAdmin => _user?.isAdmin;
bool? get isRoot => _user != null ? _user!.id == 0 : null;
bool? get isDocker => _status?.isDocker;
bool? get canReadGallery =>
_user?.permissions.has(UserPermission.readGallery);
bool? get canEditGallery =>
_user?.permissions.has(UserPermission.editGallery);
bool? get canDeleteGallery =>
_user?.permissions.has(UserPermission.deleteGallery);
bool? get canManageTasks =>
_user?.permissions.has(UserPermission.manageTasks);
bool? get canReadGallery => noUser == true
? true
: _user?.permissions.has(UserPermission.readGallery);
bool? get canEditGallery => noUser == true
? true
: _user?.permissions.has(UserPermission.editGallery);
bool? get canDeleteGallery => noUser == true
? true
: _user?.permissions.has(UserPermission.deleteGallery);
bool? get canManageTasks => noUser == true
? true
: _user?.permissions.has(UserPermission.manageTasks);
bool? get canShareGallery =>
_user?.permissions.has(UserPermission.shareGallery);
MeilisearchInfo? get meilisearch => _status?.meilisearch;
Expand Down
21 changes: 13 additions & 8 deletions lib/dialog/new_user_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class _NewUserPage extends State<NewUserPage> {
}
final i18n = AppLocalizations.of(context)!;
final maxWidth = MediaQuery.of(context).size.width;
if (auth.noUser == true) {
_isAdmin = true;
}
return Container(
padding: maxWidth < 400
? const EdgeInsets.symmetric(vertical: 20, horizontal: 5)
Expand Down Expand Up @@ -159,16 +162,18 @@ class _NewUserPage extends State<NewUserPage> {
},
obscureText: !_passwordVisible,
)),
auth.isRoot == true
auth.isRoot == true || auth.noUser == true
? _buildWithVecticalPadding(LabeledCheckbox(
value: _isAdmin,
onChanged: (b) {
if (b != null) {
setState(() {
_isAdmin = b;
});
}
},
onChanged: auth.noUser == true
? null
: (b) {
if (b != null) {
setState(() {
_isAdmin = b;
});
}
},
label: Text(i18n.admin)))
: Container(),
!_isAdmin
Expand Down
2 changes: 1 addition & 1 deletion lib/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ List<PopupMenuEntry<MoreVertSettings>> buildMoreVertSettings(
list.add(PopupMenuItem(
value: MoreVertSettings.taskManager, child: Text(i18n.taskManager)));
}
if (path != "/sessions") {
if (path != "/sessions" && auth.noUser != true) {
list.add(PopupMenuItem(
value: MoreVertSettings.sessions, child: Text(i18n.sessionManagemant)));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class HomeDrawer extends StatelessWidget {
},
)
: Container(),
auth.isAdmin == true
auth.isAdmin == true || auth.noUser == true
? ListTile(
leading: const Icon(Icons.manage_accounts),
title: Text(i18n.userManagemant),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class _SettingsPage extends State<SettingsPage>
onTap: () {
context.push("/settings/cache");
}),
auth.isAdmin == true
auth.isAdmin == true || auth.noUser == true
? ListTile(
leading: const Icon(Icons.admin_panel_settings),
title: Text(i18n.server),
Expand Down

0 comments on commit 93b8cc8

Please sign in to comment.