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

codeigniter4 / shield / 13173436689

06 Feb 2025 06:54AM UTC coverage: 92.833%. First build
13173436689

Pull #1243

github

web-flow
Merge 0ec620e68 into 492365d93
Pull Request #1243: chore: update dependencies to support PHP 8.1 - 8.4

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

2785 of 3000 relevant lines covered (92.83%)

147.04 hits per line

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

93.75
/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              = $this->getUserEntity();
36✔
107
        $user->fill($this->request->getPost($allowedPostFields));
36✔
108

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

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

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

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

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

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

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

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

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

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

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

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

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

158
        return $provider;
60✔
159
    }
160

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

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

178
        return $rules->getRegistrationRules();
60✔
179
    }
180
}
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