Skip to content

Commit

Permalink
Merge pull request #65 from lavdnone/master
Browse files Browse the repository at this point in the history
optional imap groups via domain & make domain striping optional
  • Loading branch information
violoncelloCH authored Jun 2, 2019
2 parents ccd2b22 + 8c38759 commit 406e41c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Add the following to your `config.php`:
array(
'class' => 'OC_User_IMAP',
'arguments' => array(
'127.0.0.1', 993, 'ssl', 'example.com'
'127.0.0.1', 993, 'ssl', 'example.com', true, false
),
),
),
Expand All @@ -79,9 +79,12 @@ you want to restrict the domain (4th parameter), you need to also specify
the port (2nd parameter) and sslmode (3rd parameter; set to `null` for
insecure connection).
If a domain name (e.g. example.com) is specified, then this makes sure that
only users from this domain will be allowed to login. 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.
only users from this domain will be allowed to login. If the fifth parameter
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.



Expand Down
16 changes: 12 additions & 4 deletions lib/base.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
/**
* Copyright (c) 2014 Christian Weiske <cweiske@cweiske.de>
* @author Jonas Sulzer <jonas@violoncello.ch>
* @author Christian Weiske <cweiske@cweiske.de>
* @copyright (c) 2014 Christian Weiske <cweiske@cweiske.de>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
Expand Down Expand Up @@ -168,20 +170,26 @@ public function setDisplayName($uid, $displayName) {
* Create user record in database
*
* @param string $uid The username
* @param array $groups Groups to add the user to on creation
*
* @return void
*/
protected function storeUser($uid)
{
protected function storeUser($uid, $groups) {
if (!$this->userExists($uid)) {

$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->insert('users_external')
->values([
'uid' => $query->createNamedParameter($uid),
'backend' => $query->createNamedParameter($this->backend),
]);
$query->execute();

if ($groups) {
$createduser = \OC::$server->getUserManager()->get($uid);
foreach ($groups as $group) {
\OC::$server->getGroupManager()->createGroup($group)->addUser($createduser);
}
}
}
}

Expand Down
28 changes: 21 additions & 7 deletions lib/imap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
/**
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* @author Robin Appelman <icewind@owncloud.com>
* @author Jonas Sulzer <jonas@violoncello.ch>
* @copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
Expand All @@ -22,21 +24,27 @@ class OC_User_IMAP extends \OCA\user_external\Base {
private $port;
private $sslmode;
private $domain;
private $stripeDomain;
private $groupDomain;

/**
* Create new IMAP authentication provider
*
* @param string $mailbox IMAP server domain/IP
* @param string $port IMAP server $port
* @param int $port IMAP server $port
* @param string $sslmode
* @param string $domain If provided, loging will be restricted to this domain
* @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) {
public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false) {
parent::__construct($mailbox);
$this->mailbox = $mailbox;
$this->port = $port === null ? 143 : $port;
$this->sslmode = $sslmode;
$this->domain= $domain === null ? '' : $domain;
$this->domain = $domain === null ? '' : $domain;
$this->stripeDomain = $stripeDomain;
$this->groupDomain = $groupDomain;
}

/**
Expand All @@ -54,20 +62,26 @@ public function checkPassword($uid, $password) {
$uid = str_replace("%40","@",$uid);
}

$pieces = explode('@', $uid);
if ($this->domain !== '') {
$pieces = explode('@', $uid);
if (count($pieces) === 1) {
$username = $uid . '@' . $this->domain;
} else if(count($pieces) === 2 && $pieces[1] === $this->domain) {
$username = $uid;
$uid = $pieces[0];
if ($this->stripeDomain) {
$uid = $pieces[0];
}
} else {
return false;
}
} else {
$username = $uid;
}

if ($this->groupDomain && $pieces[1]) {
$groups[] = $pieces[1];
}

$rcube = new imap_rcube();

$params = ["port"=>$this->port, "timeout"=>10];
Expand All @@ -85,7 +99,7 @@ public function checkPassword($uid, $password) {
if($canconnect) {
$rcube->closeConnection();
$uid = mb_strtolower($uid);
$this->storeUser($uid);
$this->storeUser($uid, $groups);
return $uid;
}
return false;
Expand Down

0 comments on commit 406e41c

Please sign in to comment.