Skip to content

Commit befa7bf

Browse files
committed
IMAP: set Nextcloud users' email based on login username
When the login username looks like an email address (contains '@'), the Nextcloud user's email address is set accordingly. This can be configured with a seventh parameter to the IMAP backend config. This only happens on first login of a user (equivalent to the group feature). Signed-off-by: Gabriel Hege <[email protected]>
1 parent 6d4405e commit befa7bf

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Add the following to your `config.php`:
7575
array(
7676
'class' => '\OCA\UserExternal\IMAP',
7777
'arguments' => array(
78-
'127.0.0.1', 993, 'ssl', 'example.com', true, false
78+
'127.0.0.1', 993, 'ssl', 'example.com', true, false, true
7979
),
8080
),
8181
),
@@ -91,7 +91,8 @@ is set to true, after successfull login the domain part will be striped and
9191
the rest used as username in Nextcloud. e.g. '[email protected]' will be
9292
'username' in Nextcloud. The sixth parameter toggles whether on creation of
9393
the user, it is added to a group corresponding to the name of the domain part
94-
of the address.
94+
of the address. The seventh parameter enables setting the user's email address
95+
in Nextcloud based on the login name.
9596

9697
**⚠⚠ 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. ⚠⚠
9798

lib/Base.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function setDisplayName($uid, $displayName) {
173173
*
174174
* @return void
175175
*/
176-
protected function storeUser($uid, $groups = []) {
176+
protected function storeUser($uid, $groups = [], $email = null) {
177177
if (!$this->userExists($uid)) {
178178
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
179179
$query->insert('users_external')
@@ -189,6 +189,10 @@ protected function storeUser($uid, $groups = []) {
189189
\OC::$server->getGroupManager()->createGroup($group)->addUser($createduser);
190190
}
191191
}
192+
if ($email) {
193+
$createduser = \OC::$server->getUserManager()->get($uid);
194+
$createduser->setSystemEMailAddress($email);
195+
}
192196
}
193197
}
194198

lib/IMAP.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class IMAP extends Base {
2525
private $domain;
2626
private $stripeDomain;
2727
private $groupDomain;
28+
private $setEmail;
2829

2930
/**
3031
* Create new IMAP authentication provider
@@ -36,14 +37,15 @@ class IMAP extends Base {
3637
* @param boolean $stripeDomain (whether to stripe the domain part from the username or not)
3738
* @param boolean $groupDomain (whether to add the usere to a group corresponding to the domain of the address)
3839
*/
39-
public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false) {
40+
public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false, $setEmail = false) {
4041
parent::__construct($mailbox);
4142
$this->mailbox = $mailbox;
4243
$this->port = $port === null ? 143 : $port;
4344
$this->sslmode = $sslmode;
4445
$this->domain = $domain === null ? '' : $domain;
4546
$this->stripeDomain = $stripeDomain;
4647
$this->groupDomain = $groupDomain;
48+
$this->setEmail = $setEmail;
4749
}
4850

4951
/**
@@ -81,6 +83,11 @@ public function checkPassword($uid, $password) {
8183
$username = $uid;
8284
}
8385

86+
$email = null;
87+
if ($this->setEmail && strpos($username, '@') !== false) {
88+
$email = $username;
89+
}
90+
8491
$groups = [];
8592
if ((count($pieces) > 1) && $this->groupDomain && $pieces[1]) {
8693
$groups[] = $pieces[1];
@@ -104,7 +111,7 @@ public function checkPassword($uid, $password) {
104111
if ($errorcode === 0) {
105112
curl_close($ch);
106113
$uid = mb_strtolower($uid);
107-
$this->storeUser($uid, $groups);
114+
$this->storeUser($uid, $groups, $email);
108115
return $uid;
109116
} elseif ($errorcode === CURLE_COULDNT_CONNECT ||
110117
$errorcode === CURLE_SSL_CONNECT_ERROR ||

0 commit comments

Comments
 (0)