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

daycry / auth / 44317717916

18 Nov 2026 01:49PM UTC coverage: 82.894% (+4.4%) from 78.462%
44317717916

Pull #79

github

web-flow
Merge 7c781daeb into 55dbf61d2
Pull Request #79: feat: security fixes, extension points and auth completion

1214 of 1370 new or added lines in 67 files covered. (88.61%)

12 existing lines in 5 files now uncovered.

6663 of 8038 relevant lines covered (82.89%)

97.52 hits per line

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

78.26
/src/Controllers/ForcePasswordResetController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
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 Daycry\Auth\Controllers;
15

16
use CodeIgniter\HTTP\ResponseInterface;
17
use Daycry\Auth\Authentication\Passwords;
18
use Daycry\Auth\Models\UserModel;
19
use Daycry\Auth\Services\AuditLogger;
20
use Daycry\Auth\Validation\ValidationRules;
21

22
/**
23
 * Handles forced password resets for logged-in users
24
 * whose accounts have been flagged for a mandatory password change.
25
 *
26
 * The ForcePasswordResetFilter redirects users here when
27
 * $user->requiresPasswordReset() returns true.
28
 */
29
class ForcePasswordResetController extends BaseAuthController
30
{
31
    /**
32
     * Displays the force password reset form.
33
     */
34
    public function showView(): ResponseInterface
×
35
    {
36
        if (! auth()->loggedIn()) {
×
NEW
37
            return $this->expectsJsonResponse()
×
NEW
38
                ? $this->authJsonResponse(['status' => 'error', 'message' => lang('Auth.notEnoughPrivilege')], 401)
×
NEW
39
                : redirect()->route('login');
×
40
        }
41

42
        $content = $this->view(setting('Auth.views')['force-password-reset']);
×
43

44
        return $this->response->setBody($content);
×
45
    }
46

47
    /**
48
     * Handles the force password reset form submission.
49
     */
50
    public function resetAction(): ResponseInterface
4 ✔
51
    {
52
        if (! auth()->loggedIn()) {
4 ✔
NEW
53
            return $this->expectsJsonResponse()
×
NEW
54
                ? $this->authJsonResponse(['status' => 'error', 'message' => lang('Auth.notEnoughPrivilege')], 401)
×
NEW
55
                : redirect()->route('login');
×
56
        }
57

58
        $rules = $this->getValidationRules();
4 ✔
59

60
        $postData = $this->authInput();
4 ✔
61

62
        // Errors go back to the form itself. The `force_reset` redirect is
63
        // where the filter sends flagged users and defaults to '/'.
64
        $formUrl = $this->request->getPath();
4 ✔
65

66
        if (! $this->validateRequest($postData, $rules)) {
4 ✔
67
            return $this->handleValidationError($formUrl);
1 ✔
68
        }
69

70
        $user            = auth()->user();
3 ✔
71
        $currentPassword = $this->authInput('current_password');
3 ✔
72
        $newPassword     = $this->authInput('new_password');
3 ✔
73

74
        // Verify the current password
75
        /** @var Passwords $passwords */
76
        $passwords = service('passwords');
3 ✔
77

78
        if (! $passwords->verify($currentPassword, $user->getPasswordHash())) {
3 ✔
79
            return $this->handleFailure($formUrl, lang('Auth.invalidCurrentPassword'), true, 401);
1 ✔
80
        }
81

82
        // Enforce the same password policy as registration.
83
        $check = $passwords->check((string) $newPassword, $user);
2 ✔
84

85
        if (! $check->isOK()) {
2 ✔
86
            return $this->handleFailure($formUrl, (string) $check->reason(), true, 422);
1 ✔
87
        }
88

89
        // Capture the previous hash for password-history bookkeeping.
90
        $previousHash = $user->password_hash ?? null;
1 ✔
91

92
        // Update the password
93
        $user->setPassword($newPassword);
1 ✔
94

95
        /** @var UserModel $userModel */
96
        $userModel = model(UserModel::class);
1 ✔
97
        $userModel->save($user);
1 ✔
98

99
        service('passwordChangeRecorder')->record($user, $previousHash);
1 ✔
100

101
        // Clear the force reset flag
102
        $user->undoForcePasswordReset();
1 ✔
103

104
        service('auditLogger')->record(AuditLogger::EVENT_PASSWORD_CHANGED, (int) $user->id, [
1 ✔
105
            'forced' => true,
1 ✔
106
        ]);
1 ✔
107

108
        return $this->handleSuccess(
1 ✔
109
            config('Auth')->loginRedirect(),
1 ✔
110
            lang('Auth.forceResetSuccess'),
1 ✔
111
        );
1 ✔
112
    }
113

114
    /**
115
     * Returns the rules that should be used for validation.
116
     *
117
     * @return array<string, array<string, list<string>|string>|string>
118
     */
119
    protected function getValidationRules(): array
4 ✔
120
    {
121
        $passwordRules = (new ValidationRules())->getPasswordRules();
4 ✔
122

123
        return [
4 ✔
124
            'current_password'     => 'required',
4 ✔
125
            'new_password'         => $passwordRules,
4 ✔
126
            'new_password_confirm' => 'required|matches[new_password]',
4 ✔
127
        ];
4 ✔
128
    }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc