Skip to content
Open
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
116 changes: 116 additions & 0 deletions CLI/GenerateUsersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace openvk\CLI;

use Chandler\Security\User as ChandlerUser;
use Faker\Factory as FakerFactory;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Exceptions\InvalidUserNameException;
use openvk\Web\Util\Validator;
use Random\RandomException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class GenerateUsersCommand extends Command
{
protected static $defaultName = "generate-users";
protected function configure(): void
{
$this->setDescription("Generate test user accounts for development")
->addOption(
"count",
"c",
InputOption::VALUE_REQUIRED,
"Number of users to create",
1
);
}

//Duplicate the logic from the Web/Presenters/AuthPresenter.php class
/**
* @throws RandomException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$faker = FakerFactory::create("en_US");
$count = (int) $input->getOption("count");

if ($count < 1) {
$io->error("Count must be at least 1.");

return Command::FAILURE;
}

$created = [];

for ($i = 1; $i <= $count; $i++) {
$email = $faker->unique()->safeEmail();
$password = $this->generatePassword();

try {
$user = new User();
$user->setFirst_Name($faker->firstName());
$user->setLast_Name($faker->lastName());
$user->setSex(0);
$user->setEmail($email);
$user->setSince(date("Y-m-d H:i:s"));
$user->setRegistering_Ip("127.0.0.1");
$user->setBirthday($faker->dateTimeBetween("-60 years", "-18 years")->getTimestamp());
$user->setActivated(1);
} catch (InvalidUserNameException $ex) {
$io->error("Failed to set name for user #{$i}: " . $ex->getMessage());
Comment thread
MrKrasnov marked this conversation as resolved.

return Command::FAILURE;
}

$chUser = ChandlerUser::create($email, $password);
if (!$chUser) {
$io->error("Failed to create Chandler user for {$email}");

return Command::FAILURE;
}

$user->setUser($chUser->getId());
$user->save(false);

$created[] = [
"id" => $user->getId(),
"email" => $email,
"password" => $password,
"url" => "/id" . $user->getId(),
];
}

$io->success("Created " . count($created) . " user(s).");

$rows = array_map(static fn(array $u): array => [
$u["id"],
$u["email"],
$u["password"],
$u["url"],
], $created);

$io->table(["ID", "Email", "Password", "Profile URL"], $rows);

return Command::SUCCESS;
}

/**
* @throws RandomException
*/
private function generatePassword(): string
{
do {
$password = "OvK" . bin2hex(random_bytes(4)) . "A1";
} while (!Validator::i()->passwordStrong($password));

return $password;
}

}
24 changes: 23 additions & 1 deletion CLI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,26 @@ Rebuilds photo thumbnails and image sizes.
Fetches Toncoin transactions for payment processing.

### upgrade
Performs database upgrades and migrations.
Performs database upgrades and migrations.

## Available Commands for local development

### generate-users
Creates test user accounts for local development.

**Usage:**
```bash
# Create one user (default)
php openvkctl generate-users

# Create 20 users
php openvkctl generate-users --count=20

# Create 20 users
php openvkctl generate-users -c 20
```

**Options:**
- `--count`, `-c`: Number of users to create (default: 1)

The command prints a table with profile ID, email, password, and profile URL for each created user.
9 changes: 5 additions & 4 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ public function getChandlerUser(): ChandlerUser
return new ChandlerUser($this->getRecord()->ref("ChandlerUsers", "user"));
}

public function getURL(): string
public function getURL(bool $trimBackslash = false): string
{
$backslash = $trimBackslash ? '' : '/';
if (!is_null($this->getShortCode())) {
return "/" . $this->getShortCode();
} else {
return "/id" . $this->getId();
return $backslash . $this->getShortCode();
}

return $backslash . "id" . $this->getId();
}

public function getAvatarUrl(string $size = "miniscule", $avPhoto = null): string
Expand Down
16 changes: 9 additions & 7 deletions Web/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,18 @@ public function renderEdit(): void
}

if ($this->postParam("marialstatus") <= 8 && $this->postParam("marialstatus") >= 0) {
$user->setMarital_Status($this->postParam("marialstatus"));
}
$maritalStatus = (int) $this->postParam("marialstatus");
$user->setMarital_Status($maritalStatus);

