身份验证Symfony无法正常工作

时间:2017-08-09 12:07:20

标签: symfony authentication login

我遇到symfony身份验证问题,重点是,当我创建用户时,我发送带有令牌的电子邮件通知,然后当用户点击链接时,他的帐户被激活并且我做了一些验证像令牌是正确的,用户存在帐户是活动的等,然后是我遇到问题,一切都正确,直到用户点击他的链接我做每一次验证,但当我自动登录用户并重定向用户到主页浏览器说重定向很多return $this->redirect($this->generateUrl('homepage')); 好吧,当我遇到问题时,这里是我在activateUserAction中的securityController上的代码,当我遇到问题时,我希望有人可以帮忙

用户实体

<?php

   namespace AppBundle\Entity;

   use Doctrine\ORM\Mapping as ORM;
   use Symfony\Component\Security\Core\User\AdvancedUserInterface;
   use Symfony\Component\Validator\Constraints as Assert;

/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface,\Serializable
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255, unique=true)
 * @Assert\Email(
 *     message = "The email '{{ value }}' is not a valid email.",
 *     checkMX = true
 * )
 *
 */
private $email;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;

/**
 * @Assert\NotBlank(groups={"register"})
 * @Assert\Length(min = 6)
 */
private $passwordClear;

/**
 * @var bool
 *
 * @ORM\Column(name="active", type="boolean")
 */
private $active;

/**
 * @ORM\Column(type="json_array")
 */
private $roles = array();

/**
 * @ORM\Column(name="expired", type="boolean")
 */

private $expired;

/**
 * @ORM\OneToOne(targetEntity="AppBundle\Entity\Person",cascade={"persist"})
 */
private $person;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set email
 *
 * @param string $email
 *
 * @return User
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set password
 *
 * @param string $password
 *
 * @return User
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string
 */
public function getPassword()
{
    return $this->password;
}

/**
 * get PasswordClear
 */

public function getPasswordClear(){
    return $this->passwordClear;
}

/**
 * set passwordClear
 */
public function setPasswordClear($password){
        $this->passwordClear=$password;
}

/**
 * Set active
 *
 * @param boolean $active
 *
 * @return User
 */
public function setActive($active=0)
{
    $this->active = $active;

    return $this;
}

/**
 * Get active
 *
 * @return bool
 */
public function getActive()
{
    return $this->active;
}

/**
 * Set roles
 *
 * @param array $roles
 *
 * @return User
 */
public function setRoles($roles)
{
    $this->roles = $roles;

    return $this;
}

/**
 * Get roles
 *
 * @return array
 */
public function getRoles()
{
    return array($this->roles);
}

/**
 * Set person
 *
 * @param \AppBundle\Entity\Person $person
 *
 * @return User
 */
public function setPerson(\AppBundle\Entity\Person $person = null)
{
    $this->person = $person;

    return $this;
}

/**
 * Get person
 *
 * @return \AppBundle\Entity\Person
 */
public function getPerson()
{
    return $this->person;
}

public function getUsername()
{
    return $this->getEmail();
}
public function eraseCredentials()
{
    $this->passwordClear = null;
}
public function getSalt()
{

    return null;
}

public function isAccountNonExpired(){
    return true;
}

public function isAccountNonLocked(){
    return true;
}

public function isCredentialsNonExpired(){
    return true;
}

public function isEnabled(){
    return $this->active;
}


/**
 * Set expired
 *
 * @param boolean $expired
 *
 * @return User
 */
public function setExpired($expired)
{
    $this->expired = $expired;

    return $this;
}

/**
 * Get expired
 *
 * @return boolean
 */
public function getExpired()
{
    return $this->expired;
}

/** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->email,
        $this->password,
    ));
}

/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->email,
        $this->password,
        ) = unserialize($serialized);
}

}

实体人

<?php

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 use Symfony\Component\HttpKernel\Kernel;

/**
* Person
*
* @ORM\Table(name="person")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PersonRepository")
*/
class Person
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="last_name", type="string", length=255)
 */
private $lastName;

/**
 * @var string
 * @ORM\Column(name="desccription",type="string",   length=255,nullable=true)
 */

private $description;

/**
 * @var string
 *
 * @ORM\Column(name="token", type="string", length=255)
 */
private $token;
/**
 * @var string
 *
 * @ORM\Column(name="url_validate", type="string", length=255)
 */
private $urlValidate;
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return Person
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Set lastName
 *
 * @param string $lastName
 *
 * @return Person
 */
public function setLastName($lastName)
{
    $this->lastName = $lastName;

    return $this;
}

/**
 * Get lastName
 *
 * @return string
 */
public function getLastName()
{
    return $this->lastName;
}

/**
 * Set token
 *
 * @param string $token
 *
 * @return Person
 */
public function setToken($token)
{
    $this->token = $token;

    return $this;
}

/**
 * Get token
 *
 * @return string
 */
public function getToken()
{
    return $this->token;
}

/**
 * Set urlValidate
 *
 * @param string $urlValidate
 *
 * @return Person
 */
public function setUrlValidate($urlValidate)
{
    $this->urlValidate = $urlValidate;

    return $this;
}

/**
 * Get urlValidate
 *
 * @return string
 */
public function getUrlValidate()
{
    return $this->urlValidate;
}
}

我的securityController

<?php

 namespace AppBundle\Controller;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Security
 \Core\Authentication\Token  \UsernamePasswordToken;
  use Symfony\Component\Security
  \Http\Firewall\ListenerInterface;
 use Symfony\Component\Security
\Core\Authentication\AuthenticationManagerInterface;
 use Symfony\Component\Security
\Core\Authentication\Token\Storage\TokenStorageInterface;
use AppBundle\Form\RegisterType;
use AppBundle\Entity\Register;

 class SecurityController extends Controller
 {
  /**
   * @Route("/login", name="user_login")
  */
 public function loginAction()
 {
  if($this->getUser()){
        return $this->redirectToRoute('homepage');
    }

    $authUtils = $this->get('security.authentication_utils');
    return $this->render('front/homepage/_singin.html.twig', array(
        'last_username' => $authUtils->getLastUsername(),
        'error' => $authUtils->getLastAuthenticationError(),
    ));
}
/**
 * @Route("/login_check", name="user_login_check")
 */
public function loginCheckAction()
{

}
/**
 * @Route("/logout", name="user_logout")
 */
public function logoutAction()
{
}

 /**
 *
 * @Route("/token/{token}", name="activate")
 */
public function activateUserAction($token){
    $em = $this->getDoctrine()->getManager();
    $person=$em->getRepository('AppBundle:Person')->findByToken($token);



    if($person){
    $user=$em->getRepository('AppBundle:User')->findByPerson($person);


        $user->setActive(1);
        $person->setToken("");
        $person->setActivatedDate(new \DateTime());


    $em->persist($person);
        $em->flush();
        $em->persist($user);
        $em->flush();




        $token= new UsernamePasswordToken(
            $user,
            $user->getPassword(),
            'user',
            $user->getRoles()
        );




        $this->get('security.token_storage')->setToken($token);

        return $this->redirect($this->generateUrl('homepage'));


    }else{
        return $this->redirect($this->generateUrl('caducada'));
    }
}

0 个答案:

没有答案