diff --git a/app/Listeners/Update/V21/Version2114.php b/app/Listeners/Update/V21/Version2114.php new file mode 100644 index 000000000..712820d05 --- /dev/null +++ b/app/Listeners/Update/V21/Version2114.php @@ -0,0 +1,57 @@ +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]); + } + } +} diff --git a/app/Models/Common/Media.php b/app/Models/Common/Media.php index 334430d37..5c809e7ec 100644 --- a/app/Models/Common/Media.php +++ b/app/Models/Common/Media.php @@ -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']; } diff --git a/app/Providers/Event.php b/app/Providers/Event.php index b02015516..766c9a89a 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -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', diff --git a/app/Scopes/Company.php b/app/Scopes/Company.php index 9fc85ea79..0f27e6b53 100644 --- a/app/Scopes/Company.php +++ b/app/Scopes/Company.php @@ -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', ]; diff --git a/app/Traits/Media.php b/app/Traits/Media.php index 6f6774b24..85023fc23 100644 --- a/app/Traits/Media.php +++ b/app/Traits/Media.php @@ -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); + } } diff --git a/app/Traits/Uploads.php b/app/Traits/Uploads.php index b29659ce5..c524dd905 100644 --- a/app/Traits/Uploads.php +++ b/app/Traits/Uploads.php @@ -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; } diff --git a/database/migrations/2021_05_17_000000_core_v2114.php b/database/migrations/2021_05_17_000000_core_v2114.php new file mode 100644 index 000000000..f10d7360c --- /dev/null +++ b/database/migrations/2021_05_17_000000_core_v2114.php @@ -0,0 +1,44 @@ +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'); + }); + } +}