public function __invoke() { /** @var \Tachigo\User\Aware\Component\UserAwareHook $hook */ $hook = $this->getHook(); if ($hook instanceof UserLoginFieldsHook) { // 如果是用户登录字段配置的钩子 $hook_results = $hook->getResults(); $results = array('fields' => array('username' => array('element' => 'input', 'type' => 'text', 'placeholder' => '请输入用户名', 'label' => '用户名'), 'password' => array('element' => 'input', 'type' => 'password', 'placeholder' => '请输入密码', 'label' => '密码'), 'remember' => array('element' => 'input', 'type' => 'checkbox', 'label' => '下次自动登录'))); $hook->setResults(array_merge($hook_results, $results)); } elseif ($hook instanceof UserLoginHook) { $request = $this->getRequest(); $session = $this->getSession(); $posts = $request->request; try { $username = $posts->get('username'); $password = $posts->get('password'); $remember = $posts->get('remember'); if (!$username) { throw new \Exception('请输入用户名'); } if (!$password) { throw new \Exception('请输入密码'); } $user_auth = UserAuthModel::getAuthByUsername($username); if (!$user_auth) { throw new \Exception('用户不存在'); } if ($user_auth->password != md5($password)) { throw new \Exception('密码错误'); } $user_auth->loginTimestamp = time(); // 保存 $user_auth = UserAuthModel::saveAuth($user_auth); $session['auth'] = $user_auth; $session['profile'] = $this->getUserProfile($user_auth->id); // 下次自动登录 if ($remember) { } } catch (\Exception $e) { throw $e; } } }
public function __invoke() { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $session = $this->getSession(); $posts = $request->request; try { $username = $posts->get('username'); $password = $posts->get('password'); $remember = $posts->get('remember'); if (!$username) { throw new \Exception('请输入用户名'); } if (!$password) { throw new \Exception('请输入密码'); } $user_auth = UserAuthModel::getAuthByUsername($username); if (!$user_auth) { throw new \Exception('用户不存在'); } if ($user_auth->password != md5($password)) { throw new \Exception('密码错误'); } $user_auth->loginTimestamp = time(); // 保存 $user_auth = UserAuthModel::saveAuth($user_auth); $session['auth'] = $user_auth; $session['profile'] = $this->getUserProfile($user_auth->id); // 下次自动登录 if ($remember) { } // 登录成功 $session->addFlash('success', '欢迎回来'); return new RedirectResponse($this->generateUrl('user_homepage')); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('user_login')); } } return $this->render('passport/login.html.twig'); }
private function handlePassword($auth_id) { $user_auth = UserAuthModel::getAuth($auth_id); if (!$user_auth) { throw new \Exception('用户不存在'); } $request = $this->getRequest(); $posts = $request->request; $password = $posts->get('password'); $confirm_password = $posts->get('confirm_password'); // 密码 if (!$password) { throw new \Exception('密码不能为空'); } if (strlen($password) < 6) { throw new \Exception('密码至少6位字符'); } if (!$confirm_password) { throw new \Exception('确认密码不能为空'); } if ($password != $confirm_password) { throw new \Exception('密码与确认密码不一致'); } $user_auth->password = md5($password); // 保存 UserAuthModel::saveAuth($user_auth); }