Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit e9c60d9

Browse files
author
Burgy Benjamin
committed
Merge pull request #8 from minidfx/feature/IS-7
Feature/is 7
2 parents 4271a2b + b7503c2 commit e9c60d9

File tree

7 files changed

+202
-43
lines changed

7 files changed

+202
-43
lines changed

css/settings.css

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
#imapauth input.imap_uri {
1+
#imapauth .imap_uri input, .imap_port input, .imap_max_retries input {
2+
margin-left:20px;
3+
}
4+
5+
#imapauth .imap_uri input {
26
width: 300px;
37
}
48

5-
#imapauth input.imap_port,
6-
#imapauth input.imap_max_retries {
9+
#imapauth .imap_max_retries input, .imap_port input {
710
width: 50px;
11+
}
12+
13+
#imapauth .imap_port {
14+
float: left;
15+
}
16+
17+
#imapauth .imap_ssl {
18+
padding: 9px 0 0 170px;
19+
}
20+
21+
#imapauth div.clear {
22+
clear: both;
823
}

js/settings-admin.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
$(document).ready(function () {
2+
var defaultTimeout = 1500;
23

3-
//noinspection JSJQueryEfficiency
4-
$('#imapauth').find('input').keyup($.debounce(1000, function () {
4+
var updateSettings = function () {
5+
//noinspection JSJQueryEfficiency
56
var field = $(this);
6-
//noinspection SpellCheckingInspection
7-
OC.AppConfig.setValue('user_imapauth', field.attr('name'), field.val());
7+
8+
if (field.is('input:checkbox')) {
9+
if (typeof field.attr('checked') != 'undefined') {
10+
//noinspection SpellCheckingInspection
11+
OC.AppConfig.setValue('user_imapauth', field.attr('name'), true);
12+
}
13+
else {
14+
//noinspection SpellCheckingInspection
15+
OC.AppConfig.setValue('user_imapauth', field.attr('name'), false);
16+
}
17+
}
18+
else {
19+
//noinspection SpellCheckingInspection
20+
OC.AppConfig.setValue('user_imapauth', field.attr('name'), field.val());
21+
}
822

923
$('#imap_settings_msg').fadeIn('slow').delay(1000).fadeOut('slow');
10-
}));
24+
};
25+
26+
//noinspection JSJQueryEfficiency
27+
$('#imapauth').find('input:text').keyup($.debounce(defaultTimeout, updateSettings));
28+
//noinspection JSJQueryEfficiency
29+
$('#imapauth').find('input[type=number]').change($.debounce(defaultTimeout, updateSettings));
30+
//noinspection JSJQueryEfficiency
31+
$('#imapauth').find('input:checkbox').change($.debounce(defaultTimeout, updateSettings));
1132

1233
//noinspection JSJQueryEfficiency
1334
$('#imapauth').submit(function (e) {

lib/imapauthbootstrapper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function __construct(IIMAPAuthenticatorApp $authenticator)
3636
$this->authenticator = $authenticator;
3737
}
3838

39+
/**
40+
* Initializes configuration of the IMAP authenticator.
41+
*
42+
* @inheritdoc
43+
*/
3944
public function init()
4045
{
4146
if (!defined('APP_ID'))
@@ -48,6 +53,11 @@ public function init()
4853
$this->authenticator->registerUserBackend();
4954
}
5055

56+
/**
57+
* Registers the configuration board in the ownCloud admin console.
58+
*
59+
* @inheritdoc
60+
*/
5161
public function registerAdminFunctionalities()
5262
{
5363
App::registerAdmin(APP_ID, 'settings');

lib/imapauthenticator.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ final class IMAPAuthenticator
6262
*/
6363
private $maxRetries;
6464

65-
/** @noinspection SpellCheckingInspection */
65+
/**
66+
* @var bool
67+
*/
68+
private $useSSL;
6669

6770
/**
6871
* Initializes an instance of <b>user_imapauth</b>
@@ -71,14 +74,13 @@ final class IMAPAuthenticator
7174
* @param IConfig $config
7275
* @param ILogger $logger
7376
* @param IIMAPWrapper $imapWrapper
74-
*
75-
* @internal param IUserSession $userSession
7677
*/
7778
public function __construct(IUserManager $userManager, IConfig $config, ILogger $logger, IIMAPWrapper $imapWrapper)
7879
{
79-
$host = $config->getAppValue(APP_ID, 'imap_uri', NULL);
80-
$port = $config->getAppValue(APP_ID, 'imap_port', NULL);
80+
$host = $config->getAppValue(APP_ID, 'imap_uri', NULL);
81+
$port = $config->getAppValue(APP_ID, 'imap_port', NULL);
8182
$maxRetries = $config->getAppValue(APP_ID, 'imap_max_retries', NULL);
83+
$useSSL = $config->getAppValue(APP_ID, 'imap_use_ssl', 'true') === 'true';
8284

8385
if ($host === NULL)
8486
{
@@ -95,13 +97,14 @@ public function __construct(IUserManager $userManager, IConfig $config, ILogger
9597
$config->setAppValue(APP_ID, 'max_retries', $maxRetries = 2);
9698
}
9799

98-
$this->host = $host;
99-
$this->port = intval($port);
100+
$this->host = $host;
101+
$this->port = intval($port);
100102
$this->maxRetries = intval($maxRetries);
103+
$this->useSSL = $useSSL;
101104

102105
$this->userManager = $userManager;
103-
$this->config = $config;
104-
$this->logger = $logger;
106+
$this->config = $config;
107+
$this->logger = $logger;
105108
$this->imapWrapper = $imapWrapper;
106109
}
107110

@@ -142,9 +145,13 @@ public function checkPassword($uid, $password)
142145
return FALSE;
143146
}
144147

145-
// INFO: [MiniDfx 15-11-2014 07:32:12] Try to open the inbox in read only.
146-
$loginResult = $this->imapWrapper->open("{{$this->host}:{$this->port}/imap/ssl}INBOX", $uid, $password,
147-
OP_READONLY, $this->maxRetries);
148+
/** @var string $mailbox */
149+
$mailbox = sprintf("{%s:%d/imap%s}INBOX",
150+
$this->host,
151+
$this->port,
152+
$this->useSSL ? '/ssl' : '/novalidate-cert');
153+
154+
$loginResult = $this->imapWrapper->open($mailbox, $uid, $password, OP_READONLY, $this->maxRetries);
148155

149156
if ($loginResult === FALSE)
150157
{

settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
$template->assign('imap_uri', OCP\Config::getAppValue(APP_ID, 'imap_uri'));
2323
$template->assign('imap_port', OCP\Config::getAppValue(APP_ID, 'imap_port'));
2424
$template->assign('imap_max_retries', OCP\Config::getAppValue(APP_ID, 'imap_max_retries'));
25+
$template->assign('imap_use_ssl', OCP\Config::getAppValue(APP_ID, 'imap_use_ssl') === 'true');
2526

2627
return $template->fetchPage();

templates/settings.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
<div id="imapauth" class="section" ng-controller="groupsController">
2-
<form id="imapauth">
2+
<form id="imapauth_form">
33
<h2><?php p($l->t('IMAP User Authentication')); ?></h2>
44

5-
<p>
6-
<label for="imap_uri"><?php p($l->t('Server Host')); ?></label>
7-
<input type="text" class="imap_uri" name="imap_uri" value="<?php p($_['imap_uri']); ?>"
8-
original-title="<?php p($l->t('The host of the IMAP server. i.e. mail.domain.com')); ?>">
5+
<p class="imap_uri">
6+
<label for="imap_uri">
7+
<?php p($l->t('Server Host')); ?>
8+
<input type="text" name="imap_uri" value="<?php p($_['imap_uri']); ?>"
9+
original-title="<?php p($l->t('The host of the IMAP server. i.e. mail.domain.com')); ?>">
10+
</label>
911
</p>
1012

11-
<p>
12-
<label for="imap_port"><?php p($l->t('Server Port')); ?></label>
13-
<input type="number" name="imap_port" class="imap_port" value="<?php p($_['imap_port']); ?>"
14-
original-title="<?php p($l->t('The port of the IMAP server. i.e. 143')) ?>">
15-
</p>
13+
<div class="imap_port_ssl">
14+
<div class="imap_port">
15+
<label for="imap_port">
16+
<?php p($l->t('Server Port')); ?>
17+
<input type="number" name="imap_port" value="<?php p($_['imap_port']); ?>"
18+
maxlength="5"
19+
original-title="<?php p($l->t('The port of the IMAP server. i.e. 143')) ?>">
20+
</label>
21+
</div>
22+
<div class="imap_ssl">
23+
<label for="imap_use_ssl">
24+
<input type="checkbox" name="imap_use_ssl"
25+
value="true"
26+
<?php if ($_['imap_use_ssl'] === TRUE) echo 'checked="checked"'; ?>
27+
original-title="<?php p($l->t('If the provider of your IMAP server supports SSL.')); ?>"/>
28+
<?php p($l->t('Use SSL')); ?>
29+
</label>
30+
</div>
31+
<div class="clear"></div>
32+
</div>
1633

17-
<p>
18-
<label for="imap_max_retries"><?php p($l->t('Max retries')); ?></label>
19-
<input type="number" name="imap_max_retries" value="<?php p($_['imap_max_retries']); ?>"
20-
class="imap_max_retries" maxlength="1"
21-
original-title="<?php p($l->t('The number of retries for reaching the IMAP server.')) ?>"/>
34+
<p class="imap_max_retries">
35+
<label for="imap_max_retries">
36+
<?php p($l->t('Max retries')); ?>
37+
<input type="number" name="imap_max_retries" value="<?php p($_['imap_max_retries']); ?>"
38+
maxlength="1"
39+
original-title="<?php p($l->t('The number of retries for reaching the IMAP server.')) ?>"/>
40+
</label>
2241
<span id="imap_settings_msg" class="msg success" style="display: none;">Saved</span>
2342
</p>
2443

2544
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" id="requesttoken">
2645

2746
<p>
28-
<?php p($l->t('ownCloud will send the user credentials to this Host. This plugin check it can open the INBOX of the user.')); ?>
47+
<?php p($l->t('ownCloud will send the user credentials to the host. This plugin will check whether it can open the INBOX of the user.')); ?>
2948
</p>
3049
</form>
3150
</div>

tests/IMAPAuthenticatorTest.php

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ public function when_checkPassword_is_called_with_wrong_credentials()
223223
$specifiedPassword = 'a password';
224224
/** @var int $returnedMaxRetries */
225225
$returnedMaxRetries = 2;
226+
/** @var string $returnedUseSSL */
227+
$returnedUseSSL = 'true';
226228

227229
$this->injectedConfigMock->expects($this->at(0))
228230
->method('getAppValue')
@@ -236,12 +238,82 @@ public function when_checkPassword_is_called_with_wrong_credentials()
236238
->method('getAppValue')
237239
->will($this->returnValue($returnedMaxRetries));
238240

241+
$this->injectedConfigMock->expects($this->at(3))
242+
->method('getAppValue')
243+
->will($this->returnValue($returnedUseSSL));
244+
239245
$this->createSut();
240246

241247
$this->injectedIMAPWrapperMock->expects($this->once())
242248
->method('open')
243-
->with("{{$returnedHost}:{$returnedPort}/imap/ssl}INBOX", $specifiedUsername,
244-
$specifiedPassword, OP_READONLY, $returnedMaxRetries)
249+
->with(sprintf('{%s:%d/imap/ssl}INBOX', $returnedHost, $returnedPort),
250+
$specifiedUsername,
251+
$specifiedPassword,
252+
OP_READONLY,
253+
$returnedMaxRetries)
254+
->will($this->returnValue(FALSE));
255+
256+
$this->injectedIMAPWrapperMock->expects($this->once())
257+
->method('getLastErrors')
258+
->will($this->returnValue($returnedIMAPErrors));
259+
260+
$this->injectedLoggerMock->expects($this->once())
261+
->method('warning')
262+
->with(implode(';', $returnedIMAPErrors), array('app' => APP_ID));
263+
264+
$result = $this->sut->checkPassword($specifiedUsername, $specifiedPassword);
265+
266+
$this->assertFalse($result);
267+
}
268+
269+
/**
270+
* @test
271+
*/
272+
public function when_checkPassword_is_called_with_wrong_credentials_without_SSL()
273+
{
274+
/** @var string $returnedHost */
275+
$returnedHost = 'an host';
276+
/** @var string $returnedPort */
277+
$returnedPort = 1;
278+
279+
/** @var array $returnedIMAPErrors */
280+
$returnedIMAPErrors = array('error 1',
281+
'error 2');
282+
283+
/** @var string $specifiedUsername */
284+
$specifiedUsername = 'an uid';
285+
/** @var string $specifiedPassword */
286+
$specifiedPassword = 'a password';
287+
/** @var int $returnedMaxRetries */
288+
$returnedMaxRetries = 2;
289+
/** @var string $returnedUseSSL */
290+
$returnedUseSSL = 'false';
291+
292+
$this->injectedConfigMock->expects($this->at(0))
293+
->method('getAppValue')
294+
->will($this->returnValue($returnedHost));
295+
296+
$this->injectedConfigMock->expects($this->at(1))
297+
->method('getAppValue')
298+
->will($this->returnValue($returnedPort));
299+
300+
$this->injectedConfigMock->expects($this->at(2))
301+
->method('getAppValue')
302+
->will($this->returnValue($returnedMaxRetries));
303+
304+
$this->injectedConfigMock->expects($this->at(3))
305+
->method('getAppValue')
306+
->will($this->returnValue($returnedUseSSL));
307+
308+
$this->createSut();
309+
310+
$this->injectedIMAPWrapperMock->expects($this->once())
311+
->method('open')
312+
->with(sprintf('{%s:%d/imap/novalidate-cert}INBOX', $returnedHost, $returnedPort),
313+
$specifiedUsername,
314+
$specifiedPassword,
315+
OP_READONLY,
316+
$returnedMaxRetries)
245317
->will($this->returnValue(FALSE));
246318

247319
$this->injectedIMAPWrapperMock->expects($this->once())
@@ -273,6 +345,8 @@ public function when_checkPassword_is_called_with_valid_credentials_and_user_don
273345
$specifiedPassword = 'a password';
274346
/** @var int $returnedMaxRetries */
275347
$returnedMaxRetries = 2;
348+
/** @var string $returnedUseSSL */
349+
$returnedUseSSL = 'true';
276350

277351
$this->injectedConfigMock->expects($this->at(0))
278352
->method('getAppValue')
@@ -286,11 +360,16 @@ public function when_checkPassword_is_called_with_valid_credentials_and_user_don
286360
->method('getAppValue')
287361
->will($this->returnValue($returnedMaxRetries));
288362

363+
$this->injectedConfigMock->expects($this->at(3))
364+
->method('getAppValue')
365+
->will($this->returnValue($returnedUseSSL));
366+
289367
$this->createSut();
290368

291369
$this->injectedIMAPWrapperMock->expects($this->once())
292370
->method('open')
293-
->with("{{$returnedHost}:{$returnedPort}/imap/ssl}INBOX", $specifiedUsername,
371+
->with(sprintf('{%s:%d/imap/ssl}INBOX', $returnedHost, $returnedPort),
372+
$specifiedUsername,
294373
$specifiedPassword, OP_READONLY, $returnedMaxRetries)
295374
->will($this->returnValue(TRUE));
296375

@@ -324,6 +403,8 @@ public function when_checkPassword_is_called_with_valid_credentials_and_user_exi
324403
$specifiedPassword = 'a password';
325404
/** @var int $returnedMaxRetries */
326405
$returnedMaxRetries = 2;
406+
/** @var string $returnedUseSSL */
407+
$returnedUseSSL = 'true';
327408

328409
$this->injectedConfigMock->expects($this->at(0))
329410
->method('getAppValue')
@@ -337,11 +418,16 @@ public function when_checkPassword_is_called_with_valid_credentials_and_user_exi
337418
->method('getAppValue')
338419
->will($this->returnValue($returnedMaxRetries));
339420

421+
$this->injectedConfigMock->expects($this->at(3))
422+
->method('getAppValue')
423+
->will($this->returnValue($returnedUseSSL));
424+
340425
$this->createSut();
341426

342427
$this->injectedIMAPWrapperMock->expects($this->once())
343428
->method('open')
344-
->with("{{$returnedHost}:{$returnedPort}/imap/ssl}INBOX", $specifiedUsername,
429+
->with(sprintf('{%s:%d/imap/ssl}INBOX', $returnedHost, $returnedPort),
430+
$specifiedUsername,
345431
$specifiedPassword, OP_READONLY, $returnedMaxRetries)
346432
->will($this->returnValue(TRUE));
347433

@@ -515,11 +601,11 @@ protected function setUp()
515601
{
516602
parent::setUp();
517603

518-
$this->injectedConfigMock = $this->getMock('\OCP\IConfig');
604+
$this->injectedConfigMock = $this->getMock('\OCP\IConfig');
519605
$this->injectedUserManagerMock = $this->getMock('\OCP\IUserManager');
520-
$this->injectedLoggerMock = $this->getMock('\OCP\ILogger');
606+
$this->injectedLoggerMock = $this->getMock('\OCP\ILogger');
521607
$this->injectedIMAPWrapperMock = $this->getMock('\OCA\user_imapauth\lib\Contracts\IIMAPWrapper');
522-
$this->returnedUserMock = $this->getMock('\OCP\IUser');
608+
$this->returnedUserMock = $this->getMock('\OCP\IUser');
523609
}
524610
}
525611

0 commit comments

Comments
 (0)