/** * Register services related to Users * * @param Application $app */ public function register(Application $app) { $app['user.mapper'] = $app->share(function ($app) { return new UserMapper($app['db'], new UserEntity()); }); $app['user-token.mapper'] = $app->share(function ($app) { return new TokenMapper($app['db'], new TokenEntity()); }); $app['user-role-pivot.mapper'] = $app->share(function ($app) { return new UserRolePivotMapper($app['db']); }); $app['user.service'] = $app->share(function ($app) { $verifyRegistrationView = new VerifyRegistrationView($app['mustache']); $verifyRegistrationView->setUrlGenerator($app['url_generator']); $service = new UserService(); $service->setUserMapper($app['user.mapper'])->setTokenMapper($app['user-token.mapper'])->setEmailService($app['email.service'])->setVerifyRegistrationView($verifyRegistrationView); return $service; }); $app['role.service'] = $app->share(function ($app) { return new RoleService($app['user-role-pivot.mapper']); }); $app['user.validator'] = $app->share(function ($app) { return new UserValidator($app['validator']); }); $app['user-registration.validator'] = $app->share(function ($app) { return new UserRegistrationValidator($app['validator']); }); $app['user.controller'] = $app->share(function ($app) { $controller = new UserController(); $controller->setUserService($app['user.service'])->setUserValidator($app['user.validator'])->setUserRegistrationValidator($app['user-registration.validator']); return $controller; }); $app['user.converter'] = $app->share(function ($app) { return new UserConverter($app['user.mapper']); }); $app['verify-registration.controller'] = $app->share(function ($app) { $controller = new VerifyRegistrationController(); $controller->setUserService($app['user.service']); return $controller; }); $app['reset-password.controller'] = $app->share(function ($app) { $resetPasswordView = new ResetPasswordView($app['mustache']); $resetPasswordView->setUrlGenerator($app['url_generator']); return new ResetPasswordController($app['user.service'], $app['email.service'], $resetPasswordView); }); $app->match('/users', 'user.controller:rest')->method('HEAD|POST')->bind('user-collection'); $app->match('/user', 'user.controller:rest')->method('GET|PUT')->bind('user-entity-self'); $app->match('/users/{user}', 'user.controller:rest')->method('GET')->bind('user-entity')->assert('user', '\\d+')->convert('user', 'user.converter:getUser'); $app->match('/users/{id}/verify-registration', 'verify-registration.controller:rest')->method('POST')->bind('verify-registration'); $app->match('/user/reset-password', 'reset-password.controller:rest')->method('POST|PUT')->bind('reset-password'); $this->setFirewallsAndAccessRules($app); }
/** * Send reset password email * * @param Request $request * @return array */ public function post(Request $request) { // Validate user $email = Arr::get($this->getContentAsArray($request), 'email'); $user = $this->userService->findByEmail($email); if (!$user) { return $this->createNotFoundResponse(); } // If a token exists that won't expire in the next 5 minutes, send it $token = $this->userService->findTokenBy(['user_id' => $user->getId(), 'token_type_id' => TokenEntity::TYPE_RESET_PASSWORD, ['expires', '>', time() + 5 * 60]]); // Otherwise create a new token if (!$token) { $token = $this->userService->createUserToken(['user_id' => $user->getId(), 'token_type_id' => TokenEntity::TYPE_RESET_PASSWORD, 'expires' => strtotime('+1 day', time())]); } $this->resetPasswordView->setUserToken($token); $email = $this->emailService->createFromArray(['recipient_email' => $user->getEmail(), 'subject' => 'Reset Your Password', 'message' => (string) $this->resetPasswordView]); $this->emailService->enqueueSendEmailJob($email); return $this->create204Response(204, ''); }