#71: The rebuildPermissionsCache controller method now calls a new helper class, PermissionsHelper, that rebuilds the permissions in the new album_permissions_cache DB table
This commit is contained in:
parent
c5ccc4ef9a
commit
835a3e611b
102
app/Helpers/PermissionsHelper.php
Normal file
102
app/Helpers/PermissionsHelper.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Album;
|
||||
use App\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PermissionsHelper
|
||||
{
|
||||
public function rebuildCache()
|
||||
{
|
||||
// Get a list of albums
|
||||
$albums = Album::all();
|
||||
|
||||
// Get a list of all configured permissions
|
||||
$albumUserPermissions = DB::table('album_user_permissions')->get();
|
||||
$albumGroupPermissions = DB::table('album_group_permissions')->get();
|
||||
$albumAnonPermissions = DB::table('album_anonymous_permissions')->get();
|
||||
|
||||
// Get a list of all user->group memberships
|
||||
$userGroups = DB::table('user_groups')->get();
|
||||
|
||||
// Build a matrix of new permissions
|
||||
$permissionsCache = [];
|
||||
|
||||
/** @var Album $album */
|
||||
foreach ($albums as $album)
|
||||
{
|
||||
$anonymousPermissions = array_filter($albumAnonPermissions->toArray(), function($item) use ($album)
|
||||
{
|
||||
return ($item->album_id == $album->id);
|
||||
});
|
||||
|
||||
foreach ($anonymousPermissions as $anonymousPermission)
|
||||
{
|
||||
$permissionsCache[] = [
|
||||
'album_id' => $album->id,
|
||||
'permission_id' => $anonymousPermission->permission_id,
|
||||
'created_at' => new \DateTime(),
|
||||
'updated_at' => new \DateTime()
|
||||
];
|
||||
}
|
||||
|
||||
$userPermissions = array_filter($albumUserPermissions->toArray(), function($item) use ($album)
|
||||
{
|
||||
return ($item->album_id == $album->id);
|
||||
});
|
||||
|
||||
foreach ($userPermissions as $userPermission)
|
||||
{
|
||||
$permissionsCache[] = [
|
||||
'user_id' => $userPermission->user_id,
|
||||
'album_id' => $album->id,
|
||||
'permission_id' => $userPermission->permission_id,
|
||||
'created_at' => new \DateTime(),
|
||||
'updated_at' => new \DateTime()
|
||||
];
|
||||
}
|
||||
|
||||
$groupPermissions = array_filter($albumGroupPermissions->toArray(), function($item) use ($album)
|
||||
{
|
||||
return ($item->album_id == $album->id);
|
||||
});
|
||||
|
||||
foreach ($groupPermissions as $groupPermission)
|
||||
{
|
||||
// Get a list of users in this group, and add one per user
|
||||
$usersInGroup = array_filter($userGroups->toArray(), function($item) use ($groupPermission)
|
||||
{
|
||||
return $item->group_id = $groupPermission->group_id;
|
||||
});
|
||||
|
||||
foreach ($usersInGroup as $userGroup)
|
||||
{
|
||||
$permissionsCache[] = [
|
||||
'user_id' => $userGroup->user_id,
|
||||
'album_id' => $album->id,
|
||||
'permission_id' => $groupPermission->permission_id,
|
||||
'created_at' => new \DateTime(),
|
||||
'updated_at' => new \DateTime()
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->savePermissionsCache($permissionsCache);
|
||||
}
|
||||
|
||||
private function savePermissionsCache(array $cacheToSave)
|
||||
{
|
||||
DB::transaction(function() use ($cacheToSave)
|
||||
{
|
||||
DB::table('album_permissions_cache')->truncate();
|
||||
|
||||
foreach ($cacheToSave as $cacheItem)
|
||||
{
|
||||
DB::table('album_permissions_cache')->insert($cacheItem);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ use App\Group;
|
||||
use App\Helpers\ConfigHelper;
|
||||
use App\Helpers\DbHelper;
|
||||
use App\Helpers\MiscHelper;
|
||||
use App\Helpers\PermissionsHelper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\SaveSettingsRequest;
|
||||
use App\Label;
|
||||
@ -218,6 +219,9 @@ class DefaultController extends Controller
|
||||
|
||||
public function rebuildPermissionsCache()
|
||||
{
|
||||
$helper = new PermissionsHelper();
|
||||
$helper->rebuildCache();
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
|
@ -14,9 +14,10 @@ class CreateAlbumPermissionsCacheTable extends Migration
|
||||
public function up()
|
||||
{
|
||||
Schema::create('album_permissions_cache', function (Blueprint $table) {
|
||||
$table->unsignedInteger('user_id');
|
||||
$table->unsignedInteger('user_id')->nullable(true);
|
||||
$table->unsignedInteger('album_id');
|
||||
$table->unsignedInteger('permission_id');
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('user_id')
|
||||
->references('id')->on('users')
|
||||
|
Loading…
Reference in New Issue
Block a user