Skip to content
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
1 change: 0 additions & 1 deletion app/Attachments/AttachmentWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Attachments;

use Illuminate\Http\UploadedFile;
use Livewire\Wireable;

interface AttachmentWriter extends Wireable
Expand Down
6 changes: 3 additions & 3 deletions app/Auth/Registration/Registrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Registrant

public function __construct(
private string $email,
private bool $isAdmin
private int $permissionLevel
) {
}

Expand All @@ -16,9 +16,9 @@ public function getEmail(): string
return $this->email;
}

public function isAdmin(): bool
public function getPermissionLevel(): int
{
return $this->isAdmin;
return $this->permissionLevel;
}

}
4 changes: 2 additions & 2 deletions app/Auth/Registration/RegistrationTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function createRecord(Registrant $registrant, #[\SensitiveParameter] str
{
return [
'email' => $registrant->getEmail(),
'is_admin' => $registrant->isAdmin(),
'permission_level' => $registrant->getPermissionLevel(),
'token' => Hash::make($token),
'created_at' => new Carbon
];
Expand All @@ -81,7 +81,7 @@ private function tokenExpired($createdAt)

private function readRecord($record): Registrant
{
return new Registrant($record['email'], $record['is_admin']);
return new Registrant($record['email'], $record['permission_level']);
}

public function deleteExisting(string $email): bool
Expand Down
14 changes: 14 additions & 0 deletions app/Http/Middleware/AdminAuthorization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Middleware;

use const App\Models\ADMIN_LEVEL;

class AdminAuthorization extends PermissionLevelAuthorization
{
protected function getRequiredLevel(): int
{
return ADMIN_LEVEL;
}

}
43 changes: 43 additions & 0 deletions app/Http/Middleware/BasicAuthorization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpFoundation\Response;

class BasicAuthorization
{

/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (!self::hasHeader($request) || !self::getAuthenticatedUser($request)) {
abort(Response::HTTP_UNAUTHORIZED);
}
return $next($request);
}

public static function hasHeader(Request $request): bool
{
return $request->hasHeader('Authorization');
}

public static function getAuthenticatedUser(Request $request): User|false
{
$credentials = base64_decode(substr($request->header('Authorization'), 6));
list($username, $password) = explode(':', $credentials);
$user = User::where('email', $username)->first();
if (isset($user) and Hash::check($password, $user->password)) {
return $user;
}
return false;
}

}
23 changes: 0 additions & 23 deletions app/Http/Middleware/IsAdminMiddleware.php

This file was deleted.

14 changes: 14 additions & 0 deletions app/Http/Middleware/MavenEditorAuthorization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Middleware;

use const App\Models\MAVEN_EDITOR_LEVEL;

class MavenEditorAuthorization extends PermissionLevelAuthorization
{
protected function getRequiredLevel(): int
{
return MAVEN_EDITOR_LEVEL;
}

}
31 changes: 31 additions & 0 deletions app/Http/Middleware/PermissionLevelAuthorization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class PermissionLevelAuthorization
{
protected abstract function getRequiredLevel(): int;

/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (auth()->check()) {
$user = auth()->user();
} else {
// Fallback to basic Authorization if no standard auth
$user = BasicAuthorization::getAuthenticatedUser($request);
}
if (!isset($user) or !$user->hasPermission($this->getRequiredLevel())) {
abort(Response::HTTP_FORBIDDEN);
}
return $next($request);
}
}
5 changes: 3 additions & 2 deletions app/Livewire/Auth/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Livewire\Attributes\Locked;
use Livewire\Attributes\Title;
use Livewire\Component;
use const App\Models\USER_LEVEL;

#[Title('Register')]
#[Layout('components.layouts.auth')]
Expand Down Expand Up @@ -60,8 +61,8 @@ public function register(): void
// Since the user needs a token to register, we can consider this an email verification
$user->markEmailAsVerified();

