Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ AWS_S3_BUCKET=lyratest
AWS_S3_SUBFOLDER=earth

IMAGICK_CLI="convert"
MAX_ACCOUNTS=0
10 changes: 8 additions & 2 deletions src/Module/Api/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\DTO\UserDTO;
use App\Entity\Account;
use App\Repository\AccountRepository;
use App\Service\AccountService;
use Unicorn\Flysystem\Base64DataUri;
use Windwalker\Core\Application\AppContext;
use Windwalker\Core\Attributes\Controller;
Expand Down Expand Up @@ -135,7 +136,8 @@ protected function readImageIcon(FileObject $file, string $color, AppContext $ap
public function save(
AppContext $app,
ORM $orm,
\CurrentUser $currentUser
\CurrentUser $currentUser,
AccountService $accountService
): Account {
$item = $app->input('item');

Expand All @@ -152,6 +154,8 @@ public function save(
if ($current) {
$orm->updateOne(Account::class, $account);
} else {
$accountService->validateUserNotExceedAccountLimit($currentUser);

$account = $orm->createOne(Account::class, $account);
}

Expand All @@ -162,7 +166,8 @@ public function save(
public function saveMultiple(
AppContext $app,
ORM $orm,
\CurrentUser $currentUser
\CurrentUser $currentUser,
AccountService $accountService
): array {
$items = $app->input('items');

Expand All @@ -186,6 +191,7 @@ public function saveMultiple(
if ($current) {
$orm->updateOne(Account::class, $account);
} else {
$accountService->validateUserNotExceedAccountLimit($currentUser);
$account = $orm->createOne(Account::class, $account);
}

Expand Down
34 changes: 32 additions & 2 deletions src/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,46 @@

namespace App\Service;

use Unicorn\Flysystem\Base64DataUri;
use App\Entity\Account;
use App\Entity\User;
use Windwalker\Core\Form\Exception\ValidateFailException;
use Windwalker\DI\Attributes\Service;
use Windwalker\ORM\ORM;
use Windwalker\Utilities\Cache\InstanceCacheTrait;

use function Windwalker\Query\uuid2bin;

#[Service]
class AccountService
{
use InstanceCacheTrait;

public function __construct(protected EncryptionService $encryptionService)
public function __construct(protected ORM $orm)
{
}

public function countAccounts(User $user): int
{
return (int) $this->orm->from(Account::class)
->selectRaw('COUNT(id) AS count')
->where('user_id', uuid2bin($user->getId()))
->result();
}

public function validateUserNotExceedAccountLimit(User $user): void
{
$limit = (int) env('MAX_ACCOUNTS');

if ($limit <= 0) {
return;
}

$count = $this->countAccounts($user);

if ($count >= $limit) {
throw new ValidateFailException(
'You have exceeded the maximum number of available account tokens: ' . $limit
);
}
}
}