Skip to content

Commit

Permalink
Implement portals import (#77)
Browse files Browse the repository at this point in the history
* 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
TitusKirch and simonostendorf authored Oct 24, 2023
1 parent 1d4902d commit 5310d48
Show file tree
Hide file tree
Showing 16 changed files with 653 additions and 75 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_VPN_IP=127.0.0.1
APP_IS_VPN=false

APP_PORTALS_URL=http://127.0.0.1:8000
APP_PORTALS_API_SECRET=secret
APP_PORTALS_IMPORT_PW=123

TELEGRAM_BOT_TOKEN=
TELEGRAM_BOT_INFO_CHANNEL_ID=
Expand Down
128 changes: 128 additions & 0 deletions app/Http/Controllers/PortalsController.php
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);
}
}
7 changes: 1 addition & 6 deletions app/Http/Middleware/Vpn.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ class Vpn
public function handle(Request $request, Closure $next)
{
// skip if not in production
if (app()->environment() !== 'production') {
return $next($request);
}


if (!$_SERVER['HTTP_X_REAL_IP'] || $_SERVER['HTTP_X_REAL_IP'] != env('APP_VPN_IP')) {
if (!env('APP_IS_VPN')) {
return Redirect::route('error');
}

Expand Down
9 changes: 2 additions & 7 deletions app/Http/Middleware/VpnOrPerson.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ class VpnOrPerson
public function handle(Request $request, Closure $next)
{
// skip if not in production
if (app()->environment() !== 'production') {
return $next($request);
}

if (!$_SERVER['HTTP_X_REAL_IP'] || ($_SERVER['HTTP_X_REAL_IP'] != env('APP_VPN_IP') &&
($request->session()->missing('authToken') ||
!Person::where('auth_token', $request->session()->get('authToken'))->count()))
if (!env('APP_IS_VPN') && ($request->session()->missing('authToken') ||
!Person::where('auth_token', $request->session()->get('authToken'))->count())
) {
return Redirect::route('error');
}
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Person extends Model
*/
public function getImageAttribute()
{
if (!empty($this->img) && file_exists(public_path() . '/images/' . $this->img)) {
return '/images/' . $this->img;
if (!empty($this->img)) {
return $this->img;
} else {
return '/images/default.jpg';
}
Expand Down
2 changes: 1 addition & 1 deletion app/Utils/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function sendMessage(string $channelID, string $message = null, Re
// parse output
$output = '';
if ($request->ip()) {
$output .= '*IP:* `' . ($request->ip() == env('APP_VPN_IP') ? 'VPN' : $request->ip()) . '`' . PHP_EOL;
$output .= '*IP:* `' . (env('APP_IS_VPN') ? 'VPN' : $request->ip()) . '`' . PHP_EOL;
}
if ($person) {
$output .= '*Person:* ' . $person->fullname . ' (ID: `' . $person->id . '`)' . PHP_EOL;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"doctrine/dbal": "^3.7",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"inertiajs/inertia-laravel": "^0.4.4",
Expand Down
Loading

0 comments on commit 5310d48

Please sign in to comment.