2013-11-24 44 views
0

我遇到問題。我試圖在自定義主題的佈局中製作checkAccess規則。但決定保持它以前的方式,我撤消了一切,但現在它保持記錄作爲管理員,即使當我單擊註銷,仍然呈現的東西,我不想顯示,如果沒有記錄..這是我的主要佈局自定義:Yii,即使未登錄,用戶名稱也是管理員

<?php 
    if(Yii::app()->user->name = 'admin') 
    {  
      $this->widget('bootstrap.widgets.TbNavBar',array(
        'brandLabel'=>TbHtml::b(Yii::app()->name), 
        'color'=>TbHtml::NAVBAR_COLOR_INVERSE, 
        'items'=>array(
          array(
            'class'=>'bootstrap.widgets.TbNav', 
            'items'=>array(
              array('label'=>'Home', 'url'=>array('/site/index')), 
              array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')), 
              array('label'=>'Contact', 'url'=>array('/site/contact')), 
              array('label'=>'Users', 'url'=>array('/user/index')), 
            ), 
          ), 
          array(
            'class'=>'bootstrap.widgets.TbNav', 
            'htmlOptions'=>array('class'=>'pull-right'), 
            'items'=>array(
              array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest), 
              array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest) 
            ), 
          ), 
        ), 
      )); 
    } 
    else 
    { 
      $this->widget('bootstrap.widgets.TbNavBar',array(
        'brandLabel'=>TbHtml::b(Yii::app()->name), 
        'color'=>TbHtml::NAVBAR_COLOR_INVERSE, 
        'items'=>array(
          array(
            'class'=>'bootstrap.widgets.TbNav', 
            'items'=>array(
              array('label'=>'Home', 'url'=>array('/site/index')), 
              array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')), 
              array('label'=>'Contact', 'url'=>array('/site/contact')), 
            ), 
          ), 
          array(
            'class'=>'bootstrap.widgets.TbNav', 
            'htmlOptions'=>array('class'=>'pull-right'), 
            'items'=>array(
              array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest), 
              array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest) 
            ), 
          ), 
        ), 
      )); 
    } 
    ?> 

我做的回聲在菜單上顯示當前登錄用戶的名稱,但仍然返回爲管理..我不知道它在幹什麼。這是我的控制器。

<?php 

class UserController extends Controller 
{ 
    /** 
    * @var string the default layout for the views. Defaults to '//layouts/column2', meaning 
    * using two-column layout. See 'protected/views/layouts/column2.php'. 
    */ 
    public $layout='//layouts/column2'; 

    /** 
    * @return array action filters 
    */ 
    public function filters() 
    { 
      return array(
        'accessControl', // perform access control for CRUD operations 
        'postOnly + delete', // we only allow deletion via POST request 
      ); 
    } 

    /** 
    * Specifies the access control rules. 
    * This method is used by the 'accessControl' filter. 
    * @return array access control rules 
    */ 
    public function accessRules() 
    { 
      return array(
        array('allow', // allow all users to perform 'index' and 'view' actions 
          'actions'=>array('create', 'new'), 
          'users'=>array('*'), 
        ), 
        array('allow', // allow authenticated user to perform 'create' and 'update' actions 
          'actions'=>array('create','update'), 
          'users'=>array('@'), 
        ), 
        array('allow', // allow admin user to perform 'admin' and 'delete' actions 
          'actions'=>array('index', 'view', 'admin', 'delete'), 
          'users'=>array('admin'), 
        ), 
        array('deny', // deny all users 
          'users'=>array('*'), 
        ), 
      ); 
    } 

    /** 
    * Displays a particular model. 
    * @param integer $id the ID of the model to be displayed 
    */ 
    public function actionView($id) 
    { 
      $this->render('view',array(
        'model'=>$this->loadModel($id), 
      )); 
    } 