if ($registrant->isAdmin()) {
$user->is_admin = true;
if ($registrant->getPermissionLevel() > USER_LEVEL) {
$user->permission_level = $registrant->getPermissionLevel();
$user->save();
}
RegistrationTokenRepository::get()->deleteExisting($this->email);
Expand Down
9 changes: 6 additions & 3 deletions app/Livewire/Maven/DirectoryIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public function mount(?string $path = '')
{
$this->isRoot = $path == '';
$this->path = $path;
if (!Storage::directoryExists("maven$path")) {
return redirect(route('maven.download', $path));
if (!Storage::disk('maven')->exists($path)) {
abort(404);
}
$fullPath = Storage::path("maven/$path");
if (!Storage::disk('maven')->directoryExists($path)) {
return redirect(route('maven.download', substr($path, 1)));
}
$fullPath = Storage::disk('maven')->path($path);
foreach (new DirectoryIterator($fullPath) as $file) {
if ($file->isDot()) continue;
$path = $file->getRealPath();
Expand Down
1 change: 0 additions & 1 deletion app/Livewire/Posts/EditPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Post;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Title;
use Livewire\Component;
use Throwable;

Expand Down
1 change: 0 additions & 1 deletion app/Livewire/Projects/EditProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Livewire\Projects;

use App\Models\Project;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Livewire\Component;
Expand Down
9 changes: 5 additions & 4 deletions app/Livewire/Users/UserCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@

class UserCell extends Component
{

public User $user;
public bool $isAdmin;
public int $permissionLevel;

public function mount(User $user)
{
$this->user = $user;
$this->isAdmin = $user->is_admin;
$this->permissionLevel = $user->permission_level;
}

public function updateIsAdmin(): void
public function updatePermissionLevel(): void
{
$this->authorize('update', $this->user);
$this->user->is_admin = $this->isAdmin;
$this->user->permission_level = $this->permissionLevel;
$this->user->save();
}

Expand Down
6 changes: 3 additions & 3 deletions app/Livewire/Users/UserIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UserIndex extends Component
public $users;

public string $newUserEmail = '';
public bool $newUserIsAdmin = false;
public int $newUserPermissionLevel = 0;

public function mount(): void
{
Expand All @@ -29,7 +29,7 @@ public function add(): void
'newUserEmail' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class.',email']
]);

$token = RegistrationTokenRepository::get()->create(new Registrant($this->newUserEmail, $this->newUserIsAdmin));
$token = RegistrationTokenRepository::get()->create(new Registrant($this->newUserEmail, $this->newUserPermissionLevel));

Mail::to($this->newUserEmail)->send(new Registration($this->newUserEmail, $token));

Expand All @@ -38,7 +38,7 @@ public function add(): void
// Close modal and reset fields
$this->modal('addUser')->close();
$this->newUserEmail = '';
$this->newUserIsAdmin = false;
$this->newUserPermissionLevel = 0;
}

}
20 changes: 20 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;

const ADMIN_LEVEL = 2;
const MAVEN_EDITOR_LEVEL = 1;
const USER_LEVEL = 0;

const PERMISSION_LEVEL_DISPLAY_NAMES = ['User', 'Maven Editor', 'Admin'];

class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
Expand Down Expand Up @@ -58,4 +64,18 @@ public function initials(): string
->map(fn ($word) => Str::substr($word, 0, 1))
->implode('');
}

public function hasPermission(int $level) {
return $this->permission_level >= $level;
}

public function isMavenEditor(): bool
{
return $this->permission_level >= MAVEN_EDITOR_LEVEL;
}

public function isAdmin(): bool
{
return $this->permission_level == ADMIN_LEVEL;
}
}
7 changes: 3 additions & 4 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\Response;

/**
* Post policy is quite simple at the moment: any users can view any posts, and admins can use any CRUD operation on
Expand Down Expand Up @@ -33,22 +32,22 @@ public function view(User $user, Post $post): bool
*/
public function create(User $user): bool
{
return isset($user) && $user->is_admin;
return isset($user) && $user->isAdmin();
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Post $post): bool
{
return isset($user) && $user->is_admin;
return isset($user) && $user->isAdmin();
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Post $post): bool
{
return isset($user) && $user->is_admin;
return isset($user) && $user->isAdmin();
}
}
6 changes: 3 additions & 3 deletions app/Policies/ProjectPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public function view(User $user, Project $project): bool
*/
public function create(User $user): bool
{
return isset($user) && $user->is_admin;
return isset($user) && $user->isAdmin();
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Project $project): bool
{
return isset($user) && $user->is_admin;
return isset($user) && $user->isAdmin();
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Project $project): bool
{
return isset($user) && $user->is_admin;
return isset($user) && $user->isAdmin();
}
}
Loading