added hook for password update

This commit is contained in:
Alexander Vassilevski 2021-02-21 18:40:26 -08:00
parent 69e87dbf22
commit 0fc202ac85
2 changed files with 62 additions and 18 deletions

View File

@ -11,9 +11,11 @@ class Config {
private $logger;
private $appConfiguration;
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->appConfiguration = $nextCloudConfiguration->getSystemValue(self::CONFIG_KEY);
$this->appConfiguration =
$nextCloudConfiguration->getSystemValue(self::CONFIG_KEY);
}
public function getGlesysKeys() {
return $this->getConfValue(self::CONFIG_KEY_GLESYS_KEYS);

View File

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