Skip to content

Commit

Permalink
Create user
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormattos committed Feb 13, 2021
1 parent 5390660 commit 341f8cf
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 14 deletions.
11 changes: 8 additions & 3 deletions lib/Controller/AccountController.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\Helper\JSActions;
use OCA\Libresign\Service\AccountService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -32,25 +33,29 @@ public function __construct(
* @NoCSRFRequired
* @return JSONResponse
*/
public function createToSign(string $uuid, string $email) {
public function createToSign(string $uuid, string $email, string $password, string $signPassword) {
try {
$data = [
'uuid' => $uuid,
'email' => $email
'email' => $email,
'password' => $password,
'signPassword' => $signPassword
];
$this->account->validateCreateToSign($data);
$this->account->createToSign($uuid, $email, $password, $signPassword);
} catch (\Throwable $th) {
return new JSONResponse(
[
'message' => $th->getMessage(),
'action' => JSActions::ACTION_DO_NOTHING
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
return new JSONResponse(
[
'message' => $this->l10n->t('Success'),
'data' => $return
'action' => JSActions::ACTION_SIGN
],
Http::STATUS_OK
);
Expand Down
101 changes: 99 additions & 2 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,63 @@

namespace OCA\Libresign\Service;

use OC\Files\Filesystem;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileUser;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CfsslHandler;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Files\File;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserManager;
use Sabre\DAV\UUIDUtil;

class AccountService {
/** @var IL10N */
private $l10n;
/** @var FileUserMapper */
private $fileUserMapper;
/** @var IUserManager */
protected $userManager;
/** @var SignatureService */
private $signature;
/** @var FolderService */
private $folder;
/** @var IConfig */
private $config;
/** @var NewUserMailHelper */
private $newUserMail;
/** @var CfsslHandler */
private $cfsslHandler;

public function __construct(
IL10N $l10n,
FileUserMapper $fileUserMapper
FileUserMapper $fileUserMapper,
IUserManager $userManager,
SignatureService $signature,
FolderService $folder,
IConfig $config,
NewUserMailHelper $newUserMail,
CfsslHandler $cfsslHandler
) {
$this->l10n = $l10n;
$this->fileUserMapper = $fileUserMapper;
$this->userManager = $userManager;
$this->signature = $signature;
$this->folder = $folder;
$this->config = $config;
$this->newUserMail = $newUserMail;
$this->cfsslHandler = $cfsslHandler;
}

public function validateCreateToSign(array $data) {
if (!UUIDUtil::validateUUID($data['uuid'])) {
throw new LibresignException($this->l10n->t('Invalid UUID'), 1);
}
try {
$fileUser = $this->fileUserMapper->getByUuid($data['uuid']);
$fileUser = $this->getFileUserByUuid($data['uuid']);
} catch (\Throwable $th) {
throw new LibresignException($this->l10n->t('UUID not found'), 1);
}
Expand All @@ -42,5 +74,70 @@ public function validateCreateToSign(array $data) {
if (empty($data['password'])) {
throw new LibresignException($this->l10n->t('Password is mandatory'), 1);
}
if (empty($data['signPassword'])) {
throw new LibresignException($this->l10n->t('Password to sign is mandatory'), 1);
}
}

/**
* Get fileUser by Uuid
*
* @param string $uuid
* @return FileUser
*/
private function getFileUserByUuid($uuid) {
if (!$this->fileUser) {
$this->fileUser = $this->fileUserMapper->getByUuid($uuid);
}
return $this->fileUser;
}

public function createToSign($uuid, $uid, $password, $signPassword) {
$fileUser = $this->getFileUserByUuid($uuid);
$newUser = $this->userManager->createUser($uid, $password);
$fileUser->setUserId($newUser->getUID());
$this->fileUserMapper->update($fileUser);

$newUser->setEMailAddress($uid);
if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') {
try {
$emailTemplate = $this->newUserMail->generateTemplate($newUser, false);
$this->newUserMail->sendMail($newUser, $emailTemplate);
} catch (\Exception $e) {
throw new LibresignException('Unable to send the invitation', 1);
}
}
$this->folder->setUserId($newUser->getUID());

$content = $this->cfsslHandler->generateCertificate(
$this->config->getAppValue(Application::APP_ID, 'commonName'),
[],
$this->config->getAppValue(Application::APP_ID, 'country'),
$this->config->getAppValue(Application::APP_ID, 'organization'),
$this->config->getAppValue(Application::APP_ID, 'organizationUnit'),
$signPassword,
$this->config->getAppValue(Application::APP_ID, 'cfsslUri')
);
if (!$content) {
throw new LibresignException('Failure on generate certificate', 1);
}
$this->savePfx($uid, $content);
}

private function savePfx($uid, $content) {
Filesystem::initMountPoints($uid);
$folder = $this->folder->getFolderForUser();
$filename = 'signature.pfx';
if ($folder->nodeExists($filename)) {
$node = $folder->get($filename);
if (!$node instanceof File) {
throw new LibresignException("path {$filename} already exists and is not a file!", 400);
}
$node->putContent($content);
return $node;
}

$file = $folder->newFile($filename);
$file->putContent($content);
}
}
5 changes: 5 additions & 0 deletions lib/Service/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __construct(
$this->l10n = $l10n;
$this->userId = $userId;
}

public function setUserId($userId) {
$this->userId = $userId;
}

/**
* @return Folder
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Controller/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\Libresign\Tests\Unit\Controller;

use OCA\Libresign\Controller\AccountController;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\AccountService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -35,10 +36,10 @@ public function testCreateSuccess() {
->method('t')
->will($this->returnArgument(0));

$actual = $this->controller->createToSign('uuid', 'email');
$actual = $this->controller->createToSign('uuid', 'email', 'password', 'signPassword');
$expected = new JSONResponse([
'message' => 'Success',
'data' => null
'action' => JSActions::ACTION_SIGN
], Http::STATUS_OK);
$this->assertEquals($expected, $actual);
}
Expand Down
34 changes: 32 additions & 2 deletions tests/Unit/Service/AccountServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use OCA\Libresign\Db\FileUser;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Handler\CfsslHandler;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\FolderService;
use OCA\Libresign\Service\SignatureService;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserManager;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -17,16 +23,40 @@ final class AccountServiceTest extends TestCase {
private $l10n;
/** @var FileUserMapper */
private $fileUserMapper;
/** @var IUserManager */
protected $userManager;
/** @var SignatureService */
private $signatureService;
/** @var FolderService */
private $folder;
/** @var IConfig */
private $config;
/** @var NewUserMailHelper */
private $newUserMail;
/** @var CfsslHandler */
private $cfsslHandler;

public function setUp(): void {
$this->l10n = $this->createMock(IL10N::class);
$this->l10n
->method('t')
->will($this->returnArgument(0));
$this->fileUserMapper = $this->createMock(FileUserMapper::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->signature = $this->createMock(SignatureService::class);
$this->folder = $this->createMock(FolderService::class);
$this->config = $this->createMock(IConfig::class);
$this->newUserMail = $this->createMock(NewUserMailHelper::class);
$this->cfsslHandler = $this->createMock(CfsslHandler::class);
$this->service = new AccountService(
$this->l10n,
$this->fileUserMapper
$this->fileUserMapper,
$this->userManager,
$this->signature,
$this->folder,
$this->config,
$this->newUserMail,
$this->cfsslHandler
);
}

Expand Down Expand Up @@ -123,7 +153,7 @@ public function testValidateSignPasswordDontMatch() {
'uuid' => '12345678-1234-1234-1234-123456789012',
'email' => 'valid@test.coop',
'password' => '123456789',
'signPassword' => '123456789',
'signPassword' => '',
]);
}
}
8 changes: 3 additions & 5 deletions tests/Unit/Service/WebhookServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ final class WebhookServiceTest extends TestCase {
private $fileUser;
/** @var IUser */
private $user;
/** @var FolderService */
private $folderService;
/** @var ClientService */
/** @var IClientService */
private $client;

public function setUp(): void {
Expand All @@ -49,15 +47,15 @@ public function setUp(): void {
$this->file = $this->createMock(FileMapper::class);
$this->fileUser = $this->createMock(FileUserMapper::class);
$this->user = $this->createMock(IUser::class);
$this->folderService = $this->createMock(FolderService::class);
$this->client = $this->createMock(IClientService::class);
$this->folder = $this->createMock(FolderService::class);
$this->service = new WebhookService(
$this->config,
$this->groupManager,
$this->l10n,
$this->file,
$this->fileUser,
$this->folderService,
$this->folder,
$this->client
);
}
Expand Down

0 comments on commit 341f8cf

Please sign in to comment.