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

codeigniter4 / shield / 13240274040

10 Feb 2025 11:56AM UTC coverage: 92.769% (-0.05%) from 92.822%
13240274040

push

github

web-flow
chore: update dependencies to support PHP 8.1 - 8.4 (#1243)

* move to PHP 8.1 and PHPUnit 10

* update workflows

* cs fix

* phpstan - update baseline

* fix rector

* update phpstan-strict-rules to v2

* update supported PHP version in the docs

83 of 96 new or added lines in 23 files covered. (86.46%)

3 existing lines in 1 file now uncovered.

2784 of 3001 relevant lines covered (92.77%)

146.99 hits per line

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

89.58
/src/Controllers/RegisterController.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\Controllers;
15

16
use App\Controllers\BaseController;
17
use CodeIgniter\Events\Events;
18
use CodeIgniter\HTTP\RedirectResponse;
19
use CodeIgniter\HTTP\RequestInterface;
20
use CodeIgniter\HTTP\ResponseInterface;
21
use CodeIgniter\Shield\Authentication\Authenticators\Session;
22
use CodeIgniter\Shield\Entities\User;
23
use CodeIgniter\Shield\Exceptions\ValidationException;
24
use CodeIgniter\Shield\Models\UserModel;
25
use CodeIgniter\Shield\Traits\Viewable;
26
use CodeIgniter\Shield\Validation\ValidationRules;
27
use Psr\Log\LoggerInterface;
28

29
/**
30
 * Class RegisterController
31
 *
32
 * Handles displaying registration form,
33
 * and handling actual registration flow.
34
 */
35
class RegisterController extends BaseController
36
{
37
    use Viewable;
38

39
    public function initController(
40
        RequestInterface $request,
41
        ResponseInterface $response,
42
        LoggerInterface $logger,
43
    ): void {
44
        parent::initController(
90✔
45
            $request,
90✔
46
            $response,
90✔
47
            $logger,
90✔
48
        );
90✔
49
    }
50

51
    /**
52
     * Displays the registration form.
53
     *
54
     * @return RedirectResponse|string
55
     */
56
    public function registerView()
57
    {
58
        if (auth()->loggedIn()) {
24✔
59
            return redirect()->to(config('Auth')->registerRedirect());
6✔
60
        }
61

62
        // Check if registration is allowed
63
        if (! setting('Auth.allowRegistration')) {
18✔
64
            return redirect()->back()->withInput()
6✔
65
                ->with('error', lang('Auth.registerDisabled'));
6✔
66
        }
67

68
        /** @var Session $authenticator */
69
        $authenticator = auth('session')->getAuthenticator();
12✔
70

71
        // If an action has been defined, start it up.
72
        if ($authenticator->hasAction()) {
12✔
73
            return redirect()->route('auth-action-show');
6✔
74
        }
75

76
        return $this->view(setting('Auth.views')['register']);
6✔
77
    }
78

79
    /**
80
     * Attempts to register the user.
81
     */
82
    public function registerAction(): RedirectResponse
83
    {
84
        if (auth()->loggedIn()) {
72✔
85
            return redirect()->to(config('Auth')->registerRedirect());
6✔
86
        }
87

88
        // Check if registration is allowed
89
        if (! setting('Auth.allowRegistration')) {
66✔
90
            return redirect()->back()->withInput()
6✔
91
                ->with('error', lang('Auth.registerDisabled'));
6✔
92
        }
93

94
        $users = $this->getUserProvider();
60✔
95

96
        // Validate here first, since some things,
97
        // like the password, can only be validated properly here.
98
        $rules = $this->getValidationRules();
60✔
99

100
        if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) {
60✔
101
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
24✔
102
        }
103

104
        // Save the user
105
        $allowedPostFields = array_keys($rules);
36✔
106
        $user              = $users->createNewUser($this->request->getPost($allowedPostFields));
36✔
107

108
        // Workaround for email only registration/login
109
        if ($user->username === null) {
36✔
UNCOV
110
            $user->username = null;
×
111
        }
112

113
        try {
114
            $users->save($user);
36✔
NEW
115
        } catch (ValidationException) {
×
116
            return redirect()->back()->withInput()->with('errors', $users->errors());
×
117
        }
118

119
        // To get the complete user object with ID, we need to get from the database
120
        $user = $users->findById($users->getInsertID());
36✔
121

122
        // Add to default group
123
        $users->addToDefaultGroup($user);
36✔
124

125
        Events::trigger('register', $user);
36✔
126

127
        /** @var Session $authenticator */
128
        $authenticator = auth('session')->getAuthenticator();
36✔
129

130
        $authenticator->startLogin($user);
36✔
131

132
        // If an action has been defined for register, start it up.
133
        $hasAction = $authenticator->startUpAction('register', $user);
36✔
134
        if ($hasAction) {
36✔
135
            return redirect()->route('auth-action-show');
24✔
136
        }
137

138
        // Set the user active
139
        $user->activate();
12✔
140

141
        $authenticator->completeLogin($user);
12✔
142

143
        // Success!
144
        return redirect()->to(config('Auth')->registerRedirect())
12✔
145
            ->with('message', lang('Auth.registerSuccess'));
12✔
146
    }
147

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

155
        assert($provider instanceof UserModel, 'Config Auth.userProvider is not a valid UserProvider.');
156

157
        return $provider;
60✔
158
    }
159

160
    /**
161
     * Returns the Entity class that should be used
162
     *
163
     * @deprecated 1.2.0 No longer used.
164
     */
165
    protected function getUserEntity(): User
166
    {
UNCOV
167
        $userProvider = $this->getUserProvider();
×
168

UNCOV
169
        return $userProvider->createNewUser();
×
170
    }
171

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

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