/** * 检查用户名是否使用 * * @param string $username 被检测的用户名 */ public function getCheckUsernameAction($username) { try { $user = new UserController(); $this->responseJson(200, 'OK', array('name' => 'username', 'result' => $user->getUsernameExists($username)))->send(); } catch (ValidationException $e) { $this->responseJson(400, 'Bad Request', array('name' => 'username', 'result' => true))->send(); } }
/** * 单点登录用户 * * 该方法是提供给Passport前端登录的方法, 大多数API Client不会使用本方法 * * @param string $username 用户名 * @param string $password 密码 * @param string $lastLoginDate 最后登录时间 * @param string $lastLoginIp 最后登录IP * @param string $ticket 单点登录票据 * @throws ResourceException */ public function postLoginAction($username, $password, $lastLoginDate, $lastLoginIp, $ticket = '') { $user = new UserController(); $user->putAuthAction($username, $password, $lastLoginDate, $lastLoginIp); if (0 == $user->resource->active) { throw new ResourceException('Forbidden', 403); } if ($ticket == '') { $ticket = Hash::unique_string(); } (new OnlineController())->postUserAction($user->resource->UID, $ticket); $this->response(200, 'OK', array('UID' => $user->resource->UID, 'username' => $username, 'ticket' => $ticket)); }
/** * 用户注册动作 * * @param string $username * @param string $password * @param string $nickname * @param string $regDate * @param string $regIp */ public function postRegAction($username, $password, $nickname, $regDate, $regIp) { if (!$this->security->checkToken()) { $this->response(403, 'Forbidden', '未通过安全验证')->send(); return; } $this->db->begin(); try { //RSA解密密码 if (!$this->session->has('rsa_private_key')) { $this->response(403, 'Forbidden', '传输了未经加密的密码')->send(); } $rsa = new RSA(); $rsa->setPrivateKey($this->session->get('rsa_private_key')); $password = $rsa->decrypt($password); //注册用户 $user = new UserController(); $user->postUserAction($username, $password, 'sha1_salt_sha1', 1, $regDate, $regIp); $meta = new UserMetaController(); $meta->postUserMetaAction($user->resource->UID, 'nickname', $nickname); $this->db->commit(); $this->responseJson(200, '注册成功')->send(); //登陆用户 $sso = new SsoController(); $sso->postLoginAction($username, $password, $regDate, $regIp); $this->session->set('auth', array('id' => $sso->resource->UID, 'username' => $sso->resource->username, 'ticket' => $sso->resource->ticket, 'auto_signin' => false, 'created_at' => time())); } catch (ResourceException $e) { $this->db->rollback(); switch ($e->getCode()) { case 409: $this->responseJson($e->getCode(), '用户或昵称已存在')->send(); break; case 500: $this->responseJson($e->getCode(), '服务器错误')->send(); break; default: throw new RuntimeException('使用不存在的返回值'); break; } } }