diff --git a/README.md b/README.md index a600d61..d04ba37 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/base.php b/lib/base.php index 4f5023d..e3d527c 100644 --- a/lib/base.php +++ b/lib/base.php @@ -1,6 +1,8 @@ + * @author Jonas Sulzer + * @author Christian Weiske + * @copyright (c) 2014 Christian Weiske * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. @@ -168,13 +170,12 @@ 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([ @@ -182,6 +183,13 @@ protected function storeUser($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); + } + } } } diff --git a/lib/imap.php b/lib/imap.php index 93ce093..fee0813 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -1,6 +1,8 @@ + * @author Robin Appelman + * @author Jonas Sulzer + * @copyright (c) 2012 Robin Appelman * 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 @@ 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; } @@ -68,6 +78,10 @@ public function checkPassword($uid, $password) { $username = $uid; } + if ($this->groupDomain && $pieces[1]) { + $groups[] = $pieces[1]; + } + $rcube = new imap_rcube(); $params = ["port"=>$this->port, "timeout"=>10]; @@ -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;