• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / shield / 16090699248

05 Jul 2025 06:02PM UTC coverage: 92.756%. Remained the same
16090699248

Pull #1269

github

web-flow
Merge a840c46e1 into 96825d627
Pull Request #1269: fix: code style

2830 of 3051 relevant lines covered (92.76%)

149.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.9
/src/Authentication/Passwords/ValidationRules.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter Shield.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Shield\Authentication\Passwords;
15

16
use CodeIgniter\HTTP\IncomingRequest;
17
use CodeIgniter\Shield\Authentication\Passwords;
18
use CodeIgniter\Shield\Entities\User;
19

20
/**
21
 * Class ValidationRules
22
 *
23
 * Provides auth-related validation rules for CodeIgniter 4.
24
 *
25
 * To use, add this class to Config/Validation.php, in the
26
 * $rulesets array.
27
 */
28
class ValidationRules
29
{
30
    /**
31
     * A validation helper method to check if the passed in
32
     * password will pass all of the validators currently defined.
33
     *
34
     * Handy for use in validation, but you will get a slightly
35
     * better security if this is done manually, since you can
36
     * personalize based on a specific user at that point.
37
     *
38
     * @param string $value  Field value
39
     * @param string $error1 Error that will be returned (for call without validation data array)
40
     * @param array  $data   Validation data array
41
     * @param string $error2 Error that will be returned (for call with validation data array)
42
     */
43
    public function strong_password(string $value, ?string &$error1 = null, array $data = [], ?string &$error2 = null): bool
44
    {
45
        /** @var Passwords $checker */
46
        $checker = service('passwords');
42✔
47

48
        if (function_exists('auth') && auth()->user()) {
42✔
49
            $user = auth()->user();
×
50
        } else {
51
            /** @phpstan-ignore-next-line */
52
            $user = $data === [] ? $this->buildUserFromRequest() : $this->buildUserFromData($data);
42✔
53
        }
54

55
        $result = $checker->check($value, $user);
42✔
56

57
        if (! $result->isOK()) {
42✔
58
            if ($data === []) {
×
59
                $error1 = $result->reason();
×
60
            } else {
61
                $error2 = $result->reason();
×
62
            }
63
        }
64

65
        return $result->isOK();
42✔
66
    }
67

68
    /**
69
     * Returns true if $str is $val or fewer bytes in length.
70
     */
71
    public function max_byte(?string $str, string $val): bool
72
    {
73
        return is_numeric($val) && $val >= strlen($str ?? '');
132✔
74
    }
75

76
    /**
77
     * Builds a new user instance from the global request.
78
     *
79
     * @deprecated This will be removed soon.
80
     *
81
     * @see https://github.com/codeigniter4/shield/pull/747#discussion_r1198778666
82
     */
83
    protected function buildUserFromRequest(): User
84
    {
85
        $fields = $this->prepareValidFields();
×
86

87
        /** @var IncomingRequest $request */
88
        $request = service('request');
×
89

90
        $data = $request->getPost($fields);
×
91

92
        return new User($data);
×
93
    }
94

95
    /**
96
     * Builds a new user instance from assigned data..
97
     *
98
     * @param array $data Assigned data
99
     */
100
    protected function buildUserFromData(array $data = []): User
101
    {
102
        $fields = $this->prepareValidFields();
42✔
103

104
        $data = array_intersect_key($data, array_fill_keys($fields, null));
42✔
105

106
        return new User($data);
42✔
107
    }
108

109
    /**
110
     * Prepare valid user fields
111
     */
112
    protected function prepareValidFields(): array
113
    {
114
        $config = config('Auth');
42✔
115
        $fields = array_merge($config->validFields, $config->personalFields, ['email', 'password']);
42✔
116

117
        return array_unique($fields);
42✔
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc