Merge pull request #65 from lavdnone/master

optional imap groups via domain & make domain striping optional
This commit is contained in:
violoncello.ch
2019-06-02 23:55:13 +02:00
committed by GitHub
3 changed files with 40 additions and 15 deletions

View File

@@ -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
),
),
),
@@ -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.

View File

@@ -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.
@@ -168,13 +170,12 @@ abstract class Base extends \OC\User\Backend{
* 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([
@@ -182,6 +183,13 @@ abstract class Base extends \OC\User\Backend{
'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);
}
}
}
}

View File

@@ -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.
@@ -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;
}
/**
@@ -54,13 +62,15 @@ class OC_User_IMAP extends \OCA\user_external\Base {
$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;
}
@@ -68,6 +78,10 @@ class OC_User_IMAP extends \OCA\user_external\Base {
$username = $uid;
}
if ($this->groupDomain && $pieces[1]) {
$groups[] = $pieces[1];
}
$rcube = new imap_rcube();
$params = ["port"=>$this->port, "timeout"=>10];
@@ -85,7 +99,7 @@ class OC_User_IMAP extends \OCA\user_external\Base {
if($canconnect) {
$rcube->closeConnection();
$uid = mb_strtolower($uid);
$this->storeUser($uid);
$this->storeUser($uid, $groups);
return $uid;
}
return false;