2018-09-14 21:03:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
use App\Album;
|
2018-09-23 22:20:03 +01:00
|
|
|
use App\AlbumDefaultAnonymousPermission;
|
|
|
|
use App\AlbumDefaultGroupPermission;
|
|
|
|
use App\AlbumDefaultUserPermission;
|
2018-09-16 08:41:36 +01:00
|
|
|
use App\Permission;
|
2018-09-14 21:03:07 +01:00
|
|
|
use App\User;
|
2018-09-16 08:41:36 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-09-14 21:03:07 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class PermissionsHelper
|
|
|
|
{
|
2018-09-16 22:11:53 +01:00
|
|
|
public function getAlbumIDs($permission = 'list', User $user = null)
|
2018-09-16 08:41:36 +01:00
|
|
|
{
|
|
|
|
$result = [];
|
2018-11-18 21:39:19 +00:00
|
|
|
|
|
|
|
// First check if the anonymous user can do what is being requested - if so, the permission would also inherit
|
|
|
|
// to logged-in users
|
|
|
|
$anonymousUsersCan = DB::table('album_permissions_cache')
|
|
|
|
->join('permissions', 'permissions.id', '=', 'album_permissions_cache.permission_id')
|
|
|
|
->where([
|
|
|
|
['album_permissions_cache.user_id', null],
|
|
|
|
['permissions.section', 'album'],
|
|
|
|
['permissions.description', $permission]
|
|
|
|
])
|
|
|
|
->select('album_permissions_cache.album_id')
|
|
|
|
->distinct()
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($anonymousUsersCan as $item)
|
|
|
|
{
|
|
|
|
$result[] = $item->album_id;
|
|
|
|
}
|
|
|
|
|
2018-09-16 08:41:36 +01:00
|
|
|
$query = DB::table('album_permissions_cache')
|
|
|
|
->join('permissions', 'permissions.id', '=', 'album_permissions_cache.permission_id')
|
|
|
|
->where([
|
2018-09-16 22:11:53 +01:00
|
|
|
['album_permissions_cache.user_id', (is_null($user) || $user->isAnonymous() ? null : $user->id)],
|
2018-09-16 08:41:36 +01:00
|
|
|
['permissions.section', 'album'],
|
|
|
|
['permissions.description', $permission]
|
|
|
|
])
|
|
|
|
->select('album_permissions_cache.album_id')
|
|
|
|
->distinct()
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($query as $item)
|
|
|
|
{
|
2018-11-18 21:39:19 +00:00
|
|
|
if (!in_array($item->album_id, $result))
|
|
|
|
{
|
|
|
|
$result[] = $item->album_id;
|
|
|
|
}
|
2018-09-16 08:41:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-09-14 21:03:07 +01:00
|
|
|
public function rebuildCache()
|
2018-09-16 08:41:36 +01:00
|
|
|
{
|
|
|
|
$this->rebuildAlbumCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function userCan_Album(Album $album, User $user, $permission)
|
|
|
|
{
|
2018-11-18 21:39:19 +00:00
|
|
|
// First check if the anonymous user can do what is being requested - if so, the permission would also inherit
|
|
|
|
// to logged-in users
|
|
|
|
$anonymousUsersCan = DB::table('album_permissions_cache')
|
|
|
|
->join('permissions', 'permissions.id', '=', 'album_permissions_cache.permission_id')
|
|
|
|
->where([
|
|
|
|
['album_permissions_cache.album_id', $album->id],
|
|
|
|
['album_permissions_cache.user_id', null],
|
|
|
|
['permissions.section', 'album'],
|
|
|
|
['permissions.description', $permission]
|
|
|
|
])
|
|
|
|
->count() > 0;
|
|
|
|
|
|
|
|
if ($anonymousUsersCan)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-16 08:41:36 +01:00
|
|
|
return DB::table('album_permissions_cache')
|
|
|
|
->join('permissions', 'permissions.id', '=', 'album_permissions_cache.permission_id')
|
|
|
|
->where([
|
|
|
|
['album_permissions_cache.album_id', $album->id],
|
|
|
|
['album_permissions_cache.user_id', (is_null($user) || $user->isAnonymous() ? null : $user->id)],
|
|
|
|
['permissions.section', 'album'],
|
|
|
|
['permissions.description', $permission]
|
|
|
|
])
|
|
|
|
->count() > 0;
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:32:50 +01:00
|
|
|
public function usersWhoCan_Album(Album $album, $permission)
|
|
|
|
{
|
|
|
|
$users = DB::table('album_permissions_cache')
|
|
|
|
->join('permissions', 'permissions.id', '=', 'album_permissions_cache.permission_id')
|
|
|
|
->where([
|
|
|
|
['album_permissions_cache.album_id', $album->id],
|
|
|
|
['permissions.section', 'album'],
|
|
|
|
['permissions.description', $permission]
|
|
|
|
])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// Include the album's owner (who can do everything)
|
|
|
|
$users->push($album->user);
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2018-09-16 08:41:36 +01:00
|
|
|
private function rebuildAlbumCache()
|
2018-09-14 21:03:07 +01:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
$defaultAlbumUserPermissions = AlbumDefaultUserPermission::all();
|
|
|
|
$defaultAlbumGroupPermissions = AlbumDefaultGroupPermission::all();
|
|
|
|
$defaultAnonPermissions = AlbumDefaultAnonymousPermission::all();
|
|
|
|
|
2018-09-14 21:03:07 +01:00
|
|
|
// 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)
|
|
|
|
{
|
2018-09-16 22:11:53 +01:00
|
|
|
$effectiveAlbumID = $album->effectiveAlbumIDForPermissions();
|
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
if ($effectiveAlbumID === 0)
|
2018-09-14 21:03:07 +01:00
|
|
|
{
|
2018-09-23 22:20:03 +01:00
|
|
|
/* Use the default permissions list */
|
2018-09-14 21:03:07 +01:00
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
foreach ($defaultAnonPermissions as $anonymousPermission)
|
|
|
|
{
|
|
|
|
$permissionsCache[] = [
|
|
|
|
'album_id' => $album->id,
|
|
|
|
'permission_id' => $anonymousPermission->permission_id,
|
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
];
|
|
|
|
}
|
2018-09-14 21:03:07 +01:00
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
foreach ($defaultAlbumUserPermissions 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()
|
|
|
|
];
|
|
|
|
}
|
2018-09-14 21:03:07 +01:00
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
foreach ($defaultAlbumGroupPermissions 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()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 21:03:07 +01:00
|
|
|
}
|
2018-09-23 22:20:03 +01:00
|
|
|
else
|
2018-09-14 21:03:07 +01:00
|
|
|
{
|
2018-09-23 22:20:03 +01:00
|
|
|
/* Use the specified album-specific permissions */
|
|
|
|
$anonymousPermissions = array_filter($albumAnonPermissions->toArray(), function ($item) use ($effectiveAlbumID)
|
|
|
|
{
|
|
|
|
return ($item->album_id == $effectiveAlbumID);
|
|
|
|
});
|
2018-09-14 21:03:07 +01:00
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
foreach ($anonymousPermissions as $anonymousPermission)
|
2018-09-14 21:03:07 +01:00
|
|
|
{
|
2018-09-23 22:20:03 +01:00
|
|
|
$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 ($effectiveAlbumID)
|
|
|
|
{
|
|
|
|
return ($item->album_id == $effectiveAlbumID);
|
2018-09-14 21:03:07 +01:00
|
|
|
});
|
|
|
|
|
2018-09-23 22:20:03 +01:00
|
|
|
foreach ($userPermissions as $userPermission)
|
2018-09-14 21:03:07 +01:00
|
|
|
{
|
|
|
|
$permissionsCache[] = [
|
2018-09-23 22:20:03 +01:00
|
|
|
'user_id' => $userPermission->user_id,
|
2018-09-14 21:03:07 +01:00
|
|
|
'album_id' => $album->id,
|
2018-09-23 22:20:03 +01:00
|
|
|
'permission_id' => $userPermission->permission_id,
|
2018-09-14 21:03:07 +01:00
|
|
|
'created_at' => new \DateTime(),
|
|
|
|
'updated_at' => new \DateTime()
|
|
|
|
];
|
|
|
|
}
|
2018-09-23 22:20:03 +01:00
|
|
|
|
|
|
|
$groupPermissions = array_filter($albumGroupPermissions->toArray(), function ($item) use ($effectiveAlbumID)
|
|
|
|
{
|
|
|
|
return ($item->album_id == $effectiveAlbumID);
|
|
|
|
});
|
|
|
|
|
|
|
|
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()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 21:03:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|