Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b19dcf5 authored by Arnau Vàzquez's avatar Arnau Vàzquez
Browse files

Merge branch 'create-activity' into 'nc21'

create activity when user is deleted

See merge request e/infra/selfhost/nextcloud-apps/ecloud-drop-account!12
parents 78afe859 bea4efab
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
    <description><![CDATA[in /e/ cloud self-hosting setup, nextcloud accounts are linked to mail accounts and some other things. This app sets the mail, quota and storage of the user upon creation. 
    It also completes the account deletion by cleaning other parts of the /e/ cloud setup to ensure no more data is retained when a user requests an account deletion.
    This app uses the UserDeletedEvent to invoke scripts in the docker-welcome container of /e/ cloud setup]]></description>
    <version>1.2.0</version>
    <version>1.3.0</version>
    <licence>agpl</licence>
    <author mail="dev@e.email" homepage="https://gitlab.e.foundation/">Akhil Potukuchi</author>
    <namespace>EcloudAccounts</namespace>
+38 −2
Original line number Diff line number Diff line
@@ -11,6 +11,8 @@ use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\ILogger;
use OCP\User\Events\UserDeletedEvent;
use OCP\Activity\IManager;
use OCP\IGroupManager;

require_once 'curl.class.php';

@@ -19,11 +21,15 @@ class UserDeletedListener implements IEventListener

    private $logger;
    private $config;
    private $activityManager;
    private $groupManager;

    public function __construct(ILogger $logger, IConfig $config)
    public function __construct(ILogger $logger, IConfig $config, IManager $activityManager, IGroupManager $groupManager)
    {
        $this->logger = $logger;
        $this->config = $config;
        $this->activityManager = $activityManager;
        $this->groupManager = $groupManager;
    }

    public function handle(Event $event): void
@@ -39,6 +45,36 @@ class UserDeletedListener implements IEventListener
            $this->config->getSystemValue('e_welcome_domain'),
            $this->config->getSystemValue('e_welcome_secret')
        );

        $this->createAndPublishActivity($uid);

    }

    /**
     * Create an activity to record user deletion
     * As this will be used for monitoring and sending alerts to admins
     * @param $username string
     */
    private function createAndPublishActivity(string $username)
    {
        try {
            $event = $this->activityManager->generateEvent();
            $event->setApp(Application::APP_ID)
                    ->setType('account_deletion')
                    ->setObject('user', 0, $username)
                    ->setAuthor($username)
                    ->setSubject('account_self_deletion', ['name' => $username, 'type' => 'account']);
            $admins = $this->groupManager->get('admin')->getUsers();
            foreach ($admins as $admin) {
                $event->setAffectedUser($admin->getUID());
                $this->activityManager->publish($event);
            }

        } catch (\Exception $e) {
            $this->logger->error('There has been an issue while creating and publishing activity for user deletion');
            $this->logger->logException($e, ['app' => Application::APP_ID]);
        }

    }

    /**
@@ -51,7 +87,7 @@ class UserDeletedListener implements IEventListener
     * @param $welcomeSecret string generated at ecloud selfhosting install and added as a custom var in NC's config
     * @return mixed response of the external endpoint
     */
    public function ecloudDelete(string $userID, string $welcomeDomain, string $welcomeSecret)
    private function ecloudDelete(string $userID, string $welcomeDomain, string $welcomeSecret)
    {

        $postDeleteUrl = "https://" . $welcomeDomain . "/postDelete.php";