if ($this->postParam("maritalstatus-user")) {
if (in_array((int) $this->postParam("marialstatus"), [0, 1, 8])) {
if (in_array($maritalStatus, [0, 1, 8], true)) {
Comment thread
veselcraft marked this conversation as resolved.
$user->setMarital_Status_User(null);
} else {
$mUser = (new Users())->getByAddress($this->postParam("maritalstatus-user"));
if ($mUser) {
if ($mUser->getId() !== $this->user->id) {
$partnerAddress = trim((string) $this->postParam("maritalstatus-user"));
if (empty($partnerAddress)) {
$user->setMarital_Status_User(null);
} else {
$mUser = (new Users())->getByAddress($partnerAddress);
if ($mUser && $mUser->getId() !== $this->user->id) {
$user->setMarital_Status_User($mUser->getId());
}
}
Expand Down
4 changes: 2 additions & 2 deletions Web/Presenters/templates/User/Edit.latte
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
n:attr="style => $user->getMaritalStatusUser() || ($user->getMaritalStatus() && !in_array($user->getMaritalStatus(), [0, 1, 8])) ? '' : 'display: none;'">
<td width="120" valign="top"></td>
<td>
<input type="text" placeholder="{_page_address}" name="maritalstatus-user"
n:attr="value => $user->getMaritalStatusUser() ? $user->getMaritalStatusUser()->getId() : ''" />
<input type="text" placeholder="{_marital_status_user_id}" name="maritalstatus-user"
n:attr="value => $user->getMaritalStatusUser() ? $user->getMaritalStatusUser()->getURL(true) : ''" />
</td>
</tr>
<tr>
Expand Down
8 changes: 4 additions & 4 deletions Web/static/js/al_wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,12 @@ async function withdraw(id) {
}

function toggleMaritalStatus(e) {
let elem = $("#maritalstatus-user");
$("#maritalstatus-user-select").empty();
let maritalstatus = $("#maritalstatus-user");
if ([0, 1, 8].includes(Number(e.value))) {
elem.hide();
maritalstatus.hide();
maritalstatus.find('input[name="maritalstatus-user"]').val('');
} else {
elem.show();
maritalstatus.show();
}
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"minimum-stability": "beta",
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.68",
"phpstan/phpstan": "^2.1"
"phpstan/phpstan": "^2.1",
"fakerphp/faker": "^1.24"
}
}
69 changes: 66 additions & 3 deletions composer.lock

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

1 change: 1 addition & 0 deletions locales/en.strings
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@
"your_email_address" = "Your Email address";
"your_page_address" = "Your address page";
"page_address" = "Address page";
"marital_status_user_id" = "User ID";
"current_email_address" = "Current email address";
"new_email_address" = "New email address";
"save_email_address" = "Save email address";
Expand Down
1 change: 1 addition & 0 deletions locales/ru.strings
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@
"your_email_address" = "Адрес Вашей электронной почты";
"your_page_address" = "Адрес Вашей страницы";
"page_address" = "Адрес страницы";
"marital_status_user_id" = "ID пользователя";
"current_email_address" = "Текущий адрес";
"new_email_address" = "Новый адрес";
"save_email_address" = "Сохранить адрес";
Expand Down
1 change: 1 addition & 0 deletions locales/ru_lat.strings
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@
"your_email_address" = "Adres Vašej elektronnoj počty";
"your_page_address" = "Adres Vašej stranicy";
"page_address" = "Adres stranicy";
"marital_status_user_id" = "ID polzovatelia";
"current_email_address" = "Tekusčij adres";
"new_email_address" = "Novyj adres";
"save_email_address" = "Sohranití adres";
Expand Down
1 change: 1 addition & 0 deletions locales/ru_old.strings
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@
"your_email_address" = "Адресъ Вашей электронной почты";
"your_page_address" = "Адресъ Вашей страницы";
"page_address" = "Адресъ страницы";
"marital_status_user_id" = "ID пользователя";
"current_email_address" = "Текущій адресъ";
"page_id" = "ID страницы";
"you_can_also" = "Вы вольны";
Expand Down
1 change: 1 addition & 0 deletions locales/ru_sov.strings
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@
"your_email_address" = "Адрес Вашего почтового ящика";
"your_page_address" = "Адрес Вашего досье";
"page_address" = "Адрес досье";
"marital_status_user_id" = "ID пользователя";
"current_email_address" = "Текущий адрес";
"page_id" = "Номер досье";
"you_can_also" = "Вы также можете";
Expand Down
1 change: 1 addition & 0 deletions locales/uk.strings
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@
"your_email_address" = "Адреса Вашої електронної пошти";
"your_page_address" = "Адреса Вашої сторінки";
"page_address" = "Адрес сторінки";
"marital_status_user_id" = "ID користувача";
"current_email_address" = "Поточна адреса";
"new_email_address" = "Нова адреса";
"save_email_address" = "Зберегти адресу";
Expand Down
1 change: 1 addition & 0 deletions openvkctl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ $application->add(new CLI\UpgradeCommand());
$application->add(new CLI\RebuildImagesCommand());
$application->add(new CLI\FetchToncoinTransactions());
$application->add(new CLI\CleanupPendingUploadsCommand());
$application->add(new CLI\GenerateUsersCommand());

$application->run();
Loading