Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMAP: set Nextcloud user's email based on login username #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
),
),
Expand All @@ -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. '[email protected]' 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. ⚠⚠

Expand Down
6 changes: 5 additions & 1 deletion lib/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions lib/IMAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class IMAP extends Base {
private $domain;
private $stripeDomain;
private $groupDomain;
private $setEmail;

/**
* Create new IMAP authentication provider
Expand All @@ -36,14 +37,15 @@ 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;
$this->sslmode = $sslmode;
$this->domain = $domain === null ? '' : $domain;
$this->stripeDomain = $stripeDomain;
$this->groupDomain = $groupDomain;
$this->setEmail = $setEmail;
}

/**
Expand Down Expand Up @@ -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];
Expand All @@ -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 ||
Expand Down