1414
1515use App \Events \UserEmailUpdated ;
1616use App \Events \UserPasswordResetSuccessful ;
17+ use App \Jobs \AddUserAction ;
1718use App \Jobs \PublishUserDeleted ;
1819use App \Jobs \PublishUserUpdated ;
1920use App \libs \Auth \Factories \UserFactory ;
3334use Illuminate \Support \Facades \Storage ;
3435use models \exceptions \EntityNotFoundException ;
3536use models \exceptions \ValidationException ;
37+ use Models \OAuth2 \Client ;
3638use models \utils \IEntity ;
3739use OAuth2 \IResourceServerContext ;
40+ use OAuth2 \Models \IClient ;
41+ use OAuth2 \Repositories \IClientRepository ;
3842use OpenId \Services \IUserService ;
3943use Utils \Db \ITransactionService ;
44+ use Utils \IPHelper ;
4045use Utils \Services \ILogService ;
4146use Utils \Services \IServerConfigurationService ;
4247
@@ -71,6 +76,11 @@ final class UserService extends AbstractService implements IUserService
7176 */
7277 private $ group_repository ;
7378
79+ /**
80+ * @var IClientRepository
81+ */
82+ private $ client_repository ;
83+
7484 /**
7585 * @var IResourceServerContext
7686 */
@@ -101,7 +111,8 @@ public function __construct
101111 IServerConfigurationService $ configuration_service ,
102112 ILogService $ log_service ,
103113 IResourceServerContext $ server_ctx ,
104- IUserIdentifierGeneratorService $ identifier_service
114+ IUserIdentifierGeneratorService $ identifier_service ,
115+ IClientRepository $ client_repository
105116 )
106117 {
107118 parent ::__construct ($ tx_service );
@@ -112,6 +123,31 @@ public function __construct
112123 $ this ->log_service = $ log_service ;
113124 $ this ->server_ctx = $ server_ctx ;
114125 $ this ->identifier_service = $ identifier_service ;
126+ $ this ->client_repository = $ client_repository ;
127+ }
128+
129+ private function addUserCRUDAction (User $ user , $ payload , string $ action_type = "CREATE " ) {
130+ $ payload_json = json_encode ($ payload );
131+ $ current_user = Auth::user ();
132+
133+ if ($ current_user instanceof User) {
134+ $ action = "{$ action_type } USER BY USER {$ current_user ->getEmail ()} ( {$ current_user ->getId ()}): {$ payload_json }" ;
135+ AddUserAction::dispatch ($ user ->getId (), IPHelper::getUserIp (), $ action );
136+ return ;
137+ }
138+
139+ //check if it's a service app
140+ $ current_client = $ this ->client_repository ->getClientById ($ this ->server_ctx ->getCurrentClientId ());
141+ if ($ current_client instanceof Client and
142+ $ current_client ->getApplicationType () == IClient::ApplicationType_Service) {
143+ $ action = "{$ action_type } USER BY SERVICE {$ current_client ->getApplicationName ()} ( {$ current_client ->getId ()}): {$ payload_json }" ;
144+ AddUserAction::dispatch ($ user ->getId (), IPHelper::getUserIp (), $ action );
145+ return ;
146+ }
147+
148+ $ action = "{$ action_type } USER: {$ payload_json }" ;
149+ AddUserAction::dispatch ($ user ->getId (), IPHelper::getUserIp (), $ action );
150+ return ;
115151 }
116152
117153 /**
@@ -212,7 +248,7 @@ public function saveProfileInfo($user_id, $show_pic, $show_full_name, $show_emai
212248 */
213249 public function create (array $ payload ): IEntity
214250 {
215- return $ this ->tx_service ->transaction (function () use ($ payload ) {
251+ $ user = $ this ->tx_service ->transaction (function () use ($ payload ) {
216252 if (isset ($ payload ["email " ])) {
217253 $ former_user = $ this ->repository ->getByEmailOrName (trim ($ payload ["email " ]));
218254 if (!is_null ($ former_user ))
@@ -240,6 +276,10 @@ public function create(array $payload): IEntity
240276
241277 return $ user ;
242278 });
279+
280+ $ this ->addUserCRUDAction ($ user , $ payload );
281+
282+ return $ user ;
243283 }
244284
245285 /**
@@ -324,6 +364,8 @@ public function update(int $id, array $payload): IEntity
324364 Log::warning ($ ex );
325365 }
326366
367+ $ this ->addUserCRUDAction ($ user , $ payload , "UPDATE " );
368+
327369 return $ user ;
328370 }
329371
0 commit comments