-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement portals import * Update img field (add dependencie to change cols) * Save image by portals * Use S3 image * Add portal env vars * Fix test to new feature * Fix casee of import * feat: add devcontainer (#75) * Implement portals import * Update composer file * fix(app/PortalsController): remove merge conflict marks --------- Co-authored-by: Simon Ostendorf <[email protected]>
- Loading branch information
1 parent
1d4902d
commit 5310d48
Showing
16 changed files
with
653 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Person; | ||
use GuzzleHttp\Client; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
|
||
class PortalsController extends Controller | ||
{ | ||
/** | ||
* Validate request | ||
* | ||
* @param string $password | ||
* @return boolean | ||
*/ | ||
public function validateRequest(string $password){ | ||
return $password == env('APP_PORTALS_IMPORT_PW'); | ||
} | ||
|
||
/** | ||
* validate auth token | ||
* | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function importUsers(Request $request) | ||
{ | ||
if (!$this->validateRequest($request->password)) { | ||
return response()->json([ | ||
'message' => 'Unauthorized' | ||
], 401); | ||
} | ||
|
||
|
||
// call APP_PORTALS_URL | ||
$client = new Client(); | ||
$response = $client->request('GET', env('APP_PORTALS_URL') . '/api/v1/users', [ | ||
'headers' => [ | ||
'Authorization' => env('APP_PORTALS_API_SECRET') | ||
] | ||
]); | ||
|
||
// get status code | ||
$statusCode = $response->getStatusCode(); | ||
|
||
if($statusCode != 200) { | ||
return response()->json([ | ||
'message' => 'Unauthorized', | ||
'status' => $statusCode | ||
], 401); | ||
} | ||
|
||
// get body | ||
$body = $response->getBody()->getContents(); | ||
|
||
// get users | ||
$users = json_decode($body, true)['users']; | ||
|
||
// loop through users | ||
foreach($users as $user) { | ||
// check if Person with id exists if not create new Person | ||
$person = Person::firstOrNew(['id' => $user['id']]); | ||
|
||
// set attributes | ||
if($person->id == null) { | ||
$person->id = $user['id']; | ||
} | ||
$person->firstname = $user['firstname']; | ||
$person->lastname = $user['lastname']; | ||
$person->email = $user['email']; | ||
|
||
// check if course is set | ||
if(isset($user['course'])) { | ||
$abbreviation = strtoupper($user['course']['abbreviation']); | ||
|
||
// add fallbacks for other courses if abbreviation is not INF, ET, WI, DIB or MCD | ||
if(!in_array($abbreviation, ['INF', 'ET', 'WI', 'DIB', 'MCD'])) { | ||
if($abbreviation == 'SBE') { | ||
$abbreviation = 'ET'; | ||
} else if($abbreviation == 'ET-MASTER') { | ||
$abbreviation = 'ET'; | ||
} else if($abbreviation == 'ISE-MASTER') { | ||
$abbreviation = 'INF'; | ||
} else { | ||
$abbreviation = 'INF'; | ||
} | ||
} | ||
|
||
$person->course = $abbreviation; | ||
} | ||
|
||
// import image | ||
$person->img = (!empty($user['avatarUrl']) ? $user['avatarUrl'] : ''); | ||
|
||
// cheeck roles | ||
$roles = $user['roles']; | ||
|
||
// loop through roles | ||
foreach($roles as $role) { | ||
// check if role is tutor | ||
if($role['name'] == 'tutor') { | ||
$person->is_tutor = true; | ||
} | ||
// check if role is special | ||
if($role['name'] == 'special') { | ||
$person->is_special = true; | ||
} | ||
} | ||
|
||
// set is_disabled | ||
$person->is_disabled = $user['is_disabled']; | ||
|
||
// save Person | ||
$person->save(); | ||
|
||
} | ||
|
||
// return response | ||
return response()->json([ | ||
'message' => 'User imported', | ||
'status' => $statusCode | ||
], 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.