1313 **/
1414
1515use App \Http \Controllers \GetAllTrait ;
16+ use App \Http \Controllers \Traits \RequestProcessor ;
17+ use App \Http \Controllers \UserGroupsValidationRulesFactory ;
1618use App \Http \Controllers \UserValidationRulesFactory ;
19+ use App \Http \Exceptions \HTTP403ForbiddenException ;
1720use App \Http \Utils \HTMLCleaner ;
21+ use App \Jobs \AddUserAction ;
1822use App \ModelSerializers \SerializerRegistry ;
23+ use App \Services \Auth \IGroupService ;
24+ use Auth \Group ;
1925use Auth \Repositories \IUserRepository ;
26+ use Illuminate \Http \JsonResponse ;
2027use Illuminate \Http \Request as LaravelRequest ;
28+ use Illuminate \Support \Facades \App ;
2129use Illuminate \Support \Facades \Auth ;
2230use Illuminate \Support \Facades \Request ;
2331use Illuminate \Support \Facades \Log ;
2735use models \exceptions \ValidationException ;
2836use OAuth2 \Builders \IdTokenBuilder ;
2937use OAuth2 \IResourceServerContext ;
38+ use OAuth2 \Models \IClient ;
3039use OAuth2 \Repositories \IClientRepository ;
3140use OAuth2 \ResourceServer \IUserService ;
3241use Utils \Http \HttpContentType ;
42+ use Utils \IPHelper ;
3343use Utils \Services \ILogService ;
3444use Exception ;
3545use OpenId \Services \IUserService as IOpenIdUserService ;
@@ -41,6 +51,8 @@ final class OAuth2UserApiController extends OAuth2ProtectedController
4151{
4252 use GetAllTrait;
4353
54+ use RequestProcessor;
55+
4456 protected function getAllSerializerType (): string
4557 {
4658 return SerializerRegistry::SerializerType_Private;
@@ -82,6 +94,11 @@ protected function getFilterValidatorRules(): array
8294 */
8395 private $ user_service ;
8496
97+ /**
98+ * @var IGroupService
99+ */
100+ private $ group_service ;
101+
85102 /**
86103 * @var IClientRepository
87104 */
@@ -112,6 +129,7 @@ public function __construct
112129 (
113130 IUserRepository $ repository ,
114131 IUserService $ user_service ,
132+ IGroupService $ group_service ,
115133 IResourceServerContext $ resource_server_context ,
116134 ILogService $ log_service ,
117135 IOpenIdUserService $ openid_user_service ,
@@ -122,6 +140,7 @@ public function __construct
122140 parent ::__construct ($ resource_server_context , $ log_service );
123141 $ this ->repository = $ repository ;
124142 $ this ->user_service = $ user_service ;
143+ $ this ->group_service = $ group_service ;
125144 $ this ->client_repository = $ client_repository ;
126145 $ this ->id_token_builder = $ id_token_builder ;
127146 $ this ->openid_user_service = $ openid_user_service ;
@@ -324,4 +343,31 @@ public function get($id)
324343 }
325344 }
326345
346+ /**
347+ * @param $user_id
348+ * @return JsonResponse|mixed
349+ */
350+ public function addUserToGroup ($ user_id ): mixed
351+ {
352+ return $ this ->processRequest (function () use ($ user_id ) {
353+ //check if it's a service app
354+ $ app_type = $ this ->resource_server_context ->getApplicationType ();
355+ if (App::environment () != "testing " && !empty ($ app_type ) && $ app_type != IClient::ApplicationType_Service) {
356+ throw new HTTP403ForbiddenException ("You are not allowed to perform this action. " );
357+ }
358+
359+ if (!Request::isJson ()) return $ this ->error400 ();
360+
361+ $ payload = Request::json ()->all ();
362+ // Creates a Validator instance and validates the data.
363+ $ validation = Validator::make ($ payload , UserGroupsValidationRulesFactory::build ($ payload ));
364+ if ($ validation ->fails ()) {
365+ $ ex = new ValidationException ();
366+ throw $ ex ->setMessages ($ validation ->messages ()->toArray ());
367+ }
368+ $ this ->group_service ->addUser2Groups (intval ($ user_id ), $ payload ['groups ' ]);
369+ return $ this ->updated ();
370+ });
371+ }
372+
327373}
0 commit comments