dongyiyu3953 2015-07-10 13:41
浏览 70

Yii2.0多次登录

I am creating a site in yii2.0.4. There is two login section one for normal user and other for business user and both have different tables. I have created login for both and it is working fine but when I click on logout it does not work. other problem is suppose there is an Id for logged in user is 1 and same id belongs from other table for business user than Yii takes both identity and it show two logout button one for business user and other for normal user but it should show only one logout and Yii should keep identity for that particular controller/login. Please help what can i do.

This is my controller {

use Yii;
use yii\helpers\FileHelper;
use yii\filters\AccessControl;
use yii\web\Controller;
use frontend\models\BusinessLoginForm;
use frontend\models\NewBusinessForm;
use yii\filters\VerbFilter;
use frontend\models\Business;
use common\models\UploadForm;
use yii\web\UploadedFile;
use common\models\UploadImage;
use common\models\BusinessCat;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use frontend\models\MessagesForm;
use common\models\Messages;




/**
 * Business controller
 */
class BusinessController extends Controller
{
    /**
     * @inheritdoc
     */
   public function behaviors()
    {
       return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup','confirm'],
                'rules' => [
                    [
                        'actions' => ['signup','confirm'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

     public function actions()
    {

        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


     /**
     * Creates a new Business model.
     * If creation is successful, the browser will be redirected to the 'create' page.
     * @return mixed
     */

    public function actionCreate()
    {
             if (!\Yii::$app->users->isGuest || !\Yii::$app->business->isGuest) {
                    return $this->goHome();
                }
            $mimeMagicFile = 'yii/helpers/mimeTypes.php';
            $model = new NewBusinessForm();

            $modleimage = new UploadForm();
            $modleimage->path = Yii::$app->basePath.'/../uploads/businessImages/';

            $modleimage->imageID = UploadedFile::getInstances($model, 'imageID');
            $modleimage->businessID = time();

           if ($model->load(Yii::$app->request->post()) && $model->validate()) {

               if(count($modleimage->imageID)>0){

                   if($modleimage->multipleUpload()){

                          if($business = $model->newBusiness()){
                              $newid = Yii::$app->db->getLastInsertID();

                              foreach($model->catID as $catIDs){

                                   Yii::$app->db->createCommand()
                                    ->insert('businesscat', ['businessID' => $newid,'catID'=>$catIDs])
                                    ->execute();
                             }


                                foreach ($modleimage->imageID as $file) {

                                    $uploadfilename = $modleimage->businessID ."_".$file;
                                    Yii::$app->db->createCommand()
                                    ->insert('businessimage', ['imageID' => $uploadfilename,'businessID'=>$newid])
                                    ->execute();

                                }
                                Yii::$app->getSession()->setFlash('success', 'Your business account has been created successfully. Confirmation link has been sent your email please click on that link and verify your account.');
                                return $this->redirect(['create']); 
                          }else{
                              Yii::$app->getSession()->setFlash('error', 'Error to save data.');
                          }

                         }else{
                                    $totalimages = count($modleimage->imageID);
                                   if($totalimages>4){
                                       Yii::$app->getSession()->setFlash('error', 'You can not upload more than 4 images.');
                                   }else{
                                       Yii::$app->getSession()->setFlash('error', 'You can nt upload image more than 2 MB.');
                                   } 
                         }

                     }else{

                         if($business = $model->newBusiness()){
                              $newid = Yii::$app->db->getLastInsertID();
                             foreach($model->catID as $catIDs){

                                   Yii::$app->db->createCommand()
                                    ->insert('businesscat', ['businessID' => $newid,'catID'=>$catIDs])
                                    ->execute();
                             }

                Yii::$app->getSession()->setFlash('success', 'Your business account has been created successfully. Confirmation link has been sent your email please click on the link and verify your account.');
                                return $this->redirect(['create']); 
            }else{
                Yii::$app->getSession()->setFlash('error', 'Error to save data.');
            }

                     }
                     return $this->refresh();

           } 

        return $this->render('create', [
                    'model' => $model,
                ]);


    }

        public function actionConfirm($vcode)
        {
            $model = $this->findNewBusiness($vcode,'login');
         if($model){
            Yii::$app->db->createCommand()
          ->update('business', ['mtype' => 1,'vcode' => ''], 'vcode= "'.$vcode.'"')
          ->execute();
            return $this->render('confirm');
         }

      }

    public function actionAccount()
    {
            if (Yii::$app->business->isGuest) {
                    return $this->redirect(['login']);
                }
        return $this->render('account');
    }

        public function actionMessage($id = NUll)
        {
            if (Yii::$app->business->isGuest) {
                    return $this->redirect(['login']);
                }
            $busid = Yii::$app->business->identity->businessID;

         $inbox = Messages::find()->where(['receiverID'=>$busid])->groupBy('senderID')->orderBy(['created'=>SORT_DESC])->all();



            $msgid = $id;
            $model = new MessagesForm();

            $model->utype = 1;
            $model->sndrID = Yii::$app->business->identity->businessID;
            $model->recvID = empty($id)?$inbox[0]['senderID']:$id;

            if ($model->load(Yii::$app->request->post())) {
                 if($messages = $model->saveNewMessage()){
                      return $this->refresh();
                 }else{
                     Yii::$app->getSession()->setFlash('error', 'Error to save data.');
                 }
            }

                return $this->render('message',[
                    'model' => $model,
                    'latid' => $msgid,
                ]);

        }

        public function actionLogin()
            {
                if (!\Yii::$app->business->isGuest || !\Yii::$app->users->isGuest) {
                    return $this->goHome();
                }
                $model = new BusinessLoginForm();
                if ($model->load(Yii::$app->request->post()) && $model->login()) {
                    return $this->redirect(Yii::$app->request->referrer);
                } else {
                    return $this->render('login', [
                        'model' => $model,
                    ]);
                }
            }

    public function actionLogout()
    {
        Yii::$app->business->logout();
        return $this->goHome();
    }

    protected function findNewBusiness($id,$page)
    {
       if (($model = Business::findOne(['mtype' => '0', 'vcode' => $id])) !== null) {
            return $model;
        } else {
            return $this->redirect([$page]); 
        }
    }

}

}

This is my frontend/models

<?php

namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\models\Category;
use common\models\BusinessCat;

/**
 * This is the model class for table "business".
 *
 * @property integer $businessID
 * @property string $Email
 * @property string $Password
 * @property string $Overview
 * @property string $qsignin
 * @property string $Bname
 * @property string $Address
 * @property string $website
 * @property string $Cnumber
 * @property integer $mtype
 * @property integer $busCat
 * @property integer $searchA
 * @property string $created
 */
class Business extends \yii\db\ActiveRecord
{
public $imageID;
public $catID;

/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'business';
}

/**
 * @inheritdoc
 */

public function rules()
{
    return [
       [['Email', 'Password', 'Overview', 'qsignin', 'Bname', 'Address', 'website', 'Cnumber', 'searchA','catID'], 'required'],
        ['Email', 'unique', 'targetClass' => '\common\models\Business', 'message' => 'This email address has already been taken.'],
        [['website'], 'string'],
        [['searchA'], 'string'],
        [['Email'], 'string', 'max' => 55],
        [['Password'], 'string', 'max' => 155],
        [['Overview'], 'string', 'max' => 500],
        [['qsignin'], 'string', 'max' => 10],
        [['Bname'], 'string', 'max' => 255],
        [['Address'], 'string', 'max' => 300],
        [['Cnumber'], 'string', 'max' => 15],
    ];
}

/**
 * @inheritdoc
 */

public function attributeLabels()
{
    return [
        'businessID' => 'Business ID',
        'Email' => 'Email',
        'Password' => 'Password',
        'Overview' => 'Overview',
        'qsignin' => 'Qsignin',
        'Bname' => 'Business Name',
        'Address' => 'Address',
        'website' => 'Website',
        'Cnumber' => 'Contact number',
        'mtype' => 'Member type',
        'catID' => 'Business Category',
        'searchA' => 'Search Area',
    ];
}

public function getBusinesscats()
{
    return $this->hasMany(Businesscat::className(), ['businessID' => 'businessID']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getBusinessimages()
{
    return $this->hasMany(Businessimage::className(), ['businessID' => 'businessID']);
}
}

and the I have created yii/web/Business.php and BusinessEvent.php using yii/web/User.php or UserEvent.php only I have changed the class name. and my common/models file for getidentity as same as common/models/User.php only I have changed class name.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 vue3页面el-table页面数据过多
    • ¥100 vue3中融入gRPC-web
    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题
    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
    • ¥15 找一个网络防御专家,外包的
    • ¥100 能不能让两张不同的图片md5值一样,(有尝)
    • ¥15 informer代码训练自己的数据集,改参数怎么改
    • ¥15 请看一下,学校实验要求,我需要具体代码