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

daycry / auth / 38534809977

14 Oct 2026 08:23AM UTC coverage: 77.02% (+0.2%) from 76.835%
38534809977

push

github

web-flow
Merge pull request #74 from daycry/chore/deps-sync

chore(deps): sync dependencies, fix tooling for Rector 2.7 / phpstan-codeigniter 2, IP allow-list fail-closed

136 of 145 new or added lines in 19 files covered. (93.79%)

5490 of 7128 relevant lines covered (77.02%)

80.65 hits per line

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

0.0
/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\HTTP\RedirectResponse;
18
use CodeIgniter\HTTP\ResponseInterface;
19
use Daycry\Auth\Entities\User;
20
use Daycry\Auth\Exceptions\ValidationException;
21
use Daycry\Auth\Models\UserModel;
22
use Daycry\Auth\Validation\ValidationRules;
23

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

42
        // Check if registration is allowed
43
        if (! setting('Auth.allowRegistration')) {
×
44
            return $this->handleError(
×
45
                $this->request->getUri()->getPath(),
×
46
                lang('Auth.registerDisabled'),
×
47
            );
×
48
        }
49

50
        // Check if there's a pending post-auth action
51
        if ($this->hasPostAuthAction()) {
×
52
            return $this->redirectToAuthAction();
×
53
        }
54

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

57
        return $this->response->setBody($content);
×
58
    }
59

60
    /**
61
     * Attempts to register the user.
62
     */
63
    public function registerAction(): RedirectResponse
×
64
    {
65
        // Check if already logged in
66
        if (($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) instanceof RedirectResponse) {
×
67
            return $redirect;
×
68
        }
69

70
        // Check if registration is allowed
71
        if (! setting('Auth.allowRegistration')) {
×
72
            return $this->handleError(
×
73
                config('Auth')->registerRoute(),
×
74
                lang('Auth.registerDisabled'),
×
75
            );
×
76
        }
77

78
        // Validate input
79
        $rules    = $this->getValidationRules();
×
80
        $postData = $this->request->getPost();
×
81

82
        // Fire pre-register event before validation
83
        Events::trigger('pre-register', $postData);
×
84

85
        if (! $this->validateRequest($postData, $rules)) {
×
86
            return $this->handleValidationError(config('Auth')->registerRoute());
×
87
        }
88

89
        // Save the user
90
        $users             = $this->getUserProvider();
×
91
        $allowedPostFields = array_keys($rules);
×
92
        $user              = $this->getUserEntity();
×
93
        $user->fill($this->request->getPost($allowedPostFields));
×
94

95
        // Workaround for email only registration/login
NEW
96
        $user->username ??= null;
×
97

98
        try {
99
            $users->save($user);
×
100
        } catch (ValidationException) {
×
101
            return $this->handleError(
×
102
                config('Auth')->registerRoute(),
×
103
                'Registration failed',
×
104
                true,
×
105
            )->with('errors', $users->errors());
×
106
        }
107

108
        // Get complete user object with ID
109
        $user = $users->findById($users->getInsertID());
×
110

111
        // Add to default group
112
        $users->addToDefaultGroup($user);
×
113

114
        Events::trigger('register', $user);
×
115

116
        // Start authentication process
117
        $authenticator = $this->getSessionAuthenticator();
×
118
        $authenticator->startLogin($user);
×
119

120
        // Check for post-registration action
121
        $hasAction = $authenticator->startUpAction('register', $user);
×
122
        if ($hasAction) {
×
123
            return $this->redirectToAuthAction();
×
124
        }
125

126
        // Set the user active and complete login
127
        $user->activate();
×
128
        $authenticator->completeLogin($user);
×
129

130
        return $this->handleSuccess(
×
131
            config('Auth')->registerRedirect(),
×
132
            lang('Auth.registerSuccess'),
×
133
        );
×
134
    }
135

136
    /**
137
     * Returns the User provider
138
     */
139
    protected function getUserProvider(): UserModel
×
140
    {
141
        $provider = model(setting('Auth.userProvider'));
×
142

143
        assert($provider instanceof UserModel, 'Config Auth.userProvider is not a valid UserProvider.');
×
144

145
        return $provider;
×
146
    }
147

148
    /**
149
     * Returns the Entity class that should be used
150
     */
151
    protected function getUserEntity(): User
×
152
    {
153
        return new User();
×
154
    }
155

156
    /**
157
     * Returns the rules that should be used for validation.
158
     *
159
     * @return         array<string, array<string, list<string>|string>>
160
     * @phpstan-return array<string, array<string, string|list<string>>>
161
     */
162
    protected function getValidationRules(): array
×
163
    {
164
        $rules = new ValidationRules();
×
165

166
        return $rules->getRegistrationRules();
×
167
    }
168
}
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