selfprivacy.org.app/lib/ui/pages/ssh_keys/ssh_keys.dart

145 lines
5.9 KiB
Dart
Raw Normal View History

2022-03-23 14:07:52 +00:00
import 'package:cubit_form/cubit_form.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
2022-03-23 14:07:52 +00:00
import 'package:selfprivacy/logic/cubit/forms/user/ssh_form_cubit.dart';
import 'package:selfprivacy/logic/models/job.dart';
import 'package:selfprivacy/ui/components/brand_bottom_sheet/brand_bottom_sheet.dart';
import 'package:selfprivacy/ui/components/brand_cards/brand_cards.dart';
import 'package:selfprivacy/ui/components/brand_hero_screen/brand_hero_screen.dart';
import 'package:selfprivacy/ui/components/brand_icons/brand_icons.dart';
import 'package:selfprivacy/config/brand_colors.dart';
import 'package:selfprivacy/config/brand_theme.dart';
2022-08-30 03:09:09 +00:00
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
import 'package:selfprivacy/logic/models/hive/user.dart';
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';
import 'package:selfprivacy/ui/components/brand_header/brand_header.dart';
2022-03-23 14:07:52 +00:00
part 'new_ssh_key.dart';
// Get user object as a parameter
class SshKeysPage extends StatefulWidget {
const SshKeysPage({required this.user, final super.key});
final User user;
@override
State<SshKeysPage> createState() => _SshKeysPageState();
}
class _SshKeysPageState extends State<SshKeysPage> {
@override
Widget build(final BuildContext context) => BrandHeroScreen(
heroTitle: 'ssh.title'.tr(),
heroSubtitle: widget.user.login,
heroIcon: BrandIcons.key,
children: <Widget>[
if (widget.user.login == 'root')
Column(
children: [
// Show alert card if user is root
BrandCards.outlined(
child: ListTile(
leading: Icon(
Icons.warning_rounded,
color: Theme.of(context).colorScheme.error,
),
title: Text('ssh.root.title'.tr()),
subtitle: Text('ssh.root.subtitle'.tr()),
),
)
],
),
BrandCards.outlined(
child: Column(
children: <Widget>[
ListTile(
title: Text(
'ssh.create'.tr(),
style: Theme.of(context).textTheme.headline6,
),
leading: const Icon(Icons.add_circle_outline_rounded),
onTap: () {
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (final BuildContext context) => Padding(
padding: MediaQuery.of(context).viewInsets,
child: _NewSshKey(widget.user),
),
);
},
),
const Divider(height: 0),
// show a list of ListTiles with ssh keys
// Clicking on one should delete it
Column(
children: widget.user.sshKeys.map((final String key) {
final publicKey =
key.split(' ').length > 1 ? key.split(' ')[1] : key;
final keyType = key.split(' ')[0];
final keyName = key.split(' ').length > 2
? key.split(' ')[2]
: 'ssh.no_key_name'.tr();
return ListTile(
2022-03-23 14:07:52 +00:00
title: Text('$keyName ($keyType)'),
// do not overflow text
subtitle: Text(
publicKey,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
onTap: () {
2022-03-23 14:07:52 +00:00
showDialog(
context: context,
builder: (final BuildContext context) => AlertDialog(
title: Text('ssh.delete'.tr()),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('ssh.delete_confirm_question'.tr()),
Text('$keyName ($keyType)'),
Text(publicKey),
],
2022-03-23 14:07:52 +00:00
),
),
actions: <Widget>[
TextButton(
child: Text('basis.cancel'.tr()),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text(
'basis.delete'.tr(),
style: const TextStyle(
color: BrandColors.red1,
2022-03-23 14:07:52 +00:00
),
),
onPressed: () {
context.read<JobsCubit>().addJob(
DeleteSSHKeyJob(
user: widget.user,
publicKey: key,
),
);
Navigator.of(context)
..pop()
..pop();
},
),
],
),
2022-03-23 14:07:52 +00:00
);
},
);
}).toList(),
)
],
),
),
],
);
}