Files
Docwell/Phone-App/lib/main.dart
T
tajniak81andClaude Opus 5.5 03f3434c10 Add organizations with cascading user rights
Same model as DriverVault, with organizations above workspaces:

- users get a role (user, admin, superadmin) and at most one
  organization; existing instance admins become superadmins
- rights cascade: superadmins own everything, org admins own their
  organization's workspaces, others get their workspace role
  (owner > admin > member > guest) while they stay in the org
- nobody grants a role above their own, acts on someone ranked above
  them, or changes their own role; rules live in internal/access as
  pure, tested functions
- self-service organizations: an org-less user creates one and becomes
  its admin; the last admin can delete it and is demoted
- endpoints for users, orgs, workspaces and members; token cache is
  dropped when a user's rights change
- admin panel: Users and Organizations pages scoped to the caller
- web app: organization page, workspace list, workspace settings with
  member management (EN/PL)
- fix: organization name missing after login/refresh, and stale tokens
  redirecting public pages to sign-in

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 22:53:11 +01:00

247 lines
7.1 KiB
Dart

// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:flutter/material.dart';
import 'api/docwell_api.dart';
void main() {
runApp(const DocwellApp());
}
// Android emulators reach the host machine at 10.0.2.2.
const defaultServerUrl = 'http://10.0.2.2:8080';
class DocwellApp extends StatefulWidget {
const DocwellApp({super.key, this.api});
final DocwellApi? api;
@override
State<DocwellApp> createState() => _DocwellAppState();
}
class _DocwellAppState extends State<DocwellApp> {
late final DocwellApi _api = widget.api ?? DocwellApi(defaultServerUrl);
// Kept in memory only for now; secure storage comes with the full mobile app.
User? _user;
@override
Widget build(BuildContext context) {
const brand = Color(0xFF2F5FD0);
return MaterialApp(
title: 'Docwell',
theme: ThemeData(colorSchemeSeed: brand, useMaterial3: true),
darkTheme: ThemeData(
colorSchemeSeed: brand,
brightness: Brightness.dark,
useMaterial3: true,
),
home: _user == null
? LoginScreen(api: _api, onSignedIn: (u) => setState(() => _user = u))
: HomeScreen(
api: _api,
user: _user!,
onSignOut: () => setState(() {
_api.logout();
_user = null;
}),
),
);
}
}
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key, required this.api, required this.onSignedIn});
final DocwellApi api;
final ValueChanged<User> onSignedIn;
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _email = TextEditingController();
final _password = TextEditingController();
bool _busy = false;
String? _error;
static const _messages = {
'invalid_credentials': 'Wrong email or password.',
'account_disabled': 'This account is disabled.',
'mfa_required': 'Two-step sign-in is not supported in the app yet.',
};
Future<void> _submit() async {
setState(() {
_busy = true;
_error = null;
});
try {
widget.onSignedIn(
await widget.api.login(_email.text.trim(), _password.text),
);
} on DocwellApiException catch (e) {
setState(() => _error = _messages[e.code] ?? 'Sign-in failed.');
} catch (_) {
setState(() => _error = "Can't reach the server.");
} finally {
if (mounted) setState(() => _busy = false);
}
}
@override
void dispose() {
_email.dispose();
_password.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: AutofillGroup(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Sign in to Docwell',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 24),
TextField(
key: const Key('email'),
controller: _email,
keyboardType: TextInputType.emailAddress,
autofillHints: const [AutofillHints.email],
decoration: const InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
TextField(
key: const Key('password'),
controller: _password,
obscureText: true,
autofillHints: const [AutofillHints.password],
onSubmitted: (_) => _submit(),
decoration: const InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
),
),
if (_error != null) ...[
const SizedBox(height: 12),
Text(
_error!,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
),
),
],
const SizedBox(height: 20),
FilledButton(
onPressed: _busy ? null : _submit,
child: const Text('Sign in'),
),
],
),
),
),
),
),
),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({
super.key,
required this.api,
required this.user,
required this.onSignOut,
});
final DocwellApi api;
final User user;
final VoidCallback onSignOut;
@override
State<HomeScreen> createState() => _HomeScreenState();
}
String _roleLabel(String role) => switch (role) {
'superadmin' => 'Superadmin',
'admin' => 'Admin',
_ => 'User',
};
class _HomeScreenState extends State<HomeScreen> {
late Future<String> _version = widget.api.version();
void _retry() => setState(() => _version = widget.api.version());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Docwell'),
actions: [
TextButton(
onPressed: widget.onSignOut,
child: const Text('Sign out'),
),
],
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Hello, ${widget.user.name}',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 4),
Text(
widget.user.organizationName.isEmpty
? _roleLabel(widget.user.role)
: '${_roleLabel(widget.user.role)} · ${widget.user.organizationName}',
),
const SizedBox(height: 16),
FutureBuilder<String>(
future: _version,
builder: (context, snap) {
if (snap.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
if (snap.hasError) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Server unreachable'),
const SizedBox(height: 12),
FilledButton(
onPressed: _retry,
child: const Text('Retry'),
),
],
);
}
return Text('Connected · v${snap.data}');
},
),
],
),
),
);
}
}