mirror of
https://gitlab.com/TheGamecraft/c-cms.git
synced 2026-04-21 02:39:10 -04:00
File Explorer update + Permission update
This commit is contained in:
@@ -3,9 +3,25 @@
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use League\Flysystem\FileNotFoundException;
|
||||
use mysql_xdevapi\Exception;
|
||||
|
||||
class GoogleDriveFile extends Model
|
||||
{
|
||||
protected $primaryKey = 'id'; // or null
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
// In Laravel 6.0+ make sure to also set $keyType
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected $casts = [
|
||||
'rank_permission' => 'array',
|
||||
'job_permission' => 'array',
|
||||
'user_permission' => 'array',
|
||||
];
|
||||
|
||||
public static function icon($extension)
|
||||
{
|
||||
$icon = "fas fa-file";
|
||||
@@ -37,4 +53,204 @@ class GoogleDriveFile extends Model
|
||||
}
|
||||
return $icon;
|
||||
}
|
||||
|
||||
public static function checkConfig()
|
||||
{
|
||||
$configNull = (\Crypt::decryptString(\App\Config::getData('GOOGLE_DRIVE_CLIENT_ID')) != "" && \Crypt::decryptString(\App\Config::getData('GOOGLE_DRIVE_CLIENT_SECRET')) != "" && \Crypt::decryptString(\App\Config::getData('GOOGLE_DRIVE_REFRESH_TOKEN')) != "" && \Crypt::decryptString(\App\Config::getData('GOOGLE_DRIVE_FOLDER_ID')) != "");
|
||||
$configOk = true;
|
||||
|
||||
if ($configNull)
|
||||
{
|
||||
try {
|
||||
\Storage::cloud()->listContents("/", false);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$configOk = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $configNull && $configOk;
|
||||
}
|
||||
|
||||
public static function findByName($name)
|
||||
{
|
||||
return GoogleDriveFile::where('name','=',$name)->get()->first();
|
||||
}
|
||||
|
||||
public static function findByPath($path)
|
||||
{
|
||||
return GoogleDriveFile::where('path','=',$path)->get()->first();
|
||||
}
|
||||
|
||||
public static function findByPathInDrive($path,$current_directory = '/')
|
||||
{
|
||||
if ($path != "")
|
||||
{
|
||||
$exploded_path = explode('/',$path);
|
||||
if ($exploded_path[0] == "")
|
||||
{
|
||||
array_splice($exploded_path,0,1);
|
||||
}
|
||||
$contents = collect(Storage::cloud()->listContents($current_directory, false));
|
||||
|
||||
$dir = $contents->where('type', '=', 'dir')
|
||||
->where('name', '=', $exploded_path[0])
|
||||
->first();
|
||||
|
||||
if ( ! $dir)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
array_splice($exploded_path,0,1);
|
||||
$newPath = implode('/',$exploded_path);
|
||||
if ($newPath == "")
|
||||
{
|
||||
return $dir['basename'];
|
||||
}
|
||||
else
|
||||
{
|
||||
return GoogleDriveFile::findByPathInDrive($newPath,$dir['basename']);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function createByPathInDrive($path,$current_directory = '/')
|
||||
{
|
||||
if ($path != "")
|
||||
{
|
||||
$exploded_path = explode('/',$path);
|
||||
$size = count($exploded_path);
|
||||
if ($size > 1)
|
||||
{
|
||||
$parent = self::findByName($exploded_path[$size-2]);
|
||||
\Storage::cloud()->createDir($parent->id.'/'.$exploded_path[$size-1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
\Storage::cloud()->createDir('/'.$exploded_path[$size-1]);
|
||||
}
|
||||
return self::findByPathInDrive($path);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setPermission($subject, $value)
|
||||
{
|
||||
$explodedSubject = explode('.',$subject);
|
||||
$subject = $explodedSubject[0];
|
||||
$id = $explodedSubject[1];
|
||||
|
||||
$permission = null;
|
||||
if ($subject == 'rank')
|
||||
{
|
||||
$permission = $this->rank_permission;
|
||||
}
|
||||
elseif ($subject == 'job')
|
||||
{
|
||||
$permission = $this->job_permission;
|
||||
}
|
||||
elseif ($subject == 'user')
|
||||
{
|
||||
$permission = $this->user_permission;
|
||||
}
|
||||
|
||||
[$id] = $value;
|
||||
|
||||
if ($subject == 'rank')
|
||||
{
|
||||
$this->rank_permission = $permission;
|
||||
}
|
||||
elseif ($subject == 'job')
|
||||
{
|
||||
$this->job_permission = $permission;
|
||||
}
|
||||
elseif ($subject == 'user')
|
||||
{
|
||||
$this->user_permission = $permission;
|
||||
}
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function getAllPermission($subject)
|
||||
{
|
||||
$permission = null;
|
||||
if ($subject == 'rank')
|
||||
{
|
||||
$permission = $this->rank_permission;
|
||||
}
|
||||
elseif ($subject == 'job')
|
||||
{
|
||||
$permission = $this->job_permission;
|
||||
}
|
||||
elseif ($subject == 'user')
|
||||
{
|
||||
$permission = $this->user_permission;
|
||||
}
|
||||
|
||||
return $permission;
|
||||
}
|
||||
|
||||
public function getPermission($subject)
|
||||
{
|
||||
$explodedSubject = explode('.',$subject);
|
||||
$subject = $explodedSubject[0];
|
||||
$id = $explodedSubject[1];
|
||||
|
||||
if (isset($this->getAllPermission($subject)[$id]))
|
||||
{
|
||||
return $this->getAllPermission($subject)[$id];
|
||||
}
|
||||
else
|
||||
{
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
public function canUser($user, $permission = 'r')
|
||||
{
|
||||
if (strpos($this->getPermission('rank.0'),$permission) !== false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (strpos($this->getPermission('user.'.$user->id),$permission) === false)
|
||||
{
|
||||
if (strpos($this->getPermission('job.'.$user->job->id),$permission) === false)
|
||||
{
|
||||
if (strpos($this->getPermission('rank.'.$user->rank->id),$permission) === false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function canAuthUser($perm = 'r')
|
||||
{
|
||||
return $this->canUser(\Auth::user(),$perm);
|
||||
}
|
||||
|
||||
public static function getPermForUser($folder,$user,$perm = 'r')
|
||||
{
|
||||
$dir = \App\GoogleDriveFile::find($folder);
|
||||
if ($dir == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $dir->canUser($user,$perm);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getPermForAuthUser($folder,$perm = 'r')
|
||||
{
|
||||
return self::getPermForUser($folder,\Auth::user(),$perm);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user