Skip to content

Commit

Permalink
Add user settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored May 29, 2024
1 parent 4ab13af commit 4e013e3
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/api/api_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ApiResult<T> {
if (ok) {
return data!;
} else {
return throw error!;
return throw (status, error!);
}
}

Expand Down
26 changes: 26 additions & 0 deletions lib/api/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ enum SortByGid {
abstract class _EHApi {
factory _EHApi(Dio dio, {required String baseUrl}) = __EHApi;

@POST('/user/change_name')
@MultiPart()
Future<ApiResult<BUser>> changeUserName(
@Part(name: "username") String username,
{@CancelRequest() CancelToken? cancel});
@POST('/user/change_password')
@MultiPart()
// ignore: unused_element
Future<ApiResult<dynamic>> _changeUserPassword(
@Part(name: "old") String oldPassword,
@Part(name: "t") int t,
@Part(name: "new") String newPassword,
// ignore: unused_element
{@CancelRequest() CancelToken? cancel});
@PUT('/user')
@MultiPart()
Future<ApiResult<int>> createUser(
Expand Down Expand Up @@ -364,4 +378,16 @@ class EHApi extends __EHApi {
.replace(queryParameters: queries);
return newUri.toString();
}

Future<ApiResult<dynamic>> changeUserPassword(
String oldPassword, String newPassword,
{CancelToken? cancel}) async {
int t = DateTime.now().millisecondsSinceEpoch;
final p = await _pbkdf2a.deriveKeyFromPassword(
password: oldPassword, nonce: _salt);
final p2 = await _pbkdf2b.deriveKey(
secretKey: p, nonce: _utf8Encoder.convert(t.toString()));
final p3 = base64Encode(await p2.extractBytes());
return await _changeUserPassword(p3, t, newPassword, cancel: cancel);
}
}
90 changes: 90 additions & 0 deletions lib/api/client.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@ class AuthInfo {
_isChecking = false;
}
}

void setUpdatedUser(BUser u) {
_user = u;
_log.info(
"User updated: ${u.username} (${u.id}), isAdmin: ${u.isAdmin}. permissions: ${u.permissions}");
listener.tryEmit("user_logined", null);
}
}
48 changes: 27 additions & 21 deletions lib/components/user_permissions_chips.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../api/user.dart';

class UserPermissionsChips extends StatefulWidget {
const UserPermissionsChips({super.key, this.permissions, this.onChanged});
const UserPermissionsChips(
{super.key, this.permissions, this.onChanged, this.readOnly = false});
final UserPermissions? permissions;
final ValueChanged<UserPermissions>? onChanged;
final bool readOnly;

@override
State<StatefulWidget> createState() => _UserPermissionsChips();
Expand All @@ -31,32 +33,36 @@ class _UserPermissionsChips extends State<UserPermissionsChips> {
FilterChip(
label: Text(i18n.allPermissions),
selected: _permissions.isAll,
onSelected: (bool value) {
setState(() {
if (value) {
_permissions.code = userPermissionAll;
} else {
_permissions.code = 0;
}
});
_onChanged();
},
onSelected: widget.readOnly
? null
: (bool value) {
setState(() {
if (value) {
_permissions.code = userPermissionAll;
} else {
_permissions.code = 0;
}
});
_onChanged();
},
)
];
for (var flag in UserPermission.values) {
list.add(FilterChip(
label: Text(flag.localText(context)),
selected: _permissions.has(flag),
onSelected: (bool value) {
setState(() {
if (value) {
_permissions.add(flag);
} else {
_permissions.remove(flag);
}
});
_onChanged();
},
onSelected: widget.readOnly
? null
: (bool value) {
setState(() {
if (value) {
_permissions.add(flag);
} else {
_permissions.remove(flag);
}
});
_onChanged();
},
));
}
return Wrap(spacing: 5.0, children: list);
Expand Down
12 changes: 11 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,15 @@
"failedDeleteUser": "Failed to delete user: ",
"deleteUser": "Delete user",
"display": "Display",
"cache": "Cache"
"cache": "Cache",
"cancel": "Cancel",
"changeUsername": "Change username",
"failedChangeUsername": "Failed to change username: ",
"usernameIsAlreadyUsed": "The username is already used by another user.",
"changePassword": "Change password",
"failedChangePassword": "Failed to change password: ",
"oldPassword": "Old password",
"newPassword": "New password",
"incorrectPassword": "Incorrect password.",
"changedPasswordSuccessfully": "Changed password successfully."
}
12 changes: 11 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,15 @@
"failedDeleteUser": "删除用户失败:",
"deleteUser": "删除用户",
"display": "显示",
"cache": "缓存"
"cache": "缓存",
"cancel": "取消",
"changeUsername": "修改用户名",
"failedChangeUsername": "修改用户名失败:",
"usernameIsAlreadyUsed": "此用户名已被其他用户占用。",
"changePassword": "修改密码",
"failedChangePassword": "修改密码失败:",
"oldPassword": "旧密码",
"newPassword": "新密码",
"incorrectPassword": "不正确的密码。",
"changedPasswordSuccessfully": "修改密码成功。"
}
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'pages/settings/cache.dart';
import 'pages/settings/display.dart';
import 'pages/settings/server.dart';
import 'pages/settings/server_url.dart';
import 'pages/settings/user.dart';
import 'pages/task_manager.dart';
import 'pages/users.dart';
import 'utils.dart';
Expand Down Expand Up @@ -249,6 +250,10 @@ final _router = GoRouter(
path: CacheSettingsPage.routeName,
builder: (context, state) => CacheSettingsPage(key: state.pageKey),
),
GoRoute(
path: UserSettingsPage.routeName,
builder: (context, state) => UserSettingsPage(key: state.pageKey),
),
],
);

Expand Down
8 changes: 8 additions & 0 deletions lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class _SettingsPage extends State<SettingsPage>
context.push("/settings/server/url");
})
: Container(),
auth.isAuthed
? ListTile(
leading: const Icon(Icons.account_circle),
title: Text(i18n.user),
onTap: () {
context.push("/settings/user");
})
: Container(),
ListTile(
leading: const Icon(Icons.display_settings),
title: Text(i18n.display),
Expand Down
Loading

0 comments on commit 4e013e3

Please sign in to comment.