本文整理汇总了PHP中UserIdentity::logUserIn方法的典型用法代码示例。如果您正苦于以下问题:PHP UserIdentity::logUserIn方法的具体用法?PHP UserIdentity::logUserIn怎么用?PHP UserIdentity::logUserIn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserIdentity
的用法示例。
在下文中一共展示了UserIdentity::logUserIn方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: impersonate
/**
* Logs a user in for impersonation.
*
* This method doesn’t have any sort of credential verification, and just requires the ID of the user to
* impersonate, so use it at your own peril.
*
* The new user session will only last as long as the browser session remains active; no identity cookie will be
* created.
*
* @param int $userId The user’s ID.
*
* @throws Exception
* @return bool Whether the user is now being impersonated.
*/
public function impersonate($userId)
{
$userModel = craft()->users->getUserById($userId);
if (!$userModel) {
throw new Exception(Craft::t('Could not find a user with Id of {userId}.', array('{userId}' => $userId)));
}
$this->_identity = new UserIdentity($userModel->username, null);
$this->_identity->logUserIn($userModel);
$id = $this->_identity->getId();
$states = $this->_identity->getPersistentStates();
// Run any before login logic.
if ($this->beforeLogin($id, $states, false)) {
// Fire an 'onBeforeLogin' event
$this->onBeforeLogin(new Event($this, array('username' => $userModel->username)));
$this->changeIdentity($id, $this->_identity->getName(), $states);
// Fire an 'onLogin' event
$this->onLogin(new Event($this, array('username' => $userModel->username)));
$this->_sessionRestoredFromCookie = false;
$this->_userRow = null;
$this->_userModel = null;
$this->setReturnUrl(null);
// Run any after login logic.
$this->afterLogin(false);
return !$this->getIsGuest();
}
Craft::log($userModel->username . ' tried to log in unsuccessfully.', LogLevel::Warning);
return false;
}
示例2: loginByUserId
/**
* Logs a user in for solely by their user ID.
*
* This method doesn’t have any sort of credential verification, so use it at your own peril.
*
* @param int $userId The user ID of the person to log in.
* @param bool $rememberMe Whether the user should be remembered.
* @param bool $setUsernameCookie Whether to set the username cookie or not.
*
* @return bool
* @throws Exception
*/
public function loginByUserId($userId, $rememberMe = false, $setUsernameCookie = false)
{
$userModel = craft()->users->getUserById($userId);
if (!$userModel) {
throw new Exception(Craft::t('Could not find a user with Id of {userId}.', array('{userId}' => $userId)));
}
// Require a userAgent string and an IP address to help prevent direct socket connections from trying to login.
if (!craft()->request->userAgent || !$_SERVER['REMOTE_ADDR']) {
Craft::log('Someone tried to login with userId: ' . $userId . ', without presenting an IP address or userAgent string.', LogLevel::Warning);
$this->logout(true);
$this->requireLogin();
}
$this->_identity = new UserIdentity($userModel->username, null);
$this->_identity->logUserIn($userModel);
if ($setUsernameCookie) {
$this->processUsernameCookie($userModel->username);
}
// Get how long this session is supposed to last.
$this->authTimeout = craft()->config->getUserSessionDuration($rememberMe);
$id = $this->_identity->getId();
$states = $this->_identity->getPersistentStates();
// Fire an 'onBeforeLogin' event
$event = new Event($this, array('username' => $userModel->username));
$this->onBeforeLogin($event);
// Is the event is giving us the go-ahead?
if ($event->performAction) {
// Run any before login logic.
if ($this->beforeLogin($id, $states, false)) {
$this->changeIdentity($id, $this->_identity->getName(), $states);
$user = craft()->users->getUserById($id);
if ($user) {
if ($this->authTimeout) {
if ($this->allowAutoLogin) {
// Save the necessary info to the identity cookie.
$sessionToken = craft()->security->generateRandomString(32);
$hashedToken = craft()->security->hashData(base64_encode(serialize($sessionToken)));
$uid = $this->storeSessionToken($user, $hashedToken);
$data = array($this->getName(), $sessionToken, $uid, $rememberMe ? 1 : 0, craft()->request->getUserAgent(), $this->saveIdentityStates());
$this->_identityCookie = $this->saveCookie('', $data, $this->authTimeout);
} else {
throw new Exception(Craft::t('{class}.allowAutoLogin must be set true in order to use cookie-based authentication.', array('{class}' => get_class($this))));
}
}
craft()->users->updateUserLoginInfo($user);
} else {
throw new Exception(Craft::t('Could not find a user with Id of {userId}.', array('{userId}' => $this->getId())));
}
$this->_sessionRestoredFromCookie = false;
$this->_userRow = null;
$this->_sessionRestoredFromCookie = false;
$this->_userRow = null;
$this->_userModel = null;
// Run any after login logic.
$this->afterLogin(false);
$success = !$this->getIsGuest();
} else {
$success = false;
}
} else {
$success = false;
}
if ($success) {
// Fire an 'onLogin' event
$this->onLogin(new Event($this, array('username' => $userModel->username)));
return true;
} else {
Craft::log($userModel->username . ' tried to log in unsuccessfully.', LogLevel::Warning);
return false;
}
}