Skip to content

Commit

Permalink
Fix: Delete DeviceID after Logout
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-farre committed Jan 6, 2025
1 parent 4a1b827 commit 3056359
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 46 deletions.
8 changes: 7 additions & 1 deletion Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use humhub\modules\fcmPush\components\MailerMessage;
use humhub\modules\fcmPush\components\NotificationTargetProvider;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use humhub\modules\fcmPush\helpers\WebAppHelper;
use humhub\modules\fcmPush\services\DriverService;
use humhub\modules\fcmPush\widgets\PushNotificationInfoWidget;
use humhub\modules\notification\targets\MobileTargetProvider;
Expand Down Expand Up @@ -134,9 +135,14 @@ public static function onAfterLogin()
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION, 1);
}

public static function onAfterLogout()
public static function onBeforeLogout()
{
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION, 1);
Yii::$app->view->registerJs('humhub.modules.firebase.unregisterNotification();');
}

public static function onAfterLogout()
{
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1);
}

Expand Down
1 change: 1 addition & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
[LayoutAddons::class, LayoutAddons::EVENT_INIT, [Events::class, 'onLayoutAddonInit']],
[Application::class, Application::EVENT_BEFORE_REQUEST, [Events::class, 'onBeforeRequest']],
[User::class, User::EVENT_AFTER_LOGIN, [Events::class, 'onAfterLogin']],
[User::class, User::EVENT_BEFORE_LOGOUT, [Events::class, 'onBeforeLogout']],
[User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']],
[AuthChoice::class, AuthChoice::EVENT_BEFORE_RUN, [Events::class, 'onAuthChoiceBeforeRun']],
//[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']],
Expand Down
72 changes: 28 additions & 44 deletions controllers/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,20 @@ public function beforeAction($action)

public function actionUpdate()
{
$this->requireLogin();

$driver = (new DriverService($this->module->getConfigureForm()))->getWebDriver();
if (!$driver) {
Yii::$app->response->statusCode = 400;
return $this->asJson(['success' => false, 'message' => 'No push driver available!']);
}

return $this->asJson([
'success' => (
(new TokenService())->storeTokenForUser(
Yii::$app->user->getIdentity(),
$driver,
Yii::$app->request->post('token'),
)
),
]);
return $this->update(false);
}

public function actionUpdateMobileApp()
{
$this->requireLogin();

$driver = (new DriverService($this->module->getConfigureForm()))->getMobileAppDriver();
if (!$driver) {
Yii::error('Could not update token for mobile app. No driver available.', 'fcm-push');

Yii::$app->response->statusCode = 400;
return $this->asJson(['success' => false, 'message' => 'No push driver available!']);
}

return $this->asJson([
'success' => (
(new TokenService())->storeTokenForUser(
Yii::$app->user->getIdentity(),
$driver,
Yii::$app->request->post('token'),
)
),
]);
return $this->update(true);
}


public function actionDeleteMobileApp()
{
$driver = (new DriverService($this->module->getConfigureForm()))->getMobileAppDriver();
$driverService = new DriverService($this->module->getConfigureForm());
$tokenService = new TokenService();

$driver = $driverService->getMobileAppDriver();
if (!$driver) {
Yii::error('Could not delete token for mobile app. No driver available.', 'fcm-push');

Expand All @@ -88,19 +56,35 @@ public function actionDeleteMobileApp()
}

return $this->asJson([
'success' => (
(new TokenService())->deleteToken(
Yii::$app->request->post('token'),
)
'success' => $tokenService->deleteToken(
Yii::$app->request->post('token'),
),
]);
}

private function requireLogin(): void
private function update(bool $mobile)
{
if (Yii::$app->user->isGuest) {
throw new HttpException(401, 'Login required!');
}
}

$driverService = new DriverService($this->module->getConfigureForm());
$tokenService = new TokenService();

$driver = $mobile ? $driverService->getMobileAppDriver() : $driverService->getWebDriver();
if (!$driver) {
Yii::error('Could not update token for ' . ($mobile ? 'mobile' : 'web') . ' app. No driver available.', 'fcm-push');

Yii::$app->response->statusCode = 400;
return $this->asJson(['success' => false, 'message' => 'No push driver available!']);
}

return $this->asJson([
'success' => $tokenService->storeTokenForUser(
Yii::$app->user->getIdentity(),
$driver,
Yii::$app->request->post('token'),
),
]);
}
}
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

2.1.3 (Unreleased)
-------------------------
- Fix: Delete DeviceID after Logout

2.1.2 (December 19, 2024)
-------------------------
- Fix #262: Switch Network doesn't work
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"humhub": {
"minVersion": "1.14"
},
"version": "2.1.2",
"version": "2.1.3",
"screenshots": [
"resources/screenshot1.PNG",
"resources/screenshot2.PNG"
Expand Down

0 comments on commit 3056359

Please sign in to comment.