• 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

46.43
/Internal/package-authentication/ForgotPassword.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
use ZN\Cryptography\Encode;
19

20
class ForgotPassword extends UserExtends
21
{
22
    /**
23
     * Email
24
     * 
25
     * @param string $email
26
     */
27
    public function email(string $email)
28
    {
29
        Properties::$parameters['email'] = $email;
2✔
30
    }
2✔
31

32
    /**
33
     * Verification
34
     * 
35
     * @param string $verification
36
     * 
37
     * @return ForgotPassword
38
     */
39
    public function verification(string $verification)
40
    {
41
        Properties::$parameters['verification'] = $verification;
4✔
42
    }
4✔
43

44
    /**
45
     * Password change process
46
     * 
47
     * @param string $changePassword
48
     * 
49
     * @return ForgotPassword
50
     */
51
    public function passwordChangeProcess(string $changePassword = 'before')
52
    {
53
        Properties::$parameters['changePassword'] = $changePassword;
4✔
54
    }
4✔
55

56
    /**
57
     * Forgot Password
58
     * 
59
     * @param string $email          = NULL
60
     * @param string $returnLinkPath = NULL
61
     * @param string $changePassword = 'before'
62
     * 
63
     * @return bool
64
     */
65
    public function do(string $email = NULL, string $returnLinkPath = NULL, string $changePassword = 'before') : bool
66
    {
67
        $this->controlPropertiesParameters($email, $verification, $returnLinkPath, $changePassword);
2✔
68

69
        $row = $this->getUserDataRowByEmail($email);
2✔
70

71
        if( isset($row->{$this->usernameColumn}) )
2✔
72
        {
73
            if( ! empty($this->verificationColumn) )
×
74
            {
75
                if( ! $verification || $verification !== $row->{$this->verificationColumn} )
×
76
                {
77
                    return $this->setErrorMessage('verificationOrEmailError');
×
78
                }
79
            }
80
           
81
            if( ! IS::url($returnLinkPath) )
×
82
            {
83
                $returnLinkPath = URL::site($returnLinkPath);
×
84
            }
85

86
            $newPassword    = $this->createRandomNewPassword();
×
87
            $encodePassword = $this->getEncryptionPassword($newPassword);
×
88

89
            $message = $this->getEmailTemplate
×
90
            ([
91
                'user' => $username = $row->{$this->usernameColumn},
×
92
                'pass' => $newPassword,
×
93
                'url'  => $this->encryptionReturnLink($returnLinkPath, $username, $encodePassword)
×
94
            ], 'ForgotPassword');
×
95

96
            if( $this->sendForgotPasswordEmail($email, $message) )
×
97
            {
98
                if( $changePassword === 'before' )
×
99
                {
100
                    if( $this->updateUserPassword($email, $encodePassword) )
×
101
                    {
102
                        return $this->setSuccessMessage('forgotPasswordSuccess');
×
103
                    }
104

105
                    return $this->setErrorMessage('updateError'); // @codeCoverageIgnore
106
                }
107

108
                return $this->setSuccessMessage('forgotPasswordSuccess');
×
109
            }
110
            else
111
            {
112
                return $this->setErrorMessage('emailError'); // @codeCoverageIgnore
113
            }
114
        }
115
        else
116
        {
117
            return $this->setErrorMessage('forgotPasswordError');
2✔
118
        }
119
    }
120

121
    /**
122
     * Password change complete
123
     */
124
    public function passwordChangeComplete(string $redirect = NULL)
125
    {
126
        $this->decryptionReturnLink($username, $password);
2✔
127

128
        if( $this->updateUserPasswordByUsernameAndPassword($username, $password) )
2✔
129
        {
130
            // @codeCoverageIgnoreStart
131
            if( $redirect !== NULL )
132
            {
133
                new Redirect($redirect);
134
            }
135

136
            return $this->setSuccessMessage('updateProcessSuccess');
137
            // @codeCoverageIgnoreEnd
138
        }
139

140
        return $this->setErrorMessage('forgotPasswordError');
2✔
141
    }
142

143
    /**
144
     * Protected encryption return link
145
     */
146
    protected function encryptionReturnLink($returnLinkPath, $username, $newEncodePassword)
147
    {
148
        return Base::suffix($returnLinkPath) . base64_encode($username) . '/' . base64_encode($newEncodePassword);
×
149
    }
150

151
    /**
152
     * Protected decryption return link
153
     */
154
    protected function decryptionReturnLink(&$username, &$password)
155
    {
156
        $username = base64_decode(URI::segment(-2));
2✔
157
        $password = base64_decode(URI::segment(-1));
2✔
158
    }
2✔
159

160
    /**
161
     * Protected get user data row by email
162
     */
163
    protected function getUserDataRowByEmail($email)
164
    {
165
        if( ! empty($this->emailColumn) )
2✔
166
        {
167
            $this->dbClass->where($this->emailColumn, $email); // @codeCoverageIgnore
168
        }
169
        else
170
        {
171
            $this->dbClass->where($this->usernameColumn, $email);
2✔
172
        }
173

174
        return $this->dbClass->get($this->tableName)->row();
2✔
175
    }
176

177
    /**
178
     * Protected create random new password
179
     */
180
    protected function createRandomNewPassword()
181
    {
182
        return Encode\RandomPassword::create(10);
×
183
    }
184

185
    /**
186
     * Protected send forgot password email
187
     */
188
    protected function sendForgotPasswordEmail($receiver, $message)
189
    {
190
        $return = Singleton::class('ZN\Email\Sender')
×
191
                           ->sender($this->senderMail, $this->senderName)
×
192
                           ->receiver($receiver, $receiver)
×
193
                           ->subject(Properties::$setEmailTemplateSubject ?? $this->getLang['newYourPassword'])
×
194
                           ->content($message)
×
195
                           ->send();
×
196

197
        Properties::$setEmailTemplateSubject = NULL;
×
198

199
        return $return;
×
200
    }
201

202
    /**
203
     * Protected update user password
204
     */
205
    protected function updateUserPassword($email, $password)
206
    {
207
        if( ! empty($this->emailColumn) )
×
208
        {
209
            $this->dbClass->where($this->emailColumn, $email, 'and'); // @codeCoverageIgnore
210
        }
211
        else
212
        {
213
            $this->dbClass->where($this->usernameColumn, $email, 'and');
×
214
        }
215

216
        return $this->dbClass->update($this->tableName, [$this->passwordColumn => $password]);
×
217
    }
218

219
    /**
220
     * Protected update user password with username
221
     */
222
    protected function updateUserPasswordByUsernameAndPassword($username, $newPassword)
223
    {
224
        return $this->dbClass->where($this->usernameColumn, $username)->update($this->tableName, [$this->passwordColumn => $newPassword]);
2✔
225
    }
226

227
    /**
228
     * Protected control properties parameters
229
     */
230
    protected function controlPropertiesParameters(&$email, &$verification, &$returnLinkPath, &$changePassword)
231
    {
232
        $email          = Properties::$parameters['email']          ?? $email;
2✔
233
        $verification   = Properties::$parameters['verification']   ?? NULL;
2✔
234
        $returnLinkPath = Properties::$parameters['returnLink']     ?? $returnLinkPath;
2✔
235
        $changePassword = Properties::$parameters['changePassword'] ?? $changePassword;
2✔
236

237
        Properties::$parameters = [];
2✔
238
    }
2✔
239
}
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