login - Symfony using AdvancedUserInterface -
i trying implement login system symfony2. implementing advanceduserinterface user entity, , keep getting following error when try log in. appreciated.
error:
$user must instanceof userinterface, object implementing __tostring method, or primitive string.
user entity:
<?php namespace flc\userbundle\entity; use doctrine\orm\mapping orm; use symfony\component\security\core\user\advanceduserinterface; use serializable; /** * user * * @orm\table(name="users") * @orm\entity(repositoryclass="flc\userbundle\entity\userrepository") */ class user implements advanceduserinterface, serializable { /** * @var integer * * @orm\column(name="userid", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $userid; /** * @var string * * @orm\column(name="useremail", type="string", length=50) */ private $useremail; /** * @var string * * @orm\column(name="username", type="string", length=20) */ private $username; /** * @var string * * @orm\column(name="userpassword", type="string", length=60) */ private $userpassword; /** * @var string * * @orm\column(name="usersalt", type="string", length=60, nullable=true) */ private $usersalt; /** * @var string * * @orm\column(name="userfirstname", type="string", length=40) */ private $userfirstname; /** * @var string * * @orm\column(name="userlastname", type="string", length=40) */ private $userlastname; /** * @var string * * @orm\column(name="userphone", type="string", length=10) */ private $userphone; /** * @var \datetime * * @orm\column(name="created", type="datetime") */ private $created; /** * @var boolean * * @orm\column(name="isactive", type="boolean") */ private $isactive; /** * @var array * * @orm\column(name="roles", type="json_array", nullable=true) */ private $roles; ######################### ## getter - setter functions ## ######################### /** * userid * * @return integer */ public function getuserid() { return $this->userid; } /** * set useremail * * @param string $useremail * @return user */ public function setuseremail($useremail) { $this->useremail = $useremail; return $this; } /** * useremail * * @return string */ public function getuseremail() { return $this->useremail; } /** * set username * * @param string $username * @return user */ public function setusername($username) { $this->username = $username; return $this; } /** * username * * @return string */ public function getusername() { return $this->username; } /** * set userpassword * * @param string $userpassword * @return user */ public function setuserpassword($userpassword) { $this->userpassword = $userpassword; return $this; } /** * userpassword * * @return string */ public function getuserpassword() { return $this->userpassword; } /** * set usersalt * * @param string $usersalt * @return user */ public function setusersalt($usersalt) { $this->usersalt = $usersalt; return $this; } /** * usersalt * * @return string */ public function getusersalt() { return $this->usersalt; } /** * set userfirstname * * @param string $userfirstname * @return user */ public function setuserfirstname($userfirstname) { $this->userfirstname = $userfirstname; return $this; } /** * userfirstname * * @return string */ public function getuserfirstname() { return $this->userfirstname; } /** * set userlastname * * @param string $userlastname * @return user */ public function setuserlastname($userlastname) { $this->userlastname = $userlastname; return $this; } /** * userlastname * * @return string */ public function getuserlastname() { return $this->userlastname; } /** * set userphone * * @param string $userphone * @return user */ public function setuserphone($userphone) { $this->userphone = $userphone; return $this; } /** * userphone * * @return string */ public function getuserphone() { return $this->userphone; } /** * set created * * @param \datetime $created * @return user */ public function setcreated($created) { $this->created = $created; return $this; } /** * created * * @return \datetime */ public function getcreated() { return $this->created; } /** * set isactive * * @param boolean $isactive * @return user */ public function setisactive($isactive) { $this->isactive = $isactive; return $this; } /** * isactive * * @return boolean */ public function getisactive() { return $this->isactive; } /** * set roles * * @param boolean $roles * @return user */ public function setroles(array $roles) { $this->roles = $roles; return $this; } ############################################# ## user interface functions implementation ## ############################################# public function getroles() { #return $this->roles; $roles = $this->roles; $roles[] = 'role_user'; return array_unique($roles); } public function erasecredentials() { //will implemented } public function getsalt() { return $this->getusersalt(); } public function getpassword() { return $this->getuserpassword(); } public function isaccountnonexpired() { return true; } public function isaccountnonlocked() { return true; } public function iscredentialsnonexpired() { return true; } public function isenabled() { return $this->isactive; } ##################################################### ## serializable interface functions implementation ## ##################################################### public function serialize() { return serialize(array( $this->userid, $this->username, $this->userpassword )); } public function unserialize($serialized) { list( $this->userid, $this->username, $this->userpassword) = unserialize($serialized); } }
user repository:
<?php namespace flc\userbundle\entity; use doctrine\orm\entityrepository; use symfony\component\security\core\user\userproviderinterface; use symfony\component\security\core\exception\unsupporteduserexception; use symfony\component\security\core\user\userinterface; use symfony\component\security\core\exception\usernamenotfoundexception; /** * userrepository * * class generated doctrine orm. add own custom * repository methods below. */ class userrepository extends entityrepository implements userproviderinterface { public function findonebyusernameoremail($username) { return $this->createquerybuilder('u') ->andwhere('u.username = :username or u.useremail = :email') ->setparameter('username', $username) ->setparameter('email', $username) ->getquery() ->getoneornullresult(); } public function loaduserbyusername($username) { //$user = $this->findonebyusernameoremail($username); $user = $this->findoneby(array("username"=>$username)); if (!$user) { throw new usernamenotfoundexception('no user found ' . $username); } return $user; } public function refreshuser(userinterface $user) { $class = get_class($user); if (!$this->supportsclass($class)) { throw new unsupporteduserexception(sprintf( 'instances of "%s" not supported.', $class )); } if (!$refresheduser = $this->find($user->getuserid())) { throw new usernamenotfoundexception(sprintf('user id %s not found'), json_encode($refresheduser)); } } public function supportsclass($class) { return $this->getentityname() === $class || is_subclass_of($class, $this->getentityname()); } }
security yml:
security: encoders: flc\userbundle\entity\user: bcrypt role_hierarchy: role_admin: role_user role_super_admin: [role_user, role_admin, role_allowed_to_switch] providers: flc_users: entity: { class: flcuserbundle:user } firewalls: secured_area: pattern: ^/ form_login: login_path: login_form check_path: login_check logout: path: logout target: / anonymous: ~ access_control: - { path: ^/app, roles: role_user }
try adding __tostring
method on user entity:
public function __tostring() { return (string) $this->getusername(); }
Comments
Post a Comment