    public function actionNew($id) 
    { 
      $this->render('new',array(
        'model'=>$this->loadModel($id), 
      )); 
    } 
    /** 
    * Creates a new model. 
    * If creation is successful, the browser will be redirected to the 'view' page. 
    */ 
    public function actionCreate() 
    { 
      $user = new User; 
      $gunwcuser =new Gunwcuser; 
      $game = new Game; 
      $cash = new Cash; 

      // Uncomment the following line if AJAX validation is needed 
      // $this->performAjaxValidation($model); 

      $auth = '1'; 
      $time = '0000-00-00 00:00:00'; 
      $gp = '1000'; 
      $gold = '200000'; 
      $cash1 = '10000'; 
      $rank = '19'; 

      if(isset($_POST['User'])) 
      { 


        // Set data column in DB before saving 
        $user->Status = '1'; 
        $user->MuteTime = $time; 
        $user->RestrictTime = $time; 
        $user->Authority = $auth; 
        $user->User_Level = '1'; 
        $user->Authority2 = $auth; 
        $user->attributes=$_POST['User']; 


        $gunwcuser->Status = '1'; 
        $gunwcuser->MuteTime = $time; 
        $gunwcuser->RestrictTime = $time; 
        $gunwcuser->Authority = $auth; 
        $gunwcuser->User_Level = '1'; 
        $gunwcuser->Authority2 = $auth; 
        $gunwcuser->AuthorityBackup = $auth; 
        $gunwcuser->attributes=$_POST['User']; 

        $game->attributes=$_POST['User']; 
        $game->Nickname = $user->NickName; 
        $game->Money = $gold; 
        $game->EventScore1 = '0'; 
        $game->EventScore2 = '0'; 
        $game->EventScore3 = '0'; 
        $game->AvatarWear = '0'; 
        $game->Prop1 = '0'; 
        $game->Prop2 = '0'; 
        $game->AdminGift = $gp; 
        $game->TotalScore = $gp; 
        $game->SeasonScore = $rank; 
        $game->TotalGrade = $rank; 
        $game->SeasonGrade = '0'; 
        $game->TotalRank = '0'; 
        $game->SeasonRank = '0'; 
        $game->AccumShot = '0'; 
        $game->AccumDamage = '0'; 
        $game->StageRecords = '0'; 
        $game->LastUpdateTime = $time; 
        $game->NoRankUpdate = '0'; 
        $game->ClientData = '0'; 
        $game->CountryGrade = '20'; 
        $game->CountryRank = '0'; 
        $game->GiftProhibitTime = $time; 

        $cash->Cash = $cash1; 

        if($user->save() && $gunwcuser->save() && $game->save() && $cash->save()) 
          /*$this->redirect(array('view','id'=>$user->Id, 'message'=>$message));*/ 
          $this->redirect(array('new', 'id'=>$user->Id)); 
      } 

      $this->render('create',array(
        'user'=>$user, 'gunwcuser'=>$gunwcuser, 'game'=>$game, 'cash'=>$cash, 
      )); 
    } 

    /** 
    * Updates a particular model. 
    * If update is successful, the browser will be redirected to the 'view' page. 
    * @param integer $id the ID of the model to be updated 
    */ 
    public function actionUpdate($id) 
    { 
      $model=$this->loadModel($id); 

      // Uncomment the following line if AJAX validation is needed 
      // $this->performAjaxValidation($model); 

      if(isset($_POST['User'])) 
      { 
        $model->attributes=$_POST['User']; 
        if($model->save()) 
          $this->redirect(array('view','id'=>$model->Id)); 
      } 

      $this->render('update',array(
        'model'=>$model, 
      )); 
    } 

    /** 
    * Deletes a particular model. 
    * If deletion is successful, the browser will be redirected to the 'admin' page. 
    * @param integer $id the ID of the model to be deleted 
    */ 
    public function actionDelete($id) 
    { 
      $this->loadModel($id)->delete(); 

      // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser 
      if(!isset($_GET['ajax'])) 
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); 
    } 

    /** 
    * Lists all models. 
    */ 
    public function actionIndex() 
    { 
      $dataProvider=new CActiveDataProvider('User'); 
      $this->render('index',array(
        'dataProvider'=>$dataProvider, 
      )); 
    } 

    /** 
    * Manages all models. 
    */ 
    public function actionAdmin() 
    { 
      $model=new User('search'); 
      $model->unsetAttributes(); // clear any default values 
      if(isset($_GET['User'])) 
        $model->attributes=$_GET['User']; 

      $this->render('admin',array(
        'model'=>$model, 
      )); 
    } 

    /** 
    * Returns the data model based on the primary key given in the GET variable. 
    * If the data model is not found, an HTTP exception will be raised. 
    * @param integer $id the ID of the model to be loaded 
    * @return User the loaded model 
    * @throws CHttpException 
    */ 
    public function loadModel($id) 
    { 
      $model=User::model()->findByPk($id); 
      if($model===null) 
        throw new CHttpException(404,'The requested page does not exist.'); 
      return $model; 
    } 

    /** 
    * Performs the AJAX validation. 
    * @param User $model the model to be validated 
    */ 
    protected function performAjaxValidation($model) 
    { 
      if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') 
      { 
        echo CActiveForm::validate($model); 
        Yii::app()->end(); 
      } 
    } 
} 

回答

2
if(Yii::app()->user->name = 'admin') 

應該

if(Yii::app()->user->name == 'admin') 

見==是比較=是設置:)

而且這件事情

array('allow', // allow all users to perform 'index' and 'view' actions 
    'actions'=>array('create', 'new'), 
    'users'=>array('*'), 
), 

也許評論是絕對錯誤的,但它現在允許創建和新的行動:)

+0

對評論的歉意,這是由gii和我編輯的文件,我改變了你說的那個條件,不知何故它停止了這個bug,謝謝反正 – nosthertus