55namespace Admin \User \Handler ;
66
77use Admin \App \Exception \NotFoundException ;
8+ use Admin \App \Form \AbstractForm ;
89use Admin \User \Form \EditUserAvatarForm ;
910use Admin \User \Form \EditUserForm ;
1011use Admin \User \Service \UserAvatarServiceInterface ;
12+ use Admin \User \Service \UserRoleServiceInterface ;
1113use Admin \User \Service \UserServiceInterface ;
1214use Core \App \Message ;
15+ use Core \User \Entity \UserRole ;
16+ use Core \User \Enum \UserRoleEnum ;
1317use Dot \DependencyInjection \Attribute \Inject ;
1418use Dot \FlashMessenger \FlashMessengerInterface ;
1519use Dot \Log \Logger ;
2327use Psr \Http \Server \RequestHandlerInterface ;
2428use Throwable ;
2529
30+ use function array_filter ;
31+ use function array_map ;
2632use function array_merge ;
2733
34+ /**
35+ * @phpstan-import-type SelectDataType from AbstractForm
36+ */
2837class PostUserAvatarEditHandler implements RequestHandlerInterface
2938{
3039 #[Inject(
3140 UserServiceInterface::class,
41+ UserRoleServiceInterface::class,
3242 UserAvatarServiceInterface::class,
3343 RouterInterface::class,
3444 TemplateRendererInterface::class,
@@ -39,6 +49,7 @@ class PostUserAvatarEditHandler implements RequestHandlerInterface
3949 )]
4050 public function __construct (
4151 protected UserServiceInterface $ userService ,
52+ protected UserRoleServiceInterface $ userRoleService ,
4253 protected UserAvatarServiceInterface $ userAvatarService ,
4354 protected RouterInterface $ router ,
4455 protected TemplateRendererInterface $ template ,
@@ -59,6 +70,19 @@ public function handle(ServerRequestInterface $request): ResponseInterface
5970 return new EmptyResponse (StatusCodeInterface::STATUS_NOT_FOUND );
6071 }
6172
73+ /** @var UserRole[] $userRoles */
74+ $ userRoles = $ this ->userRoleService ->getUserRoleRepository ()->findAll ();
75+ $ userRoles = array_map (
76+ /** @return SelectDataType */
77+ fn (UserRole $ userRole ): array => [
78+ 'label ' => $ userRole ->getName ()->value ,
79+ 'value ' => $ userRole ->getUuid ()->toString (),
80+ 'selected ' => $ user ->hasRole ($ userRole ),
81+ ],
82+ $ userRoles
83+ );
84+ $ userRoles = array_filter ($ userRoles , fn (array $ role ) => $ role ['label ' ] !== UserRoleEnum::Guest->value );
85+
6286 $ this ->editUserAvatarForm
6387 ->setAttribute (
6488 'action ' ,
@@ -69,7 +93,8 @@ public function handle(ServerRequestInterface $request): ResponseInterface
6993 ->setAttribute (
7094 'action ' ,
7195 $ this ->router ->generateUri ('user::user-edit ' , ['uuid ' => $ user ->getUuid ()->toString ()])
72- );
96+ )
97+ ->setRoles ($ userRoles );
7398
7499 try {
75100 $ this ->editUserAvatarForm ->setData (
0 commit comments