join('permissions', 'permissions.id', '=', 'album_permissions_cache.permission_id') ->where([ ['album_permissions_cache.user_id', (is_null($user) ? null : $user->id)], ['permissions.section', 'album'], ['permissions.description', $permission] ]) ->select('album_permissions_cache.album_id') ->distinct() ->get(); foreach ($query as $item) { $result[] = $item->album_id; } return $result; } public function rebuildCache() { $this->rebuildAlbumCache(); } public function userCan_Album(Album $album, User $user, $permission) { 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; } private function rebuildAlbumCache() { // 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); } }); } }