From ab92cb2d3f3704a16f0577ccbf19536ab32e038d Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Wed, 11 Sep 2024 17:54:50 +0200 Subject: [PATCH 1/4] Export core user data by "Legal" module --- Events.php | 36 ++++++++++++++++++++++++++++++++++++ config.php | 7 +++---- docs/CHANGELOG.md | 4 ++++ module.json | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Events.php b/Events.php index 350e958..8c463d1 100644 --- a/Events.php +++ b/Events.php @@ -8,6 +8,16 @@ namespace humhub\modules\rest; use humhub\components\Event; +use humhub\modules\comment\models\Comment; +use humhub\modules\file\models\File; +use humhub\modules\legal\events\UserDataCollectionEvent; +use humhub\modules\like\models\Like; +use humhub\modules\post\models\Post; +use humhub\modules\rest\definitions\CommentDefinitions; +use humhub\modules\rest\definitions\FileDefinitions; +use humhub\modules\rest\definitions\LikeDefinitions; +use humhub\modules\rest\definitions\PostDefinitions; +use humhub\modules\rest\definitions\UserDefinitions; use Yii; class Events @@ -157,4 +167,30 @@ private static function addModuleNotFoundRoutes($moduleId) ['pattern' => "{$moduleId}/", 'route' => "rest/{$moduleId}/{$moduleId}/not-supported"], ]); } + + public static function onLegalModuleUserDataExport(UserDataCollectionEvent $event) + { + $event->addExportData('user', UserDefinitions::getUser($event->user)); + + $event->addExportData('post', array_map(function ($post) { + return PostDefinitions::getPost($post); + }, Post::findAll(['created_by' => $event->user->id]))); + + $event->addExportData('comment', array_map(function ($comment) { + return CommentDefinitions::getComment($comment); + }, Comment::findAll(['created_by' => $event->user->id]))); + + $event->addExportData('like', array_map(function ($like) { + return LikeDefinitions::getLike($like); + }, Like::findAll(['created_by' => $event->user->id]))); + + $files = File::findAll(['created_by' => $event->user->id]); + $event->addExportData('file', array_map(function ($file) { + return FileDefinitions::getFile($file); + }, $files)); + + foreach ($files as $file) { + $event->addExportFile($file->file_name, $file->store->get()); + } + } } diff --git a/config.php b/config.php index 78b1b3e..f8a4212 100644 --- a/config.php +++ b/config.php @@ -8,13 +8,12 @@ use humhub\components\Application; -/** @noinspection MissedFieldInspection */ return [ 'id' => 'rest', 'class' => 'humhub\modules\rest\Module', 'namespace' => 'humhub\modules\rest', 'events' => [ - [Application::class, Application::EVENT_BEFORE_REQUEST, ['\humhub\modules\rest\Events', 'onBeforeRequest']] - ] + [Application::class, Application::EVENT_BEFORE_REQUEST, ['\humhub\modules\rest\Events', 'onBeforeRequest']], + ['humhub\modules\legal\services\ExportService', 'collectUserData', ['humhub\modules\rest\Events', 'onLegalModuleUserDataExport']], + ], ]; -?> \ No newline at end of file diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d93dd53..c65786f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ Changelog ========= +0.10.3 (Unreleased) +-------------------------- +- Enh #: Collect user data for Legal module + 0.10.2 (September 5, 2024) -------------------------- - Enh #175: User language will be used by default diff --git a/module.json b/module.json index be97a9e..cfee703 100644 --- a/module.json +++ b/module.json @@ -5,7 +5,7 @@ "keywords": [ "api", "rest" ], - "version": "0.10.2", + "version": "0.10.3", "homepage": "https://github.com/humhub/rest", "humhub": { "minVersion": "1.16" From 2ed2eae4135b8dbe029a270c0994102f46d49cea Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Wed, 11 Sep 2024 20:02:24 +0200 Subject: [PATCH 2/4] Export additional user data --- Events.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Events.php b/Events.php index 8c463d1..8704bf7 100644 --- a/Events.php +++ b/Events.php @@ -8,16 +8,24 @@ namespace humhub\modules\rest; use humhub\components\Event; +use humhub\modules\activity\models\Activity; use humhub\modules\comment\models\Comment; use humhub\modules\file\models\File; use humhub\modules\legal\events\UserDataCollectionEvent; use humhub\modules\like\models\Like; +use humhub\modules\notification\models\Notification; use humhub\modules\post\models\Post; +use humhub\modules\rest\definitions\ActivityDefinitions; use humhub\modules\rest\definitions\CommentDefinitions; use humhub\modules\rest\definitions\FileDefinitions; +use humhub\modules\rest\definitions\InviteDefinitions; use humhub\modules\rest\definitions\LikeDefinitions; +use humhub\modules\rest\definitions\NotificationDefinitions; use humhub\modules\rest\definitions\PostDefinitions; +use humhub\modules\rest\definitions\SpaceDefinitions; use humhub\modules\rest\definitions\UserDefinitions; +use humhub\modules\rest\models\Invite; +use humhub\modules\space\models\Space; use Yii; class Events @@ -184,6 +192,25 @@ public static function onLegalModuleUserDataExport(UserDataCollectionEvent $even return LikeDefinitions::getLike($like); }, Like::findAll(['created_by' => $event->user->id]))); + $event->addExportData('activity', array_map(function ($activity) { + return ActivityDefinitions::getActivity($activity); + }, Activity::find() + ->innerJoin('content', 'activity.id = content.object_id and content.object_model = :activityClass', ['activityClass' => Activity::class]) + ->where(['created_by' => $event->user->id]) + ->all())); + + $event->addExportData('invite', array_map(function ($invite) { + return InviteDefinitions::getInvite($invite); + }, Invite::findAll(['created_by' => $event->user->id]))); + + $event->addExportData('notification', array_map(function ($notification) { + return NotificationDefinitions::getNotification($notification); + }, Notification::findAll(['user_id' => $event->user->id]))); + + $event->addExportData('space', array_map(function ($space) { + return SpaceDefinitions::getSpace($space); + }, Space::findAll(['created_by' => $event->user->id]))); + $files = File::findAll(['created_by' => $event->user->id]); $event->addExportData('file', array_map(function ($file) { return FileDefinitions::getFile($file); From 1ee461100044c667236f20b9f0d6c19cf94512e7 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Thu, 12 Sep 2024 10:46:28 +0200 Subject: [PATCH 3/4] Export additional user data --- Events.php | 45 +++++++++++++++++- definitions/UserDefinitions.php | 84 ++++++++++++++++++++++++++++++--- 2 files changed, 121 insertions(+), 8 deletions(-) diff --git a/Events.php b/Events.php index 8704bf7..e48ef9b 100644 --- a/Events.php +++ b/Events.php @@ -11,6 +11,7 @@ use humhub\modules\activity\models\Activity; use humhub\modules\comment\models\Comment; use humhub\modules\file\models\File; +use humhub\modules\friendship\models\Friendship; use humhub\modules\legal\events\UserDataCollectionEvent; use humhub\modules\like\models\Like; use humhub\modules\notification\models\Notification; @@ -25,7 +26,13 @@ use humhub\modules\rest\definitions\SpaceDefinitions; use humhub\modules\rest\definitions\UserDefinitions; use humhub\modules\rest\models\Invite; +use humhub\modules\space\models\Membership; use humhub\modules\space\models\Space; +use humhub\modules\user\models\Auth; +use humhub\modules\user\models\Follow; +use humhub\modules\user\models\Group; +use humhub\modules\user\models\Mentioning; +use humhub\modules\user\models\Session; use Yii; class Events @@ -159,7 +166,7 @@ public static function onBeforeRequest($event) ['pattern' => 'rest/admin/index', 'route' => 'rest/admin', 'verb' => ['POST', 'GET']], // Catch all to ensure verbs - ['pattern' => 'rest/', 'route' => 'rest/error/notfound'] + ['pattern' => 'rest/', 'route' => 'rest/error/notfound'], ], true); @@ -180,6 +187,35 @@ public static function onLegalModuleUserDataExport(UserDataCollectionEvent $even { $event->addExportData('user', UserDefinitions::getUser($event->user)); + $event->addExportData('password', UserDefinitions::getPassword($event->user->currentPassword)); + + $event->addExportData('friendship', array_map(function ($friendship) { + return UserDefinitions::getFriendship($friendship); + }, Friendship::findAll(['user_id' => $event->user->id]))); + + $event->addExportData('mentioning', array_map(function ($mentioning) { + return UserDefinitions::getMentioning($mentioning); + }, Mentioning::findAll(['user_id' => $event->user->id]))); + + $event->addExportData('user-follow', array_map(function ($follow) { + return UserDefinitions::getUserFollow($follow); + }, Follow::findAll(['user_id' => $event->user->id]))); + + $event->addExportData('auth', array_map(function ($auth) { + return UserDefinitions::getUserAuth($auth); + }, Auth::findAll(['user_id' => $event->user->id]))); + + $event->addExportData('session', array_map(function ($session) { + return UserDefinitions::getUserHttpSession($session); + }, Session::findAll(['user_id' => $event->user->id]))); + + $event->addExportData('group', array_map(function ($group) { + return UserDefinitions::getGroup($group); + }, Group::find() + ->innerJoin('group_user', 'group_user.group_id = group.id') + ->where(['group_user.user_id' => $event->user->id]) + ->all())); + $event->addExportData('post', array_map(function ($post) { return PostDefinitions::getPost($post); }, Post::findAll(['created_by' => $event->user->id]))); @@ -211,6 +247,13 @@ public static function onLegalModuleUserDataExport(UserDataCollectionEvent $even return SpaceDefinitions::getSpace($space); }, Space::findAll(['created_by' => $event->user->id]))); + $event->addExportData('space-membership', array_map(function ($membership) { + return SpaceDefinitions::getSpaceMembership($membership); + }, Membership::find() + ->innerJoin('space', 'space.id = space_membership.space_id') + ->where(['space.created_by' => $event->user->id]) + ->all())); + $files = File::findAll(['created_by' => $event->user->id]); $event->addExportData('file', array_map(function ($file) { return FileDefinitions::getFile($file); diff --git a/definitions/UserDefinitions.php b/definitions/UserDefinitions.php index 76ad4a4..e2bbe10 100644 --- a/definitions/UserDefinitions.php +++ b/definitions/UserDefinitions.php @@ -7,25 +7,29 @@ namespace humhub\modules\rest\definitions; +use humhub\modules\friendship\models\Friendship; +use humhub\modules\user\models\Auth; +use humhub\modules\user\models\Follow; +use humhub\modules\user\models\Mentioning; +use humhub\modules\user\models\Password; use humhub\modules\user\models\Profile; use humhub\modules\user\models\Group; +use humhub\modules\user\models\Session; use humhub\modules\user\models\User; use yii\helpers\Url; - /** - * Class AccountController + * Class UserDefinitions */ class UserDefinitions { - public static function getUserShort(User $user) { return [ 'id' => $user->id, 'guid' => $user->guid, 'display_name' => $user->displayName, - 'url' => Url::to(['/', 'container' => $user], true) + 'url' => Url::to(['/', 'container' => $user], true), ]; } @@ -37,7 +41,7 @@ public static function getUser(User $user) 'display_name' => $user->displayName, 'url' => Url::to(['/', 'container' => $user], true), 'account' => static::getAccount($user), - 'profile' => static::getProfile($user->profile) + 'profile' => static::getProfile($user->profile), ]; } @@ -75,8 +79,74 @@ public static function getGroup(Group $group) 'description' => $group->description, 'show_at_registration' => $group->show_at_registration, 'show_at_directory' => $group->show_at_directory, - 'sort_order' => $group->sort_order + 'sort_order' => $group->sort_order, + ]; + } + + public static function getFriendship(Friendship $friendship) + { + return [ + 'id' => $friendship->id, + 'user_id' => $friendship->user_id, + 'friend_user_id' => $friendship->friend_user_id, + 'created_at' => $friendship->created_at, ]; } -} + public static function getPassword(?Password $password) + { + if (!$password) { + return []; + } + + return [ + 'id' => $password->id, + 'user_id' => $password->user_id, + 'algorithm' => $password->algorithm, + 'password' => $password->password, + 'salt' => $password->salt, + 'created_at' => $password->created_at, + ]; + } + + public static function getMentioning(Mentioning $mentioning) + { + return [ + 'id' => $mentioning->id, + 'object_model' => $mentioning->object_model, + 'object_id' => $mentioning->object_id, + 'user_id' => $mentioning->user_id, + ]; + } + + public static function getUserFollow(Follow $follow) + { + return [ + 'id' => $follow->id, + 'object_model' => $follow->object_model, + 'object_id' => $follow->object_id, + 'user_id' => $follow->user_id, + 'send_notifications' => $follow->send_notifications, + ]; + } + + public static function getUserAuth(Auth $auth) + { + return [ + 'id' => $auth->id, + 'user_id' => $auth->user_id, + 'source' => $auth->source, + 'source_id' => $auth->source_id, + ]; + } + + public static function getUserHttpSession(Session $session) + { + return [ + 'id' => $session->id, + 'expire' => $session->expire, + 'user_id' => $session->user_id, + 'data' => $session->data, + ]; + } +} From 378d02209be52d514434cfdc334449ac6a8e3c19 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Thu, 12 Sep 2024 12:08:45 +0200 Subject: [PATCH 4/4] Export additional user data --- Events.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Events.php b/Events.php index e48ef9b..e7ee16a 100644 --- a/Events.php +++ b/Events.php @@ -32,6 +32,7 @@ use humhub\modules\user\models\Follow; use humhub\modules\user\models\Group; use humhub\modules\user\models\Mentioning; +use humhub\modules\user\models\Password; use humhub\modules\user\models\Session; use Yii; @@ -187,7 +188,9 @@ public static function onLegalModuleUserDataExport(UserDataCollectionEvent $even { $event->addExportData('user', UserDefinitions::getUser($event->user)); - $event->addExportData('password', UserDefinitions::getPassword($event->user->currentPassword)); + $event->addExportData('password', array_map(function ($password) { + return UserDefinitions::getPassword($password); + }, Password::findAll(['user_id' => $event->user->id]))); $event->addExportData('friendship', array_map(function ($friendship) { return UserDefinitions::getFriendship($friendship); @@ -249,10 +252,7 @@ public static function onLegalModuleUserDataExport(UserDataCollectionEvent $even $event->addExportData('space-membership', array_map(function ($membership) { return SpaceDefinitions::getSpaceMembership($membership); - }, Membership::find() - ->innerJoin('space', 'space.id = space_membership.space_id') - ->where(['space.created_by' => $event->user->id]) - ->all())); + }, Membership::findAll(['user_id' => $event->user->id]))); $files = File::findAll(['created_by' => $event->user->id]); $event->addExportData('file', array_map(function ($file) {