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

daycry / auth / 16343465380

17 Jul 2025 11:07AM UTC coverage: 59.224% (-0.6%) from 59.854%
16343465380

push

github

web-flow
Merge pull request #23 from daycry/development

Improvements

57 of 292 new or added lines in 16 files covered. (19.52%)

6 existing lines in 4 files now uncovered.

1939 of 3274 relevant lines covered (59.22%)

22.81 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
NEW
38
        if ($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) {
×
NEW
39
            return $redirect;
×
40
        }
41

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

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

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

NEW
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
NEW
66
        if ($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) {
×
NEW
67
            return $redirect;
×
68
        }
69

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

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

NEW
82
        if (! $this->validateRequest($postData, $rules)) {
×
NEW
83
            return $this->handleValidationError(config('Auth')->registerRoute());
×
84
        }
85

86
        // Save the user
NEW
87
        $users             = $this->getUserProvider();
×
88
        $allowedPostFields = array_keys($rules);
×
89
        $user              = $this->getUserEntity();
×
90
        $user->fill($this->request->getPost($allowedPostFields));
×
91

92
        // Workaround for email only registration/login
93
        if ($user->username === null) {
×
94
            $user->username = null;
×
95
        }
96

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

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

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

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

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

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

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

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

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

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

144
        return $provider;
×
145
    }
146

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

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

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

© 2026 Coveralls, Inc