Updated varible name in apt middleware and created a config file for api keys env varibles

This commit is contained in:
Anna-Sara Sélea 2025-11-20 07:48:40 +01:00
parent 5000e589b8
commit daeef2c731
6 changed files with 75 additions and 48 deletions

View file

@ -15,7 +15,7 @@ class ParticipantExporter extends Exporter
public static function getColumns(): array public static function getColumns(): array
{ {
return [ return [
ExportColumn::make('participant_id'), ExportColumn::make('lan_id'),
ExportColumn::make('first_name'), ExportColumn::make('first_name'),
ExportColumn::make('surname'), ExportColumn::make('surname'),
ExportColumn::make('grade'), ExportColumn::make('grade'),

View file

@ -14,49 +14,59 @@ class ParticipantController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$ability = $request->ability; $permission = $request->permission;
if ($ability === "key_1") { if ($permission === "key_1") {
$participants = Participant::all()->makeHidden(['comment', 'emailed', 'paid', 'member', 'gdpr']); $participants = Participant::all()->makeHidden(['comment', 'emailed', 'paid', 'member', 'gdpr']);
$volunteers = Volunteer::all()->makeHidden(['gdpr']); $volunteers = Volunteer::all()->makeHidden(['gdpr', 'emailed']);
$dataArr = [ $dataArr = [
'participant' => $participants, 'code' => 200,
'volunteer' => $volunteers 'participants' => $participants,
'volunteers' => $volunteers
]; ];
return $dataArr; return $dataArr;
} }
if ($ability === "key_2") { if ($permission === "key_2") {
$participants = Participant::all()->select('participant_id', 'first_name', 'surname'); $participants = Participant::all()->select('participant_id', 'first_name', 'surname');
$volunteers = Volunteer::all()->select('first_name', 'surname'); $volunteers = Volunteer::all()->select('first_name', 'surname');
$dataArr = [ $dataArr = [
'participant' => $participants, 'code' => 200,
'volunteer' => $volunteers 'participants' => $participants,
'volunteers' => $volunteers
]; ];
return $dataArr; return $dataArr;
} }
if ($ability === "key_3") { if ($permission === "key_3") {
$participants = Participant::all()->makeHidden(['comment', 'emailed', 'paid', 'member', 'gdpr']); $participants = Participant::all()->makeHidden(['comment', 'emailed', 'paid', 'member', 'gdpr']);
return $participants; return $dataArr = [
'code' => 200,
'participants' => $participants,
];
} }
if ($ability === "key_4") { if ($permission === "key_4") {
$participants = Participant::all()->select('participant_id', 'first_name', 'surname'); $participants = Participant::all()->select('participant_id', 'first_name', 'surname');
return $participants; return $dataArr = [
'code' => 200,
'participants' => $participants,
];
} }
return false; return response()->json([
'code' => 401, 'message' => 'Unauthorized'
]);
} }
/** /**
@ -72,13 +82,12 @@ class ParticipantController extends Controller
*/ */
public function store(Request $request) public function store(Request $request)
{ {
$ability = $request->ability; $permission = $request->permission;
if ($ability === "key_1") { if ($permission === "key_1") {
$request->validate([ $request->validate([
'member' => 'required',
'first_name' => 'required', 'first_name' => 'required',
'surname' => 'required', 'surname' => 'required',
'grade' => 'required', 'grade' => 'required',
@ -87,20 +96,20 @@ class ParticipantController extends Controller
'guardian_name' => 'required', 'guardian_name' => 'required',
'guardian_phone' => 'required', 'guardian_phone' => 'required',
'guardian_email' => 'required', 'guardian_email' => 'required',
'visiting' => 'required', 'is_visiting' => 'required',
'gdpr' => 'required', 'gdpr' => 'required',
'friends' => 'nullable', 'friends' => 'nullable',
'special_diet' => 'nullable', 'special_diet' => 'nullable',
]); ]);
$count = Participant::where('visiting', 0)->count(); $count = Participant::where('is_visiting', 0)->count();
$status = ""; $status = "";
if ($count < 2 && $request->visiting === 0) { if ($count < 2 && $request->is_visiting === 0) {
$status = "lan"; $status = "lan";
} }
else if ($request->visiting === 1) { else if ($request->is_visiting === 1) {
$status = "besök"; $status = "besök";
} }
@ -109,7 +118,7 @@ class ParticipantController extends Controller
} }
Participant::create([ Participant::create([
'member' => $request->member, 'member' => 1,
'first_name' => $request->first_name, 'first_name' => $request->first_name,
'surname' => $request->surname, 'surname' => $request->surname,
'grade' => $request->grade, 'grade' => $request->grade,
@ -118,7 +127,7 @@ class ParticipantController extends Controller
'guardian_name' => $request->guardian_name, 'guardian_name' => $request->guardian_name,
'guardian_phone' => $request->guardian_phone, 'guardian_phone' => $request->guardian_phone,
'guardian_email' => $request->guardian_email, 'guardian_email' => $request->guardian_email,
'visiting' => $request->visiting, 'is_visiting' => $request->is_visiting,
'gdpr' => $request->gdpr, 'gdpr' => $request->gdpr,
'friends' => $request->friends, 'friends' => $request->friends,
'special_diet' => $request->special_diet, 'special_diet' => $request->special_diet,
@ -127,13 +136,13 @@ class ParticipantController extends Controller
return response()->json([ return response()->json([
'success' => true, 'message' => 'Participant was created successfully' 'code' => 200, 'message' => 'Participant was created successfully'
]); ]);
} }
return response()->json([ return response()->json([
'success' => false, 'message' => 'Unauthorized' 'code' => 200, 'message' => 'Unauthorized'
]); ]);
} }

View file

@ -12,55 +12,55 @@ class VersionController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$ability = $request->ability; $permission = $request->permission;
if ($ability === "key_1") { if ($permission === "key_1") {
$latest_version_participants = Version::where('table', 'participants')->latest()->first(); $latest_version_participants = Version::where('table', 'participants')->latest()->first();
$latest_version_volunteers = Version::where('table', 'volunteers')->latest()->first(); $latest_version_volunteers = Version::where('table', 'volunteers')->latest()->first();
return response()->json([ return response()->json([
'success' => true, 'participants' => $latest_version_participants ? $latest_version_participants->version : null , 'volunteers' => $latest_version_volunteers ? $latest_version_volunteers->version : null 'code' => 200, 'participants' => $latest_version_participants ? $latest_version_participants->version : null , 'volunteers' => $latest_version_volunteers ? $latest_version_volunteers->version : null
]); ]);
} }
if ($ability === "key_2") { if ($permission === "key_2") {
$latest_version_participants = Version::where('table', 'participants')->latest()->first(); $latest_version_participants = Version::where('table', 'participants')->latest()->first();
$latest_version_volunteers = Version::where('table', 'volunteers')->latest()->first(); $latest_version_volunteers = Version::where('table', 'volunteers')->latest()->first();
return response()->json([ return response()->json([
'success' => true, 'participants' => $latest_version_participants ? $latest_version_participants->version : null , 'volunteers' => $latest_version_volunteers ? $latest_version_volunteers->version : null 'code' => 200, 'participants' => $latest_version_participants ? $latest_version_participants->version : null , 'volunteers' => $latest_version_volunteers ? $latest_version_volunteers->version : null
]); ]);
} }
if ($ability === "key_3") { if ($permission === "key_3") {
$latest_version_participants = Version::where('table', 'participants')->latest()->first(); $latest_version_participants = Version::where('table', 'participants')->latest()->first();
return response()->json([ return response()->json([
'success' => true, 'participants' => $latest_version_participants ? $latest_version_participants->version : null 'code' => 200, 'participants' => $latest_version_participants ? $latest_version_participants->version : null
]); ]);
} }
if ($ability === "key_4") { if ($permission === "key_4") {
$latest_version_participants = Version::where('table', 'participants')->latest()->first(); $latest_version_participants = Version::where('table', 'participants')->latest()->first();
return response()->json([ return response()->json([
'success' => true, 'participants' => $latest_version_participants ? $latest_version_participants->version : null 'code' => 200, 'participants' => $latest_version_participants ? $latest_version_participants->version : null
]); ]);
} }
return response()->json([ return response()->json([
'success' => false, 'message' => 'Unauthorized' 'code' => 401, 'message' => 'Unauthorized'
]); ]);
} }

View file

@ -12,9 +12,9 @@ class VolunteerController extends Controller
*/ */
public function store(Request $request) public function store(Request $request)
{ {
$ability = $request->ability; $permission = $request->permission;
if ($ability === "key_1") { if ($permission === "key_1") {
$request->validate([ $request->validate([
@ -38,13 +38,13 @@ class VolunteerController extends Controller
return response()->json([ return response()->json([
'success' => true, 'message' => 'Volunteer was created successfully' 'code' => 200, 'message' => 'Volunteer was created successfully'
]); ]);
} }
return response()->json([ return response()->json([
'success' => false, 'message' => 'Unauthorized' 'code' => 401, 'message' => 'Unauthorized'
]); ]);
} }

View file

@ -15,24 +15,24 @@ class ApiToken
*/ */
public function handle(Request $request, Closure $next): Response public function handle(Request $request, Closure $next): Response
{ {
if ($request->api_token === env('API_KEY_1')) { if ($request->api_token === config('apikeys.key_1')) {
$request->merge(["ability" => "key_1"]); $request->merge(["permission" => "key_1"]);
} elseif ($request->api_token === env('API_KEY_2')) { } elseif ($request->api_token === config('apikeys.key_2')) {
$request->merge(["ability" => "key_2"]); $request->merge(["permission" => "key_2"]);
} elseif ($request->api_token === env('API_KEY_3')) { } elseif ($request->api_token === config('apikeys.key_3')) {
$request->merge(["ability" => "key_3"]); $request->merge(["permission" => "key_3"]);
} elseif ($request->api_token === env('API_KEY_4')) { } elseif ($request->api_token === config('apikeys.key_4')) {
$request->merge(["ability" => "key_4"]); $request->merge(["permission" => "key_4"]);
} else { } else {
return response()->json('Unauthorized', 401); return response()->json(['code' => 401, 'message' => 'Unauthorized']);
} }
return $next($request); return $next($request);
} }

18
config/apikeys.php Normal file
View file

@ -0,0 +1,18 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| API KEYS
|--------------------------------------------------------------------------
|
|
*/
'key_1' => env('API_KEY_1'),
'key_2' => env('API_KEY_2'),
'key_3' => env('API_KEY_3'),
'key_4' => env('API_KEY_4'),
];