|
21 | 21 | use phpMyFAQ\Controller\AbstractController; |
22 | 22 | use phpMyFAQ\Core\Exception; |
23 | 23 | use phpMyFAQ\Filter; |
| 24 | +use phpMyFAQ\Mail; |
24 | 25 | use phpMyFAQ\Session\Token; |
25 | 26 | use phpMyFAQ\Translation; |
26 | 27 | use phpMyFAQ\User\CurrentUser; |
| 28 | +use phpMyFAQ\Utils; |
27 | 29 | use Symfony\Component\HttpFoundation\JsonResponse; |
28 | 30 | use Symfony\Component\HttpFoundation\Request; |
29 | 31 | use Symfony\Component\HttpFoundation\Response; |
| 32 | +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
30 | 33 | use Symfony\Component\Routing\Attribute\Route; |
31 | 34 |
|
32 | 35 | class UserController extends AbstractController |
@@ -128,4 +131,80 @@ public function updateData(Request $request): JsonResponse |
128 | 131 |
|
129 | 132 | return $jsonResponse; |
130 | 133 | } |
| 134 | + |
| 135 | + /** |
| 136 | + * @throws Exception |
| 137 | + */ |
| 138 | + #[Route('api/user/data/update', methods: ['PUT'])] |
| 139 | + public function updatePassword(Request $request): JsonResponse |
| 140 | + { |
| 141 | + $jsonResponse = new JsonResponse(); |
| 142 | + $configuration = Configuration::getConfigurationInstance(); |
| 143 | + $user = CurrentUser::getCurrentUser($configuration); |
| 144 | + |
| 145 | + $data = json_decode($request->getContent()); |
| 146 | + |
| 147 | + $username = trim((string) Filter::filterVar($data->username, FILTER_SANITIZE_SPECIAL_CHARS)); |
| 148 | + $email = trim((string) Filter::filterVar($data->email, FILTER_VALIDATE_EMAIL)); |
| 149 | + if ($username !== '' && $username !== '0' && ($email !== '' && $email !== '0')) { |
| 150 | + $loginExist = $user->getUserByLogin($username); |
| 151 | + |
| 152 | + if ($loginExist && ($email == $user->getUserData('email'))) { |
| 153 | + try { |
| 154 | + $newPassword = $user->createPassword(); |
| 155 | + } catch (Exception $exception) { |
| 156 | + $jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST); |
| 157 | + $jsonResponse->setData(['error' => $exception->getMessage()]); |
| 158 | + return $jsonResponse; |
| 159 | + } |
| 160 | + |
| 161 | + try { |
| 162 | + $user->changePassword($newPassword); |
| 163 | + } catch (\Exception $exception) { |
| 164 | + $jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST); |
| 165 | + $jsonResponse->setData(['error' => $exception->getMessage()]); |
| 166 | + return $jsonResponse; |
| 167 | + } |
| 168 | + |
| 169 | + $text = Translation::get('lostpwd_text_1') . |
| 170 | + sprintf('<br><br>Username: %s', $username) . |
| 171 | + sprintf('<br>New Password: %s<br><br>', $newPassword) . |
| 172 | + Translation::get('lostpwd_text_2'); |
| 173 | + |
| 174 | + $mailer = new Mail($configuration); |
| 175 | + try { |
| 176 | + $mailer->addTo($email); |
| 177 | + } catch (Exception $exception) { |
| 178 | + $jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST); |
| 179 | + $jsonResponse->setData(['error' => $exception->getMessage()]); |
| 180 | + return $jsonResponse; |
| 181 | + } |
| 182 | + |
| 183 | + $mailer->subject = Utils::resolveMarkers('[%sitename%] Username / password request', $configuration); |
| 184 | + $mailer->message = $text; |
| 185 | + try { |
| 186 | + $result = $mailer->send(); |
| 187 | + } catch (Exception | TransportExceptionInterface $exception) { |
| 188 | + $jsonResponse->setStatusCode(Response::HTTP_BAD_REQUEST); |
| 189 | + $jsonResponse->setData(['error' => $exception->getMessage()]); |
| 190 | + return $jsonResponse; |
| 191 | + } |
| 192 | + |
| 193 | + unset($mailer); |
| 194 | + // Trust that the email has been sent |
| 195 | + $jsonResponse->setStatusCode(Response::HTTP_OK); |
| 196 | + $jsonResponse->setData(['success' => Translation::get('lostpwd_mail_okay')]); |
| 197 | + } else { |
| 198 | + $jsonResponse->setStatusCode(Response::HTTP_CONFLICT); |
| 199 | + $jsonResponse->setData(['error' => Translation::get('lostpwd_err_1')]); |
| 200 | + return $jsonResponse; |
| 201 | + } |
| 202 | + } else { |
| 203 | + $jsonResponse->setStatusCode(Response::HTTP_CONFLICT); |
| 204 | + $jsonResponse->setData(['error' => Translation::get('lostpwd_err_2')]); |
| 205 | + return $jsonResponse; |
| 206 | + } |
| 207 | + |
| 208 | + return $jsonResponse; |
| 209 | + } |
131 | 210 | } |
0 commit comments