Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to disable login requirements on albums #2480

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions app/Http/Middleware/LoginRequired.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\ConfigurationException;
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Internal\FrameworkException;
use App\Exceptions\Internal\LycheeLogicException;
use App\Models\Configs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand All @@ -19,32 +20,45 @@
*/
class LoginRequired
{
public const ROOT = 'root';
public const ALBUM = 'album';

/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
*
* @return mixed
* @param Request $request the incoming request to serve
* @param \Closure $next the next operation to be applied to the
* request
* @param string $requiredStatus the required login status; either
* {@link self::ROOT} or
* {@link self::ALBUM}
*
* @throws ConfigurationException
* @throws FrameworkException
*/
public function handle(Request $request, \Closure $next): mixed
public function handle(Request $request, \Closure $next, string $requiredStatus): mixed
{
// We are logged in. Proceed.
if (Auth::user() !== null) {
return $next($request);
}

$dir_url = config('app.dir_url');
if (Features::inactive('livewire') && !Str::startsWith($request->getRequestUri(), $dir_url . '/livewire/')) {
return $next($request);
}

if ($requiredStatus !== self::ALBUM && $requiredStatus !== self::ROOT) {
throw new LycheeLogicException($requiredStatus . ' is not a valid login requirement.');
}

try {
if (!Configs::getValueAsBool('login_required')) {
// Login is not required. Proceed.
return $next($request);
}

if (Auth::user() !== null) {
// We are logged in. Proceed.
if ($requiredStatus === self::ALBUM && Configs::getValueAsBool('login_required_root_only')) {
return $next($request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\Models\Extensions\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';

public function getConfigs(): array
{
return [
[
'key' => 'login_required_root_only',
'value' => '1',
'is_secret' => false,
'cat' => self::GALLERY,
'type_range' => self::BOOL,
'description' => 'Require user to login only on root. A user with a direct link to an album can still access it.',
],
];
}
};
12 changes: 6 additions & 6 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
/**
* ALBUMS.
*/
Route::post('/Albums::get', [AlbumsController::class, 'get'])->middleware(['login_required']);
Route::post('/Albums::getPositionData', [AlbumsController::class, 'getPositionData'])->middleware(['login_required']);
Route::post('/Albums::tree', [AlbumsController::class, 'tree'])->middleware(['login_required']);
Route::post('/Albums::get', [AlbumsController::class, 'get'])->middleware(['login_required:root']);
Route::post('/Albums::getPositionData', [AlbumsController::class, 'getPositionData'])->middleware(['login_required:root']);
Route::post('/Albums::tree', [AlbumsController::class, 'tree'])->middleware(['login_required:root']);

/**
* ALBUM.
*/
Route::post('/Album::get', [AlbumController::class, 'get'])->middleware(['login_required']);
Route::post('/Album::get', [AlbumController::class, 'get'])->middleware(['login_required:album']);
Route::post('/Album::getPositionData', [AlbumController::class, 'getPositionData']);
Route::post('/Album::unlock', [AlbumController::class, 'unlock']);
Route::post('/Album::add', [AlbumController::class, 'add']);
Expand Down Expand Up @@ -68,8 +68,8 @@
/**
* PHOTO.
*/
Route::post('/Photo::get', [PhotoController::class, 'get'])->middleware(['login_required']);
Route::post('/Photo::getRandom', [PhotoController::class, 'getRandom'])->middleware(['login_required']);
Route::post('/Photo::get', [PhotoController::class, 'get'])->middleware(['login_required:album']);
Route::post('/Photo::getRandom', [PhotoController::class, 'getRandom']);
Route::post('/Photo::setTitle', [PhotoController::class, 'setTitle']);
Route::post('/Photo::setDescription', [PhotoController::class, 'setDescription']);
Route::post('/Photo::setStar', [PhotoController::class, 'setStar']);
Expand Down
4 changes: 2 additions & 2 deletions routes/web-livewire.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
Route::get('/jobs', Jobs::class)->name('jobs');
Route::get('/maintenance', Maintenance::class)->name('maintenance');

Route::middleware(['login_required'])
Route::middleware(['login_required:album'])
->group(function () {
Route::get('/map/{albumId?}', Map::class)->name('livewire-map');
Route::get('/frame/{albumId?}', Frame::class)->name('livewire-frame');
Expand All @@ -58,7 +58,7 @@
Route::prefix(Features::when('livewire', '', 'livewire'))
->group(function () {
Route::get('/landing', Landing::class)->name('landing');
Route::get('/gallery', Albums::class)->middleware(['login_required'])->name('livewire-gallery');
Route::get('/gallery', Albums::class)->middleware(['login_required:root'])->name('livewire-gallery');
Route::get('/', [RedirectController::class, 'view'])->name('livewire-index');
});
});
Loading