本文整理匯總了PHP中common\models\User::getCurrent方法的典型用法代碼示例。如果您正苦於以下問題:PHP User::getCurrent方法的具體用法?PHP User::getCurrent怎麽用?PHP User::getCurrent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類common\models\User
的用法示例。
在下文中一共展示了User::getCurrent方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: beforeAction
/**
**在請求交由action處理之前,判斷用戶屬性,如果當前用戶沒有登錄,則將頁麵跳轉到登錄頁麵,即該模塊的所有操作都需要在用戶登錄狀態下進行.
* @param \yii\base\Action $action
* @return bool|\yii\web\Response
* @throws \yii\web\ForbiddenHttpException
*/
public function beforeAction($action)
{
if (!User::getCurrent()) {
return Yii::$app->user->loginRequired();
}
return parent::beforeAction($action);
}
示例2: actionChangePassword
/**
**顯示修改賬號登陸密碼的表單(get),修改賬號的登陸密碼(post).
* @return string whether or not the password has been changed successfully
*/
public function actionChangePassword()
{
if (Yii::$app->request->isGet) {
$csrf = Yii::$app->request->csrfToken;
return $this->render('change-password', ['csrf' => $csrf]);
}
$old_password = Yii::$app->request->post('old_password');
$new_password = Yii::$app->request->post('new_password');
$new_password_confirm = Yii::$app->request->post('new_password_confirm');
if (!$old_password || !$new_password || !$new_password_confirm) {
AjaxResponse::fail(null, 'Three inputs are required');
}
if ($new_password != $new_password_confirm) {
AjaxResponse::fail(null, 'Your confirm password is different from the new password. Please try again.');
}
$user = User::getCurrent();
if (!$user->validatePassword($old_password)) {
AjaxResponse::fail(null, 'Your old password was incorrect. Please try again.');
}
$user->setPassword($new_password);
$user->save();
Yii::$app->user->logout();
AjaxResponse::success();
}