NewUser.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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',
  31. 'password' => 'required|string|min:16|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->o_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[] = [
  67. $this->c->Router->link('AdminUsersNew'),
  68. __('Add user'),
  69. ];
  70. return $this;
  71. }
  72. /**
  73. * Подготавливает массив данных для формы
  74. */
  75. protected function formNew(array $data): array
  76. {
  77. return [
  78. 'action' => $this->c->Router->link('AdminUsersNew'),
  79. 'hidden' => [
  80. 'token' => $this->c->Csrf->create('AdminUsersNew'),
  81. ],
  82. 'sets' => [
  83. 'reg' => [
  84. 'legend' => __('Add user legend'),
  85. 'fields' => [
  86. 'username' => [
  87. 'autofocus' => true,
  88. 'type' => 'text',
  89. 'maxlength' => '25',
  90. 'value' => $data['username'] ?? null,
  91. 'caption' => __('Username'),
  92. 'help' => 'Login format',
  93. 'required' => true,
  94. 'pattern' => '^.{2,25}$',
  95. ],
  96. 'email' => [
  97. 'type' => 'text',
  98. 'maxlength' => '80',
  99. 'value' => $data['email'] ?? null,
  100. 'caption' => __('Email'),
  101. 'help' => 'Email info',
  102. 'required' => true,
  103. 'pattern' => '.+@.+',
  104. ],
  105. 'password' => [
  106. 'type' => 'text',
  107. 'caption' => __('Passphrase'),
  108. 'help' => 'Passphrase help',
  109. 'required' => true,
  110. 'pattern' => '^.{16,}$',
  111. ],
  112. ],
  113. ],
  114. ],
  115. 'btns' => [
  116. 'add' => [
  117. 'type' => 'submit',
  118. 'value' => __('Add'),
  119. ],
  120. ],
  121. ];
  122. }
  123. }