Files
Docwell/Phone-App/lib/api/docwell_api.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

108 lines
3.0 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
/// Minimal docwell-server client. Will be replaced by a client generated
/// from api/openapi.yaml.
class DocwellApi {
DocwellApi(String baseUrl, {http.Client? client})
: baseUrl = baseUrl.replaceAll(RegExp(r'/+$'), ''),
_client = client ?? http.Client();
final String baseUrl;
final http.Client _client;
/// PocketBase auth token of the signed-in user, sent as a bearer token.
String? token;
Future<String> version() async {
final body = await _send('GET', '/api/v1/version');
return body['version'] as String;
}
/// Signs in with email and password and keeps the token for later calls.
Future<User> login(String email, String password) async {
final body = await _send('POST', '/api/v1/auth/login', {
'email': email,
'password': password,
});
token = body['token'] as String;
return User.fromJson(body['user'] as Map<String, dynamic>);
}
Future<User> me() async =>
User.fromJson(await _send('GET', '/api/v1/auth/me'));
void logout() => token = null;
Future<Map<String, dynamic>> _send(
String method,
String path, [
Object? body,
]) async {
final req = http.Request(method, Uri.parse('$baseUrl$path'));
req.headers['Accept'] = 'application/json';
if (token != null) req.headers['Authorization'] = 'Bearer $token';
if (body != null) {
req.headers['Content-Type'] = 'application/json';
req.body = jsonEncode(body);
}
final res = await http.Response.fromStream(
await _client.send(req).timeout(const Duration(seconds: 15)),
);
final data = res.body.isEmpty
? <String, dynamic>{}
: jsonDecode(res.body) as Map<String, dynamic>;
if (res.statusCode >= 300) {
throw DocwellApiException(
res.statusCode,
data['error'] as String? ?? 'unknown',
);
}
return data;
}
}
class User {
User({
required this.id,
required this.email,
required this.name,
this.role = 'user',
this.organization = '',
this.organizationName = '',
});
factory User.fromJson(Map<String, dynamic> json) => User(
id: json['id'] as String,
email: json['email'] as String,
name: json['name'] as String,
role: json['role'] as String? ?? 'user',
organization: json['organization'] as String? ?? '',
organizationName: json['organizationName'] as String? ?? '',
);
final String id;
final String email;
final String name;
/// Instance role: user, admin (of their organization) or superadmin.
final String role;
final String organization;
final String organizationName;
bool get isManager => role == 'admin' || role == 'superadmin';
}
class DocwellApiException implements Exception {
DocwellApiException(this.status, [this.code = 'unknown']);
final int status;
/// Machine-readable error code from the server, e.g. "invalid_credentials".
final String code;
@override
String toString() => 'Docwell API error $status ($code)';
}