Skip to content

Commit

Permalink
Merge pull request #35 from LyseonTech/feature-send-email
Browse files Browse the repository at this point in the history
Feature send email
  • Loading branch information
vitormattos authored Feb 13, 2021
2 parents b12fef9 + e6118cb commit 82df417
Show file tree
Hide file tree
Showing 15 changed files with 518 additions and 121 deletions.
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

// Pages
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#external', 'url' => '/external', 'verb' => 'GET'],
['name' => 'page#sign', 'url' => '/sign/{uuid}', 'verb' => 'GET']
],
];
149 changes: 74 additions & 75 deletions composer.lock

Large diffs are not rendered by default.

49 changes: 26 additions & 23 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
namespace OCA\Libresign\AppInfo;

use OCA\Files\Event\LoadSidebar;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Helper\JSConfigHelper;
use OCA\Libresign\Listener\LoadSidebarListener;
use OCA\Libresign\Storage\ClientStorage;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\Util;

class Application extends App implements IBootstrap {
Expand All @@ -19,7 +27,7 @@ public function __construct() {
}

public function boot(IBootContext $context): void {
$this->registerHooks();
$this->registerHooks($context);
}

public function register(IRegistrationContext $context): void {
Expand All @@ -36,27 +44,22 @@ public function register(IRegistrationContext $context): void {
});
}

private function registerHooks(): void {
Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig');
}

/**
* @param array $settings
*/
public function extendJsConfig(array $settings) {
$appConfig = json_decode($settings['array']['oc_appconfig'], true);

$appConfig['libresign'] = [
'user' => [
'name' => 'Jhon Doe'
],
'sign' => [
'pdf' => 'http://asfadsf.asdfasdf',
'filename' => 'Contract',
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
]
];

$settings['array']['oc_appconfig'] = json_encode($appConfig);
private function registerHooks($context): void {
$request = $context->getServerContainer()->get(IRequest::class);
$path = $request->getRawPathInfo();
$regex = '/' . self::APP_ID . '\/sign\/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/';
if (!preg_match($regex, $path)) {
return;
}
$jsConfigHelper = new JSConfigHelper(
$context->getServerContainer()->get(ISession::class),
$request,
$context->getServerContainer()->get(FileMapper::class),
$context->getServerContainer()->get(FileUserMapper::class),
$this->getContainer()->get(IL10N::class),
$context->getServerContainer()->get(IRootFolder::class),
$context->getServerContainer()->get(IURLGenerator::class)
);
Util::connectHook('\OCP\Config', 'js', $jsConfigHelper, 'extendJsConfig');
}
}
2 changes: 1 addition & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function index() {
* @NoCSRFRequired
* @PublicPage
*/
public function external() {
public function sign($uuid) {
Util::addScript(Application::APP_ID, 'libresign-external');
$response = new TemplateResponse(Application::APP_ID, 'external', [], TemplateResponse::RENDER_AS_BASE);
$policy = new ContentSecurityPolicy();
Expand Down
16 changes: 11 additions & 5 deletions lib/Controller/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\Libresign\Controller;

use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Service\MailService;
use OCA\Libresign\Service\WebhookService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
Expand All @@ -17,18 +18,22 @@ class WebhookController extends ApiController {
/** @var IL10N */
private $l10n;
/** @var WebhookService */
private $service;
private $webhook;
/** @var MailService */
private $mail;

public function __construct(
IRequest $request,
IUserSession $userSession,
IL10N $l10n,
WebhookService $service
WebhookService $webhook,
MailService $mail
) {
parent::__construct(Application::APP_ID, $request);
$this->userSession = $userSession;
$this->l10n = $l10n;
$this->service = $service;
$this->webhook = $webhook;
$this->mail = $mail;
}

/**
Expand All @@ -47,7 +52,7 @@ public function register(array $file, array $users, string $name, ?string $callb
'userManager' => $user
];
try {
$this->service->validate($data);
$this->webhook->validate($data);
} catch (\Throwable $th) {
return new JSONResponse(
[
Expand All @@ -56,7 +61,8 @@ public function register(array $file, array $users, string $name, ?string $callb
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
$return = $this->service->save($data);
$return = $this->webhook->save($data);
$this->mail->notifyAllUnsigned();
return new JSONResponse(
[
'message' => $this->l10n->t('Success'),
Expand Down
18 changes: 18 additions & 0 deletions lib/Db/FileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\Libresign\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

/**
Expand All @@ -23,4 +24,21 @@ class FileMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'libresign_file');
}

/**
* Return LibreSign file by ID
*
* @return Entity Row of table libresign_file
*/
public function getById($id) {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);

return $this->findEntity($qb);
}
}
4 changes: 2 additions & 2 deletions lib/Db/FileUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function __construct() {
$this->addType('userId', 'string');
$this->addType('uuid', 'string');
$this->addType('email', 'string');
$this->addType('first_name', 'string');
$this->addType('full_name', 'string');
$this->addType('firstName', 'string');
$this->addType('fullName', 'string');
$this->addType('createdAt', 'string');
$this->addType('signed', 'string');
}
Expand Down
30 changes: 30 additions & 0 deletions lib/Db/FileUserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\Libresign\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

/**
Expand All @@ -23,4 +24,33 @@ class FileUserMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'libresign_file_user');
}

/**
* Returns all users who have not signed
*
* @return Entity[] all fetched entities
*/
public function findUnsigned() {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->isNull('signed')
);

return $this->findEntities($qb);
}

public function getByUuid(string $uuid) {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('uuid', $qb->createNamedParameter($uuid, IQueryBuilder::PARAM_STR))
);

return $this->findEntity($qb);
}
}
10 changes: 10 additions & 0 deletions lib/Helper/JSActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OCA\Libresign\Helper;

