Skip to content

Commit

Permalink
Merge pull request #177 from humhub/fix/167-export-core-user-data
Browse files Browse the repository at this point in the history
Export core user data by "Legal" module
  • Loading branch information
luke- authored Sep 13, 2024
2 parents b29237a + 378d022 commit dd409c5
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 13 deletions.
108 changes: 107 additions & 1 deletion Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
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\friendship\models\Friendship;
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\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\Password;
use humhub\modules\user\models\Session;
use Yii;

class Events
Expand Down Expand Up @@ -141,7 +167,7 @@ public static function onBeforeRequest($event)
['pattern' => 'rest/admin/index', 'route' => 'rest/admin', 'verb' => ['POST', 'GET']],

// Catch all to ensure verbs
['pattern' => 'rest/<tmpParam:.*>', 'route' => 'rest/error/notfound']
['pattern' => 'rest/<tmpParam:.*>', 'route' => 'rest/error/notfound'],

], true);

Expand All @@ -157,4 +183,84 @@ private static function addModuleNotFoundRoutes($moduleId)
['pattern' => "{$moduleId}/<tmpParam:.*>", 'route' => "rest/{$moduleId}/{$moduleId}/not-supported"],
]);
}

public static function onLegalModuleUserDataExport(UserDataCollectionEvent $event)
{
$event->addExportData('user', UserDefinitions::getUser($event->user));

$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);
}, 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])));

$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])));

$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])));

$event->addExportData('space-membership', array_map(function ($membership) {
return SpaceDefinitions::getSpaceMembership($membership);
}, Membership::findAll(['user_id' => $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());
}
}
}
7 changes: 3 additions & 4 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
],
];
?>
84 changes: 77 additions & 7 deletions definitions/UserDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}

Expand All @@ -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),
];
}

Expand Down Expand Up @@ -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,
];
}
}
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit dd409c5

Please sign in to comment.