From e0b2626cc0f1039a3104d88753ae8622941f0ac6 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Fri, 24 May 2019 13:36:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20make=20it=20possible?= =?UTF-8?q?=20to=20add=20the=20user=20to=20an=20array=20of=20groups=20(for?= =?UTF-8?q?=20later=20extendability=20e.g.=20#69)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonas Sulzer --- lib/base.php | 10 +++++++++- lib/imap.php | 27 ++++++++------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/base.php b/lib/base.php index cbf144c..77c150c 100644 --- a/lib/base.php +++ b/lib/base.php @@ -170,10 +170,11 @@ 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') @@ -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 a7499e7..fee0813 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -31,9 +31,11 @@ class OC_User_IMAP extends \OCA\user_external\Base { * 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, $stripeDomain = true, $groupDomain = false) { parent::__construct($mailbox); @@ -76,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]; @@ -93,26 +99,9 @@ public function checkPassword($uid, $password) { if($canconnect) { $rcube->closeConnection(); $uid = mb_strtolower($uid); - $this->storeUser($uid, $pieces[1]); + $this->storeUser($uid, $groups); return $uid; } return false; } - - protected function storeUser($uid, $group) { - 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($this->groupDomain && $group) { - $createduser = \OC::$server->getUserManager()->get($uid); - \OC::$server->getGroupManager()->createGroup($group)->addUser($createduser); - } - } - } }