final class JSActions {
public const ACTION_REDIRECT = 100;
public const ACTION_CREATE_USER = 150;
public const ACTION_DO_NOTHING = 200;
public const ACTION_SIGN = 250;
}
112 changes: 112 additions & 0 deletions lib/Helper/JSConfigHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace OCA\Libresign\Helper;

use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;

class JSConfigHelper {
/** @var ISession */
private $session;
/** @var IRequest */
private $request;
/** @var FileMapper */
private $fileMapper;
/** @var FileUserMapper */
private $fileUserMapper;
/** @var IL10N */
private $l10n;
/** @var IRootFolder */
private $root;
/** @var IURLGenerator */
private $urlGenerator;
public function __construct(
ISession $session,
IRequest $request,
FileMapper $fileMapper,
FileUserMapper $fileUserMapper,
IL10N $l10n,
IRootFolder $root,
IURLGenerator $urlGenerator
) {
$this->session = $session;
$this->request = $request;
$this->fileMapper = $fileMapper;
$this->fileUserMapper = $fileUserMapper;
$this->l10n = $l10n;
$this->root = $root;
$this->urlGenerator = $urlGenerator;
}

/**
* @param array $settings
*/
public function extendJsConfig(array $settings) {
$appConfig = json_decode($settings['array']['oc_appconfig'], true);
$uuid = $this->request->getParam('uuid');
$userId = $this->session->get('user_id');
try {
$fileUser = $this->fileUserMapper->getByUuid($uuid);
} catch (\Throwable $th) {
$appConfig['libresign']['action'] = JSActions::ACTION_DO_NOTHING;
$appConfig['libresign']['errors'][] = $this->l10n->t('Invalid uuid');
$settings['array']['oc_appconfig'] = json_encode($appConfig);
return;
}
$fileUserId = $fileUser->getUserId();
if (!$fileUserId) {
if ($userId) {
$appConfig['libresign']['action'] = JSActions::ACTION_DO_NOTHING;
$appConfig['libresign']['errors'][] = $this->l10n->t('This is not your file');
$settings['array']['oc_appconfig'] = json_encode($appConfig);
return;
}
$appConfig['libresign']['action'] = JSActions::ACTION_CREATE_USER;
$settings['array']['oc_appconfig'] = json_encode($appConfig);
return;
}
if (!$userId) {
$appConfig['libresign']['action'] = JSActions::ACTION_REDIRECT;

$appConfig['libresign']['redirect'] = $this->urlGenerator->linkToRoute('core.login.showLoginForm', [
'redirect_url' => $this->urlGenerator->linkToRoute(
'libresign.Page.sign',
['uuid' => $uuid]
),
]);
$appConfig['libresign']['errors'][] = $this->l10n->t('You are not logged in. Please log in.');
$settings['array']['oc_appconfig'] = json_encode($appConfig);
return;
}
if ($fileUserId != $userId) {
$appConfig['libresign']['action'] = JSActions::ACTION_DO_NOTHING;
$appConfig['libresign']['errors'][] = $this->l10n->t('Invalid user');
$settings['array']['oc_appconfig'] = json_encode($appConfig);
return;
}
$fileData = $this->fileMapper->getById($fileUser->getLibresignFileId());
$fileToSign = $this->root->getById($fileData->getFileId());
if (count($fileToSign) < 1) {
$appConfig['libresign']['action'] = JSActions::ACTION_DO_NOTHING;
$appConfig['libresign']['errors'][] = $this->l10n->t('File not found');
$settings['array']['oc_appconfig'] = json_encode($appConfig);
return;
}
$fileToSign = $fileToSign[0];
$appConfig['libresign']['action'] = JSActions::ACTION_SIGN;
$appConfig['libresign']['user']['name'] = $fileUser->getFirstName();
$appConfig['libresign']['sign'] = [
'pdf' => [
'base64' => $fileToSign->getContent()
],
'filename' => $fileData->getName(),
'description' => $fileData->getDescription()
];
$settings['array']['oc_appconfig'] = json_encode($appConfig);
}
}
Loading

0 comments on commit 82df417

Please sign in to comment.