conflicts resolution
This commit is contained in:
105
frontend/pweb/lib/services/accounts.dart
Normal file
105
frontend/pweb/lib/services/accounts.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:pshared/api/requests/login_data.dart';
|
||||
import 'package:pshared/models/account/account.dart';
|
||||
import 'package:pshared/models/describable.dart';
|
||||
import 'package:pshared/models/storable.dart';
|
||||
|
||||
import 'mock_ids.dart';
|
||||
|
||||
class InvalidCredentialsException implements Exception {
|
||||
@override
|
||||
String toString() => 'InvalidCredentialsException';
|
||||
}
|
||||
|
||||
class DuplicateAccountException implements Exception {
|
||||
@override
|
||||
String toString() => 'DuplicateAccountException';
|
||||
}
|
||||
|
||||
class AccountLoginResult {
|
||||
final Account account;
|
||||
final String roleId;
|
||||
|
||||
const AccountLoginResult({
|
||||
required this.account,
|
||||
required this.roleId,
|
||||
});
|
||||
}
|
||||
|
||||
class _AccountRecord {
|
||||
final Account account;
|
||||
final String password;
|
||||
final String roleId;
|
||||
|
||||
const _AccountRecord({
|
||||
required this.account,
|
||||
required this.password,
|
||||
required this.roleId,
|
||||
});
|
||||
}
|
||||
|
||||
class AccountsService {
|
||||
final List<_AccountRecord> _accounts = [
|
||||
_AccountRecord(
|
||||
account: Account(
|
||||
storable: newStorable(id: companyAccountRef),
|
||||
describable: newDescribable(name: 'Sendico Company'),
|
||||
avatarUrl: null,
|
||||
lastName: 'Owner',
|
||||
login: 'company@sendico.com',
|
||||
locale: 'ru',
|
||||
),
|
||||
password: 'password123A',
|
||||
roleId: companyRoleId,
|
||||
),
|
||||
_AccountRecord(
|
||||
account: Account(
|
||||
storable: newStorable(id: recipientAccountRef),
|
||||
describable: newDescribable(name: 'John Recipient'),
|
||||
avatarUrl: null,
|
||||
lastName: 'Doe',
|
||||
login: 'recipient@sendico.com',
|
||||
locale: 'ru',
|
||||
),
|
||||
password: 'password123A',
|
||||
roleId: recipientRoleId,
|
||||
),
|
||||
];
|
||||
|
||||
Future<AccountLoginResult> login(String email, String password, {String? locale}) async {
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
final normalized = email.trim().toLowerCase();
|
||||
final record = _accounts.where((acc) => acc.account.login.toLowerCase() == normalized).singleOrNull;
|
||||
|
||||
if (record == null || record.password != password) {
|
||||
throw InvalidCredentialsException();
|
||||
}
|
||||
|
||||
return AccountLoginResult(account: record.account, roleId: record.roleId);
|
||||
}
|
||||
|
||||
Future<AccountLoginResult> signup(AccountData data, {String roleId = recipientRoleId}) async {
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
|
||||
final normalized = data.login.trim().toLowerCase();
|
||||
if (_accounts.any((acc) => acc.account.login.toLowerCase() == normalized)) {
|
||||
throw DuplicateAccountException();
|
||||
}
|
||||
|
||||
final account = Account(
|
||||
storable: newStorable(id: 'account-${_accounts.length + 1}'),
|
||||
describable: newDescribable(name: data.name),
|
||||
avatarUrl: null,
|
||||
lastName: data.lastName,
|
||||
login: normalized,
|
||||
locale: data.locale,
|
||||
);
|
||||
|
||||
_accounts.add(_AccountRecord(account: account, password: data.password, roleId: roleId));
|
||||
return AccountLoginResult(account: account, roleId: roleId);
|
||||
}
|
||||
|
||||
Account? getByRef(String accountRef) => _accounts.where((acc) => acc.account.id == accountRef).singleOrNull?.account;
|
||||
}
|
||||
18
frontend/pweb/lib/services/auth.dart
Normal file
18
frontend/pweb/lib/services/auth.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
class AuthenticationService {
|
||||
Future<bool> verifyTwoFactorCode(String code) async {
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
|
||||
if (code == '000000') {
|
||||
return true;
|
||||
} else {
|
||||
throw const WrongCodeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WrongCodeException implements Exception {
|
||||
const WrongCodeException();
|
||||
|
||||
@override
|
||||
String toString() => 'WrongCodeException';
|
||||
}
|
||||
12
frontend/pweb/lib/services/mock_ids.dart
Normal file
12
frontend/pweb/lib/services/mock_ids.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
// Centralized identifiers for mock auth/permission data to keep the
|
||||
// mock services in sync and make it easy to swap in a real API later.
|
||||
const String mockOrganizationRef = 'org-sendico';
|
||||
|
||||
const String companyRoleId = 'role-company';
|
||||
const String recipientRoleId = 'role-recipient';
|
||||
|
||||
const String companyAccountRef = 'account-company';
|
||||
const String recipientAccountRef = 'account-recipient';
|
||||
|
||||
const String accountsPolicyDescriptionId = 'policy-accounts';
|
||||
const String rolesPolicyDescriptionId = 'policy-roles';
|
||||
Reference in New Issue
Block a user