/** * @inheritdoc */ protected function getValue($event) { if ($event->name == BaseActiveRecord::EVENT_BEFORE_INSERT) { $attr = ArrayHelper::getValue($this->attributes, BaseActiveRecord::EVENT_BEFORE_INSERT, $this->createdAtAttribute); if (is_array($attr)) { $attr = current($attr); } if ($value = $event->sender->{$attr}) { return $value; } } return CurrentUser::getId($this->asRobot, $this->robot); }
/** * @param $permission * @param null $user * * @return bool * @throws \Exception */ public static function checkAccess($permission, $user = null) { if ($permission instanceof Action) { $permission = self::formPermissionByAction($permission); } if (CurrentUser::isGuest()) { return self::hasChild('guest', $permission); } else { return self::auth()->checkAccess(self::getUserId($user), $permission); } }
public static function upload($file, $name = null, $dir = self::F_FILES, $slug = null, $data = null, $delete = true) { if (is_null($dir)) { $dir = self::F_FILES; } $dir = trim($dir); $filePath = ''; if (strpos($file, 'http') === 0) { $tmp = Yii::getAlias('@runtime') . DIRECTORY_SEPARATOR . uniqid("fu"); file_put_contents($tmp, file_get_contents($file)); $filePath = $tmp; $name = $name ? $name : basename($file); } elseif (is_string($file)) { $filePath = Yii::getAlias($file); } elseif ($file instanceof UploadedFile) { $filePath = $file->tempName; $name = $name ? $name : $file->name; } $name = $name ? $name : basename($filePath); $sec = new Security(); while (FileUpload::find()->where(["path" => $uniquePath = md5($sec->generateRandomString())])->one()) { } $dirSlug = $dir; if (!is_dir($dir) && !($dir = self::getFolder($dirSlug))) { if (!$dir) { throw new \Exception("Folder for param '{$dirSlug}' is not set"); } else { throw new \Exception("Folder '{$dir}' not found"); } } $fullPath = self::formPath($uniquePath, $dir); if (!FileHelper::createDirectory(dirname($fullPath))) { throw new \Exception("Can't create folder '{" . dirname($fullPath) . "}'"); } if (!file_exists($filePath)) { throw new \Exception('File not loaded or not exist'); } if (is_uploaded_file($filePath)) { if (!move_uploaded_file($filePath, $fullPath)) { throw new \Exception('Unknown upload error'); } } elseif ($delete ? !rename($filePath, $fullPath) : !copy($filePath, $fullPath)) { throw new \Exception('Failed to write file to disk'); } $info = pathinfo($name); $fileUpload = new self(); //Фиск для сессии, при аяксовом запросе if (isset(Yii::$app->session)) { Yii::$app->session->open(); $fileUpload->session = Yii::$app->session->getIsActive() ? Yii::$app->session->getId() : null; Yii::$app->session->close(); } $fileUpload->user_id = CurrentUser::getId(1); $fileUpload->data = !is_null($data) ? json_encode($data) : null; $fileUpload->mime_type = FileHelper::getMimeType($fullPath); $fileUpload->md5 = md5_file($fullPath); $fileUpload->folder = $dirSlug; $fileUpload->path = $uniquePath; $fileUpload->slug = $slug; $fileUpload->size = filesize($fullPath); if (!($extension = strtolower(ArrayHelper::getValue($info, "extension")))) { $extension = ArrayHelper::getValue(FileHelper::getExtensionsByMimeType($fileUpload->mime_type), 0); } $fileUpload->name = basename($name, '.' . $extension); $fileUpload->extension = $extension; if ($fileUpload->save()) { return $fileUpload; } else { $fileUpload->deleteFile(); return null; } }