added password update hook

This commit is contained in:
Alexander Vassilevski 2021-02-21 19:06:57 -08:00
parent b99446dff1
commit fe013d6eea
2 changed files with 64 additions and 18 deletions

View File

@ -11,9 +11,11 @@ class Config {
private $logger; private $logger;
private $appConfiguration; private $appConfiguration;
private $logContext = ['app' => 'ct_auto_mail_hooks']; private $logContext = ['app' => 'ct_auto_mail_hooks'];
public function __construct(ILogger $logger, IConfig $nextCloudConfiguration) { public function __construct(
ILogger $logger, IConfig $nextCloudConfiguration) {
$this->logger = $logger; $this->logger = $logger;
$this->appConfiguration = $nextCloudConfiguration->getSystemValue(self::CONFIG_KEY); $this->appConfiguration =
$nextCloudConfiguration->getSystemValue(self::CONFIG_KEY);
} }
public function getGlesysKeys() { public function getGlesysKeys() {
return $this->getConfValue(self::CONFIG_KEY_GLESYS_KEYS); return $this->getConfValue(self::CONFIG_KEY_GLESYS_KEYS);

View File

@ -19,6 +19,8 @@ class UserHooks {
'\OC\User', 'postCreateUser',$this->createUserCB()); '\OC\User', 'postCreateUser',$this->createUserCB());
$this->userManager->listen( $this->userManager->listen(
'\OC\User', 'preDelete', $this->deleteUserCB()); '\OC\User', 'preDelete', $this->deleteUserCB());
$this->userManager->listen(
'\OC\User', 'postSetPassword', $this->updateEmailPasswordCB());
} }
private function createUserCB() { private function createUserCB() {
return function (\OC\User\User $user, string $password) { return function (\OC\User\User $user, string $password) {
@ -33,7 +35,6 @@ class UserHooks {
'email' => $uid . $email_suffix, 'email' => $uid . $email_suffix,
'quota' => $quota // in MB 'quota' => $quota // in MB
); );
// $this->logger->warning("newuser:". print_r($newuser,1));
if($this->createEmailAccount($newuser)) { if($this->createEmailAccount($newuser)) {
$this->logger->warning( $this->logger->warning(
"Automatically created mail account for uid " . $uid "Automatically created mail account for uid " . $uid
@ -54,17 +55,32 @@ class UserHooks {
$email_suffix = $this->config->getEmailAddressSuffix(); $email_suffix = $this->config->getEmailAddressSuffix();
$email = $uid . $email_suffix; $email = $uid . $email_suffix;
$user_data = array('email' => $email); $user_data = array('email' => $email);
// $this->logger->warning("user_data:". print_r($user_data,1));
if($this->deleteEmailAccount($user_data)) if($this->deleteEmailAccount($user_data))
$this->logger->warning( $this->logger->warning(
"Deleted mail account: " . $email, $this->logContext); "Deleted mail account: " . $email, $this->logContext);
else $this->logger->error("Error deleting mail account" . $uid); else $this->logger->error("Error deleting mail account" . $uid);
}; };
} }
private function createImapFolders($newuser) { private function updateEmailPasswordCB() {
return function (\OC\User\User $user, string $password) {
$uid = $user->getUID();
$email_suffix = $this->config->getEmailAddressSuffix();
$email = $uid . $email_suffix;
$user_data = array(
'email' => $email,
'password' => $password
);
if($this->updateEmailPassword($user_data))
$this->logger->warning(
"Updated glesys password for account: " . $email,
$this->logContext);
else $this->logger->error("Error updating mail password for account" . $uid);
};
}
private function createImapFolders($user_data) {
$server = 'mail.glesys.se'; $server = 'mail.glesys.se';
$email = $newuser['email']; $email = $user_data['email'];
$passw = $newuser['password']; $passw = $user_data['password'];
$folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive'); $folders = array('Sent', 'Drafts', 'Trash', 'Junk', 'Archive');
$mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN); $mbox = \imap_open("{" . $server . "}", $email, $passw, OP_HALFOPEN);
if(!$mbox){ if(!$mbox){
@ -84,15 +100,13 @@ class UserHooks {
\imap_close($mbox); \imap_close($mbox);
return true; return true;
} }
private function createEmailAccount($newuser) { private function createEmailAccount($user_data) {
$keys = $this->config->getGlesysKeys(); $keys = $this->config->getGlesysKeys();
$this->logger->debug("glesysKeys:" . $keys);
$fields = array( $fields = array(
'emailaccount' => $newuser['email'], 'emailaccount' => $user_data['email'],
'password' => $newuser['password'], 'password' => $user_data['password'],
'quota' => $newuser['quota'] 'quota' => $user_data['quota']
); );
// $this->logger->warning("fields:" . print_r($fields, 1));
$query = http_build_query($fields); $query = http_build_query($fields);
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, curl_setopt($ch, CURLOPT_URL,
@ -110,26 +124,56 @@ class UserHooks {
curl_close($ch); curl_close($ch);
$xml = simplexml_load_string($result); $xml = simplexml_load_string($result);
$code = $xml->status->code; $code = $xml->status->code;
$this->logger->warning("xml status text:" . $xml->status->text); if ($code == 200) return $this->createImapFolders($user_data);
if ($code == 200) return $this->createImapFolders($newuser); else $this->logger->warning(
"Error: non-200 status: " . $xml->status->text);
return false; return false;
} }
private function deleteEmailAccount($user){ private function deleteEmailAccount($user_data){
$ch = curl_init(); $ch = curl_init();
$keys = $this->config->getGlesysKeys(); $keys = $this->config->getGlesysKeys();
$this->logger->debug("glesysKeys:" . $keys);
curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/'); curl_setopt($ch, CURLOPT_URL, 'https://api.glesys.com/email/delete/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user["email"]); curl_setopt($ch, CURLOPT_POSTFIELDS, "email=" . $user_data["email"]);
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys); curl_setopt($ch, CURLOPT_USERPWD, $keys);
$result = curl_exec($ch); $result = curl_exec($ch);
if (curl_errno($ch)) {
$message = 'Error: ' . curl_error($ch);
$this->logger->error($message);
return false;
}
curl_close($ch);
return true;
}
private function updateEmailPassword($user_data) {
$keys = $this->config->getGlesysKeys();
$fields = array(
'emailaccount' => $user_data['email'],
'password' => $user_data['password']
);
$query = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://api.glesys.com/email/editaccount/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $keys);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
$message = 'Error:' . curl_error($ch); $message = 'Error:' . curl_error($ch);
$this->logger->error($message); $this->logger->error($message);
return false; return false;
} }
curl_close($ch); curl_close($ch);
$xml = simplexml_load_string($result);
$code = $xml->status->code;
if ($code != 200){
$this->logger->warning(
"Error: non-200 status: " . $xml->status->text);
return false;
}
return true; return true;
} }
} }