Example #1
0
 public function init()
 {
     parent::init();
     $view = $this->getView();
     $module = Yii::$app->getModule("versioning");
     $user_id = Yii::$app->user->id;
     $groups = \amilna\versioning\components\Libs::userGroups($user_id);
     $groups = [1];
     $bundle = NotificationAsset::register($view);
     $this->bundle = $bundle;
     $searchModel = new VersionSearch();
     $dataProvider = $searchModel->search([]);
     $query = $dataProvider->query;
     $query->andWhere([Version::tableName() . ".status" => true])->andWhere(Record::tableName() . ".record_id is not null");
     if (count($this->models) > 0) {
         $query->andWhere([Record::tableName() . ".model" => $this->models])->andWhere(Record::tableName() . ".filter_viewers = false");
     }
     if ($user_id > 0) {
         $query->andWhere("concat(','," . Record::tableName() . ".viewers,',') not like '%," . $user_id . ",%'")->andWhere(Record::tableName() . ".filter_viewers = false OR (" . Record::tableName() . ".filter_viewers = true AND (" . Record::tableName() . ".owner_id = :uid OR " . Record::tableName() . ".group_id in (" . implode(",", $groups) . ")) )", [":uid" => $user_id]);
     } else {
         $query->limit(10);
     }
     $query->orderBy(Route::tableName() . ".time DESC," . Version::tableName() . ".id DESC");
     $script = "\t\t\n\t\t" . PHP_EOL;
     $view->registerJs($script);
     echo $this->render($this->viewPath, ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'module' => $module, 'widget' => $this]);
 }
Example #2
0
 public static function mkView($app, $eventName, $event)
 {
     $module = $app->getModule("versioning");
     $controller = $app->requestedAction->controller;
     $rotname = (isset($controller->module->module) ? $controller->module->id . "/" : "") . $controller->id;
     $user_id = $app->user->id;
     //$action_param = $controller->actionParams;
     $action_param = $app->request->queryParams;
     if ($user_id > 0) {
         $params = [];
         foreach ($action_param as $p) {
             if (!is_float($p) && is_numeric($p)) {
                 array_push($params, $p);
             }
         }
         if (count($params) > 0 || isset($action_param["vrid"])) {
             $searchModel = new VersionSearch();
             $dataProvider = $searchModel->search([]);
             $query = $dataProvider->query;
             $query->andWhere([Version::tableName() . ".status" => true]);
             if (isset($action_param["vrid"])) {
                 $query->andWhere([Record::tableName() . ".id" => $action_param["vrid"]]);
             } else {
                 //$query->andWhere(Route::tableName().".route like :route",[":route"=>$rotname."%"]);
                 $query->andWhere([Record::tableName() . ".record_id" => $params]);
             }
             $groups = self::userGroups($user_id);
             try {
                 $allowall = count($dataProvider->getModels()) > 0 ? false : true;
                 foreach ($dataProvider->getModels() as $mod) {
                     $m = $mod;
                     $v = $mod->version;
                     if ($v) {
                         $allow = false;
                         if (isset(Yii::$app->user->identity->isAdmin)) {
                             $allow = Yii::$app->user->identity->isAdmin;
                         } else {
                             $allow = in_array(Yii::$app->user->identity->username, $module->admins);
                         }
                         $inarr = false;
                         foreach ($v->getPrimaryKey(true) as $k => $p) {
                             if (isset($action_param[$k])) {
                                 if ($action_param[$k] == $p) {
                                     $inarr = true;
                                 }
                             }
                         }
                         $users = $m->record->viewers == null ? [] : explode(",", $m->record->viewers);
                         $group_id = $m->record->group_id;
                         if ($inarr && !$allow) {
                             if (in_array($group_id, $groups) || $m->record->owner_id == $user_id) {
                                 $allow = true;
                             }
                             if (!$allow && !$m->record->filter_viewers) {
                                 $views = $module->views;
                                 $mviews = [$rotname . "/" . $module->defaults["view"]];
                                 foreach ($views as $mw => $vws) {
                                     if ($mw == $m->record->model) {
                                         foreach ($vws as $vw) {
                                             $mviews[] = $rotname . "/" . $vw;
                                         }
                                     }
                                 }
                                 $allow = in_array($app->requestedRoute, $mviews);
                             }
                         } else {
                             $allow = true;
                         }
                         if ($allow) {
                             array_push($users, $user_id);
                             $m->record->viewers = implode(",", array_unique($users));
                             $m->record->save();
                         }
                     } else {
                         $allow = true;
                     }
                     $allowall = !$allowall ? $allow : true;
                 }
                 if (!$allowall) {
                     return $controller->redirect(["//" . $rotname]);
                 }
             } catch (yii\db\Exception $e) {
             }
         }
     } else {
         $param = $app->request->queryParams;
         if (isset($param['asusername'])) {
             $userClass = $module->userClass;
             $user = $userClass::findOne(["username" => $param['asusername']]);
             if ($user) {
                 $user_id = $user->id;
                 $app->session->set('asuserid', $user_id);
                 $cookie = new \yii\web\Cookie(['name' => 'asuserid', 'value' => $user_id]);
                 $cookie->expire = time() + 60 * 60 * 24 * 365;
                 // (1 year)
                 $app->response->cookies->add($cookie);
             }
         }
     }
 }
Example #3
0
 public function attributeLabels()
 {
     return array_merge(parent::attributeLabels(), ['time' => Yii::t('app', 'Time'), 'recordModel' => Yii::t('app', 'Model'), 'routeUser' => Yii::t('app', 'User')]);
 }
Example #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getVersions()
 {
     return $this->hasMany(Version::className(), ['route_id' => 'id'])->where("isdel=0");
 }
 /**
  * Finds the Version model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Version the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Version::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }