Add company_id column to media tables
This commit is contained in:
		
							
								
								
									
										57
									
								
								app/Listeners/Update/V21/Version2114.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								app/Listeners/Update/V21/Version2114.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Listeners\Update\V21; | ||||
|  | ||||
| use App\Abstracts\Listeners\Update as Listener; | ||||
| use App\Events\Install\UpdateFinished as Event; | ||||
| use App\Models\Common\Media; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class Version2114 extends Listener | ||||
| { | ||||
|     const ALIAS = 'core'; | ||||
|  | ||||
|     const VERSION = '2.1.14'; | ||||
|  | ||||
|     /** | ||||
|      * Handle the event. | ||||
|      * | ||||
|      * @param  $event | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function handle(Event $event) | ||||
|     { | ||||
|         if ($this->skipThisUpdate($event)) { | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         $this->updateMediaTables(); | ||||
|     } | ||||
|  | ||||
|     public function updateMediaTables() | ||||
|     { | ||||
|         $company_ids = []; | ||||
|  | ||||
|         foreach (Media::withTrashed()->cursor() as $media) { | ||||
|             $company_id = null; | ||||
|  | ||||
|             if (preg_match('/\d{4}(\/\d{2}){2}\/(\d+)\//', $media->directory, $matches) && isset($matches[2])) { // 2021/04/09/34235/invoices | ||||
|                 $company_id = $matches[2]; | ||||
|             } elseif (preg_match('/^(\d+)\//', $media->directory, $matches) && isset($matches[1])) { // 34235/invoices | ||||
|                 $company_id = $matches[1]; | ||||
|             } | ||||
|  | ||||
|             if (null === $company_id) { | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             $company_ids[$company_id][] = $media->id; | ||||
|         } | ||||
|  | ||||
|         foreach ($company_ids as $company_id => $media_ids) { | ||||
|             DB::table('media')->whereIn('id', $media_ids)->update(['company_id' => $company_id]); | ||||
|             DB::table('mediables')->whereIn('media_id', $media_ids)->update(['company_id' => $company_id]); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -2,14 +2,15 @@ | ||||
|  | ||||
| namespace App\Models\Common; | ||||
|  | ||||
| use App\Traits\Tenants; | ||||
| use Illuminate\Database\Eloquent\SoftDeletes; | ||||
| use Plank\Mediable\Media as BaseMedia; | ||||
|  | ||||
| class Media extends BaseMedia | ||||
| { | ||||
|     use SoftDeletes; | ||||
|     use SoftDeletes, Tenants; | ||||
|  | ||||
|     protected $tenantable = false; | ||||
|     protected $tenantable = true; | ||||
|  | ||||
|     protected $dates = ['deleted_at']; | ||||
| } | ||||
|   | ||||
| @@ -31,6 +31,7 @@ class Event extends Provider | ||||
|             'App\Listeners\Update\V21\Version218', | ||||
|             'App\Listeners\Update\V21\Version219', | ||||
|             'App\Listeners\Update\V21\Version2112', | ||||
|             'App\Listeners\Update\V21\Version2114', | ||||
|         ], | ||||
|         'Illuminate\Auth\Events\Login' => [ | ||||
|             'App\Listeners\Auth\Login', | ||||
|   | ||||
| @@ -28,7 +28,7 @@ class Company implements Scope | ||||
|  | ||||
|         // Skip for specific tables | ||||
|         $skip_tables = [ | ||||
|             'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', 'role_companies', | ||||
|             'jobs', 'firewall_ips', 'firewall_logs', 'migrations', 'notifications', 'role_companies', | ||||
|             'role_permissions', 'sessions', 'user_companies', 'user_dashboards', 'user_permissions', 'user_roles', | ||||
|         ]; | ||||
|  | ||||
|   | ||||
| @@ -34,4 +34,26 @@ trait Media | ||||
|  | ||||
|         return $media; | ||||
|     } | ||||
|  | ||||
|     public function attachMedia($media, $tags): void | ||||
|     { | ||||
|         $tags = (array)$tags; | ||||
|         $increments = $this->getOrderValueForTags($tags); | ||||
|  | ||||
|         $ids = $this->extractPrimaryIds($media); | ||||
|  | ||||
|         foreach ($tags as $tag) { | ||||
|             $attach = []; | ||||
|             foreach ($ids as $id) { | ||||
|                 $attach[$id] = [ | ||||
|                     'company_id' => company_id(), | ||||
|                     'tag' => $tag, | ||||
|                     'order' => ++$increments[$tag], | ||||
|                 ]; | ||||
|             } | ||||
|             $this->media()->attach($attach); | ||||
|         } | ||||
|  | ||||
|         $this->markMediaDirty($tags); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -9,8 +9,6 @@ use MediaUploader; | ||||
|  | ||||
| trait Uploads | ||||
| { | ||||
|     public $company_id_index = 3; | ||||
|  | ||||
|     public function getMedia($file, $folder = 'settings', $company_id = null) | ||||
|     { | ||||
|         $path = ''; | ||||
| @@ -21,7 +19,13 @@ trait Uploads | ||||
|  | ||||
|         $path = $this->getMediaFolder($folder, $company_id); | ||||
|  | ||||
|         return MediaUploader::makePrivate()->fromSource($file)->toDirectory($path)->upload(); | ||||
|         return MediaUploader::makePrivate() | ||||
|                             ->beforeSave(function(MediaModel $media) { | ||||
|                                 $media->company_id = company_id(); | ||||
|                             }) | ||||
|                             ->fromSource($file) | ||||
|                             ->toDirectory($path) | ||||
|                             ->upload(); | ||||
|     } | ||||
|  | ||||
|     public function importMedia($file, $folder = 'settings', $company_id = null, $disk = null) | ||||
| @@ -34,7 +38,11 @@ trait Uploads | ||||
|  | ||||
|         $path = $this->getMediaFolder($folder, $company_id) . '/' . basename($file); | ||||
|  | ||||
|         return MediaUploader::makePrivate()->importPath($disk, $path); | ||||
|         return MediaUploader::makePrivate() | ||||
|                             ->beforeSave(function(MediaModel $media) { | ||||
|                                 $media->company_id = company_id(); | ||||
|                             }) | ||||
|                             ->importPath($disk, $path); | ||||
|     } | ||||
|  | ||||
|     public function deleteMediaModel($model, $parameter, $request = null) | ||||
| @@ -89,19 +97,6 @@ trait Uploads | ||||
|         $path = $media->basename; | ||||
|  | ||||
|         if (!empty($media->directory)) { | ||||
|             // 2021/04/09/34235/invoices | ||||
|             $folders = explode('/', $media->directory); | ||||
|  | ||||
|             // No company_id in folder path | ||||
|             if (empty($folders[$this->company_id_index])) { | ||||
|                 return false; | ||||
|             } | ||||
|  | ||||
|             // Check if company can access media | ||||
|             if ($folders[$this->company_id_index] != company_id()) { | ||||
|                 return false; | ||||
|             } | ||||
|  | ||||
|             $path = $media->directory . '/' . $media->basename; | ||||
|         } | ||||
|  | ||||
|   | ||||
							
								
								
									
										44
									
								
								database/migrations/2021_05_17_000000_core_v2114.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								database/migrations/2021_05_17_000000_core_v2114.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| <?php | ||||
|  | ||||
| use Illuminate\Database\Migrations\Migration; | ||||
| use Illuminate\Database\Schema\Blueprint; | ||||
| use Illuminate\Support\Facades\Schema; | ||||
|  | ||||
| class CoreV2114 extends Migration | ||||
| { | ||||
|     /** | ||||
|      * Run the migrations. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function up() | ||||
|     { | ||||
|         Schema::table('media', function (Blueprint $table) { | ||||
|             $table->unsignedInteger('company_id')->after('id'); | ||||
|  | ||||
|             $table->index('company_id'); | ||||
|         }); | ||||
|  | ||||
|         Schema::table('mediables', function (Blueprint $table) { | ||||
|             $table->unsignedInteger('company_id')->after('media_id'); | ||||
|  | ||||
|             $table->index('company_id'); | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Reverse the migrations. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function down() | ||||
|     { | ||||
|         Schema::table('media', function (Blueprint $table) { | ||||
|             $table->dropColumn('company_id'); | ||||
|         }); | ||||
|  | ||||
|         Schema::table('mediables', function (Blueprint $table) { | ||||
|             $table->dropColumn('company_id'); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user