本文整理汇总了PHP中AppBundle\Entity\User::setToken方法的典型用法代码示例。如果您正苦于以下问题:PHP User::setToken方法的具体用法?PHP User::setToken怎么用?PHP User::setToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AppBundle\Entity\User
的用法示例。
在下文中一共展示了User::setToken方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: indexAction
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$user = new User();
$alert = null;
$form = $this->createFormBuilder($user)->add('username', TextType::class)->add('password', PasswordType::class)->add('login', SubmitType::class, array('label' => 'Login'))->add('create', SubmitType::class, array('label' => 'Create'))->getForm();
$form->handleRequest($request);
$session = new Session();
$cookie_token = $session->get('package_user_token');
if ($form->isSubmitted() && $form->isValid() || $cookie_token) {
// ... perform some action, such as saving the task to the database
$sm = $this->getDoctrine();
$user = $sm->getRepository('AppBundle:User')->findOneByUsername($user->getUsername());
if ($form->get('create')->isClicked() == 'create') {
if ($user == null) {
$user = new User();
$user->setUsername($form->get('username')->getData());
$passw = $form->get('password')->getData();
$user->setPassword($passw);
$date = new \DateTime();
$token = $passw + $date->getTimestamp();
$user->setToken($token);
$session->set('package_user_token', $token);
$sm->getManager()->persist($user);
$sm->getManager()->flush();
return $this->redirect($this->generateUrl('packages_index'));
} else {
$alert = "This username already exists.";
}
} else {
if ($form->get('login')->isClicked() == 'login' || $cookie_token) {
if ($cookie_token) {
$user = $sm->getRepository('AppBundle:User')->findOneByToken($cookie_token);
}
if ($user != null) {
$passwForm = md5($form->get('password')->getData());
if (strcmp($passwForm, $user->getPassword()) === 0) {
$token = $user->getToken();
$session->set('package_user_token', $token);
return $this->redirect($this->generateUrl('packages_index'));
} else {
$alert = "Password doesn't match.";
}
} else {
$alert = "This username doesn't exists.";
}
}
}
}
return $this->render('default/index.html.twig', array('form' => $form->createView(), 'alert' => $alert));
}
示例2: loginAction
/**
* Generate token
*
* @ApiDoc(
* resource = true,
* description = "Validate the couple user/password and if it exists, returns an auth token",
* output = "string",
* statusCodes = {
* 200 = "Returned when successful",
* 404 = "Returned when the user is not found"
* }
* )
*
*
* @param string $username the user login
* @param string $password the user password
*
* @return string
*
* @throws AccessDeniedException when no user found
*
* @Route("/login/username/{username}/password/{password}", name="login")
* @Method("GET")
*/
public function loginAction($username, $password)
{
if (!$username || !$password) {
throw $this->createNotFoundException('Required parameter(s) missing');
}
$password = md5($password);
if (!$this->canLogin($username, $password)) {
throw $this->createNotFoundException('User not found');
} else {
$em = $this->getDoctrine()->getManager();
$token = $this->generateToken($username, $password);
$user = $em->getRepository('AppBundle:User')->findOneByToken($token);
if (!$user) {
$user = new User();
$user->setToken($token['token'])->setIp($this->container->get('request')->getClientIp())->setExpiryDate($this->getExpiryDate());
$em->persist($user);
$em->flush();
}
$view = $this->view($token, 200)->setFormat('json');
}
return $view;
}
示例3: loginTwitter
/**
* Login user with twitterId
* @param $request
* @return bool
*/
public function loginTwitter($request)
{
$twitterId = $request->request->get('user_id');
$name = $request->request->get('name');
$photo = $request->request->get('picture_url');
$platform = $request->request->get('platform');
try {
// If user exists
if ($twitterId && ($user = $this->em->getRepository('AppBundle:User')->findOneByTwitterId($twitterId))) {
$user->setTwitterId($twitterId);
if (isset($name)) {
$user->setName($name);
}
$user->setPlatform($platform);
$user->setPhoto($photo);
$this->em->persist($user);
$this->em->flush();
// New twitter user
} else {
$user = new User();
$user->setPlatform($platform);
$user->setPhoto($photo);
$user->setTwitterId($twitterId);
if (isset($name)) {
$user->setName($name);
}
$token = $request->request->get('token');
if (!$token) {
$random = substr(md5(rand()), 0, 7);
$newToken = sha1('MIDGET' . $random . 'NINJA');
$user->setToken($newToken);
}
$this->em->persist($user);
$this->em->flush();
}
$user = $this->em->getRepository('AppBundle:User')->getOneByToken($user->getToken());
return $user;
} catch (\ExportException $e) {
return $e->getMessage();
}
}
示例4: loadUserByOAuthUserResponse
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$uri = $this->request->getUri();
$isFacebook = false;
$isGoogle = false;
$isLive = false;
$isTwitter = false;
if (strpos($uri, '/login_google') !== false) {
$isGoogle = true;
}
if (strpos($uri, '/login_facebook') !== false) {
$isFacebook = true;
}
if (strpos($uri, '/login_live') !== false) {
$isLive = true;
}
if (strpos($uri, '/login/check-twitter') !== false) {
$isTwitter = true;
}
if ($isGoogle === false && $isFacebook === false && $isLive === false && $isTwitter === false) {
throw new \Exception("Invalid social network login attempt");
}
$social = "";
if ($isGoogle) {
$social = "google";
}
if ($isFacebook) {
$social = "facebook";
}
if ($isLive) {
$social = "live";
}
if ($isTwitter) {
$social = "twitter";
}
//check to see if the user is logged in and if she is logged in with the same social network
$isLoggedInAlready = $this->session->has('user');
$isLoggedInAlreadyId = $this->session->get('user')['id'];
if ($isLoggedInAlready && $this->session->get('user')['social'] == $social) {
return $this->loadUserByUsername($isLoggedInAlreadyId);
}
$social_id = $response->getUsername();
$nickname = $response->getNickname();
$realName = $response->getRealName();
$email = $response->getEmail();
$avatar = $response->getProfilePicture();
//set data in session. upon logging out we just erase the whole array.
$sessionData = array();
$sessionData['social_id'] = $social_id;
$sessionData['nickname'] = $nickname;
$sessionData['realName'] = $realName;
$sessionData['email'] = $email;
$sessionData['avatar'] = $avatar;
$sessionData['social'] = $social;
$user = null;
if ($isLoggedInAlready) {
$user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneById($isLoggedInAlreadyId);
} else {
if ($isFacebook) {
$user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByFid($social_id);
} else {
if ($isGoogle) {
$user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByGid($social_id);
} else {
if ($isLive) {
$user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByLid($social_id);
} else {
if ($isTwitter) {
$user = $this->doctrine->getRepository('AppBundle\\Entity\\User')->findOneByTid($social_id);
}
}
}
}
}
if ($user == null) {
$user = new User();
$user->setSecret($response->getTokenSecret());
$user->setToken($response->getAccessToken());
//change these only the user hasn't been registered before.
}
if ($isFacebook) {
$user->setFid($social_id);
} else {
if ($isGoogle) {
$user->setGid($social_id);
} else {
if ($isLive) {
$user->setLid($social_id);
} else {
if ($isTwitter) {
$user->setTid($social_id);
}
}
}
}
//$user->setLastLogin(new \DateTime('now'));
//$user->setSocial($social);
// SET E-MAIL
//if all emails are empty, set the first one to this one.
//save all changes
//.........这里部分代码省略.........