diff --git a/config/services.yml b/config/services.yml index bc3120c..122a181 100644 --- a/config/services.yml +++ b/config/services.yml @@ -20,6 +20,9 @@ services: - '@template' - '@user' - '@notification_manager' + - '@dbal.conn' + - '%tables.phpbb.wpn.notification_push%' + - '%tables.phpbb.wpn.push_subscriptions%' tags: - { name: event.listener } diff --git a/event/listener.php b/event/listener.php index 619fd9f..a90220b 100644 --- a/event/listener.php +++ b/event/listener.php @@ -12,6 +12,7 @@ use phpbb\config\config; use phpbb\controller\helper as controller_helper; +use phpbb\db\driver\driver_interface; use phpbb\language\language; use phpbb\notification\manager; use phpbb\request\request_interface; @@ -50,6 +51,15 @@ class listener implements EventSubscriberInterface /** @var manager */ protected $phpbb_notifications; + /** @var driver_interface */ + protected $db; + + /** @var string */ + protected $notification_webpush_table; + + /** @var string */ + protected $push_subscriptions_table; + /** * Constructor * @@ -61,8 +71,11 @@ class listener implements EventSubscriberInterface * @param template $template Template object * @param user $user * @param manager $phpbb_notifications Notifications manager object + * @param driver_interface $db Database connection + * @param string $notification_webpush_table Web Push notification table + * @param string $push_subscriptions_table Web Push subscriptions table */ - public function __construct(config $config, controller_helper $controller_helper, form_helper $form_helper, language $language, request_interface $request, template $template, user $user, manager $phpbb_notifications) + public function __construct(config $config, controller_helper $controller_helper, form_helper $form_helper, language $language, request_interface $request, template $template, user $user, manager $phpbb_notifications, driver_interface $db, $notification_webpush_table, $push_subscriptions_table) { $this->config = $config; $this->controller_helper = $controller_helper; @@ -72,6 +85,9 @@ public function __construct(config $config, controller_helper $controller_helper $this->template = $template; $this->user = $user; $this->phpbb_notifications = $phpbb_notifications; + $this->db = $db; + $this->notification_webpush_table = $notification_webpush_table; + $this->push_subscriptions_table = $push_subscriptions_table; } public static function getSubscribedEvents() @@ -83,6 +99,7 @@ public static function getSubscribedEvents() 'core.acp_main_notice' => 'compatibility_notice', 'core.help_manager_add_block_after' => 'wpn_faq', 'core.page_footer' => 'append_agreement', + 'core.delete_user_before' => 'delete_user_data', ]; } @@ -221,6 +238,29 @@ public function append_agreement() $this->template->append_var('AGREEMENT_TEXT', $this->language->lang('NOTIFY_WEBPUSH_PRIVACY_POLICY', $this->config['sitename'])); } + /** + * Delete Web Push data belonging to deleted users + * + * @param \phpbb\event\data $event The event object + * @return void + */ + public function delete_user_data($event) + { + $user_ids = array_map('intval', $event['user_ids']); + + if (!$user_ids) + { + return; + } + + foreach ([$this->notification_webpush_table, $this->push_subscriptions_table] as $table) + { + $sql = 'DELETE FROM ' . $table . ' + WHERE ' . $this->db->sql_in_set('user_id', $user_ids); + $this->db->sql_query($sql); + } + } + /** * Can notifications be used by the user? * diff --git a/tests/event/fixtures/webpush.xml b/tests/event/fixtures/webpush.xml index c6fd1c2..501d206 100644 --- a/tests/event/fixtures/webpush.xml +++ b/tests/event/fixtures/webpush.xml @@ -1,6 +1,40 @@ + notification_type_id + item_id + item_parent_id + user_id + push_data + notification_time + push_token + + 1 + 1 + 0 + 2 + {} + 1 + token2 + + + 1 + 2 + 0 + 3 + {} + 2 + token3 + + + 1 + 3 + 0 + 4 + {} + 3 + token4 +
subscription_id @@ -34,4 +68,52 @@ 0
+ + user_id + username + username_clean + user_permissions + user_sig + user_type + user_avatar + user_avatar_type + user_inactive_reason + user_posts + + 2 + User 2 + user 2 + + + 0 + + + 0 + 0 + + + 3 + User 3 + user 3 + + + 0 + + + 0 + 0 + + + 4 + User 4 + user 4 + + + 0 + + + 0 + 0 + +
diff --git a/tests/event/listener_test.php b/tests/event/listener_test.php index 6ba9747..a0be270 100644 --- a/tests/event/listener_test.php +++ b/tests/event/listener_test.php @@ -13,6 +13,7 @@ use phpbb\webpushnotifications\ext; require_once __DIR__ . '/../../../../../includes/functions_acp.php'; +require_once __DIR__ . '/../../../../../includes/functions_user.php'; class listener_test extends \phpbb_database_test_case { @@ -100,6 +101,8 @@ protected function setUp(): void 'pwa_icon_small' => '', 'pwa_icon_large' => '', 'pwa_short_name' => '', + 'num_users' => 3, + 'newest_user_id' => 4, ]); $this->form_helper = new \phpbb\webpushnotifications\form\form_helper( @@ -140,7 +143,10 @@ protected function set_listener() $this->request, $this->template, $this->user, - $this->notifications + $this->notifications, + $this->db, + 'phpbb_wpn_notification_push', + 'phpbb_wpn_push_subscriptions' ); } @@ -159,9 +165,44 @@ public function test_getSubscribedEvents() 'core.acp_main_notice', 'core.help_manager_add_block_after', 'core.page_footer', + 'core.delete_user_before', ], array_keys(\phpbb\webpushnotifications\event\listener::getSubscribedEvents())); } + public function test_user_deletion_removes_webpush_data() + { + global $cache, $config, $db, $phpbb_container, $phpbb_dispatcher; + + $this->set_listener(); + + $config = $this->config; + $db = $this->db; + $cache = new \phpbb\cache\driver\dummy(); + $phpbb_container = new \phpbb_mock_container_builder(); + $phpbb_container->set('auth.provider_collection', new \phpbb\auth\provider_collection($phpbb_container, $this->config)); + $phpbb_container->set('notification_manager', new \phpbb_mock_notification_manager()); + $phpbb_container->setParameter('tables.auth_provider_oauth_token_storage', 'phpbb_oauth_tokens'); + $phpbb_container->setParameter('tables.auth_provider_oauth_states', 'phpbb_oauth_states'); + $phpbb_container->setParameter('tables.auth_provider_oauth_account_assoc', 'phpbb_oauth_accounts'); + $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications'); + + $phpbb_dispatcher = new \phpbb\event\dispatcher(); + $phpbb_dispatcher->addSubscriber($this->listener); + + user_delete('retain', [2, 3], true); + + $this->assertSqlResultEquals([ + ['user_id' => '4'], + ], 'SELECT user_id FROM phpbb_users ORDER BY user_id'); + + $this->assertSqlResultEquals([ + ['user_id' => '4'], + ], 'SELECT user_id FROM phpbb_wpn_notification_push ORDER BY user_id'); + $this->assertSqlResultEquals([ + ['user_id' => '4'], + ], 'SELECT user_id FROM phpbb_wpn_push_subscriptions ORDER BY user_id'); + } + public function test_load_language_on_setup($lang_set_ext = []) { $this->set_listener();