static function makeHtml($notification = null)
 {
     if ($notification == null) {
         return "";
     }
     $auth_id = $notification->info;
     if ($auth_id == null) {
         return "";
     }
     $auth = ConnectionAuth::findOne($auth_id);
     if (!$auth) {
         return "";
     }
     $connection = Connection::findOne($auth->connection_id);
     $source = ConnectionPath::findOne(['conn_id' => $connection->id, 'path_order' => 0])->domain;
     $path_order = ConnectionPath::find()->where(['conn_id' => $connection->id])->count() - 1;
     $destination = ConnectionPath::findOne(['conn_id' => $connection->id, 'path_order' => $path_order])->domain;
     $reservation = Reservation::findOne($connection->reservation_id);
     $title = Yii::t("notify", 'Pending authorization');
     $msg = Yii::t("notify", 'The connection is from') . " <b>" . $source . "</b> " . Yii::t("notify", 'to') . " <b>" . $destination . "</b>";
     $msg .= ". " . Yii::t("notify", 'The request bandwidth is') . " " . $reservation->bandwidth . " Mbps.";
     $date = Yii::$app->formatter->asDatetime($notification->date);
     $link = '/circuits/authorization/answer?id=' . $reservation->id . '&domain=' . $auth->domain;
     $html = Notification::makeHtml('pending_authorization.png', $date, $title, $msg, $link);
     if ($notification->viewed == true) {
         return '<li>' . $html . '</li>';
     }
     return '<li class="notification_new">' . $html . '</li>';
 }
 static function create($connection_id)
 {
     $connection = Connection::findOne($connection_id);
     if (!$connection) {
         return;
     }
     $reservation = Reservation::findOne($connection->reservation_id);
     if (!$reservation) {
         return;
     }
     if ($reservation->type == Reservation::TYPE_TEST) {
         return;
     }
     $user_id = $reservation->request_user_id;
     //Confere se já foi feita uma notificação de algum circuito desta reserva, se sim, reutiliza a mesma notificação
     $not = null;
     $notifications = Notification::find()->where(['user_id' => $reservation->request_user_id, 'type' => Notification::TYPE_RESERVATION])->all();
     foreach ($notifications as $notification) {
         if ($notification->info == $reservation->id) {
             $not = $notification;
             break;
         }
     }
     if ($not) {
         //Se já existe, atualiza e coloca nova data
         //Pequena maquipulação do horário para que nunca existam duas notificações com o mesmo horário
         $date = new \DateTime('now', new \DateTimeZone("UTC"));
         $dateAux = $date->format("Y-m-d H:i:s");
         while (Notification::find()->where(['user_id' => $user_id, 'date' => $dateAux])->one()) {
             $date->modify('-1 second');
             $dateAux = $date->format("Y-m-d H:i:s");
         }
         $not->date = $dateAux;
         $not->viewed = 0;
         $not->save();
     } else {
         //Se for nova, cria notificação
         $not = new Notification();
         $not->user_id = $reservation->request_user_id;
         //Pequena maquipulação do horário para que nunca existam duas notificações com o mesmo horário
         $date = new \DateTime('now', new \DateTimeZone("UTC"));
         $dateAux = $date->format("Y-m-d H:i:s");
         while (Notification::find()->where(['user_id' => $user_id, 'date' => $dateAux])->one()) {
             $date->modify('-1 second');
             $dateAux = $date->format("Y-m-d H:i:s");
         }
         $not->date = $dateAux;
         $not->type = Notification::TYPE_RESERVATION;
         $not->viewed = 0;
         $not->info = (string) $reservation->id;
         $not->save();
     }
 }
