Skip to content

Commit df9cbe6

Browse files
bilbolodzPiotr Goczał
authored andcommitted
Added posibility to limit users access by user_regexp parameter
Signed-off-by: Piotr Goczał <p.goczal@intra.pixel.com.pl>
1 parent 85a13f4 commit df9cbe6

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ IMAP user and password need to be given for the Nextcloud login.
6262

6363

6464
### Configuration
65-
The parameters are `host, port, sslmode, domain`.
65+
The parameters are `host, port, sslmode, domain, user_regexp`.
6666
Possible values for sslmode are `ssl` or `tls`.
6767
Add the following to your `config.php`:
6868

6969
'user_backends' => array(
7070
array(
7171
'class' => 'OC_User_IMAP',
7272
'arguments' => array(
73-
'127.0.0.1', 993, 'ssl', 'example.com', true, false
73+
'127.0.0.1', 993, 'ssl', 'example.com', true, false, '^user[0-9]?$|^admin_user$|^other_admin_user$'
7474
),
7575
),
7676
),
@@ -88,6 +88,9 @@ the rest used as username in Nextcloud. e.g. 'username@example.com' will be
8888
the user, it is added to a group corresponding to the name of the domain part
8989
of the address.
9090

91+
In case when not all email account should have access to nexclodu platform you can limit allowed users adding optional user_regexp setting.
92+
That should be PHP preg_match patern. Be carreful with these setting especially with ^$ chars. Without ^$ patern 'user1' will match also 'other_user10' account!
93+
9194
**⚠⚠ Warning:** If you are [**upgrading** from versions **<0.6.0**](https://github.com/nextcloud/user_external/releases/tag/v0.6.0), beside adapting your `config.php` you also have to change the `backend` column in the `users_external` table of the database. In your pre 0.6.0 database it may look like `{127.0.0.1:993/imap/ssl/readonly}INBOX` or similar, but now it has to be just `127.0.0.1` for everything to work flawless again. ⚠⚠
9295

9396

lib/imap.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ class OC_User_IMAP extends \OCA\user_external\Base {
3535
* @param boolean $stripeDomain (whether to stripe the domain part from the username or not)
3636
* @param boolean $groupDomain (whether to add the usere to a group corresponding to the domain of the address)
3737
*/
38-
public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false) {
38+
public function __construct($mailbox, $port = null, $sslmode = null, $domain = null, $stripeDomain = true, $groupDomain = false, $user_regexp = null) {
3939
parent::__construct($mailbox);
4040
$this->mailbox = $mailbox;
4141
$this->port = $port === null ? 143 : $port;
4242
$this->sslmode = $sslmode;
4343
$this->domain = $domain === null ? '' : $domain;
4444
$this->stripeDomain = $stripeDomain;
4545
$this->groupDomain = $groupDomain;
46+
$this->user_regexp = $user_regexp === null ? '' : $user_regexp;
4647
}
4748

4849
/**
@@ -80,6 +81,16 @@ public function checkPassword($uid, $password) {
8081
$username = $uid;
8182
}
8283

84+
if ($this->user_regexp != '') {
85+
if (!preg_match('/'.$this->user_regexp.'/', $username)) {
86+
OC::$server->getLogger()->error(
87+
'ERROR: User:'.$username.' does NOT match user regexp: '.$this->user_regexp,
88+
['app' => 'user_external']
89+
);
90+
return false;
91+
}
92+
}
93+
8394
$groups = [];
8495
if ($this->groupDomain && $pieces[1]) {
8596
$groups[] = $pieces[1];

0 commit comments

Comments
 (0)