• 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.13
/src/Controllers/RegisterController.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\Events\Events;
17
use CodeIgniter\Exceptions\LogicException;
18
use CodeIgniter\HTTP\RedirectResponse;
19
use CodeIgniter\HTTP\ResponseInterface;
20
use Daycry\Auth\Entities\User;
21
use Daycry\Auth\Exceptions\ValidationException;
22
use Daycry\Auth\Models\UserModel;
23
use Daycry\Auth\Validation\ValidationRules;
24

25
/**
26
 * Class RegisterController
27
 *
28
 * Handles displaying registration form,
29
 * and handling actual registration flow.
30
 */
31
class RegisterController extends BaseAuthController
32
{
33
    /**
34
     * Displays the registration form.
35
     */
36
    public function registerView(): ResponseInterface
1 ✔
37
    {
38
        // Check if already logged in
39
        if (($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) instanceof RedirectResponse) {
1 ✔
40
            return $redirect;
×
41
        }
42

43
        // Check if registration is allowed
44
        if (! setting('Auth.allowRegistration')) {
1 ✔
45
            // Redirect to login: redirecting back to the register page would loop.
46
            return $this->handleFailure(
1 ✔
47
                config('Auth')->loginRoute(),
1 ✔
48
                lang('Auth.registerDisabled'),
1 ✔
49
                true,
1 ✔
50
                403,
1 ✔
51
            );
1 ✔
52
        }
53

54
        // Check if there's a pending post-auth action
55
        if ($this->hasPostAuthAction()) {
×
56
            return $this->redirectToAuthAction();
×
57
        }
58

59
        $content = $this->view(setting('Auth.views')['register']);
×
60

61
        return $this->response->setBody($content);
×
62
    }
63

64
    /**
65
     * Attempts to register the user.
66
     */
67
    public function registerAction(): ResponseInterface
3 ✔
68
    {
69
        // Check if already logged in
70
        if (($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) instanceof RedirectResponse) {
3 ✔
71
            return $redirect;
×
72
        }
73

74
        // Check if registration is allowed
75
        if (! setting('Auth.allowRegistration')) {
3 ✔
76
            return $this->handleFailure(
1 ✔
77
                config('Auth')->loginRoute(),
1 ✔
78
                lang('Auth.registerDisabled'),
1 ✔
79
                true,
1 ✔
80
                403,
1 ✔
81
            );
1 ✔
82
        }
83

84
        // Validate input
85
        $rules    = $this->getValidationRules();
2 ✔
86
        $postData = $this->authInput();
2 ✔
87

88
        // Fire pre-register event before validation
89
        Events::trigger('pre-register', $postData);
2 ✔
90

91
        if (! $this->validateRequest($postData, $rules)) {
2 ✔
92
            return $this->handleValidationError(config('Auth')->registerRoute());
×
93
        }
94

95
        // Save the user
96
        $users             = $this->getUserProvider();
2 ✔
97
        $allowedPostFields = array_keys($rules);
2 ✔
98
        $user              = $this->getUserEntity();
2 ✔
99
        $user->fill($this->authInput($allowedPostFields));
2 ✔
100

101
        // Workaround for email only registration/login
102
        $user->username ??= null;
2 ✔
103

104
        try {
105
            $users->save($user);
2 ✔
106
        } catch (ValidationException) {
×
NEW
107
            if ($this->expectsJsonResponse()) {
×
NEW
108
                return $this->authJsonResponse(['status' => 'error', 'errors' => $users->errors()], 422);
×
109
            }
110

NEW
111
            return redirect()->to(config('Auth')->registerRoute())->withInput()->with('errors', $users->errors());
×
112
        }
113

114
        // Get complete user object with ID
115
        $user = $users->findById($users->getInsertID());
2 ✔
116

117
        // Add to default group
118
        $users->addToDefaultGroup($user);
2 ✔
119

120
        Events::trigger('register', $user);
2 ✔
121

122
        // Start authentication process
123
        $authenticator = $this->getSessionAuthenticator();
2 ✔
124
        $authenticator->startLogin($user);
2 ✔
125

126
        // Check for post-registration action
127
        $hasAction = $authenticator->startUpAction('register', $user);
2 ✔
128
        if ($hasAction) {
2 ✔
129
            return $this->redirectToAuthAction();
1 ✔
130
        }
131

132
        // Set the user active and complete login
133
        $user->activate();
1 ✔
134
        $authenticator->completeLogin($user);
1 ✔
135

136
        if ($this->expectsJsonResponse()) {
1 ✔
137
            if (($pending = $this->pendingMfaResponse()) !== null) {
1 ✔
NEW
138
                return $pending;
×
139
            }
140

141
            return $this->authJsonResponse(['status' => 'success', 'authenticated' => true, 'redirect' => config('Auth')->registerRedirect(), 'message' => lang('Auth.registerSuccess')], 201);
1 ✔
142
        }
143

NEW
144
        return $this->handleSuccess(config('Auth')->registerRedirect(), lang('Auth.registerSuccess'));
×
145
    }
146

147
    /**
148
     * Returns the User provider
149
     */
150
    protected function getUserProvider(): UserModel
2 ✔
151
    {
152
        $provider = model(setting('Auth.userProvider'));
2 ✔
153

154
        // An explicit check, not assert(): assertions are disabled in production
155
        // (zend.assertions=-1), where a wrong provider would fatal later instead.
156
        if (! $provider instanceof UserModel) {
2 ✔
157
            throw new LogicException('Config Auth.userProvider must be ' . UserModel::class . ' or a subclass.');
×
158
        }
159

160
        return $provider;
2 ✔
161
    }
162

163
    /**
164
     * Returns the Entity class that should be used
165
     */
166
    protected function getUserEntity(): User
2 ✔
167
    {
168
        return new User();
2 ✔
169
    }
170

171
    /**
172
     * Returns the rules that should be used for validation.
173
     *
174
     * @return         array<string, array<string, list<string>|string>>
175
     * @phpstan-return array<string, array<string, string|list<string>>>
176
     */
177
    protected function getValidationRules(): array
2 ✔
178
    {
179
        $rules = new ValidationRules();
2 ✔
180

181
        return $rules->getRegistrationRules();
2 ✔
182
    }
183
}
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