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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\GoogleDriveFile;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Psy\Util\Str;
|
||||
@@ -245,9 +246,6 @@ class GoogleDriveController extends Controller
|
||||
$contents = collect(Storage::cloud()->listContents($folder, $recursive));
|
||||
}
|
||||
|
||||
//dd($contents);
|
||||
//$meta = collect(Storage::cloud()->listContents($folder, true));
|
||||
//dd($meta);
|
||||
return view('admin.files.Google Drive.explorer',['directories' => $contents->where('type', '=', 'dir')->sortByDesc('name'), 'files' => $contents->where('type', '=', 'file'), 'currentDir' => $folder]);
|
||||
}
|
||||
|
||||
@@ -256,7 +254,7 @@ class GoogleDriveController extends Controller
|
||||
$error = [];
|
||||
if(\App\Config::getData('is_Google_Drive_enabled') == "true")
|
||||
{
|
||||
if ($this->checkConfig())
|
||||
if (GoogleDriveFile::checkConfig())
|
||||
{
|
||||
$structure = $this->getFileStructure();
|
||||
$this->checkStructure($structure,'/','/',$error);
|
||||
@@ -273,61 +271,135 @@ class GoogleDriveController extends Controller
|
||||
return $error;
|
||||
}
|
||||
|
||||
public function checkStructure($structure,$parent,$id,&$error)
|
||||
public function checkStructure()
|
||||
{
|
||||
$mydir = $this->listLockDirectory($id);
|
||||
$structure = $this->getFileStructure();
|
||||
|
||||
foreach ($structure as $key => $value)
|
||||
foreach ($structure as $directory => $value)
|
||||
{
|
||||
$found = false;
|
||||
$newDirID = null;
|
||||
$p = null;
|
||||
foreach ($mydir as $dir)
|
||||
$basename = GoogleDriveFile::findByPathInDrive($directory);
|
||||
if ($basename == false)
|
||||
{
|
||||
$p = $dir['basename'];
|
||||
if ($dir['extension'] == $key)
|
||||
$basename = GoogleDriveFile::createByPathInDrive($directory);
|
||||
}
|
||||
|
||||
$googleDriveFile = GoogleDriveFile::findByPath($directory);
|
||||
if ($googleDriveFile == null)
|
||||
{
|
||||
$googleDriveFile = new GoogleDriveFile();
|
||||
$googleDriveFile->id = $basename;
|
||||
$googleDriveFile->type = 'directory';
|
||||
$googleDriveFile->rank_permission = $value['rank'];
|
||||
$googleDriveFile->job_permission = [];
|
||||
$googleDriveFile->user_permission = [];
|
||||
$googleDriveFile->path = $directory;
|
||||
$name = explode('/',$directory);
|
||||
$googleDriveFile->name = $name[count($name)-1];
|
||||
$googleDriveFile->save();
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($googleDriveFile->id != $basename)
|
||||
{
|
||||
$found = true;
|
||||
$newDirID = $dir['basename'];
|
||||
break;
|
||||
$googleDriveFile->id = $basename;
|
||||
$googleDriveFile->save();
|
||||
}
|
||||
}
|
||||
if (!$found)
|
||||
{
|
||||
array_push($error,'Dossier 🔒.'.$key.' manquant... Le dossier a été créer');
|
||||
\Storage::cloud()->createDir($parent.'/🔒.'.$key);
|
||||
$tempdir = $this->listLockDirectory($parent);
|
||||
$p = $tempdir->where('extension','=',$key)->first()['basename'];
|
||||
}
|
||||
if ($value != [])
|
||||
{
|
||||
$this->checkStructure($value,$p,$newDirID,$error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function editPermission($folder)
|
||||
{
|
||||
$f = GoogleDriveFile::find($folder);
|
||||
if ($f == null)
|
||||
{
|
||||
$metadata = \Storage::cloud()->getMetadata($folder);
|
||||
$f = new GoogleDriveFile();
|
||||
$f->id = $folder;
|
||||
$f->type = 'directory';
|
||||
$f->name = $metadata['name'];
|
||||
$f->rank_permission = [1 => 'rwp'];
|
||||
$f->job_permission = [];
|
||||
$f->user_permission = [];
|
||||
$f->path = $this->recreatePath($folder);
|
||||
$f->save();
|
||||
}
|
||||
return view('admin.files.Google Drive.permission',['dir' => $f]);
|
||||
}
|
||||
|
||||
public function getFileStructure()
|
||||
{
|
||||
return collect([
|
||||
'Privé' => [
|
||||
'Cadet' => [],
|
||||
'ETAMAS' => [],
|
||||
'Officier' => [],
|
||||
'Staff' => [
|
||||
'Guide' => []
|
||||
]
|
||||
'🔒.Privé' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'Publique' => [
|
||||
'Fichier' => [],
|
||||
'Image' => []
|
||||
'🔒.Privé/🔒.Cadet' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Privé/🔒.ETAMAS' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Privé/🔒.Officier' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Privé/🔒.Staff' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Privé/🔒.Staff/🔒.Guide' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Publique' => [
|
||||
'rank' => [1 => 'rwp',0 => 'r'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Publique/🔒.Fichier' => [
|
||||
'rank' => [1 => 'rwp',0 => 'r'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Publique/🔒.Image' => [
|
||||
'rank' => [1 => 'rwp',0 => 'r'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Système' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Système/🔒.Fichier' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Système/🔒.Image' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Système/🔒.Image/🔒.Nouvelle' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'🔒.Système/🔒.Image/🔒.Profil' => [
|
||||
'rank' => [1 => 'rwp'],
|
||||
'job' => [],
|
||||
'user' => []
|
||||
],
|
||||
'Système' => [
|
||||
'Fichier' => [],
|
||||
'Image' => [
|
||||
'Nouvelle' => [],
|
||||
'Profil' => []
|
||||
]
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -340,24 +412,70 @@ class GoogleDriveController extends Controller
|
||||
return $dir;
|
||||
}
|
||||
|
||||
public function checkConfig()
|
||||
public function recreatePath($folder)
|
||||
{
|
||||
$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)
|
||||
$path = [];
|
||||
$name = [];
|
||||
$directories = collect(json_decode($this->getPathArray(),true));
|
||||
foreach ($directories as $dir)
|
||||
{
|
||||
try {
|
||||
$contents = collect(Storage::cloud()->listContents("/", false));
|
||||
}
|
||||
catch (\Exception $e)
|
||||
$path[$dir['basename']] = $dir['dirname'];
|
||||
$name[$dir['basename']] = $dir['name'];
|
||||
}
|
||||
$realPath = $name[$folder];
|
||||
$foo = $folder;
|
||||
while ($foo != "")
|
||||
{
|
||||
$bar = explode('/',$path[$foo]);
|
||||
$foo = $bar[count($bar)-1];
|
||||
if ($foo != "")
|
||||
{
|
||||
$configOk = false;
|
||||
$realPath = $name[$foo].'/'.$realPath;
|
||||
}
|
||||
}
|
||||
return $realPath;
|
||||
}
|
||||
|
||||
return $configNull && $configOk;
|
||||
public function editPermissionModal($folder,$subject,$id)
|
||||
{
|
||||
$dir = GoogleDriveFile::find($folder);
|
||||
$foo = null;
|
||||
$perm = null;
|
||||
if ($subject == 'rank')
|
||||
{
|
||||
if ($id == 0)
|
||||
{
|
||||
$foo = new \App\Rank();
|
||||
$foo->name = "Utilisateur non authentifié";
|
||||
$foo->id = 0;
|
||||
if (isset($dir->rank_permission[$id]))
|
||||
{
|
||||
$perm = $dir->rank_permission[$id];
|
||||
}
|
||||
else
|
||||
{
|
||||
$perm = "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$foo = \App\Rank::find($id);
|
||||
$perm = $dir->rank_permission[$id];
|
||||
}
|
||||
}
|
||||
elseif ($subject == 'job')
|
||||
{
|
||||
$foo = \App\Job::find($id);
|
||||
$perm = $dir->job_permission[$id];
|
||||
}
|
||||
else
|
||||
{
|
||||
$foo = \App\User::find($id);
|
||||
$perm = $dir->user_permission[$id];
|
||||
}
|
||||
return view('admin.files.Google Drive.permission.edit',['folder' => $dir,'subject' => $foo,'perm' => $perm,'s' => $subject]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -62,5 +62,6 @@ class Kernel extends HttpKernel
|
||||
'staff' => \App\Http\Middleware\AccesStaff::class,
|
||||
'admin' => \App\Http\Middleware\AccesAdmin::class,
|
||||
'perm' => \App\Http\Middleware\CheckPerm::class,
|
||||
'fileperm' => \App\Http\Middleware\CheckFilePerm::class,
|
||||
];
|
||||
}
|
||||
|
||||
62
app/Http/Middleware/CheckFilePerm.php
Normal file
62
app/Http/Middleware/CheckFilePerm.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use \App\GoogleDriveFile;
|
||||
use \App\Config;
|
||||
|
||||
class CheckFilePerm
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next,$type,$permission = 'r')
|
||||
{
|
||||
if (GoogleDriveFile::checkConfig() && Config::getData('is_Google_Drive_enabled') == 'true')
|
||||
{
|
||||
if ($type == 'file')
|
||||
{
|
||||
$dir = GoogleDriveFile::find($request->d);
|
||||
if ($dir != null)
|
||||
{
|
||||
if (\Auth::check())
|
||||
{
|
||||
if ($dir->canUser(\Auth::user(),$permission) == false)
|
||||
{
|
||||
clog('navigate','danger','Vous n\'avez pas la permission d\'accéder a ce fichier',\Auth::user()->id);
|
||||
return redirect('/admin')->with('error','Vous n\'avez pas la permission d\'accéder a ce fichier');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (strpos($dir->getPermission('rank.0'),$permission) === false)
|
||||
{
|
||||
clog('navigate','danger','Un utilisateur non authentifié a tenter de télécharger un fichier privé','0');
|
||||
abort(401,'Vous n\'avez pas la permission d\'accéder a ce fichier');
|
||||
}
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
if (\Auth::check())
|
||||
{
|
||||
if (\Auth::user()->permission('config_edit'))
|
||||
{
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
abort(401,'Vous n\'avez pas la permission d\'accéder a ce fichier');
|
||||
}
|
||||
abort(500);
|
||||
}
|
||||
else
|
||||
{
|
||||
clog('navigate','danger','Google Drive n\'est pas activé ou les identifiants sont incorrect',\Auth::user()->id);
|
||||
return redirect('/admin')->with('error','Google Drive n\'est pas activé ou les identifiants sont incorrect');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ function clog(string $type,string $result,string $event,$user_id = null,$obj_typ
|
||||
$log->user_id = \Auth::User()->id;
|
||||
}
|
||||
|
||||
|
||||
if ($obj_type != null)
|
||||
{
|
||||
$log->logable_type = $obj_type;
|
||||
|
||||
Reference in New Issue
Block a user