예제 #1
0
 /**
  * @inheritdoc
  */
 public static function findOneCustom($condition, $checkRole = null)
 {
     // FIND ROLE
     $rolePrevious = 0;
     $roleSession = $rolePrevious;
     $roleCookie = $rolePrevious;
     $model = null;
     // SET Role: by MANUALLY. Default: at user requested time
     if (!is_null($checkRole)) {
         $rolePrevious = $checkRole;
         Yii::$app->session->set('user.role', $rolePrevious);
     }
     // Check Role: by PHP session. Default: at every loading page
     if (Yii::$app->session->has('user.role')) {
         $rolePrevious = intval(Yii::$app->session->get('user.role'));
         $roleSession = $rolePrevious;
     } elseif (Yii::$app->getRequest()->getCookies()->has('user_role')) {
         $rolePrevious = intval(Yii::$app->getRequest()->getCookies()->getValue('user_role'));
         $roleCookie = $rolePrevious;
         Yii::$app->session->set('user.role', $rolePrevious);
     }
     // FIND USER BY SELECTED ROLE
     if ($rolePrevious) {
         $user = null;
         if ($rolePrevious >= static::ROLE_ADMIN) {
             $user = Users::findOne($condition);
         } elseif ($rolePrevious >= static::ROLE_CLIENTES) {
             $user = Clientes::findOne($condition);
         }
         if ($user) {
             $model = new Identity();
             $model->data = $user->attributes;
             $model->data['id'] = $user->getPrimaryKey();
             // IF ALREADY LOGGED - RESTORE current user logged in ROLE
             if ($rolePrevious != $model->role) {
                 // UPDATE: Role: PHP Session: at runtime
                 if ($roleSession) {
                     Yii::$app->session->set('user.role', $model->role);
                 }
                 // UPDATE: Role: COOKIE: at runtime
                 if ($roleCookie) {
                     Yii::$app->getResponse()->getCookies()->add(new \yii\web\Cookie(['name' => 'user_role', 'value' => (string) $model->role, 'expire' => time() + 3600 * 24 * 30]));
                 }
             }
         }
     }
     // RETURN a searched user found or null if not
     return $model;
 }
예제 #2
0
 /**
  * Finds the Clientes model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Clientes the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Clientes::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }