|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CodeIgniter\Shield\Validation; |
| 6 | + |
| 7 | +use CodeIgniter\Shield\Authentication\Passwords; |
| 8 | +use CodeIgniter\Shield\Config\Auth; |
| 9 | + |
| 10 | +class ValidationRules |
| 11 | +{ |
| 12 | + private Auth $config; |
| 13 | + |
| 14 | + /** |
| 15 | + * Auth Table names |
| 16 | + */ |
| 17 | + private array $tables; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + /** @var Auth $authConfig */ |
| 22 | + $authConfig = config('Auth'); |
| 23 | + |
| 24 | + $this->config = $authConfig; |
| 25 | + $this->tables = $this->config->tables; |
| 26 | + } |
| 27 | + |
| 28 | + public function getRegistrationRules(): array |
| 29 | + { |
| 30 | + helper('setting'); |
| 31 | + |
| 32 | + $setting = setting('Validation.registration'); |
| 33 | + if ($setting !== null) { |
| 34 | + return $setting; |
| 35 | + } |
| 36 | + |
| 37 | + $usernameRules = $this->config->usernameValidationRules; |
| 38 | + $usernameRules['rules'][] = sprintf( |
| 39 | + 'is_unique[%s.username]', |
| 40 | + $this->tables['users'] |
| 41 | + ); |
| 42 | + |
| 43 | + $emailRules = $this->config->emailValidationRules; |
| 44 | + $emailRules['rules'][] = sprintf( |
| 45 | + 'is_unique[%s.secret]', |
| 46 | + $this->tables['identities'] |
| 47 | + ); |
| 48 | + |
| 49 | + $passwordRules = $this->getPasswordRules(); |
| 50 | + $passwordRules['rules'][] = 'strong_password[]'; |
| 51 | + |
| 52 | + return [ |
| 53 | + 'username' => $usernameRules, |
| 54 | + 'email' => $emailRules, |
| 55 | + 'password' => $passwordRules, |
| 56 | + 'password_confirm' => $this->getPasswordConfirmRules(), |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + public function getLoginRules(): array |
| 61 | + { |
| 62 | + helper('setting'); |
| 63 | + |
| 64 | + return setting('Validation.login') ?? [ |
| 65 | + // 'username' => $this->config->usernameValidationRules, |
| 66 | + 'email' => $this->config->emailValidationRules, |
| 67 | + 'password' => $this->getPasswordRules(), |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + public function getPasswordRules(): array |
| 72 | + { |
| 73 | + return [ |
| 74 | + 'label' => 'Auth.password', |
| 75 | + 'rules' => ['required', Passwords::getMaxLengthRule()], |
| 76 | + 'errors' => [ |
| 77 | + 'max_byte' => 'Auth.errorPasswordTooLongBytes', |
| 78 | + ], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + public function getPasswordConfirmRules(): array |
| 83 | + { |
| 84 | + return [ |
| 85 | + 'label' => 'Auth.passwordConfirm', |
| 86 | + 'rules' => 'required|matches[password]', |
| 87 | + ]; |
| 88 | + } |
| 89 | +} |
0 commit comments