diff --git a/README.md b/README.md index e4114dc..1353890 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Add the following to your `config.php`: array( 'class' => '\OCA\UserExternal\IMAP', 'arguments' => array( - '127.0.0.1', 993, 'ssl', 'example.com', true, false + '127.0.0.1', 993, 'ssl', 'example.com', true, false, true ), ), ), @@ -91,7 +91,8 @@ is set to true, after successfull login the domain part will be striped and the rest used as username in Nextcloud. e.g. 'username@example.com' will be 'username' in Nextcloud. The sixth parameter toggles whether on creation of the user, it is added to a group corresponding to the name of the domain part -of the address. +of the address. The seventh parameter enables setting the user's email address +in Nextcloud based on the login name. **⚠⚠ Warning:** If you are [**upgrading** from versions **<0.6.0**](https://github.com/nextcloud/user_external/releases/tag/v0.6.0), beside adapting your `config.php` you also have to change the `backend` column in the `users_external` table of the database. In your pre 0.6.0 database it may look like `{127.0.0.1:993/imap/ssl/readonly}INBOX` or similar, but now it has to be just `127.0.0.1` for everything to work flawless again. ⚠⚠ diff --git a/lib/Base.php b/lib/Base.php index 5e27d68..665488e 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -173,7 +173,7 @@ public function setDisplayName($uid, $displayName) { * * @return void */ - protected function storeUser($uid, $groups = []) { + protected function storeUser($uid, $groups = [], $email = null) { if (!$this->userExists($uid)) { $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $query->insert('users_external') @@ -189,6 +189,10 @@ protected function storeUser($uid, $groups = []) { \OC::$server->getGroupManager()->createGroup($group)->addUser($createduser); } } + if ($email) { + $createduser = \OC::$server->getUserManager()->get($uid); + $createduser->setSystemEMailAddress($email); + } } } diff --git a/lib/IMAP.php b/lib/IMAP.php index 2ad784f..5c35503 100644 --- a/lib/IMAP.php +++ b/lib/IMAP.php @@ -25,6 +25,7 @@ class IMAP extends Base { private $domain; private $stripeDomain; private $groupDomain; + private $setEmail; /** * Create new IMAP authentication provider @@ -36,7 +37,7 @@ class IMAP extends Base { * @param boolean $stripeDomain (whether to stripe the domain part from the username or not) * @param boolean $groupDomain (whether to add the usere to a group corresponding to the domain of the address) */ - public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false) { + public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false, $setEmail = false) { parent::__construct($mailbox); $this->mailbox = $mailbox; $this->port = $port === null ? 143 : $port; @@ -44,6 +45,7 @@ public function __construct($mailbox, $port = null, $sslmode = null, $domain = n $this->domain = $domain === null ? '' : $domain; $this->stripeDomain = $stripeDomain; $this->groupDomain = $groupDomain; + $this->setEmail = $setEmail; } /** @@ -81,6 +83,11 @@ public function checkPassword($uid, $password) { $username = $uid; } + $email = null; + if ($this->setEmail && strpos($username, '@') !== false) { + $email = $username; + } + $groups = []; if ((count($pieces) > 1) && $this->groupDomain && $pieces[1]) { $groups[] = $pieces[1]; @@ -104,7 +111,7 @@ public function checkPassword($uid, $password) { if ($errorcode === 0) { curl_close($ch); $uid = mb_strtolower($uid); - $this->storeUser($uid, $groups); + $this->storeUser($uid, $groups, $email); return $uid; } elseif ($errorcode === CURLE_COULDNT_CONNECT || $errorcode === CURLE_SSL_CONNECT_ERROR ||