Exemplo n.º 3
0
 public function actionView($id)
 {
     $reservation = Reservation::findOne($id);
     $totalConns = $reservation->getConnections()->count();
     Yii::trace($totalConns);
     if ($totalConns == 1) {
         return $this->redirect(['/circuits', 'id' => $reservation->getConnections()->select(['id'])->asArray()->one()['id']]);
     }
     //Confere se algum pedido de autorização da expirou
     /*
             if($reservation){
                 $connectionsExpired = $conn = Connection::find()->where(['reservation_id' => $reservation->id])->andWhere(['<=','start', DateUtils::now()])->all();
                 foreach($connectionsExpired as $connection){
                     $requests = ConnectionAuth::find()->where(['connection_id' => $connection->id, 'status' => Connection::AUTH_STATUS_PENDING])->all();
                     foreach($requests as $request){
                         $request->changeStatusToExpired();
                         $connection->auth_status= Connection::AUTH_STATUS_EXPIRED;
                         $connection->save();
                         Notification::createConnectionNotification($connection->id);
                     }
                 }
             }
     
             //Confere a permissão
             $domains_name = [];
             foreach(self::whichDomainsCan('reservation/read') as $domain) $domains_name[] = $domain->name;
             $permission = false;
             if(Yii::$app->user->getId() == $reservation->request_user_id) $permission = true; //Se é quem requisitou
             else {
                 $conns = Connection::find()->where(['reservation_id' => $reservation->id])->select(["id"])->all();
                 if(!empty($conns)){
                     $conn_ids = [];
                     foreach($conns as $conn) $conn_ids[] = $conn->id;
                 
                     $paths = ConnectionPath::find()
                              ->where(['in', 'domain', $domains_name])
                              ->andWhere(['in', 'conn_id', $conn_ids])
                              ->select(["conn_id"])->distinct(true)->one();
                      
                     if(!empty($paths)) $permission = true;
                 }
             }
             
             if(!$permission){ //Se ele não tiver permissão em nenhum domínio do path e não for quem requisitou
                 return $this->goHome();
             }*/
     $connDataProvider = new ActiveDataProvider(['query' => $reservation->getConnections(), 'sort' => false, 'pagination' => ['pageSize' => 5]]);
     return $this->render('view/view', ['reservation' => $reservation, 'connDataProvider' => $connDataProvider]);
 }
 /**
  * ACTION REJECT ALL
  * @param string $id
  * @param string $domainTop
  * @param string $message
  */
 public function actionRejectAll($id = null, $domainTop = null, $message = null)
 {
     Yii::trace("Reject ALL");
     Yii::trace("Reservation ID: " . $id);
     Yii::trace("Msg: " . $message);
     if ($id && $domainTop) {
         $userId = Yii::$app->user->getId();
         $reservation = Reservation::findOne(['id' => $id]);
         $allRequest = null;
         $connections = Connection::find()->where(['reservation_id' => $id])->all();
         foreach ($connections as $conn) {
             if ($allRequest == null) {
                 $allRequest = ConnectionAuth::find()->where(['connection_id' => $conn->id, 'domain' => $domainTop]);
             } else {
                 $allRequest->union(ConnectionAuth::find()->where(['connection_id' => $conn->id, 'domain' => $domainTop]));
             }
         }
         $allRequest = $allRequest->all();
         $domainRoles = User::findOne(['id' => $userId])->getRoles()->all();
         $requests = [];
         foreach ($allRequest as $request) {
             if ($request->manager_user_id == $userId) {
                 $requests[$request->id] = $request;
             } else {
                 foreach ($domainRoles as $domainRule) {
                     $groupId = $domainRule->getGroup()->id;
                     if ($request->manager_group_id == $groupId) {
                         $requests[$request->id] = $request;
                     }
                 }
             }
         }
         foreach ($requests as $req) {
             if ($req->status == Connection::AUTH_STATUS_PENDING) {
                 if ($req->type == ConnectionAuth::TYPE_GROUP) {
                     $req->manager_user_id = Yii::$app->user->getId();
                 }
                 if ($message) {
                     $req->manager_message = $message;
                 }
                 $req->status = Connection::AUTH_STATUS_REJECTED;
                 $req->save();
                 $flow = new BpmFlow();
                 $flow->response($req->connection_id, $req->domain, BpmFlow::STATUS_NO);
             }
         }
     }
 }
