Skip to content

Commit

Permalink
👌 IMPROVE: make it possible to add the user to an array of groups (fo…
Browse files Browse the repository at this point in the history
…r later extendability e.g. #69)

Signed-off-by: Jonas Sulzer <jonas@violoncello.ch>
Signed-off-by: none <vlad@teksperts.nyc>
  • Loading branch information
violoncelloCH authored and none committed Jun 2, 2019
1 parent f99c4da commit 8c38759
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
10 changes: 9 additions & 1 deletion lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
}
}
}
}

Expand Down
27 changes: 8 additions & 19 deletions lib/imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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);
}
}
}
}

0 comments on commit 8c38759

Please sign in to comment.