Skip to content
This repository has been archived by the owner on Apr 5, 2020. It is now read-only.

Commit

Permalink
Update plugin to latest KB changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Jun 3, 2016
1 parent 81a0909 commit 896df54
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 43 deletions.
7 changes: 3 additions & 4 deletions Controller/Webhook.php → Controller/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Kanboard\Plugin\Sendgrid\Controller;

use Kanboard\Controller\Base;
use Kanboard\Controller\BaseController;
use Kanboard\Plugin\Sendgrid\EmailHandler;

/**
Expand All @@ -11,7 +11,7 @@
* @package sendgrid
* @author Frederic Guillot
*/
class Webhook extends Base
class WebhookController extends BaseController
{
/**
* Handle Sendgrid webhooks
Expand All @@ -21,8 +21,7 @@ class Webhook extends Base
public function receiver()
{
$this->checkWebhookToken();

$handler = new EmailHandler($this->container);
echo $handler->receiveEmail($_POST) ? 'PARSED' : 'IGNORED';
$this->response->text($handler->receiveEmail($_POST) ? 'PARSED' : 'IGNORED');
}
}
81 changes: 58 additions & 23 deletions EmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
require_once __DIR__.'/vendor/autoload.php';

use Kanboard\Core\Base;
use Kanboard\Core\Tool;
use Kanboard\Core\Mail\ClientInterface;
use League\HTMLToMarkdown\HtmlConverter;

defined('SENDGRID_API_USER') or define('SENDGRID_API_USER', '');
defined('SENDGRID_API_KEY') or define('SENDGRID_API_KEY', '');

/**
* Sendgrid Mail Handler
*
Expand All @@ -33,11 +29,11 @@ class EmailHandler extends Base implements ClientInterface
public function sendEmail($email, $name, $subject, $html, $author)
{
$payload = array(
'api_user' => SENDGRID_API_USER,
'api_key' => SENDGRID_API_KEY,
'api_user' => $this->getApiUser(),
'api_key' => $this->getApiKey(),
'to' => $email,
'toname' => $name,
'from' => MAIL_FROM,
'from' => $this->helper->mail->getMailSenderAddress(),
'fromname' => $author,
'html' => $html,
'subject' => $subject,
Expand All @@ -63,45 +59,84 @@ public function receiveEmail(array $payload)
$sender = isset($envelope['to'][0]) ? $envelope['to'][0] : '';

// The user must exists in Kanboard
$user = $this->user->getByEmail($envelope['from']);
$user = $this->userModel->getByEmail($envelope['from']);

if (empty($user)) {
$this->logger->debug('SendgridWebhook: ignored => user not found');
return false;
}

// The project must have a short name
$project = $this->project->getByIdentifier(Tool::getMailboxHash($sender));
$project = $this->projectModel->getByIdentifier($this->helper->mail->getMailboxHash($sender));

if (empty($project)) {
$this->logger->debug('SendgridWebhook: ignored => project not found');
return false;
}

// The user must be member of the project
if (! $this->projectPermission->isMember($project['id'], $user['id'])) {
if (! $this->projectPermissionModel->isMember($project['id'], $user['id'])) {
$this->logger->debug('SendgridWebhook: ignored => user is not member of the project');
return false;
}

// Get the Markdown contents
if (! empty($payload['html'])) {
// Finally, we create the task
return (bool) $this->taskCreationModel->create(array(
'project_id' => $project['id'],
'title' => $this->helper->mail->filterSubject($payload['subject']),
'description' => $this->getTaskDescription($payload),
'creator_id' => $user['id'],
));
}

/**
* Get task description
*
* @access public
* @param array $payload
* @return string
*/
protected function getTaskDescription(array $payload)
{
$description = '';

if (!empty($payload['html'])) {
$htmlConverter = new HtmlConverter(array('strip_tags' => true));
$description = $htmlConverter->convert($payload['html']);
}
else if (! empty($payload['text'])) {
} elseif (!empty($payload['text'])) {
$description = $payload['text'];
}
else {
$description = '';

return $description;
}

/**
* Get API token
*
* @access public
* @return string
*/
public function getApiKey()
{
if (defined('SENDGRID_API_KEY')) {
return SENDGRID_API_KEY;
}

// Finally, we create the task
return (bool) $this->taskCreation->create(array(
'project_id' => $project['id'],
'title' => $payload['subject'],
'description' => $description,
'creator_id' => $user['id'],
));
return $this->configModel->get('sendgrid_api_key');
}

/**
* Get API user
*
* @access public
* @return string
*/
public function getApiUser()
{
if (defined('SENDGRID_API_USER')) {
return SENDGRID_API_USER;
}

return $this->configModel->get('sendgrid_api_user');
}
}
8 changes: 5 additions & 3 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kanboard\Plugin\Sendgrid;

use Kanboard\Core\Security\Role;
use Kanboard\Core\Translator;
use Kanboard\Core\Plugin\Base;

Expand All @@ -17,12 +18,13 @@ public function initialize()
{
$this->emailClient->setTransport('sendgrid', '\Kanboard\Plugin\Sendgrid\EmailHandler');
$this->template->hook->attach('template:config:integrations', 'sendgrid:integration');
$this->route->addRoute('/sendgrid/handler/:token', 'webhook', 'receiver', 'sendgrid');
$this->route->addRoute('/sendgrid/handler/:token', 'WebhookController', 'receiver', 'sendgrid');
$this->applicationAccessMap->add('WebhookController', 'receiver', Role::APP_PUBLIC);
}

public function onStartup()
{
Translator::load($this->language->getCurrentLanguage(), __DIR__.'/Locale');
Translator::load($this->languageModel->getCurrentLanguage(), __DIR__.'/Locale');
}

public function getPluginDescription()
Expand All @@ -37,7 +39,7 @@ public function getPluginAuthor()

public function getPluginVersion()
{
return '1.0.3';
return '1.0.4';
}

public function getPluginHomepage()
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ Author
- Frederic Guillot
- License MIT

Requirements
------------

- Kanboard >= 1.0.29
- Sendgrid API credentials

Installation
------------

- Create a folder **plugins/Sendgrid**
- Copy all files under this directory
You have the choice between 3 methods:

1. Install the plugin from the Kanboard plugin manager in one click
2. Download the zip file and decompress everything under the directory `plugins/Sendgrid`
3. Clone this repository into the folder `plugins/Sendgrid`

Note: Plugin folder is case-sensitive.

Use Sendgrid to send emails
---------------------------
Expand Down
13 changes: 12 additions & 1 deletion Template/integration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<h3><img src="<?= $this->url->dir() ?>plugins/Sendgrid/sendgrid-icon.png"/>&nbsp;Sendgrid</h3>
<div class="listing">
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->url->href('webhook', 'receiver', array('plugin' => 'sendgrid', 'token' => $values['webhook_token']), false, '', true) ?>"/><br/>
<input type="text" class="auto-select" readonly="readonly" value="<?= $this->url->href('WebhookController', 'receiver', array('plugin' => 'sendgrid', 'token' => $values['webhook_token']), false, '', true) ?>">

<?= $this->form->label(t('Sendgrid API user'), 'sendgrid_api_user') ?>
<?= $this->form->text('sendgrid_api_user', $values) ?>

<?= $this->form->label(t('Sendgrid API key'), 'sendgrid_api_key') ?>
<?= $this->form->password('sendgrid_api_key', $values) ?>

<p class="form-help"><a href="https://kanboard.net/plugin/sendgrid" target="_blank"><?= t('Help on Sendgrid integration') ?></a></p>

<div class="form-actions">
<input type="submit" value="<?= t('Save') ?>" class="btn btn-blue">
</div>
</div>
18 changes: 8 additions & 10 deletions Test/EmailHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
require_once 'tests/units/Base.php';

use Kanboard\Plugin\Sendgrid\EmailHandler;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\TaskFinder;
use Kanboard\Model\Project;
use Kanboard\Model\ProjectUserRole;
use Kanboard\Model\User;
use Kanboard\Model\TaskFinderModel;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\ProjectUserRoleModel;
use Kanboard\Model\UserModel;
use Kanboard\Core\Security\Role;

class SendgridTest extends Base
Expand All @@ -30,11 +29,10 @@ public function testSendEmail()
public function testHandlePayload()
{
$w = new EmailHandler($this->container);
$p = new Project($this->container);
$pp = new ProjectUserRole($this->container);
$u = new User($this->container);
$tc = new TaskCreation($this->container);
$tf = new TaskFinder($this->container);
$p = new ProjectModel($this->container);
$pp = new ProjectUserRoleModel($this->container);
$u = new UserModel($this->container);
$tf = new TaskFinderModel($this->container);

$this->assertEquals(2, $u->create(array('username' => 'me', 'email' => 'me@localhost')));

Expand Down

0 comments on commit 896df54

Please sign in to comment.