Skip to content

Commit

Permalink
👌 IMPROVE: move group creation to imap class && check for two additio…
Browse files Browse the repository at this point in the history
…nal parameters wheter to stripe domain and create group based on domain or not

Signed-off-by: Jonas Sulzer <jonas@violoncello.ch>
  • Loading branch information
violoncelloCH committed May 22, 2019
1 parent 826bfbd commit 135db50
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
10 changes: 1 addition & 9 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,16 @@ 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([
'uid' => $query->createNamedParameter($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);
}
}

}

/**
Expand Down
33 changes: 28 additions & 5 deletions lib/imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
Expand All @@ -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);
}
}
}
}

0 comments on commit 135db50

Please sign in to comment.