Skip to content

Commit

Permalink
update with check self institution
Browse files Browse the repository at this point in the history
  • Loading branch information
CrysR committed Feb 3, 2025
1 parent fb4a75c commit f51cdb9
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 190 deletions.
46 changes: 44 additions & 2 deletions app/Helpers/InstitutionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,44 @@
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;

use TokenHelper;

class InstitutionHelper
{
{
// This calls the API endpoint that checks if the current self user has been allowlisted in any institution's email lists.
// Returns the institution id, access_type of the current user if set anywhere.
public static function checkSelfInst(Request $request) {
[$tok, $tokErr] = TokenHelper::GetToken($request);
if ($tok == "") {
return ["","", $tokErr];
}

$headers = [
'Authorization' => 'Bearer '.$tok,
'accept' => 'application/json',
'Cache-Control' => 'no-cache',
];

$resp = Http::withHeaders($headers)->get(env('BACKEND_URL').'/check_self');
if ($resp->getStatusCode() != 200 ) {
$errMsg = json_decode($resp->getBody());
if ($errMsg == null) {
return ["","", $resp->getStatusCode()];
}
return ["","", $resp->getStatusCode().": ".$errMsg->detail];
}

$affected = DB::table('users')
->where('email', $request->user()->email)
->update(['access_type' => $resp["access_type"], 'inst_id' => $resp["inst_id"]]);
if ($affected != 1) {
return ["", "", "Error: User table update affected ".$affected." rows. 1 affected row expected."];
}
return [$resp["inst_id"], $resp["access_type"], ""];
}

// Returns the institution id and an error if any, as a tuple.
public static function getInstitution(Request $request)
{
Expand All @@ -20,7 +55,14 @@ public static function getInstitution(Request $request)
// Datakinder with no institution has to set their institution otherwise we will return an error
return ["", "Datakinder must set an institution to proceed."];
}
return ["", "No institution set for this user."];

// Call check self in case the user is set as an allowed user for any institution.
[$inst, $access, $err] = InstitutionHelper::checkSelfInst($request);
if ($err != "") {
return ["", $err];
}

return [$inst, ""];
}

// Set the institution id for Datakinders, return an error if any.
Expand Down
Loading

0 comments on commit f51cdb9

Please sign in to comment.