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

znframework / fullpack-edition / 8716966645

17 Apr 2024 05:48AM UTC coverage: 98.608% (-1.0%) from 99.589%
8716966645

push

github

zntr
Updated test.yml

10341 of 10487 relevant lines covered (98.61%)

33.88 hits per line

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

81.19
/Internal/package-authentication/Register.php
1
<?php namespace ZN\Authentication;
2
/**
3
 * ZN PHP Web Framework
4
 * 
5
 * "Simplicity is the ultimate sophistication." ~ Da Vinci
6
 * 
7
 * @package ZN
8
 * @license MIT [http://opensource.org/licenses/MIT]
9
 * @author  Ozan UYKUN [ozan@znframework.com]
10
 */
11

12
use ZN\IS;
13
use ZN\Base;
14
use ZN\Singleton;
15
use ZN\Request\URL;
16
use ZN\Request\URI;
17
use ZN\Response\Redirect;
18

19
class Register extends UserExtends
20
{
21
    /**
22
     * Auto login.
23
     * 
24
     * @param mixed $autoLogin = true
25
     */
26
    public function autoLogin($autoLogin = true)
27
    {
28
        Properties::$parameters['autoLogin'] = $autoLogin;
2✔
29
    }
2✔
30

31
    /**
32
     * Do register.
33
     * 
34
     * @param string|array  $data                 = NULL
35
     * @param mixed         $autoLogin            = false
36
     * @param string        $activationReturnLink = ''
37
     * 
38
     * @return Bool
39
     */
40
    public function do($data = NULL, $autoLogin = false, string $activationReturnLink = '') : bool
41
    {
42
        $this->autoMatchColumns($data);
52✔
43
        $this->controlPropertiesParameters($data, $autoLogin, $activationReturnLink);
52✔
44
        
45
        if( ! empty($this->joinTables) )
52✔
46
        {
47
            $joinData = $data;
4✔
48
            $data     = $data[$this->tableName] ?? [$this->tableName];
4✔
49
        }
50

51
        if( ! isset($data[$this->usernameColumn]) || ! isset($data[$this->passwordColumn]) )
52✔
52
        {
53
            return $this->setErrorMessage('registerUsernameError');
2✔
54
        }
55

56
        $loginUsername   = $data[$this->usernameColumn];
50✔
57
        $loginPassword   = $data[$this->passwordColumn];
50✔
58
        $encodePassword  = $this->getEncryptionPassword($loginPassword);
50✔
59
        $usernameControl = $this->getTotalRowsByUsername($loginUsername);
50✔
60

61
        if( empty($usernameControl) )
50✔
62
        {
63
            $data[$this->passwordColumn] = $encodePassword;
48✔
64

65
            if( ! $this->registerUserInformations($data) )
48✔
66
            {
67
                return $this->setErrorMessage('registerUnknownError');
4✔
68
            }
69

70
            if( ! empty($this->joinTables) )
44✔
71
            {
72
                $this->insertJoinUserDataByUsername($loginUsername, $joinData);
4✔
73
            }
74

75
            $this->setSuccessMessage('registerSuccess');
44✔
76

77
            if( ! empty($this->activationColumn) )
44✔
78
            {
79
                if( ! IS::email($loginUsername) )
4✔
80
                {
81
                    $email = $data[$this->emailColumn] ?? NULL;
4✔
82
                }
83
                else
84
                {
85
                    $email = NULL;
×
86
                }
87

88
                if( empty($activationReturnLink) )
4✔
89
                {
90
                    throw new Exception\ActivationReturnLinkNotFoundException;
2✔
91
                }
92
                
93
                $this->sendActivationEmail($loginUsername, $encodePassword, $activationReturnLink, $email);
2✔
94
            }
95
            else
96
            {
97
                if( $autoLogin === true )
40✔
98
                {
99
                    $this->doLogin($loginUsername, $loginPassword);
8✔
100
                }
101
                elseif( is_string($autoLogin) )
32✔
102
                {
103
                    $this->redirectAutoLogin($autoLogin);
2✔
104
                }
105
            }
106

107
            return true;
40✔
108
        }
109
        else
110
        {
111
            return $this->setErrorMessage('registerError');
2✔
112
        }
113
    }
114

115
    /**
116
     * Activation complete.
117
     * 
118
     * 5.7.3[changed]
119
     * 
120
     * @param string|int          $userUriKey = 'user
121
     * @param string|int|callable $decryptor  = 'pass'
122
     * 
123
     * @return bool
124
     */
125
    public function activationComplete($userUriKey = 'user', $decryptor = 'pass') : bool
126
    {
127
        # Return link values.
128
        # 5.7.3[added]
129
        if( is_scalar($userUriKey) )
12✔
130
        {
131
            $user = URI::get($userUriKey); 
10✔
132
        }
133
        # invalid usage
134
        else
135
        {
136
            throw new Exception\InvalidArgumentException(NULL, '1.');
2✔
137
        }
138
        
139
        # 5.7.3[added]
140
        # scalar
141
        if( is_scalar($decryptor) )
10✔
142
        {
143
            $pass = URI::get($decryptor);
6✔
144
        }
145
        # callable
146
        elseif( is_callable($decryptor) )
4✔
147
        {
148
            $pass = $decryptor();
2✔
149
        }
150
        # invalid usage
151
        else
152
        {
153
            throw new Exception\InvalidArgumentException(NULL, '2.');
2✔
154
        }
155

156
        if( ! empty($user) && ! empty($pass) )
8✔
157
        {
158
            $row = $this->getUserDataByUsernameAndPassword($user, $pass);
×
159

160
            if( ! empty($row) )
×
161
            {
162
                $this->updateUserActivationColumnByUsername($user);
×
163

164
                return $this->setSuccessMessage('activationComplete');
×
165
            }
166
            else
167
            {
168
                return $this->setErrorMessage('activationCompleteError');
×
169
            }
170
        }
171
        else
172
        {
173
            return $this->setErrorMessage('activationCompleteError');
8✔
174
        }
175
    }
176

177
    /**
178
     * Resend activation e-mail.
179
     * 
180
     * @param string $username
181
     * @param string $returnLink
182
     * @param string $email = NULL
183
     * 
184
     * @return bool
185
     */
186
    public function resendActivationEmail(string $username, string $returnLink, string $email = NULL) : bool
187
    {
188
        if( empty($this->activationColumn) )
6✔
189
        {
190
            throw new Exception\ActivationColumnException;
2✔
191
        }
192

193
        $data = $this->isResendActivationEmailByValue($email ?? $username);
4✔
194
        
195
        if( empty($data) )
4✔
196
        {
197
            return $this->setErrorMessage('resendActivationError');
2✔
198
        }
199
        
200
        return $this->sendActivationEmail($username, $data->{$this->passwordColumn}, $returnLink, $email);
2✔
201
    }
202

203
    /**
204
     * Protected send activation email
205
     * 
206
     * @param string $user
207
     * @param string $pass 
208
     * @param string $activationReturnLink
209
     * @param string $email
210
     * 
211
     * @return bool
212
     */
213
    protected function sendActivationEmail($user, $pass, $activationReturnLink, $email)
214
    {
215
        $url = Base::suffix($activationReturnLink);
4✔
216

217
        if( ! IS::url($url) )
4✔
218
        {
219
            $url = URL::site($url);
4✔
220
        }
221

222
        # 5.7.3[added]
223
        # Sets activation email content
224
        $message = $this->getEmailTemplate
4✔
225
        ([
226
            'url'  => $url,
4✔
227
            'user' => $user,
4✔
228
            'pass' => $pass
4✔
229
        ], 'Activation');
4✔
230

231
        $user = $email ?? $user;
4✔
232

233
        if( ! IS::email($user) )
4✔
234
        {
235
            throw new Exception\InvalidEmailException(NULL, $user);
2✔
236
        }
237

238
        $emailclass = Singleton::class('ZN\Email\Sender');
2✔
239

240
        $emailclass->sender($this->senderMail, $this->senderName)
2✔
241
                   ->receiver($user, $user)
×
242
                   ->subject(Properties::$setEmailTemplateSubject ?? $this->getLang['activationProcess'])
×
243
                   ->content($message);
×
244

245
        Properties::$setEmailTemplateSubject = NULL;
×
246

247
        if( $emailclass->send() )
×
248
        {
249
            return $this->setSuccessMessage('activationEmail');
×
250
        }
251
        else
252
        {
253
            return $this->setErrorMessage('emailError'); // @codeCoverageIgnore
254
        }
255
    }
256

257
    /**
258
     * Protected is resend activation email by value
259
     */
260
    protected function isResendActivationEmailByValue($value)
261
    {
262
        return $this->dbClass->where($this->usernameColumn, $value, 'and')
4✔
263
                    ->where($this->activationColumn, '0')
4✔
264
                    ->get($this->tableName)
4✔
265
                    ->row();
4✔
266
    }
267

268
    /**
269
     * Protected get user data by username and password
270
     */
271
    protected function getUserDataByUsernameAndPassword($username, $password)
272
    {
273
        return $this->dbClass->where($this->usernameColumn, $username, 'and')
×
274
                             ->where($this->passwordColumn, $password)
×
275
                             ->get($this->tableName)
×
276
                             ->row();
×
277
    }
278

279
    /**
280
     * Protected update user activation column by username
281
     */
282
    protected function updateUserActivationColumnByUsername($username)
283
    {
284
        $this->dbClass->where($this->usernameColumn, $username)
×
285
                              ->update($this->tableName, [$this->activationColumn => '1']);
×
286
    }
×
287

288
    /**
289
     * Protected redirect auto login
290
     */
291
    protected function redirectAutoLogin($path)
292
    {
293
        new Redirect($path, 0, [], Properties::$redirectExit);
2✔
294
    }
2✔
295

296
    /**
297
     * Get total rows by username
298
     */
299
    protected function getTotalRowsByUsername($username)
300
    {
301
        return $this->getUserTableByUsername($username)->totalRows();
50✔
302
    }
303

304
    /**
305
     * Get join column by username
306
     */
307
    protected function getJoinColumnByUsername($username)
308
    {
309
        return $this->getUserTableByUsername($username)->row()->{$this->joinColumn};
4✔
310
    }
311

312
    /**
313
     * Protected insert join user data by username
314
     */
315
    protected function insertJoinUserDataByUsername($username, &$joinData)
316
    {
317
        $joinCol = $this->getJoinColumnByUsername($username);
4✔
318

319
        foreach( $this->joinTables as $table => $joinColumn )
4✔
320
        {
321
            $joinData[$table][$this->joinTables[$table]] = $joinCol;
4✔
322

323
            $this->dbClass->insert($table, $joinData[$table]);
4✔
324
        }
325
    }
4✔
326

327
    /**
328
     * Protected register user informations
329
     */
330
    protected function registerUserInformations($data)
331
    {
332
        return $this->dbClass->insert($this->tableName, $data);
48✔
333
    }
334

335
    /**
336
     * Protected do login
337
     */
338
    protected function doLogin($username, $password)
339
    {
340
        (new Login)->do($username, $password);
8✔
341
    }
8✔
342

343
    /**
344
     * Protected control properties parameters
345
     */
346
    protected function controlPropertiesParameters(&$data, &$autoLogin, &$activationReturnLink)
347
    {
348
        $data                   = Properties::$parameters['column']     ?? $data;
52✔
349
        $autoLogin              = Properties::$parameters['autoLogin']  ?? $autoLogin;
52✔
350
        $activationReturnLink   = Properties::$parameters['returnLink'] ?? $activationReturnLink;
52✔
351

352
        Properties::$parameters = [];
52✔
353
    }
52✔
354
}
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