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

Merge branch 'fix-mailbox-mapper-error' into 'nc22'

Move API call to beforeUserDeletedListener

See merge request e/infra/ecloud/nextcloud-apps/ecloud-drop-account!16
parents d93e2c75 1356b23e
......@@ -31,8 +31,6 @@ use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\User\Events\UserDeletedEvent;
use OCA\EcloudAccounts\Listeners\UserDeletedListener;
use OCA\EcloudAccounts\Listeners\BeforeUserDeletedListener;
use OCA\EcloudAccounts\Service\LDAPConnectionService;
use OCP\User\Events\BeforeUserDeletedEvent;
......@@ -52,7 +50,6 @@ class Application extends App implements IBootstrap
public function register(IRegistrationContext $context): void
{
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class);
$context->registerEventListener(UserChangedEvent::class, UserChangedListener::class);
}
......
......@@ -19,11 +19,19 @@ class MailboxMapper
{
$this->config = $config;
$this->logger = $logger;
$params = $this->getConnectionParams();
$this->conn = DriverManager::getConnection($params);
$this->initConnection();
}
private function initConnection() {
try {
$params = $this->getConnectionParams();
$this->conn = DriverManager::getConnection($params);
}
catch(Exception $e) {
$this->logger->info('Error connecting to SQL raw backend: ' . $e->getMessage());
}
}
private function getConnectionParams()
{
$config = $this->config->getSystemValue('user_backend_sql_raw');
......
......@@ -5,12 +5,16 @@ declare(strict_types=1);
namespace OCA\EcloudAccounts\Listeners;
use Exception;
use Curl;
use OCA\EcloudAccounts\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\ILogger;
use OCP\User\Events\BeforeUserDeletedEvent;
use OCA\EcloudAccounts\Service\LDAPConnectionService;
require_once 'curl.class.php';
class BeforeUserDeletedListener implements IEventListener
{
private $logger;
......@@ -32,9 +36,19 @@ class BeforeUserDeletedListener implements IEventListener
$user = $event->getUser();
$email = $user->getEMailAddress();
$uid = $user->getUID();
$isUserOnLDAP = $this->LDAPConnectionService->isUserOnLDAPBackend($user);
$this->logger->info("PostDelete user {user}", array('user' => $uid));
$this->ecloudDelete(
$uid,
$this->config->getSystemValue('e_welcome_domain'),
$this->config->getSystemValue('e_welcome_secret'),
$email,
$isUserOnLDAP
);
try {
if ($this->LDAPConnectionService->isLDAPEnabled() && $this->LDAPConnectionService->isUserOnLDAPBackend($user)) {
if ($this->LDAPConnectionService->isLDAPEnabled() && $isUserOnLDAP) {
$conn = $this->LDAPConnectionService->getLDAPConnection();
$this->deleteAliasEntries($conn, $email);
$this->LDAPConnectionService->closeLDAPConnection($conn);
......@@ -44,6 +58,52 @@ class BeforeUserDeletedListener implements IEventListener
}
}
/**
* 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, string $email, bool $isUserOnLDAP = false)
{
$endpoint = 'postDelete.php';
if ($isUserOnLDAP) {
$endpoint = 'postDeleteLDAP.php';
}
$postDeleteUrl = "https://" . $welcomeDomain . $endpoint;
$curl = new Curl();
/**
* send action to docker_welcome
* Handling the non NC part of deletion process
*/
try {
$params = [
'sec' => $welcomeSecret,
'uid' => $userID,
'email' => $email
];
$headers = array(
'Content-Type: application/json'
);
$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_ID]);
}
return null;
}
private function deleteAliasEntries($conn, string $email)
{
$aliasBaseDn = getenv('LDAP_ALIASES_BASE_DN');
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment