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 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?