Skip to content

A self-hosted media server and file management system

authentication for pulse and analytics

aminnausina884d3442cea
diff --git a/app/Http/Controllers/Api/V1/CategoryController.php b/app/Http/Controllers/Api/V1/CategoryController.php
index 7bf720d1..b53aa1e3 100755
--- a/app/Http/Controllers/Api/V1/CategoryController.php
+++ b/app/Http/Controllers/Api/V1/CategoryController.php
@@ -17,11 +17,11 @@ class CategoryController extends Controller {
* Display a listing of the resource.
*/
public function index() {
- try {
- if (! Auth::user() || Auth::user()->id !== 1) {
- abort(403, 'Unauthorized action.');
- }
+ if (! Auth::user() || Auth::user()->id !== 1) {
+ abort(403, 'Unauthorized action.');
+ }
+ try {
return $this->success(
CategoryResource::collection(
Category::all()->sortBy('name')
@@ -49,6 +49,10 @@ class CategoryController extends Controller {
* @param int $category_id
*/
public function update(CategoryUpdateRequest $request, Category $category) {
+ if (! Auth::user() || Auth::user()->id !== 1) {
+ abort(403, 'Unauthorized action.');
+ }
+
try {
$validated = $request->validated();
diff --git a/app/Http/Controllers/Api/V1/SubTaskController.php b/app/Http/Controllers/Api/V1/SubTaskController.php
index 8b58fae6..fd41edcb 100644
--- a/app/Http/Controllers/Api/V1/SubTaskController.php
+++ b/app/Http/Controllers/Api/V1/SubTaskController.php
@@ -38,11 +38,11 @@ class SubTaskController extends Controller {
* Display the specified resource.
*/
public function show(Request $request, Task $task) {
- try {
- if (! Auth::user() || Auth::user()->id !== 1) {
- abort(403, 'Unauthorized action.');
- }
+ if (! Auth::user() || Auth::user()->id !== 1) {
+ abort(403, 'Unauthorized action.');
+ }
+ try {
return
SubTasksResource::collection(
SubTask::where('task_id', $task->id)->get()->sortBy('id')
diff --git a/app/Http/Controllers/Api/V1/TasksController.php b/app/Http/Controllers/Api/V1/TasksController.php
index f52beee1..27654e00 100644
--- a/app/Http/Controllers/Api/V1/TasksController.php
+++ b/app/Http/Controllers/Api/V1/TasksController.php
@@ -20,11 +20,11 @@ class TasksController extends Controller {
* Display a listing of the resource.
*/
public function index() {
- try {
- if (! Auth::user() || Auth::user()->id !== 1) {
- abort(403, 'Unauthorized action.');
- }
+ if (! Auth::user() || Auth::user()->id !== 1) {
+ abort(403, 'Unauthorized action.');
+ }
+ try {
return
TasksResource::collection(
Task::all()->sortByDesc('created_at')
diff --git a/app/Http/Controllers/Api/V1/UserController.php b/app/Http/Controllers/Api/V1/UserController.php
index 18e3ea77..6f98d6ce 100644
--- a/app/Http/Controllers/Api/V1/UserController.php
+++ b/app/Http/Controllers/Api/V1/UserController.php
@@ -16,11 +16,11 @@ class UserController extends Controller {
* Display a listing of the resource.
*/
public function index() {
- try {
- if (! Auth::user() || Auth::user()->id !== 1) {
- abort(403, 'Unauthorized action.');
- }
+ if (! Auth::user() || Auth::user()->id !== 1) {
+ abort(403, 'Unauthorized action.');
+ }
+ try {
return
UserResource::collection(
User::all()->sortBy('name')
@@ -45,11 +45,11 @@ class UserController extends Controller {
}
public function SessionCount() {
- try {
- if (! Auth::user() || Auth::user()->id !== 1) {
- abort(403, 'Unauthorized action.');
- }
+ if (! Auth::user() || Auth::user()->id !== 1) {
+ abort(403, 'Unauthorized action.');
+ }
+ try {
$count = DB::table('sessions')
->whereNotNull('user_id')
->count();
diff --git a/app/Http/Controllers/DirectoryController.php b/app/Http/Controllers/DirectoryController.php
index d9b7a17d..2f834061 100755
--- a/app/Http/Controllers/DirectoryController.php
+++ b/app/Http/Controllers/DirectoryController.php
@@ -43,10 +43,9 @@ class DirectoryController extends Controller {
$dir = trim(strtolower($request?->dir ?? ''));
$folderName = trim(strtolower($request?->folderName ?? ''));
- if (isset($privateCategories[$dir]) && (! $request->user('sanctum')) || (Auth::user() && Auth::user()->id !== 1)) {
- $data['message'] = 'Unauthorized';
+ if (isset($privateCategories[$dir]) && (! $request->user('sanctum') || (Auth::user() && Auth::user()->id !== 1))) {
- return $this->error(null, 'Access to this folder is forbidden', 403);
+ return $this->error(null, "Access to this folder is forbidden", 403);
}
$dirRaw = Category::select('id', 'default_folder_id')->firstWhere('name', 'ilike', '%' . $dir . '%');
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index ba2f7b2a..34c9ce93 100755
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -21,13 +21,13 @@ class AppServiceProvider extends ServiceProvider {
*/
public function boot(): void {
//
- Pulse::user(fn ($user) => [
+ Pulse::user(fn($user) => [
'name' => $user->name,
'extra' => $user->email,
]);
Gate::define('viewPulse', function (?User $user) {
- return $user?->isAdmin();
+ return $user?->id == 1;
});
LogViewer::auth(function ($request) {
diff --git a/resources/js/router/index.js b/resources/js/router/index.js
index 724fa347..06a413a1 100755
--- a/resources/js/router/index.js
+++ b/resources/js/router/index.js
@@ -81,6 +81,7 @@ const router = createRouter({
},
{
path: '/dashboard',
+ meta: { protected: true },
redirect: '/dashboard/overview',
},
{
diff --git a/resources/js/service/api.ts b/resources/js/service/api.ts
index 81e1c928..91389056 100644
--- a/resources/js/service/api.ts
+++ b/resources/js/service/api.ts
@@ -8,17 +8,15 @@ const handleResponse = (response: any) => {
const handleError = (error: { response: { status: number; data: { message: any } }; message: any }) => {
// if the server throws an error (404, 500 etc.)
console.log(error);
- if (error.response.status === 403) {
- //|| error.response.status === 500
- window.location.href = `/${error.response.status}?message=${error?.response?.data?.message ?? error?.message}`;
- return;
- }
+ // if (error.response.status === 403) {
+ // //|| error.response.status === 500
+ // window.location.href = `/${error.response.status}?message=${error?.response?.data?.message ?? error?.message}`;
+ // return;
+ // }
- toast('Error', { type: 'danger', description: error.message });
+ toast('Error', { type: 'danger', description: error.response.data.message ?? error.message });
if (error.response.status === 401 || error.response.status == 422 || error.response.status == 500 || error.response.status == 404) throw error;
- console.log(error);
-
return error.response;
};
diff --git a/resources/js/views/DashboardView.vue b/resources/js/views/DashboardView.vue
index 9bb09715..65487468 100644
--- a/resources/js/views/DashboardView.vue
+++ b/resources/js/views/DashboardView.vue
@@ -25,10 +25,12 @@ import LucideUsers from '~icons/lucide/users';
import DashboardActivity from '@/components/dashboard/DashboardActivity.vue';
import DashboardUsers from '@/components/dashboard/DashboardUsers.vue';
import DashboardTasks from '@/components/dashboard/DashboardTasks.vue';
+import { useAuthStore } from '@/stores/AuthStore';
-const { pageTitle, selectedSideBar } = storeToRefs(useAppStore());
const { stateTaskStats, stateTotalLibrariesSize } = storeToRefs(useDashboardStore()) as { stateTaskStats: Ref<TaskStatsResponse>; stateTotalLibrariesSize: Ref<string> };
+const { pageTitle, selectedSideBar } = storeToRefs(useAppStore());
const { cycleSideBar } = useAppStore();
+const { userData } = storeToRefs(useAuthStore());
const route = useRoute();
const dashboardTab = ref<{ name: string; title?: string; icon?: any }>();
@@ -40,6 +42,7 @@ const dashboardTabs = computed<
description?: string;
info?: { value: string; icon?: Component };
icon?: Component;
+ disabled?: boolean;
}[]
>(() => {
return [
@@ -54,6 +57,7 @@ const dashboardTabs = computed<
description: '',
info: { value: `Total Size: ${stateTotalLibrariesSize?.value ?? '?'}` },
icon: ProiconsLibrary,
+ disabled: userData.value?.id !== 1,
},
// {
// name: 'activity',
@@ -66,12 +70,14 @@ const dashboardTabs = computed<
description: '',
info: { value: 'Logged In: ?' },
icon: LucideUsers,
+ disabled: userData.value?.id !== 1,
},
{
name: 'tasks',
description: '',
info: { value: `Currently Running: ${stateTaskStats.value?.count_running ?? '?'}` },
icon: CircumServer,
+ disabled: userData.value?.id !== 1,
},
];
});
@@ -122,13 +128,20 @@ watch(
<SidebarCard
v-for="(tab, index) in dashboardTabs"
:key="index"
- :link="`/dashboard/${tab.name}`"
+ :link="tab.disabled ? '' : `/dashboard/${tab.name}`"
:class="`
items-center justify-between !gap-2
capitalize overflow-hidden bg-white hover:bg-primary-800
ring-inset ring-purple-600 hover:ring-purple-600/50 hover:ring-[0.125rem] ${dashboardTab?.name == tab.name && 'ring-[0.125rem]'}
+ aria-disabled:cursor-not-allowed aria-disabled:hover:ring-neutral-200 aria-disabled:hover:dark:ring-neutral-700 aria-disabled:opacity-60
`"
- @click="dashboardTab = tab"
+ @click="
+ () => {
+ if (tab.disabled) return;
+ dashboardTab = tab;
+ }
+ "
+ :aria-disabled="tab.disabled"
>
<template #header>
<h2 class="w-full flex-1" :title="tab.title ?? tab.name">{{ tab.title ?? tab.name }}</h2>
diff --git a/routes/web.php b/routes/web.php
index 13ca1a8c..70c40c4b 100755
--- a/routes/web.php
+++ b/routes/web.php
@@ -4,6 +4,8 @@ use App\Events\TaskEnded;
use App\Http\Controllers\DirectoryController;
use App\Http\Controllers\MediaController;
use App\Models\Task;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
@@ -38,12 +40,14 @@ Route::get('/broadcast', function () {
broadcast(new TaskEnded($task));
});
-Route::middleware(['web'])
- ->group(function () {
- Route::get('/pulse', function () {
+Route::middleware(['web'])->group(function () {
+ Route::get('/pulse', function () {
+ if (Gate::allows('viewPulse', Auth::user())) {
return view('vendor.pulse.dashboard');
- });
+ }
+ abort(403);
});
+});
Route::get('/welcome', function () {
return view('welcome');