I am doing a project that allow user to automatically login after they click the activation link.
Here is my activation controller, i don’t see anything wrong but I just can’t login the user, each time the user clicks on the link it still not login the user…
Is you authenticate method returning true - are you hitting the login method??
Looks like you’re using the pwd field that’s returned from the DB. Is this pwd encrypted/hashed? If so, is your authenticate method taking that into account? I mean, is is rehashing again and failing?
Thanks for your help! All i want to do is automatically login the user. Right now even i get rid of the "authenticate", just login the user, it gives me the blank screen(all white) and not login the user (i am sure there is no syntax error…) i am using yii-user
But, if you’re getting a black screen, it could indicate somethings amiss with your routing/permissions. Are you even getting to the controller’s action?
Also, not sure what’s going on with your indentation but why do you have 2 else statements with the same output? I don’t know what’s preceding it so I’m guessing when I say this.
if (isset($find->activkey) && ($find->activkey == $activkey))
{
$find->activkey = UserModule::encrypting(microtime());
$find->status = 1;
$find->save();
$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
$lastVisit->lastvisit = time();
$lastVisit->save();
$webUser = new CWebUser();
$webUser->changeIdentity($lastVisit->id, $lastVisit->username, array());
$this->render('/user/message', array('title' => UserModule::t("User activation"), 'content' => UserModule::t("<br>Welcome to my site. Your account is activated now.<br> You can <a href='http://mysite.com/user/profile'>Login Here</a>")));
}
else
{
$this->render('/user/message', array('title' => UserModule::t("User activation"), 'content' => UserModule::t("Incorrect activation URL.")));
}
Yes, that will log the user in. By using changeIdentity, you’ll not fire the onBeforeLogin event and will skip over cookie authentication.
The login method uses changeIdentity to log the user in.
public function login($identity,$duration=0)
{
$id=$identity->getId();
$states=$identity->getPersistentStates();
if($this->beforeLogin($id,$states,false))
{
$this->changeIdentity($id,$identity->getName(),$states);
if($duration>0)
{
if($this->allowAutoLogin)
$this->saveToCookie($duration);
else
throw new CException(Yii::t('yii','{class}.allowAutoLogin must be set true in order to use cookie-based authentication.',
array('{class}'=>get_class($this))));
}
$this->afterLogin(false);
}
return !$this->getIsGuest();
}
It’s a protected method; you need to override the class. Mine looks like this.
<?php
/**
* WebUser class file.
*
* @author Matt Skelton
* @date 8-Jun-2011
*/
/**
* Provides additional properties and functionality to CWebUser.
*/
class BNDWebUser extends CWebUser
{
/**
* Holds a reference to the currently logged in user model.
* @var User The currently logged in User Model.
*/
private $_model;
/**
* Retrieves a User model from the database
* @param integer $id the id of the User to be retrieved
* @return User the user model
*/
public function loadUser()
{
if ($this->_model === null)
{
$this->_model = User::model()->findByPk($this->id);
}
return $this->_model;
}
/**
* Changes the current logged in user's identity information
* @param mixed $id the id that uniquely identifies the user
* @param string $name the name used for display purposes
* @param array $states a list of states that needs to be persisted across the session
*/
public function changeIdentity($id, $name, $states = array())
{
parent::changeIdentity($id, $name, $states);
}
/**
* Returns a boolean indicating if the user has completed registration
* @return boolean if the user has completed registration
*/
public function getIsRegistered()
{
$isRegistered = false;
if (!$this->isGuest)
{
$user = $this->loadUser();
if ($user)
{
$isRegistered = true;
}
}
return $isRegistered;
}
/**
* Returns a boolean indicating if the user logged in through an OpenId provider
* @return boolean if the user logged in through an OpenId provider
*/
public function getIsOpenIdUser()
{
$identifier = $this->getState('openIdIdentifier');
return isset($identifier);
}
/**
* Returns a boolean indicating if a user's account has been linked to the OpenId provider that they signed in with.
* @return boolean if the accounts are linked
*/
public function getIsOpenIdLinked()
{
$isLinked = false;
if ($this->getIsOpenIdUser())
{
$openProfile = OpenProfile::model()->findByPk($this->getState('openIdIdentifier'));
if ($openProfile->user_id === $this->id)
{
$isLinked = true;
}
}
return $isLinked;
}
/**
* Returns a boolean if the user is in Working Mode. In Working Mode, the user has
* seleted an organization to work in.
* @return boolean if the user is working
*/
public function getIsWorking()
{
$isWorking = false;
if ($this->getState('organizationName') && $this->getState('organizationId'))
{
$isWorking = true;
}
return $isWorking;
}
/**
* Returns a boolean indicating is the user is working on the specified organization
* @param type $organizationId the ID of the organization to query
* @return boolean if the user is working on the organization
*/
public function isWorkingOn($organizationId)
{
$isWorking = false;
if ($this->getIsWorking() && ($this->getState('organizationId') === $organizationId))
{
$isWorking = true;
}
return $isWorking;
}
public function getRole()
{
return $this->getState('role');
}
public function getRoleForOrganization($organizationId)
{
$employee = Employee::model()->findByPk(array(
'organization_id' => $organizationId,
'user_id' => $this->id));
return $employee->role->name;
}
public function isOwner($organizationId = null)
{
$roleName = $this->getRole();
if ($organizationId)
{
$roleName = $this->getRoleForOrganization($organizationId);
}
return (strtolower($roleName) === Employee::ROLE_ADMIN);
}
public function isUser($organizationId = null)
{
$roleName = $this->getRole();
if ($organizationId)
{
$roleName = $this->getRoleForOrganization($organizationId);
}
return (strtolower($roleName) === Employee::ROLE_USER);
}
public function canView($organizationId = null)
{
$canView = false;
if ($this->isUser($organizationId) || $this->isOwner($organizationId))
{
$canView = true;
}
return $canView;
}
public function canModify($organizationId = null)
{
$canView = false;
if ($this->isOwner($organizationId))
{
$canView = true;
}
return $canView;
}
/**
* Returns a boolean indicating if the current user is an admin (proprietor) in at least 1 organization
*/
public function getIsAProprietor()
{
$isProprietor = false;
$user = $this->loadUser();
foreach ($user->roles as $role)
{
if (strtolower($role->role->name) == Employee::ROLE_ADMIN)
{
$isProprietor = true;
break;
}
}
return $isProprietor;
}
}
?>