Test Connection for email account

This commit is contained in:
yuri
2015-04-15 12:10:59 +03:00
parent 6db658c0b3
commit b778f74628
11 changed files with 233 additions and 9 deletions

View File

@@ -41,7 +41,7 @@ class Email extends \Espo\Core\Controllers\Record
throw new BadRequest();
}
if (empty($data['password'])) {
if (is_null($data['password'])) {
if ($data['type'] == 'preferences') {
if (!$this->getUser()->isAdmin() && $data['id'] != $this->getUser()->id) {
throw new Forbidden();

View File

@@ -44,5 +44,27 @@ class EmailAccount extends \Espo\Core\Controllers\Record
throw new Forbidden();
}
}
public function actionTestConnection($params, $data, $request)
{
if (!$request->isPost()) {
throw new BadRequest();
}
if (is_null($data['password'])) {
$emailAccount = $this->getEntityManager()->getEntity('EmailAccount', $data['id']);
if (!$emailAccount) {
throw new Error();
}
if ($emailAccount->get('assignedUserId') != $this->getUser()->id && !$this->getUser()->isAdmin()) {
throw new Forbidden();
}
$data['password'] = $this->getContainer()->get('crypt')->decrypt($emailAccount->get('password'));
}
return $this->getRecordService()->testConnection($data);
}
}

View File

@@ -18,7 +18,7 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
namespace Espo\Modules\Crm\Controllers;
@@ -30,9 +30,9 @@ class InboundEmail extends \Espo\Core\Controllers\Record
throw new Forbidden();
}
}
public function actionGetFolders($params, $data, $request)
{
{
return $this->getRecordService()->getFolders(array(
'host' => $request->get('host'),
'port' => $request->get('port'),
@@ -41,7 +41,23 @@ class InboundEmail extends \Espo\Core\Controllers\Record
'password' => $request->get('password'),
'id' => $request->get('id')
));
}
public function actionTestConnection($params, $data, $request)
{
if (!$request->isPost()) {
throw new BadRequest();
}
if (is_null($data['password'])) {
$inboundEmail = $this->getEntityManager()->getEntity('InboundEmail', $data['id']);
if (!$inboundEmail) {
throw new Error();
}
$data['password'] = $this->getContainer()->get('crypt')->decrypt($inboundEmail->get('password'));
}
return $this->getRecordService()->testConnection($data);
}
}

View File

@@ -26,7 +26,8 @@
],
[
{"name":"monitoredFolders"},{"name":"password"}
]
],
[{"name": "testConnection", "customLabel": null, "view": "Crm:InboundEmail.Fields.TestConnection"}]
]
},
{

View File

@@ -121,6 +121,27 @@ class InboundEmail extends \Espo\Services\Record
return $foldersArr;
}
public function testConnection(array $params)
{
$imapParams = array(
'host' => $params['host'],
'port' => $params['port'],
'user' => $params['username'],
'password' => $params['password']
);
if (!empty($params['ssl'])) {
$imapParams['ssl'] = 'SSL';
}
$storage = new \Espo\Core\Mail\Mail\Storage\Imap($imapParams);
if ($storage->getFolders()) {
return true;
}
throw new Error();
}
public function fetchFromMailServer(Entity $inboundEmail)
{
if ($inboundEmail->get('status') != 'Active') {

View File

@@ -21,9 +21,11 @@
"labels": {
"Create EmailAccount": "Create Email Account",
"IMAP": "IMAP",
"Main": "Main"
"Main": "Main",
"Test Connection": "Test Connection"
},
"messages": {
"couldNotConnectToImap": "Could not connect to IMAP server"
"couldNotConnectToImap": "Could not connect to IMAP server",
"connectionIsOk": "Connection is Ok"
}
}

View File

@@ -22,7 +22,8 @@
],
[
{"name":"monitoredFolders"},{"name":"password"}
]
],
[{"name": "testConnection", "customLabel": null, "view": "EmailAccount.Fields.TestConnection"}]
]
}
]

View File

@@ -92,6 +92,27 @@ class EmailAccount extends Record
return $foldersArr;
}
public function testConnection(array $params)
{
$imapParams = array(
'host' => $params['host'],
'port' => $params['port'],
'user' => $params['username'],
'password' => $params['password']
);
if (!empty($params['ssl'])) {
$imapParams['ssl'] = 'SSL';
}
$storage = new \Espo\Core\Mail\Mail\Storage\Imap($imapParams);
if ($storage->getFolders()) {
return true;
}
throw new Error();
}
public function createEntity($data)
{
if (!$this->getUser()->isAdmin()) {

View File

@@ -0,0 +1,31 @@
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
Espo.define('Crm:Views.InboundEmail.Fields.TestConnection', 'Views.EmailAccount.Fields.TestConnection', function (Dep) {
return Dep.extend({
url: 'InboundEmail/action/testConnection',
});
});

View File

@@ -0,0 +1,109 @@
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
Espo.define('Views.EmailAccount.Fields.TestConnection', 'Views.Fields.Base', function (Dep) {
return Dep.extend({
readOnly: true,
_template: '<button class="btn btn-default disabled" data-action="testConnection">{{translate \'Test Connection\' scope=\'EmailAccount\'}}</button>',
url: 'EmailAccount/action/testConnection',
events: {
'click [data-action="testConnection"]': function () {
this.test();
},
},
fetch: function () {
return {};
},
checkAvailability: function () {
if (this.model.get('host')) {
this.$el.find('button').removeClass('disabled');
} else {
this.$el.find('button').addClass('disabled');
}
},
afterRender: function () {
this.checkAvailability();
this.stopListening(this.model, 'change:host');
this.listenTo(this.model, 'change:host', function () {
this.checkAvailability();
}, this);
},
getData: function () {
var data = {
'host': this.model.get('host'),
'port': this.model.get('port'),
'ssl': this.model.get('ssl'),
'username': this.model.get('username'),
'password': this.model.get('password') || null,
'id': this.model.id
};
return data;
},
test: function () {
var data = this.getData();
var $btn = this.$el.find('button');
$btn.addClass('disabled');
Espo.Ui.notify(this.translate('pleaseWait', 'messages'));
$.ajax({
url: this.url,
type: 'POST',
data: JSON.stringify(data),
error: function (xhr, status) {
var statusReason = xhr.getResponseHeader('X-Status-Reason') || '';
statusReason = statusReason.replace(/ $/, '');
statusReason = statusReason.replace(/,$/, '');
var msg = this.translate('Error') + ' ' + xhr.status;
if (statusReason) {
msg += ': ' + statusReason;
}
Espo.Ui.error(msg);
console.error(msg);
xhr.errorIsHandled = true;
$btn.removeClass('disabled');
}.bind(this)
}).done(function () {
$btn.removeClass('disabled');
Espo.Ui.success(this.translate('connectionIsOk', 'messages', 'EmailAccount'));
}.bind(this));
},
});
});

View File

@@ -90,7 +90,7 @@ Espo.define('Views.OutboundEmail.Fields.TestSend', 'Views.Fields.Base', function
type: 'POST',
data: JSON.stringify(data),
error: function (xhr, status) {
var statusReason = xhr.getResponseHeader('X-Status-Reason');
var statusReason = xhr.getResponseHeader('X-Status-Reason') || '';
statusReason = statusReason.replace(/ $/, '');
statusReason = statusReason.replace(/,$/, '');