Initial commit: unionflow-mobile-apps
Application Flutter complète (sans build artifacts). Signed-off-by: lions dev Team
This commit is contained in:
91
lib/features/admin/bloc/admin_users_bloc.dart
Normal file
91
lib/features/admin/bloc/admin_users_bloc.dart
Normal file
@@ -0,0 +1,91 @@
|
||||
library admin_users_bloc;
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import '../data/models/admin_user_model.dart';
|
||||
import '../data/repositories/admin_user_repository.dart';
|
||||
part 'admin_users_event.dart';
|
||||
part 'admin_users_state.dart';
|
||||
|
||||
@injectable
|
||||
class AdminUsersBloc extends Bloc<AdminUsersEvent, AdminUsersState> {
|
||||
final AdminUserRepository _repository;
|
||||
|
||||
AdminUsersBloc(this._repository) : super(AdminUsersInitial()) {
|
||||
on<AdminUsersLoadRequested>(_onLoadRequested);
|
||||
on<AdminUserDetailRequested>(_onDetailRequested);
|
||||
on<AdminUserDetailWithRolesRequested>(_onDetailWithRolesRequested);
|
||||
on<AdminUserRolesUpdateRequested>(_onRolesUpdateRequested);
|
||||
on<AdminRolesLoadRequested>(_onRolesLoadRequested);
|
||||
}
|
||||
|
||||
Future<void> _onLoadRequested(AdminUsersLoadRequested e, Emitter<AdminUsersState> emit) async {
|
||||
emit(AdminUsersLoading());
|
||||
try {
|
||||
final result = await _repository.search(
|
||||
page: e.page ?? 0,
|
||||
size: e.size ?? 20,
|
||||
search: e.search,
|
||||
);
|
||||
emit(AdminUsersLoaded(
|
||||
users: result.users,
|
||||
totalCount: result.totalCount,
|
||||
currentPage: result.currentPage,
|
||||
pageSize: result.pageSize,
|
||||
totalPages: result.totalPages,
|
||||
));
|
||||
} catch (err) {
|
||||
emit(AdminUsersError(err.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onDetailRequested(AdminUserDetailRequested e, Emitter<AdminUsersState> emit) async {
|
||||
emit(AdminUsersLoading());
|
||||
try {
|
||||
final user = await _repository.getById(e.userId);
|
||||
if (user == null) {
|
||||
emit(AdminUsersError('Utilisateur non trouvé'));
|
||||
return;
|
||||
}
|
||||
final roles = await _repository.getUserRoles(e.userId);
|
||||
emit(AdminUserDetailLoaded(user: user, userRoles: roles));
|
||||
} catch (err) {
|
||||
emit(AdminUsersError(err.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onDetailWithRolesRequested(AdminUserDetailWithRolesRequested e, Emitter<AdminUsersState> emit) async {
|
||||
emit(AdminUsersLoading());
|
||||
try {
|
||||
final user = await _repository.getById(e.userId);
|
||||
if (user == null) {
|
||||
emit(AdminUsersError('Utilisateur non trouvé'));
|
||||
return;
|
||||
}
|
||||
final userRoles = await _repository.getUserRoles(e.userId);
|
||||
final allRoles = await _repository.getRealmRoles();
|
||||
emit(AdminUserDetailLoaded(user: user, userRoles: userRoles, allRoles: allRoles));
|
||||
} catch (err) {
|
||||
emit(AdminUsersError(err.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onRolesUpdateRequested(AdminUserRolesUpdateRequested e, Emitter<AdminUsersState> emit) async {
|
||||
try {
|
||||
await _repository.setUserRoles(e.userId, e.roleNames);
|
||||
emit(AdminUserRolesUpdated());
|
||||
add(AdminUserDetailWithRolesRequested(e.userId));
|
||||
} catch (err) {
|
||||
emit(AdminUsersError(err.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onRolesLoadRequested(AdminRolesLoadRequested e, Emitter<AdminUsersState> emit) async {
|
||||
try {
|
||||
final roles = await _repository.getRealmRoles();
|
||||
emit(AdminRolesLoaded(roles));
|
||||
} catch (err) {
|
||||
emit(AdminUsersError(err.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
29
lib/features/admin/bloc/admin_users_event.dart
Normal file
29
lib/features/admin/bloc/admin_users_event.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
part of 'admin_users_bloc.dart';
|
||||
|
||||
abstract class AdminUsersEvent {}
|
||||
|
||||
class AdminUsersLoadRequested extends AdminUsersEvent {
|
||||
final int page;
|
||||
final int size;
|
||||
final String? search;
|
||||
AdminUsersLoadRequested({this.page = 0, this.size = 20, this.search});
|
||||
}
|
||||
|
||||
class AdminUserDetailRequested extends AdminUsersEvent {
|
||||
final String userId;
|
||||
AdminUserDetailRequested(this.userId);
|
||||
}
|
||||
|
||||
/// Charge détail utilisateur + liste complète des rôles (pour édition)
|
||||
class AdminUserDetailWithRolesRequested extends AdminUsersEvent {
|
||||
final String userId;
|
||||
AdminUserDetailWithRolesRequested(this.userId);
|
||||
}
|
||||
|
||||
class AdminUserRolesUpdateRequested extends AdminUsersEvent {
|
||||
final String userId;
|
||||
final List<String> roleNames;
|
||||
AdminUserRolesUpdateRequested(this.userId, this.roleNames);
|
||||
}
|
||||
|
||||
class AdminRolesLoadRequested extends AdminUsersEvent {}
|
||||
45
lib/features/admin/bloc/admin_users_state.dart
Normal file
45
lib/features/admin/bloc/admin_users_state.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
part of 'admin_users_bloc.dart';
|
||||
|
||||
abstract class AdminUsersState {}
|
||||
|
||||
class AdminUsersInitial extends AdminUsersState {}
|
||||
|
||||
class AdminUsersLoading extends AdminUsersState {}
|
||||
|
||||
class AdminUsersLoaded extends AdminUsersState {
|
||||
final List<AdminUserModel> users;
|
||||
final int totalCount;
|
||||
final int currentPage;
|
||||
final int pageSize;
|
||||
final int totalPages;
|
||||
AdminUsersLoaded({
|
||||
required this.users,
|
||||
required this.totalCount,
|
||||
required this.currentPage,
|
||||
required this.pageSize,
|
||||
required this.totalPages,
|
||||
});
|
||||
}
|
||||
|
||||
class AdminUsersError extends AdminUsersState {
|
||||
final String message;
|
||||
AdminUsersError(this.message);
|
||||
}
|
||||
|
||||
class AdminUserDetailLoaded extends AdminUsersState {
|
||||
final AdminUserModel user;
|
||||
final List<AdminRoleModel> userRoles;
|
||||
final List<AdminRoleModel> allRoles;
|
||||
AdminUserDetailLoaded({
|
||||
required this.user,
|
||||
required this.userRoles,
|
||||
this.allRoles = const [],
|
||||
});
|
||||
}
|
||||
|
||||
class AdminRolesLoaded extends AdminUsersState {
|
||||
final List<AdminRoleModel> roles;
|
||||
AdminRolesLoaded(this.roles);
|
||||
}
|
||||
|
||||
class AdminUserRolesUpdated extends AdminUsersState {}
|
||||
66
lib/features/admin/data/models/admin_user_model.dart
Normal file
66
lib/features/admin/data/models/admin_user_model.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
/// Modèle pour un utilisateur admin (Keycloak) - aligné sur l'API /api/admin/users
|
||||
library admin_user_model;
|
||||
|
||||
class AdminUserModel {
|
||||
final String id;
|
||||
final String? username;
|
||||
final String? email;
|
||||
final String? prenom;
|
||||
final String? nom;
|
||||
final bool? enabled;
|
||||
final List<String>? realmRoles;
|
||||
|
||||
AdminUserModel({
|
||||
required this.id,
|
||||
this.username,
|
||||
this.email,
|
||||
this.prenom,
|
||||
this.nom,
|
||||
this.enabled,
|
||||
this.realmRoles,
|
||||
});
|
||||
|
||||
String get displayName {
|
||||
if (prenom != null && nom != null) return '$prenom $nom';
|
||||
if (prenom != null) return prenom!;
|
||||
if (nom != null) return nom!;
|
||||
return username ?? email ?? id;
|
||||
}
|
||||
|
||||
factory AdminUserModel.fromJson(Map<String, dynamic> json) {
|
||||
final roles = json['realmRoles'] as List<dynamic>?;
|
||||
return AdminUserModel(
|
||||
id: json['id'] as String? ?? '',
|
||||
username: json['username'] as String?,
|
||||
email: json['email'] as String?,
|
||||
prenom: json['prenom'] as String?,
|
||||
nom: json['nom'] as String?,
|
||||
enabled: json['enabled'] as bool?,
|
||||
realmRoles: roles?.map((e) => e is Map ? (e['name'] as String?) ?? e.toString() : e.toString()).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'username': username,
|
||||
'email': email,
|
||||
'prenom': prenom,
|
||||
'nom': nom,
|
||||
'enabled': enabled,
|
||||
'realmRoles': realmRoles,
|
||||
};
|
||||
}
|
||||
|
||||
class AdminRoleModel {
|
||||
final String id;
|
||||
final String name;
|
||||
final String? description;
|
||||
|
||||
AdminRoleModel({required this.id, required this.name, this.description});
|
||||
|
||||
factory AdminRoleModel.fromJson(Map<String, dynamic> json) => AdminRoleModel(
|
||||
id: json['id'] as String? ?? '',
|
||||
name: json['name'] as String? ?? '',
|
||||
description: json['description'] as String?,
|
||||
);
|
||||
}
|
||||
105
lib/features/admin/data/repositories/admin_user_repository.dart
Normal file
105
lib/features/admin/data/repositories/admin_user_repository.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
/// Repository pour la gestion des utilisateurs admin (API /api/admin/users)
|
||||
library admin_user_repository;
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:unionflow_mobile_apps/core/network/api_client.dart';
|
||||
import '../models/admin_user_model.dart';
|
||||
|
||||
abstract class AdminUserRepository {
|
||||
Future<AdminUserSearchResult> search({int page = 0, int size = 20, String? search});
|
||||
Future<AdminUserModel?> getById(String id);
|
||||
Future<List<AdminRoleModel>> getRealmRoles();
|
||||
Future<List<AdminRoleModel>> getUserRoles(String userId);
|
||||
Future<void> setUserRoles(String userId, List<String> roleNames);
|
||||
/// Associe un utilisateur (email) à une organisation (réservé SUPER_ADMIN).
|
||||
Future<void> associerOrganisation({required String email, required String organisationId});
|
||||
}
|
||||
|
||||
class AdminUserSearchResult {
|
||||
final List<AdminUserModel> users;
|
||||
final int totalCount;
|
||||
final int currentPage;
|
||||
final int pageSize;
|
||||
final int totalPages;
|
||||
|
||||
AdminUserSearchResult({
|
||||
required this.users,
|
||||
required this.totalCount,
|
||||
required this.currentPage,
|
||||
required this.pageSize,
|
||||
required this.totalPages,
|
||||
});
|
||||
}
|
||||
|
||||
@LazySingleton(as: AdminUserRepository)
|
||||
class AdminUserRepositoryImpl implements AdminUserRepository {
|
||||
final ApiClient _apiClient;
|
||||
static const String _base = '/api/admin/users';
|
||||
|
||||
AdminUserRepositoryImpl(this._apiClient);
|
||||
|
||||
@override
|
||||
Future<AdminUserSearchResult> search({int page = 0, int size = 20, String? search}) async {
|
||||
final query = <String, dynamic>{'page': page, 'size': size};
|
||||
if (search != null && search.isNotEmpty) query['search'] = search;
|
||||
final response = await _apiClient.get(_base, queryParameters: query);
|
||||
if (response.statusCode != 200) throw Exception('Erreur ${response.statusCode}');
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final list = data['users'] as List<dynamic>? ?? [];
|
||||
return AdminUserSearchResult(
|
||||
users: list.map((e) => AdminUserModel.fromJson(e as Map<String, dynamic>)).toList(),
|
||||
totalCount: (data['totalCount'] as num?)?.toInt() ?? 0,
|
||||
currentPage: (data['currentPage'] as num?)?.toInt() ?? 0,
|
||||
pageSize: (data['pageSize'] as num?)?.toInt() ?? size,
|
||||
totalPages: (data['totalPages'] as num?)?.toInt() ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AdminUserModel?> getById(String id) async {
|
||||
final response = await _apiClient.get('$_base/$id');
|
||||
if (response.statusCode == 200) {
|
||||
return AdminUserModel.fromJson(response.data as Map<String, dynamic>);
|
||||
}
|
||||
if (response.statusCode == 404) return null;
|
||||
throw Exception('Erreur ${response.statusCode}');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<AdminRoleModel>> getRealmRoles() async {
|
||||
final response = await _apiClient.get('$_base/roles');
|
||||
if (response.statusCode != 200) return [];
|
||||
final list = response.data as List<dynamic>? ?? [];
|
||||
return list.map((e) => AdminRoleModel.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<AdminRoleModel>> getUserRoles(String userId) async {
|
||||
final response = await _apiClient.get('$_base/$userId/roles');
|
||||
if (response.statusCode != 200) return [];
|
||||
final list = response.data as List<dynamic>? ?? [];
|
||||
return list.map((e) => AdminRoleModel.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setUserRoles(String userId, List<String> roleNames) async {
|
||||
final response = await _apiClient.put('$_base/$userId/roles', data: roleNames);
|
||||
if (response.statusCode != 200) throw Exception('Erreur ${response.statusCode}');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> associerOrganisation({required String email, required String organisationId}) async {
|
||||
const path = '/api/admin/associer-organisation';
|
||||
final response = await _apiClient.post(
|
||||
path,
|
||||
data: {'email': email, 'organisationId': organisationId},
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
final msg = response.data is Map && response.data['message'] != null
|
||||
? response.data['message'] as String
|
||||
: 'Erreur ${response.statusCode}';
|
||||
throw Exception(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../../../../core/utils/logger.dart';
|
||||
import '../../bloc/admin_users_bloc.dart';
|
||||
import '../../data/models/admin_user_model.dart';
|
||||
import '../../data/repositories/admin_user_repository.dart';
|
||||
import '../../../organizations/data/models/organization_model.dart';
|
||||
import '../../../organizations/data/services/organization_service.dart';
|
||||
import '../../../../shared/design_system/unionflow_design_system.dart';
|
||||
import '../../../../shared/widgets/core_card.dart';
|
||||
import '../../../../shared/design_system/components/uf_app_bar.dart';
|
||||
import '../../../../shared/design_system/components/uf_buttons.dart';
|
||||
|
||||
/// Page détail d'un utilisateur + édition des rôles
|
||||
class UserManagementDetailPage extends StatelessWidget {
|
||||
final String userId;
|
||||
|
||||
const UserManagementDetailPage({super.key, required this.userId});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
appBar: const UFAppBar(
|
||||
title: 'Détail utilisateur',
|
||||
),
|
||||
body: BlocBuilder<AdminUsersBloc, AdminUsersState>(
|
||||
builder: (context, state) {
|
||||
if (state is AdminUsersLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (state is AdminUsersError) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(state.message),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () => context.read<AdminUsersBloc>().add(AdminUserDetailRequested(userId)),
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
if (state is AdminUserDetailLoaded) {
|
||||
return _UserDetailContent(
|
||||
user: state.user,
|
||||
userRoles: state.userRoles,
|
||||
allRoles: state.allRoles,
|
||||
userId: userId,
|
||||
);
|
||||
}
|
||||
return const SizedBox();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _UserDetailContent extends StatefulWidget {
|
||||
final AdminUserModel user;
|
||||
final List<AdminRoleModel> userRoles;
|
||||
final List<AdminRoleModel> allRoles;
|
||||
final String userId;
|
||||
|
||||
const _UserDetailContent({
|
||||
required this.user,
|
||||
required this.userRoles,
|
||||
required this.allRoles,
|
||||
required this.userId,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_UserDetailContent> createState() => _UserDetailContentState();
|
||||
}
|
||||
|
||||
class _UserDetailContentState extends State<_UserDetailContent> {
|
||||
late Set<String> _selectedRoleNames;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_selectedRoleNames = widget.userRoles.map((r) => r.name).toSet();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<AdminUsersBloc, AdminUsersState>(
|
||||
listener: (context, state) {
|
||||
if (state is AdminUserRolesUpdated) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Rôles mis à jour')),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
CoreCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(widget.user.displayName, style: AppTypography.headerSmall),
|
||||
const SizedBox(height: 8),
|
||||
if (widget.user.email != null)
|
||||
Text('Email: ${widget.user.email}', style: AppTypography.bodyTextSmall),
|
||||
if (widget.user.username != null)
|
||||
Text('Username: ${widget.user.username}', style: AppTypography.bodyTextSmall),
|
||||
Text(
|
||||
'Statut: ${widget.user.enabled == true ? "Actif" : "Inactif"}',
|
||||
style: AppTypography.bodyTextSmall.copyWith(
|
||||
color: widget.user.enabled == true ? AppColors.success : AppColors.error,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'RÔLES (SÉLECTION)',
|
||||
style: AppTypography.subtitleSmall.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 1.1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...widget.allRoles.map((role) {
|
||||
final selected = _selectedRoleNames.contains(role.name);
|
||||
return CheckboxListTile(
|
||||
title: Text(role.name, style: AppTypography.bodyTextSmall),
|
||||
activeColor: AppColors.primaryGreen,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
value: selected,
|
||||
onChanged: (v) {
|
||||
setState(() {
|
||||
if (v == true) {
|
||||
_selectedRoleNames.add(role.name);
|
||||
} else {
|
||||
_selectedRoleNames.remove(role.name);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}),
|
||||
const SizedBox(height: 24),
|
||||
UFPrimaryButton(
|
||||
label: 'Enregistrer les rôles',
|
||||
onPressed: () {
|
||||
context.read<AdminUsersBloc>().add(
|
||||
AdminUserRolesUpdateRequested(widget.userId, _selectedRoleNames.toList()),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Divider(height: 1),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'ASSOCIER À UNE ORGANISATION',
|
||||
style: AppTypography.subtitleSmall.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 1.1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Permet à cet utilisateur (ex. admin d\'organisation) de voir « Mes organisations » et d\'accéder au dashboard de l\'organisation.',
|
||||
style: AppTypography.bodyTextSmall.copyWith(color: AppColors.textSecondaryLight),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton.icon(
|
||||
onPressed: widget.user.email == null || widget.user.email!.isEmpty
|
||||
? null
|
||||
: () => _openAssocierOrganisationDialog(context, widget.user.email!),
|
||||
icon: const Icon(Icons.business, size: 18),
|
||||
label: const Text('Associer à une organisation'),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: AppColors.primaryGreen,
|
||||
side: const BorderSide(color: AppColors.primaryGreen),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openAssocierOrganisationDialog(BuildContext context, String userEmail) async {
|
||||
final orgService = GetIt.I<OrganizationService>();
|
||||
final adminRepo = GetIt.I<AdminUserRepository>();
|
||||
List<OrganizationModel> organisations = [];
|
||||
try {
|
||||
organisations = await orgService.getOrganizations(page: 0, size: 200);
|
||||
} catch (e, st) {
|
||||
AppLogger.error('UserManagementDetail: chargement organisations échoué', error: e, stackTrace: st);
|
||||
if (!context.mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Impossible de charger les organisations')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!context.mounted) return;
|
||||
final orgsWithId = organisations.where((o) => o.id != null && o.id!.isNotEmpty).toList();
|
||||
if (orgsWithId.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Aucune organisation disponible. Créez-en une d\'abord.')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
String? selectedOrgId = orgsWithId.first.id;
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (ctx) => StatefulBuilder(
|
||||
builder: (ctx2, setDialogState) {
|
||||
return AlertDialog(
|
||||
title: const Text('Associer à une organisation'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Utilisateur: $userEmail', style: AppTypography.bodyTextSmall),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedOrgId,
|
||||
isExpanded: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Organisation',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: orgsWithId
|
||||
.map((o) => DropdownMenuItem(value: o.id, child: Text(o.nom, overflow: TextOverflow.ellipsis)))
|
||||
.toList(),
|
||||
onChanged: (v) => setDialogState(() => selectedOrgId = v),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(),
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () async {
|
||||
if (selectedOrgId == null) return;
|
||||
try {
|
||||
await adminRepo.associerOrganisation(email: userEmail, organisationId: selectedOrgId!);
|
||||
if (ctx.mounted) Navigator.of(ctx).pop();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Utilisateur associé à l\'organisation avec succès.')),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (ctx.mounted) {
|
||||
ScaffoldMessenger.of(ctx).showSnackBar(
|
||||
SnackBar(content: Text('Erreur: ${e.toString().replaceFirst('Exception: ', '')}')),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('Associer'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
213
lib/features/admin/presentation/pages/user_management_page.dart
Normal file
213
lib/features/admin/presentation/pages/user_management_page.dart
Normal file
@@ -0,0 +1,213 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../../bloc/admin_users_bloc.dart';
|
||||
import '../../data/models/admin_user_model.dart';
|
||||
import '../../../../shared/design_system/unionflow_design_system.dart';
|
||||
import '../../../../shared/widgets/core_card.dart';
|
||||
import '../../../../shared/widgets/mini_avatar.dart';
|
||||
import '../../../../shared/design_system/components/uf_app_bar.dart';
|
||||
import 'user_management_detail_page.dart';
|
||||
|
||||
/// Page de gestion des utilisateurs (SUPER_ADMIN) - liste paginée
|
||||
class UserManagementPage extends StatelessWidget {
|
||||
const UserManagementPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (_) => GetIt.I<AdminUsersBloc>()..add(AdminUsersLoadRequested()),
|
||||
child: const _UserManagementView(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _UserManagementView extends StatefulWidget {
|
||||
const _UserManagementView();
|
||||
|
||||
@override
|
||||
State<_UserManagementView> createState() => _UserManagementViewState();
|
||||
}
|
||||
|
||||
class _UserManagementViewState extends State<_UserManagementView> {
|
||||
final _searchController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.background,
|
||||
appBar: UFAppBar(
|
||||
title: 'Gestion des utilisateurs',
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh, size: 20),
|
||||
onPressed: () => context.read<AdminUsersBloc>().add(AdminUsersLoadRequested()),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: TextField(
|
||||
controller: _searchController,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Rechercher (email, nom...)',
|
||||
hintStyle: AppTypography.subtitleSmall,
|
||||
prefixIcon: const Icon(Icons.search, size: 18),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(RadiusTokens.md),
|
||||
borderSide: const BorderSide(color: AppColors.lightBorder),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(RadiusTokens.md),
|
||||
borderSide: const BorderSide(color: AppColors.lightBorder),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(RadiusTokens.md),
|
||||
borderSide: const BorderSide(color: AppColors.primaryGreen),
|
||||
),
|
||||
filled: true,
|
||||
fillColor: AppColors.lightSurface,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
),
|
||||
onSubmitted: (v) => context.read<AdminUsersBloc>().add(
|
||||
AdminUsersLoadRequested(search: v.isEmpty ? null : v),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: BlocBuilder<AdminUsersBloc, AdminUsersState>(
|
||||
builder: (context, state) {
|
||||
if (state is AdminUsersLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (state is AdminUsersError) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(state.message, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () => context.read<AdminUsersBloc>().add(AdminUsersLoadRequested()),
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
if (state is AdminUsersLoaded) {
|
||||
if (state.users.isEmpty) {
|
||||
return const Center(child: Text('Aucun utilisateur'));
|
||||
}
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
context.read<AdminUsersBloc>().add(AdminUsersLoadRequested(
|
||||
search: _searchController.text.isEmpty ? null : _searchController.text,
|
||||
));
|
||||
},
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
itemCount: state.users.length + 1,
|
||||
itemBuilder: (context, i) {
|
||||
if (i == state.users.length) {
|
||||
return _buildPagination(context, state);
|
||||
}
|
||||
return _buildUserTile(context, state.users[i]);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
return const SizedBox();
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildUserTile(BuildContext context, AdminUserModel user) {
|
||||
return CoreCard(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => BlocProvider(
|
||||
create: (_) => GetIt.I<AdminUsersBloc>()..add(AdminUserDetailWithRolesRequested(user.id)),
|
||||
child: UserManagementDetailPage(userId: user.id),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
MiniAvatar(
|
||||
imageUrl: null, // AdminUserModel n'a pas de champ avatar
|
||||
fallbackText: (user.prenom?.substring(0, 1) ?? user.username?.substring(0, 1) ?? '?').toUpperCase(),
|
||||
size: 36,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
user.displayName,
|
||||
style: AppTypography.actionText,
|
||||
),
|
||||
Text(
|
||||
user.email ?? user.username ?? user.id,
|
||||
style: AppTypography.subtitleSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.chevron_right,
|
||||
size: 16,
|
||||
color: AppColors.textSecondaryLight,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPagination(BuildContext context, AdminUsersLoaded state) {
|
||||
if (state.totalPages <= 1) return const SizedBox(height: 24);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: state.currentPage > 0
|
||||
? () => context.read<AdminUsersBloc>().add(AdminUsersLoadRequested(
|
||||
page: state.currentPage - 1,
|
||||
size: state.pageSize,
|
||||
search: _searchController.text.isEmpty ? null : _searchController.text,
|
||||
))
|
||||
: null,
|
||||
),
|
||||
Text('${state.currentPage + 1} / ${state.totalPages}'),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_forward),
|
||||
onPressed: state.currentPage < state.totalPages - 1
|
||||
? () => context.read<AdminUsersBloc>().add(AdminUsersLoadRequested(
|
||||
page: state.currentPage + 1,
|
||||
size: state.pageSize,
|
||||
search: _searchController.text.isEmpty ? null : _searchController.text,
|
||||
))
|
||||
: null,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user