2021-01-24 06:10:44 +00:00
|
|
|
<?php
|
|
|
|
namespace OCA\CTAutoMailHooks\Hooks;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCA\CTAutoMailHooks\Config;
|
|
|
|
class UserHooks {
|
|
|
|
private $logger;
|
|
|
|
private $config;
|
|
|
|
private $userManager;
|
|
|
|
private $logContext = ['app' => 'ct_auto_mail_hooks'];
|
|
|
|
public function __construct(
|
|
|
|
ILogger $logger, Config $config, IUserManager $userManager) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
}
|
|
|
|
public function register() {
|
|
|
|
$this->userManager->listen(
|
|
|
|
'\OC\User', 'postCreateUser',$this->createUserCB());
|
|
|
|
$this->userManager->listen(
|
|
|
|
'\OC\User', 'preDelete', $this->deleteUserCB());
|
2021-02-22 03:06:57 +00:00
|
|
|
$this->userManager->listen(
|
|
|
|
'\OC\User', 'postSetPassword', $this->updateEmailPasswordCB());
|
2021-01-24 06:10:44 +00:00
|
|
|
}
|
|
|
|
private function createUserCB() {
|
|
|
|
return function (\OC\User\User $user, string $password) {
|
|
|
|
$newuser = array(
|
2023-09-05 20:25:24 +00:00
|
|
|
'email' => $user->getUID() . $this->config->getEmailAddressSuffix(),
|
|
|
|
'displayed_name' => $user->getDisplayName(),
|
|
|
|
'raw_password' => $password,
|
|
|
|
'quota_bytes' => $this->config->getEmailQuotaMB() * 1024 * 1024,
|
|
|
|
'enabled' => true,
|
|
|
|
'enable_imap' => true,
|
|
|
|
'spam_enable' => true,
|
|
|
|
'spam_mark_as_read' => true,
|
|
|
|
'spam_threshold' => 80
|
2021-01-24 06:10:44 +00:00
|
|
|
);
|
2023-09-05 20:25:24 +00:00
|
|
|
|
2021-01-24 06:10:44 +00:00
|
|
|
if($this->createEmailAccount($newuser)) {
|
|
|
|
$this->logger->warning(
|
|
|
|
"Automatically created mail account for uid " . $uid
|
|
|
|
. " with e-mail address \"" . $newuser["email"]
|
|
|
|
. " e-mail address suffix was \"". $email_suffix . "\"."
|
|
|
|
, $this->logContext);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->logger->error(
|
|
|
|
"Error creating mail account OR mail folders for uid "
|
|
|
|
. $uid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-09-05 20:25:24 +00:00
|
|
|
|
2021-01-24 06:10:44 +00:00
|
|
|
private function deleteUserCB() {
|
|
|
|
return function (\OC\User\User $user) {
|
2023-09-05 20:25:24 +00:00
|
|
|
$email = $user->getUID() . $this->config->getEmailAddressSuffix();
|
|
|
|
if($this->deleteEmailAccount($email)) {
|
|
|
|
$this->logger->warning("Deleted mail account: " . $email, $this->logContext);
|
|
|
|
} else {
|
|
|
|
$this->logger->error("Error deleting mail account" . $email);
|
|
|
|
}
|
2021-01-24 06:10:44 +00:00
|
|
|
};
|
|
|
|
}
|
2023-09-05 20:25:24 +00:00
|
|
|
|
2021-02-22 03:06:57 +00:00
|
|
|
private function updateEmailPasswordCB() {
|
|
|
|
return function (\OC\User\User $user, string $password) {
|
2023-09-05 20:25:24 +00:00
|
|
|
$email = $user->getUID() . $this->config->getEmailAddressSuffix();
|
|
|
|
if($this->updateEmailPassword($email, $password)) {
|
|
|
|
$this->logger->warning("Updated glesys password for account: " . $email, $this->logContext);
|
|
|
|
} else {
|
|
|
|
$this->logger->error("Error updating mail password for account" . $email);
|
|
|
|
}
|
2021-02-22 03:29:12 +00:00
|
|
|
};
|
|
|
|
}
|
2023-09-05 20:25:24 +00:00
|
|
|
|
2021-02-22 03:06:57 +00:00
|
|
|
private function createEmailAccount($user_data) {
|
2023-09-27 18:38:50 +00:00
|
|
|
$ch = curl_init($this.config->getMailUAPI . 'user');
|
|
|
|
$payload = json_encode($user_data);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
|
|
|
|
'Authorization:' . $this->config->getMailUKeys()));
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
$message = 'createEmailAccount Error:' . curl_error($ch);
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($code == 200) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->error("createEmailAccount returned ".$code." message: ".$result);
|
|
|
|
return false;
|
2021-01-24 06:10:44 +00:00
|
|
|
}
|
2023-09-05 20:25:24 +00:00
|
|
|
|
|
|
|
private function deleteEmailAccount($user_data) {
|
2023-09-27 18:38:50 +00:00
|
|
|
$ch = curl_init($this.config->getMailUAPI . 'user/' . urlencode($user_data->email));
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->config->getMailUKeys()));
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
$message = 'deleteEmailAccount Error:' . curl_error($ch);
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($code == 200) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->error("deleteEmailAccount returned ".$code." message: ".$result);
|
|
|
|
return false;
|
2021-02-22 03:06:57 +00:00
|
|
|
}
|
2023-09-05 20:25:24 +00:00
|
|
|
|
2021-02-22 03:06:57 +00:00
|
|
|
private function updateEmailPassword($user_data) {
|
2023-09-27 18:38:50 +00:00
|
|
|
$ch = curl_init($this.config->getMailUAPI . 'user/' . urlencode($user_data->email));
|
|
|
|
$payload = json_encode(array("raw_password" => $user_data['password']));
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
|
|
|
|
'Authorization:' . $this->config->getMailUKeys()));
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
$message = 'updateEmailPassword Error:' . curl_error($ch);
|
|
|
|
$this->logger->error($message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($code == 200) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->error("updateEmailPassword returned ".$code." message: ".$result);
|
|
|
|
return false;
|
2021-01-24 06:10:44 +00:00
|
|
|
}
|
|
|
|
}
|