public function delete() { $roles = UserDomainRole::findByGroup($this)->all(); foreach ($roles as $role) { $role->delete(); } try { $connection = Yii::$app->db; $command = $connection->createCommand("DELETE FROM meican_auth_item WHERE name='" . $this->role_name . "'")->execute(); return true; } catch (Exception $e) { return false; } }
static function createToGroup($group_id, $domain, $reservation_id, $auth_id, $date = null) { $group = Group::findOne($group_id); $domain = Domain::findOne(['name' => $domain]); if (!$group || !$domain) { return false; } //Confere todos papeis associados ao grupo $roles = UserDomainRole::findByGroup($group); foreach ($roles->all() as $role) { if ($role->domain == null || $role->domain == $domain->name) { //Se papel for para todos dominios ou para dominio espeficido //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' => $role->user_id, 'type' => Notification::TYPE_AUTHORIZATION])->all(); foreach ($notifications as $notification) { $cauth = ConnectionAuth::findOne($notification->info); if ($cauth) { if ($cauth->domain == $domain->name) { $conn = Connection::findOne($cauth->connection_id); if ($conn) { if ($conn->reservation_id == $reservation_id) { $not = $notification; break; } } } } } if ($not) { //Se já existe, atualiza e coloca nova data //Pode receber uma data por parametro, neste caso, utiliza essa data como a data da criação da notificação if ($date) { $not->date = $date; } else { //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' => $role->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 = $role->user_id; if (isset($date)) { $not->date = $date; } else { //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' => $role->user_id, 'date' => $dateAux])->one()) { $date->modify('-1 second'); $dateAux = $date->format("Y-m-d H:i:s"); } $not->date = $dateAux; } $not->date = $dateAux; $not->type = Notification::TYPE_AUTHORIZATION; $not->viewed = 0; $not->info = (string) $auth_id; $not->save(); } } } }