Skip to content

Commit

Permalink
Add company_id column to media tables
Browse files Browse the repository at this point in the history
  • Loading branch information
burakcakirel committed May 17, 2021
1 parent bdb51a2 commit d13e41f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 20 deletions.
57 changes: 57 additions & 0 deletions app/Listeners/Update/V21/Version2114.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
5 changes: 3 additions & 2 deletions app/Models/Common/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
1 change: 1 addition & 0 deletions app/Providers/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion app/Scopes/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function apply(Builder $builder, Model $model)

// 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',
];

Expand Down
22 changes: 22 additions & 0 deletions app/Traits/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ public function 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);
}
}
29 changes: 12 additions & 17 deletions app/Traits/Uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

trait Uploads
{
public $company_id_index = 3;

public function getMedia($file, $folder = 'settings', $company_id = null)
{
$path = '';
Expand All @@ -21,7 +19,13 @@ public function getMedia($file, $folder = 'settings', $company_id = null)

$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)
Expand All @@ -34,7 +38,11 @@ public function importMedia($file, $folder = 'settings', $company_id = null, $di

$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)
Expand Down Expand Up @@ -89,19 +97,6 @@ public function getMediaPathOnStorage($media)
$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;
}

Expand Down
44 changes: 44 additions & 0 deletions database/migrations/2021_05_17_000000_core_v2114.php
Original file line number Diff line number Diff line change
@@ -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');
});
}
}

0 comments on commit d13e41f

Please sign in to comment.