Exemplo n.º 5
0
 public function actionCancel($id)
 {
     $conn = Connection::findOne($id);
     $permission = false;
     $reservation = Reservation::findOne(['id' => $conn->reservation_id]);
     if (Yii::$app->user->getId() == $reservation->request_user_id) {
         $permission = true;
     } else {
         $domains_name = [];
         foreach (self::whichDomainsCan('reservation/delete') as $domain) {
             $domains_name[] = $domain->name;
         }
         $paths = ConnectionPath::find()->where(['in', 'domain', $domains_name])->andWhere(['conn_id' => $conn->id])->select(["conn_id"])->distinct(true)->one();
         if (!empty($paths)) {
             $permission = true;
         }
     }
     if ($permission) {
         $conn->requestCancel();
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 6
0
 public function beforeDelete()
 {
     if (parent::beforeDelete()) {
         $task = $this->getScheduledTask();
         if ($task) {
             return $task->delete();
         }
     }
     return false;
 }
Exemplo n.º 7
0
 public function searchByDomains($params)
 {
     $this->load($params);
     $userId = Yii::$app->user->getId();
     $now = DateUtils::now();
     $authorizations = [];
     //Armazena os pedidos
     $reservationsVisited = [];
     //Armazena as reservas ja incluidas nos pedidos e o dominio ao qual o pedido foi feito.
     //Pega todas requisições feitas para o usuário
     if ($this->domain) {
         $userRequests = ConnectionAuth::find()->where(['domain' => $this->domain, 'manager_user_id' => $userId, 'status' => Connection::AUTH_STATUS_PENDING])->all();
     } else {
         $userRequests = ConnectionAuth::find()->where(['manager_user_id' => $userId, 'status' => Connection::AUTH_STATUS_PENDING])->all();
     }
     foreach ($userRequests as $request) {
         //Limpa mantendo apenas 1 por reserva
         $uniq = true;
         $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['<=', 'start', DateUtils::now()])->one();
         if (isset($conn)) {
             $request->changeStatusToExpired();
             $conn->auth_status = Connection::AUTH_STATUS_EXPIRED;
             $conn->save();
             ReservationNotification::create($conn->id);
         } else {
             $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['>', 'start', DateUtils::now()])->one();
             foreach ($reservationsVisited as $res) {
                 if ($conn->reservation_id == $res[0] && $request->domain == $res[1]) {
                     $uniq = false;
                 }
             }
             if ($uniq) {
                 $aux = [];
                 $aux[0] = $conn->reservation_id;
                 $aux[1] = $request->domain;
                 $reservationsVisited[] = $aux;
                 $source = $conn->getFirstPath()->one();
                 $destination = $conn->getLastPath()->one();
                 if (!$this->src_domain || $this->src_domain == $source->domain) {
                     if (!$this->dst_domain || $this->dst_domain == $destination->domain) {
                         $form = new AuthorizationForm();
                         $form->setValues(Reservation::findOne(['id' => $conn->reservation_id]), $request->domain, $source->domain, $destination->domain);
                         $authorizations[] = $form;
                     }
                 }
             }
         }
     }
     //Pega todos os papeis do usuário
     $domainRoles = User::findOne(['id' => $userId])->getRoles()->all();
     foreach ($domainRoles as $role) {
         //Passa por todos papeis
         if ($this->domain) {
             $groupRequests = ConnectionAuth::find()->where(['domain' => $this->domain, 'manager_group_id' => $role->getGroup()->id, 'status' => Connection::AUTH_STATUS_PENDING])->all();
         } else {
             $groupRequests = ConnectionAuth::find()->where(['manager_group_id' => $role->getGroup()->id, 'status' => Connection::AUTH_STATUS_PENDING])->all();
         }
         foreach ($groupRequests as $request) {
             //Passa por todas requisições para testar se o dominio corresponde
             $domain = Domain::findOne(['name' => $request->domain]);
             if ($domain) {
                 if ($role->domain == NULL || $role->domain == $domain->name) {
                     $uniq = true;
                     $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['<=', 'start', DateUtils::now()])->one();
                     if (isset($conn)) {
                         $request->changeStatusToExpired();
                         $conn->auth_status = Connection::AUTH_STATUS_EXPIRED;
                         $conn->save();
                         ReservationNotification::create($conn->id);
                     } else {
                         $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['>', 'start', DateUtils::now()])->one();
                         foreach ($reservationsVisited as $res) {
                             if ($conn->reservation_id == $res[0] && $domain->name == $res[1]) {
                                 $uniq = false;
                             }
                         }
                         if ($uniq) {
                             $aux = [];
                             $aux[0] = $conn->reservation_id;
                             $aux[1] = $request->domain;
                             $reservationsVisited[] = $aux;
                             $source = $conn->getFirstPath()->one();
                             $destination = $conn->getLastPath()->one();
                             if (!$this->src_domain || $this->src_domain == $source->domain) {
                                 if (!$this->dst_domain || $this->dst_domain == $destination->domain) {
                                     $form = new AuthorizationForm();
                                     $form->setValues(Reservation::findOne(['id' => $conn->reservation_id]), $request->domain, $source->domain, $destination->domain);
                                     $authorizations[] = $form;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $dataProvider = new ArrayDataProvider(['allModels' => $authorizations, 'sort' => false, 'pagination' => ['pageSize' => 15]]);
     return $dataProvider;
 }
Exemplo n.º 8
0
 /**
  * 
  * @param unknown $connection_id
  * @param unknown $domainTop
  * @return boolean
  */
 public static function execute($connection_id, $domainTop)
 {
     $flows = BpmFlow::find()->where(['domain' => $domainTop, 'connection_id' => $connection_id]);
     if ($flows->count() == 0) {
         return false;
     }
     foreach ($flows->all() as $flow) {
         $connection = Connection::findOne(['id' => $connection_id]);
         $reservation = Reservation::findOne(['id' => $connection->reservation_id]);
         switch ($flow->type) {
             //Domain
             case 'Domain':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkDomain($flow, $connection);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //User
             //User
             case 'User':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkUser($flow, $reservation);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //Group
             //Group
             case 'Group':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkGroup($flow, $reservation);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //Bandwidth
             //Bandwidth
             case 'Bandwidth':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkBandwidth($flow, $reservation);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //Request_User_Authorization
             //Request_User_Authorization
             case 'Request_User_Authorization':
                 if ($flow->status == self::STATUS_WAITING) {
                     return false;
                 } else {
                     if ($flow->status == self::STATUS_READY) {
                         return BpmFlow::createUserAuth($flow, $reservation);
                     }
                     if ($flow->status != self::STATUS_READY) {
                         BpmFlow::nextNodes($flow);
                     }
                 }
                 break;
                 //Request_Group_Autorization
             //Request_Group_Autorization
             case 'Request_Group_Authorization':
                 if ($flow->status == self::STATUS_WAITING) {
                     return false;
                 } else {
                     if ($flow->status == self::STATUS_READY) {
                         return BpmFlow::createGroupAuth($flow, $reservation);
                     }
                     if ($flow->status != self::STATUS_READY) {
                         BpmFlow::nextNodes($flow);
                     }
                 }
                 break;
                 //Hour
             //Hour
             case 'Hour':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkHour($flow, $reservation);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //WeekDay
             //WeekDay
             case 'WeekDay':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkWeekday($flow, $connection);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //Duration
             //Duration
             case 'Duration':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkDuration($flow, $connection);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //Domain
             //Domain
             case 'Device':
                 if ($flow->status == self::STATUS_READY) {
                     BpmFlow::checkDevice($flow, $connection);
                 }
                 if ($flow->status != self::STATUS_READY) {
                     BpmFlow::nextNodes($flow);
                 }
                 break;
                 //Accept_Automatically
             //Accept_Automatically
             case 'Accept_Automatically':
                 Yii::trace("Requisição ACEITA");
                 BpmFlow::removeFlow($flow);
                 return false;
                 break;
                 //Deny_Automatically
             //Deny_Automatically
             case 'Deny_Automatically':
                 Yii::trace("Requisição NEGADA");
                 BpmFlow::removeFlow($flow);
                 return false;
                 break;
         }
     }
     return true;
 }
Exemplo n.º 9
0
 public function searchTerminatedByDomains($params, $allowed_domains)
 {
     $validDomains = [];
     $this->load($params);
     $userId = Yii::$app->user->getId();
     $domains_name = [];
     foreach ($allowed_domains as $domain) {
         $domains_name[] = $domain->name;
     }
     $connPaths = [];
     if ($this->src_domain && $this->dst_domain) {
         //if(in_array($this->src_domain, $domains_name) && in_array($this->dst_domain, $domains_name)){
         $dstPaths = ConnectionPath::findBySql("\n                    SELECT cp1.conn_id\n                    FROM (\n                        SELECT conn_id, MAX(`path_order`) AS last_path\n                        FROM `meican_connection_path`\n                        GROUP BY `conn_id`\n                        ) cp2\n                    JOIN    `meican_connection_path` cp1\n                    ON      cp1.conn_id = cp2.conn_id AND cp1.domain = '" . $this->dst_domain . "' AND cp1.path_order = cp2.last_path")->asArray()->all();
         $allowedConns = [];
         foreach ($dstPaths as $dstPath) {
             $allowedConns[] = $dstPath['conn_id'];
         }
         //filtra por conn aceitas pela query anterior
         $connPaths = ConnectionPath::find()->where(['in', 'domain', [$this->src_domain]])->andWhere(['path_order' => 0])->andWhere(['in', 'conn_id', $allowedConns])->select(["conn_id"])->distinct(true)->asArray()->all();
         //}
     } elseif ($this->src_domain) {
         //if(in_array($this->src_domain, $domains_name)){
         $connPaths = ConnectionPath::find()->where(['in', 'domain', [$this->src_domain]])->andWhere(['path_order' => 0])->select(["conn_id"])->distinct(true)->asArray()->all();
         //}
     } elseif ($this->dst_domain) {
         //if(in_array($this->dst_domain, $domains_name)){
         $connPaths = ConnectionPath::findBySql("\n                    SELECT cp1.conn_id\n                    FROM (\n                        SELECT conn_id, MAX(`path_order`) AS last_path\n                        FROM `meican_connection_path`\n                        GROUP BY `conn_id`\n                        ) cp2\n                    JOIN    `meican_connection_path` cp1\n                    ON      cp1.conn_id = cp2.conn_id AND cp1.domain = '" . $this->dst_domain . "' AND cp1.path_order = cp2.last_path")->asArray()->all();
         //}
     } else {
         foreach ($allowed_domains as $domain) {
             $validDomains[] = $domain->name;
         }
         $connPaths = ConnectionPath::find()->where(['in', 'domain', $validDomains])->select(["conn_id"])->distinct(true)->asArray()->all();
     }
     $validConnIds = [];
     foreach ($connPaths as $connPath) {
         $validConnIds[] = $connPath['conn_id'];
     }
     $validConns = Connection::find()->where(['in', 'id', $validConnIds])->select('reservation_id')->distinct(true)->asArray()->all();
     $validIds = [];
     foreach ($validConns as $conn) {
         $validIds[] = $conn['reservation_id'];
     }
     $invalidConnections = Connection::find()->where(['>=', 'finish', DateUtils::now()])->andWhere(['status' => ["PENDING", "CREATED", "CONFIRMED", "SUBMITTED", "PROVISIONED"]])->select(["reservation_id"])->distinct(true)->asArray()->all();
     $invalidIds = [];
     foreach ($invalidConnections as $conn) {
         $invalidIds[] = $conn['reservation_id'];
     }
     if (!$this->src_domain && !$this->dst_domain) {
         $reservations = Reservation::find()->where(['in', 'id', $validIds])->orWhere(['request_user_id' => Yii::$app->user->getId()])->andWhere(['not in', 'id', $invalidIds])->andWhere(['type' => self::TYPE_NORMAL])->orderBy(['date' => SORT_DESC])->asArray()->all();
     } else {
         $reservations = Reservation::find()->where(['not in', 'id', $invalidIds])->andWhere(['in', 'id', $validIds])->andWhere(['type' => self::TYPE_NORMAL])->orderBy(['date' => SORT_DESC])->asArray()->all();
     }
     $validResIds = [];
     foreach ($reservations as $res) {
         if ($res['request_user_id'] == $userId) {
             $validResIds[] = $res['id'];
         } else {
             if (!$this->request_user) {
                 $conns = Connection::find()->where(['reservation_id' => $res['id']])->select(["id"])->asArray()->all();
                 if (!empty($conns)) {
                     $conn_ids = [];
                     foreach ($conns as $conn) {
                         $conn_ids[] = $conn['id'];
                     }
                     $paths = ConnectionPath::find()->where(['in', 'domain', $domains_name])->andWhere(['in', 'conn_id', $conn_ids])->select(["conn_id"])->distinct(true)->asArray()->all();
                     if (!empty($paths)) {
                         $validResIds[] = $res['id'];
                     }
                 }
             }
         }
     }
     $dataProvider = new ActiveDataProvider(['query' => Reservation::find()->where(['in', 'id', $validResIds])->orderBy(['date' => SORT_DESC]), 'sort' => false, 'pagination' => ['pageSize' => 10]]);
     return $dataProvider;
 }
Exemplo n.º 10
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getReservation()
 {
     return $this->hasOne(Reservation::className(), ['id' => 'reservation_id']);
 }
Exemplo n.º 11
0
 static function createNotificationsGroup($user_id, $group_name, $domain)
 {
     $user = User::findOne($user_id);
     $group = Group::findOne(['role_name' => $group_name]);
     if ($user && $group) {
         Yii::trace("Criar notificações do grupo " . $group->name . " para usuário " . $user->name);
         //Busca todas autorizações pendentes do grupo
         //Se tem dominio, procura só as relacionadas ao dominio do papel
         if ($domain) {
             $auths = ConnectionAuth::find()->where(['status' => Connection::AUTH_STATUS_PENDING, 'domain' => $domain, 'type' => ConnectionAuth::TYPE_GROUP, 'manager_group_id' => $group->id])->all();
         } else {
             $auths = ConnectionAuth::find()->where(['status' => Connection::AUTH_STATUS_PENDING, 'type' => ConnectionAuth::TYPE_GROUP, 'manager_group_id' => $group->id])->all();
         }
         //Passa por todas criando uma notificação
         foreach ($auths as $auth) {
             $connection = Connection::findOne($auth->connection_id);
             $reservation = Reservation::findOne($connection->reservation_id);
             AuthorizationNotification::createToUser($user->id, $auth->domain, $connection->reservation_id, $auth->id, $reservation->date);
         }
     }
 }