From 135db5002fc5c5adb2278ceec85d73e8d3208fa0 Mon Sep 17 00:00:00 2001 From: Jonas Sulzer Date: Wed, 22 May 2019 22:52:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20move=20group=20creati?= =?UTF-8?q?on=20to=20imap=20class=20&&=20check=20for=20two=20additional=20?= =?UTF-8?q?parameters=20wheter=20to=20stripe=20domain=20and=20create=20gro?= =?UTF-8?q?up=20based=20on=20domain=20or=20not?= 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 | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/base.php b/lib/base.php index a6c5f89..cbf144c 100644 --- a/lib/base.php +++ b/lib/base.php @@ -173,10 +173,8 @@ public function setDisplayName($uid, $displayName) { * * @return void */ - protected function storeUser($uid) - { + protected function storeUser($uid) { if (!$this->userExists($uid)) { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $query->insert('users_external') ->values([ @@ -184,13 +182,7 @@ protected function storeUser($uid) 'backend' => $query->createNamedParameter($this->backend), ]); $query->execute(); - $pieces = explode('@',$uid,2); - if($pieces[1]) { - $createduser = \OC::$server->getUserManager()->get($uid); - \OC::$server->getGroupManager()->createGroup($pieces[1])->addUser($createduser); - } } - } /** diff --git a/lib/imap.php b/lib/imap.php index 7b17ed7..e170e85 100644 --- a/lib/imap.php +++ b/lib/imap.php @@ -24,6 +24,8 @@ class OC_User_IMAP extends \OCA\user_external\Base { private $port; private $sslmode; private $domain; + private $stripeDomain; + private $groupDomain; /** * Create new IMAP authentication provider @@ -33,12 +35,14 @@ class OC_User_IMAP extends \OCA\user_external\Base { * @param string $sslmode * @param string $domain If provided, loging will be restricted to this domain */ - 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; } /** @@ -56,13 +60,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; } @@ -86,9 +92,26 @@ public function checkPassword($uid, $password) { if($canconnect) { $uid = mb_strtolower($uid); - $this->storeUser($uid); + $this->storeUser($uid, $pieces[1]); 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($groupDomain && $group) { + $createduser = \OC::$server->getUserManager()->get($uid); + \OC::$server->getGroupManager()->createGroup($group)->addUser($createduser); + } + } + } }