From 2775569936f013523dbd28c5bc17016341c37430 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 9 Feb 2022 13:48:16 -0700 Subject: [PATCH] * Notifications: Admins now configure the mail account to use for email notifications instead of the user account --- notifications/inc/class.notifications.inc.php | 66 ++++++++++++++----- .../inc/class.notifications_email.inc.php | 17 +++-- notifications/lang/egw_en.lang | 1 + notifications/setup/setup.inc.php | 1 + notifications/templates/default/config.xet | 8 +-- 5 files changed, 66 insertions(+), 27 deletions(-) diff --git a/notifications/inc/class.notifications.inc.php b/notifications/inc/class.notifications.inc.php index d1f87507c4..6120a14cc7 100644 --- a/notifications/inc/class.notifications.inc.php +++ b/notifications/inc/class.notifications.inc.php @@ -215,28 +215,25 @@ class notifications { * it's an int with the account id or the e-mail address of a non-eGW user */ public function set_sender($_sender) { - // Check configured sender first - that overrides all - $config = Api\Config::read('notifications'); - if($config && ($config['async_account' || $config['async_email']])) + if(is_object($_sender)) { - $send_user = $config['async_account']; - $email = $config['async_email'] ?: $GLOBALS['egw']->accounts->id2name($send_user,'account_email'); - } - - if(is_object($_sender)) { $this->sender = $_sender; return true; - } else { + } + else + { // no object atm, we have to handle this and make a pseudo-object - if(is_numeric($_sender)) { - $this->sender = (object) $GLOBALS['egw']->accounts->read($_sender); + if(is_numeric($_sender)) + { + $this->sender = (object)$GLOBALS['egw']->accounts->read($_sender); return true; } - if(is_string($_sender) && strpos($_sender,'@')) { - $this->sender = (object) array ( - 'account_email' => $this->get_addresspart($_sender,'email'), - 'account_fullname' => $this->get_addresspart($_sender,'fullname'), - ); + if(is_string($_sender) && strpos($_sender, '@')) + { + $this->sender = (object)array( + 'account_email' => $this->get_addresspart($_sender, 'email'), + 'account_fullname' => $this->get_addresspart($_sender, 'fullname'), + ); return true; } } @@ -829,12 +826,45 @@ class notifications { * @param array $_data * @return boolean */ - public function set_popupdata($_appname, $_data) { + public function set_popupdata($_appname, $_data) + { $this->popup_data = array( 'appname' => $_appname, - 'data' => $_data + 'data' => $_data ); return true; } + + /** + * Hook for site configuration + * Gets the appropriate mail accounts to offer to use for notifications + * + * @param $data + * @return void + */ + public function config($data) + { + $result = ['sel_options' => ['async_identity' => []]]; + $identities = iterator_to_array(EGroupware\Api\Mail\Account::search(false, 'params', 'ABS(egw_ea_valid.account_id) ASC, ')); + + // We only want identities for all users, and prefer SMTP only + $smtp_only = []; + $others = []; + foreach($identities as $id => $identity) + { + // Identities should be sorted so all users are first. Stop when we get to the others. + if(!in_array('0', $identity['account_id'])) + { + break; + } + $destination = $identity['acc_imap_host'] ? 'others' : 'smtp_only'; + $$destination[$id] = $identity['acc_name']; + } + + // Put SMTP only identities first + $result['sel_options']['async_identity'] = $smtp_only + $others; + + return $result; + } } diff --git a/notifications/inc/class.notifications_email.inc.php b/notifications/inc/class.notifications_email.inc.php index f86e048b91..fae5f66109 100644 --- a/notifications/inc/class.notifications_email.inc.php +++ b/notifications/inc/class.notifications_email.inc.php @@ -79,14 +79,21 @@ class notifications_email implements notifications_iface { // Use configured mail ac $ident = null; - if($this->config->async_account) + if($this->config->async_identity) { - $mail_identities = Api\Mail\Account::identities([], true, 'params', $this->config->async_account); - foreach ($mail_identities as $mi) + $ident = Api\Mail\Account::read($this->config->async_identity); + } + else + { + if($this->config->async_account) { - if ($mi['ident_email'] == $this->config->async_email || !$this->config->async_email) + $mail_identities = Api\Mail\Account::identities([], true, 'params', $this->config->async_account); + foreach($mail_identities as $mi) { - $ident = Api\Mail\Account::read($mi['acc_id']); + if($mi['ident_email'] == $this->config->async_email || !$this->config->async_email) + { + $ident = Api\Mail\Account::read($mi['acc_id']); + } } } } diff --git a/notifications/lang/egw_en.lang b/notifications/lang/egw_en.lang index b50f472888..e8c761986d 100644 --- a/notifications/lang/egw_en.lang +++ b/notifications/lang/egw_en.lang @@ -55,6 +55,7 @@ last month notifications en Last month linked entries: common en Linked entries: login aborted, application exit? notifications en Login aborted, application exit? login in egroupware: notifications en login in EGroupware: +mail account to use for notifications admin en Mail account to use for notifications mail backend admin en Mail backend mark all as read notifications en mark all as read mark as read notifications en mark as read diff --git a/notifications/setup/setup.inc.php b/notifications/setup/setup.inc.php index 3d68322385..609b345469 100644 --- a/notifications/setup/setup.inc.php +++ b/notifications/setup/setup.inc.php @@ -33,6 +33,7 @@ $setup_info[NOTIFICATION_APP]['hooks'][] = 'after_navbar'; $setup_info[NOTIFICATION_APP]['hooks'][] = 'settings'; $setup_info[NOTIFICATION_APP]['hooks'][] = 'admin'; $setup_info[NOTIFICATION_APP]['hooks']['deleteaccount'] = 'notifications.notifications.deleteaccount'; +$setup_info[NOTIFICATION_APP]['hooks']['config'] = 'notifications.notifications.config'; /* Dependencies for this app to work */ $setup_info[NOTIFICATION_APP]['depends'][] = array( diff --git a/notifications/templates/default/config.xet b/notifications/templates/default/config.xet index b82bf8e616..4d53deb21a 100644 --- a/notifications/templates/default/config.xet +++ b/notifications/templates/default/config.xet @@ -35,11 +35,11 @@ - - - + +