NewUser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages\Admin\Users;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\Admin\Users;
  13. use RuntimeException;
  14. use function \ForkBB\__;
  15. class NewUser extends Users
  16. {
  17. /**
  18. * Подготавливает данные для шаблона добавление пользователя
  19. */
  20. public function view(array $args, string $method): Page
  21. {
  22. $this->c->Lang->load('register');
  23. $data = [];
  24. if ('POST' === $method) {
  25. $v = $this->c->Validator->reset()
  26. ->addValidators([
  27. ])->addRules([
  28. 'token' => 'token:AdminUsersNew',
  29. 'email' => 'required|string:trim|email:noban,unique',
  30. 'username' => 'required|string:trim|username|noURL:1',
  31. 'password' => 'required|string|min:16|max:100000|password',
  32. ])->addAliases([
  33. 'email' => 'Email',
  34. 'username' => 'Username',
  35. 'password' => 'Passphrase',
  36. ])->addMessages([
  37. 'password.password' => 'Pass format',
  38. 'username.login' => 'Login format',
  39. ]);
  40. if ($v->validation($_POST)) {
  41. $user = $this->c->users->create();
  42. $user->username = $v->username;
  43. $user->password = \password_hash($v->password, PASSWORD_DEFAULT);
  44. $user->group_id = $this->c->config->i_default_user_group;
  45. $user->email = $v->email;
  46. $user->email_confirmed = 0;
  47. $user->activate_string = '';
  48. $user->u_mark_all_read = \time();
  49. $user->email_setting = $this->c->config->i_default_email_setting;
  50. $user->timezone = $this->c->config->o_default_timezone;
  51. $user->dst = $this->c->config->b_default_dst;
  52. $user->language = $this->c->config->o_default_lang;
  53. $user->style = $this->c->config->o_default_style;
  54. $user->registered = \time();
  55. $user->registration_ip = '127.0.0.1';
  56. $user->signature = '';
  57. $this->c->users->insert($user);
  58. return $this->c->Redirect->page('User', ['id' => $user->id, 'name' => $user->username])
  59. ->message('New user added redirect');
  60. }
  61. $this->fIswev = $v->getErrors();
  62. $data = $v->getData();
  63. }
  64. $this->nameTpl = 'admin/users';
  65. $this->formNew = $this->formNew($data);
  66. $this->aCrumbs[] = [$this->c->Router->link('AdminUsersNew'), 'Add user'];
  67. return $this;
  68. }
  69. /**
  70. * Подготавливает массив данных для формы
  71. */
  72. protected function formNew(array $data): array
  73. {
  74. return [
  75. 'action' => $this->c->Router->link('AdminUsersNew'),
  76. 'hidden' => [
  77. 'token' => $this->c->Csrf->create('AdminUsersNew'),
  78. ],
  79. 'sets' => [
  80. 'reg' => [
  81. 'legend' => 'Add user legend',
  82. 'fields' => [
  83. 'username' => [
  84. 'autofocus' => true,
  85. 'type' => 'text',
  86. 'maxlength' => '25',
  87. 'value' => $data['username'] ?? null,
  88. 'caption' => 'Username',
  89. 'help' => 'Login format',
  90. 'required' => true,
  91. 'pattern' => '^.{2,25}$',
  92. ],
  93. 'email' => [
  94. 'type' => 'text',
  95. 'maxlength' => '80',
  96. 'value' => $data['email'] ?? null,
  97. 'caption' => 'Email',
  98. 'help' => 'Email info',
  99. 'required' => true,
  100. 'pattern' => '.+@.+',
  101. ],
  102. 'password' => [
  103. 'type' => 'text',
  104. 'caption' => 'Passphrase',
  105. 'help' => 'Passphrase help',
  106. 'required' => true,
  107. 'pattern' => '^.{16,}$',
  108. ],
  109. ],
  110. ],
  111. ],
  112. 'btns' => [
  113. 'add' => [
  114. 'type' => 'submit',
  115. 'value' => __('Add'),
  116. ],
  117. ],
  118. ];
  119. }
  120. }