1919use Contexts \Authorization \Domain \UserIdentity \Models \UserStatus ;
2020use Contexts \Shared \Application \BaseCoordinator ;
2121use Illuminate \Contracts \Pagination \LengthAwarePaginator ;
22+ use Contexts \Shared \Policies \CompositePolicy ;
23+ use Contexts \Authorization \Domain \Policies \GlobalPermissionPolicy ;
2224
2325class UserIdentityCoordinator extends BaseCoordinator
2426{
2527 public function __construct (
2628 private UserRepository $ repository ,
2729 private UserIdentityFactory $ factory
28- ) {}
30+ ) {
31+ }
2932
3033 public function create (CreateUserDTO $ data ): UserIdentity
3134 {
35+ CompositePolicy::allOf ([
36+ new GlobalPermissionPolicy ('user.create ' ),
37+ ])->check ();
38+
3239 $ user = $ this ->factory ->create (
3340 UserId::null (),
3441 new Email ($ data ->email ),
@@ -42,16 +49,28 @@ public function create(CreateUserDTO $data): UserIdentity
4249
4350 public function getUser (int $ id ): UserIdentity
4451 {
52+ CompositePolicy::allOf ([
53+ new GlobalPermissionPolicy ('user.get ' ),
54+ ])->check ();
55+
4556 return $ this ->repository ->getById (UserId::fromInt ($ id ));
4657 }
4758
4859 public function getUserList (GetUserListDTO $ data ): LengthAwarePaginator
4960 {
61+ CompositePolicy::allOf ([
62+ new GlobalPermissionPolicy ('user.list ' ),
63+ ])->check ();
64+
5065 return $ this ->repository ->paginate ($ data ->currentPage , $ data ->perPage , $ data ->toCriteria ());
5166 }
5267
5368 public function updateUser (int $ id , UpdateUserDTO $ data ): UserIdentity
5469 {
70+ CompositePolicy::allOf ([
71+ new GlobalPermissionPolicy ('user.update ' ),
72+ ])->check ();
73+
5574 $ user = $ this ->repository ->getById (UserId::fromInt ($ id ));
5675 $ user ->modify (
5776 $ data ->email ? new Email ($ data ->email ) : null ,
@@ -69,6 +88,10 @@ public function updateUser(int $id, UpdateUserDTO $data): UserIdentity
6988
7089 public function subspendUser (int $ id )
7190 {
91+ CompositePolicy::allOf ([
92+ new GlobalPermissionPolicy ('user.subspend ' ),
93+ ])->check ();
94+
7295 $ user = $ this ->repository ->getById (UserId::fromInt ($ id ));
7396 $ user ->subspend ();
7497
@@ -79,6 +102,10 @@ public function subspendUser(int $id)
79102
80103 public function deleteUser (int $ id )
81104 {
105+ CompositePolicy::allOf ([
106+ new GlobalPermissionPolicy ('user.delete ' ),
107+ ])->check ();
108+
82109 $ user = $ this ->repository ->getById (UserId::fromInt ($ id ));
83110 $ user ->delete ();
84111
@@ -89,6 +116,10 @@ public function deleteUser(int $id)
89116
90117 public function changePassword (int $ userId , string $ newPassword )
91118 {
119+ CompositePolicy::allOf ([
120+ new GlobalPermissionPolicy ('user.changePassword ' ),
121+ ])->check ();
122+
92123 $ user = $ this ->repository ->getById (UserId::fromInt ($ userId ));
93124 $ user ->changePassword ($ newPassword );
94125
@@ -97,6 +128,10 @@ public function changePassword(int $userId, string $newPassword)
97128
98129 public function syncRoles (int $ userId , array $ roleIds ): void
99130 {
131+ CompositePolicy::allOf ([
132+ new GlobalPermissionPolicy ('user.syncRoles ' ),
133+ ])->check ();
134+
100135 $ newRoles = new RoleIdCollection (
101136 array_map (fn ($ id ) => RoleId::fromInt ($ id ), $ roleIds )
102137 );
0 commit comments