Skip to content

Commit

Permalink
UCRM 5991 - Barcode plugin - Install hook and composer update
Browse files Browse the repository at this point in the history
  • Loading branch information
janprochazkacz committed Jul 8, 2024
1 parent 1fa1675 commit 54bae40
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 40 deletions.
Binary file modified plugins/barcode-generator/barcode-generator.zip
Binary file not shown.
6 changes: 4 additions & 2 deletions plugins/barcode-generator/src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
}
},
"require": {
"tecnickcom/tc-lib-barcode": "^1.17",
"monolog/monolog": "^2.7",
"symfony/http-foundation": "^6.0",
"ubnt/ucrm-plugin-sdk": "^0.10.0"
"ubnt/ucrm-plugin-sdk": "^0.10.0",
"symfony/filesystem": "^6.4",
"tecnickcom/tc-lib-barcode": "^2.2",
"nette/utils": "^4.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest"
Expand Down
161 changes: 125 additions & 36 deletions plugins/barcode-generator/src/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions plugins/barcode-generator/src/hook_install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use UBarcode\Service\TokenInstaller;
use Ubnt\UcrmPluginSdk\Service\PluginLogManager;

chdir(__DIR__);

require __DIR__ . '/vendor/autoload.php';

$logger = new Logger('UBarcode');
$logger->pushHandler(new StreamHandler('data/plugin.log'));

$logManager = PluginLogManager::create();

(new TokenInstaller())->install();

$logger->info('Security token has been generated. Use it as a part of request string.');
4 changes: 2 additions & 2 deletions plugins/barcode-generator/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"configuration": [
{
"key": "token",
"label": "Security token",
"description": "Fill it with random string and use it as a part of request.",
"label": "Security token (token)",
"description": "This is a generated random string for use as a part of the request.",
"required": 1
}
]
Expand Down
28 changes: 28 additions & 0 deletions plugins/barcode-generator/src/src/Service/TokenInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);


namespace UBarcode\Service;


use Nette\Utils\Json;
use Symfony\Component\Filesystem\Filesystem;
use UBarcode\Util\Strings;

final class TokenInstaller
{
private string $token;
private Filesystem $filesystem;

public function __construct()
{
$this->filesystem = new Filesystem();
$this->token = Strings::generateUuidWithDashes();
}

public function install(): void
{
$this->filesystem->dumpFile('data/config.json', Json::encode(['token' => $this->token]));
}
}
19 changes: 19 additions & 0 deletions plugins/barcode-generator/src/src/Util/Strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace UBarcode\Util;

final class Strings
{
public static function generateUuidWithDashes(): string
{
$uuidBin = random_bytes(18);
$uuidBin &= "\xFF\xFF\xFF\xFF\x0F\xFF\xF0\x0F\xFF\x03\xFF\xF0\xFF\xFF\xFF\xFF\xFF\xFF";
$uuidBin |= "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00";
$uuidHex = bin2hex($uuidBin);
$uuidHex[8] = $uuidHex[13] = $uuidHex[18] = $uuidHex[23] = '-';

return $uuidHex;
}
}

0 comments on commit 54bae40

Please sign in to comment.