diff --git a/appinfo/app.php b/appinfo/app.php index 92701e9fd1ae28c100ec90e4ba150b0c9b93d093..fd7a0946ef4dc181bf4ce4122e5fcc29d36791a9 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -21,5 +21,7 @@ * */ -$app = new \OCA\EcloudDropAccount\AppInfo\Application(); -$app->getContainer()->query('UserHooks')->register(); \ No newline at end of file +use OCA\EcloudDropAccount\AppInfo\Application; + +$app = \OC::$server->query(Application::class); +$app->register(); \ No newline at end of file diff --git a/appinfo/info.xml b/appinfo/info.xml index f173dd419d20071e76820458252a11d1aff52d7b..0f309206945e56aa04b72a8b147028eece9fa9cf 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -4,15 +4,15 @@ ecloud_drop_account Ecloud Drop Account postDelete actions dedicated to /e/ cloud setup - - 0.0.1 + + 0.0.2 agpl Florent VINCENT EcloudDropAccount tools https://gitlab.e.foundation/e/management/issues - + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index feece2e75328697116a435c64e8ef8e8c6535090..d9a18ea58db8e613e78572a2ecc50fb58093e857 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -1,4 +1,7 @@ * @@ -23,35 +26,26 @@ namespace OCA\EcloudDropAccount\AppInfo; -use OCA\EcloudDropAccount\Hooks\UserHooks; +use OCA\EcloudDropAccount\Events\UserDeletedListener; use OCP\AppFramework\App; -use OCP\ILogger; -use OCP\IContainer; -use OCP\ServerContainer; -use OCP\IUserManager; -use OCP\IConfig; - - -class Application extends App { - - const APP_NAME = 'ecloud_drop_account'; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\User\Events\UserDeletedEvent; - public function __construct () - { - parent::__construct(self::APP_NAME); +class Application extends App +{ - $container = $this->getContainer(); + const APP_NAME = 'ecloud_drop_account'; - - $container->registerService('UserHooks', function($c) { - return new UserHooks( - $c->query('ServerContainer')->getUserManager(), - $c->query('ServerContainer')->getLogger(), - $c->query('ServerContainer')->getConfig() - ); - }); - - } + public function __construct() + { + parent::__construct(self::APP_NAME); + } + public function register() + { + /* @var IEventDispatcher $eventDispatcher */ + $eventDispatcher = $this->getContainer()->query(IEventDispatcher::class); + $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class); + } } diff --git a/lib/Events/UserDeletedListener.php b/lib/Events/UserDeletedListener.php new file mode 100644 index 0000000000000000000000000000000000000000..2864cbacf88dac4304e4a2e696868e7c79944a77 --- /dev/null +++ b/lib/Events/UserDeletedListener.php @@ -0,0 +1,83 @@ +logger = $logger; + $this->config = $config; + } + + public function handle(Event $event): void + { + if (!($event instanceof UserDeletedEvent)) { + return; + } + + $uid = $event->getUser()->getUID(); + $this->logger->info("PostDelete user {user}", array('user' => $uid)); + $this->ecloudDelete( + $uid, + $this->config->getSystemValue('e_welcome_domain'), + $this->config->getSystemValue('e_welcome_secret') + ); + } + + /** + * Once NC deleted the account, + * perform specific ecloud selfhosting actions + * post delete action is delegated to the welcome container + * + * @param $userID string + * @param $welcomeDomain string main NC domain (welcome container) + * @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) + { + + $postDeleteUrl = "https://" . $welcomeDomain . "/postDelete.php"; + $curl = new Curl(); + + /** + * send action to docker_welcome + * Handling the non NC part of deletion process + */ + try { + + $headers = array( + 'Content-Type: application/json' + ); + $params = array( + 'sec' => $welcomeSecret, + 'uid' => $userID + ); + + $answer = $curl->post($postDeleteUrl, $params, $headers); + + return json_decode($answer, true); + } catch (\Exception $e) { + $this->logger->error('There has been an issue while contacting the external deletion script'); + $this->logger->logException($e, ['app' => Application::APP_NAME]); + } + + return null; + } +} diff --git a/lib/Hooks/curl.class.php b/lib/Events/curl.class.php similarity index 79% rename from lib/Hooks/curl.class.php rename to lib/Events/curl.class.php index c188d269fc3381b6db6179ca447b40142a5ef2b9..84b15baa90ebc3a9b28fcb221bb2e70c38588c11 100644 --- a/lib/Hooks/curl.class.php +++ b/lib/Events/curl.class.php @@ -16,8 +16,9 @@ class Curl * @param array $userOptions * @return mixed */ - public function get($url, $params = array(), $headers = array(), $userOptions = array()) { - return $this->request('GET',$url,$params,$headers,$userOptions); + public function get($url, $params = array(), $headers = array(), $userOptions = array()) + { + return $this->request('GET', $url, $params, $headers, $userOptions); } /** @@ -29,8 +30,9 @@ class Curl * @param array $userOptions * @return mixed */ - public function post($url, $params = array(), $headers = array(), $userOptions = array()) { - return $this->request('POST',$url,$params,$headers,$userOptions); + public function post($url, $params = array(), $headers = array(), $userOptions = array()) + { + return $this->request('POST', $url, $params, $headers, $userOptions); } @@ -45,18 +47,19 @@ class Curl * @return mixed * @throws Exception */ - private function request($method, $url, $params = array(), $headers = array(), $userOptions = array()) { + private function request($method, $url, $params = array(), $headers = array(), $userOptions = array()) + { $ch = curl_init(); $method = strtoupper($method); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => $headers ); - array_merge($options,$userOptions); + array_merge($options, $userOptions); switch ($method) { case 'GET': - if($params) { - $url = $url.'?'.http_build_query($params); + if ($params) { + $url = $url . '?' . http_build_query($params); } break; case 'POST': @@ -73,7 +76,7 @@ class Curl $response = curl_exec($ch); - if($errno = curl_errno($ch)) { + if ($errno = curl_errno($ch)) { var_dump($errno); $errorMessage = curl_strerror($errno); throw new Exception("Curl error $errno - $errorMessage"); diff --git a/lib/Hooks/UserHooks.php b/lib/Hooks/UserHooks.php deleted file mode 100644 index 8ceb765ebb9fe1cbbc6d8b14109a384cf5dbe6d8..0000000000000000000000000000000000000000 --- a/lib/Hooks/UserHooks.php +++ /dev/null @@ -1,98 +0,0 @@ -userManager = $userManager; - $this->logger = $logger; - $this->config = $config; - } - - public function register() { - - - - /** - * when auto delete action is done by user, fire postDelete hook - * to send postDelete actions to be done for /e/ specific setup - * - * username in ecloud-selfhost setup IS in the form user@$DOMAIN - * - */ - - $callback = function(IUser $user) { - - $externalDelete = $this->ecloudDelete($user->getUID()); - - }; - $this->userManager->listen('\OC\User', 'postDelete', $callback); - } - - /** - * Once NC deleted account datas - * do specific ecloud selfhosting actions - * post delete action is delegated welcome container - * - * CHECK : compare user account and domain, to be sure it's identical - * actually only comparing $trusted_domains[0] >> main domain - * - * TODO : handle account deletion with multiple trusted domains!! - * - */ - public function ecloudDelete($userID) { - - // build welcome domain url from main NC domain - $welcomeDomain = "https://".$this->config->getSystemValue('e_welcome_domain'); - $postDeleteScript = "/postDelete.php"; - - /** - * welcome secret is generated at ecloud selfhosting install - * and added as a custom var in NC's config - */ - $welcomeSecret = $this->config->getSystemValue('e_welcome_secret'); - - $curl = new Curl(); - - /** - * send action to docker_welcome - * Handling the non NC part of deletion process - */ - try { - - $headers = array( - 'Content-Type: application/json' - ) ; - $params = array( - 'sec' => $welcomeSecret, - 'uid' => $userID - ); - - $url = $welcomeDomain.$postDeleteScript; - - $answer = $curl->post($url,$params,$headers); - - return json_decode($answer,true); - - - - } catch (\Exception $e) { - $this->logger->error('There has been an issue while contacting the external deletion script'); - $this->logger->logException($e, ['app' => Application::APP_NAME]); - } - - } - -} \ No newline at end of file