Skip to content

Commit

Permalink
Merge pull request #88 from fsr5-fhaachen/dev
Browse files Browse the repository at this point in the history
merge dev to main
  • Loading branch information
simonostendorf authored Oct 30, 2023
2 parents 7fa866b + 293f677 commit f9355dc
Show file tree
Hide file tree
Showing 26 changed files with 2,261 additions and 1,567 deletions.
35 changes: 35 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// https://aka.ms/devcontainer.json
{
"name": "Laravel Sail",
"dockerComposeFile": [
"../docker-compose.yml"
],
"service": "laravel.test",
"workspaceFolder": "/var/www/html",
"customizations": {
"vscode": {
"extensions": [
"bmewburn.vscode-intelephense-client",
"bradlc.vscode-tailwindcss",
"christian-kohler.path-intellisense",
"esbenp.prettier-vscode",
"gruntfuggly.todo-tree",
"junstyle.php-cs-fixer",
"MehediDracula.php-namespace-resolver",
"mike-co.import-sorter",
"mikestead.dotenv",
"ms-azuretools.vscode-docker",
"neilbrayfield.php-docblocker",
"steoates.autoimport",
"Vue.volar",
"xdebug.php-debug"
],
"settings": {}
}
},
"remoteUser": "sail",
"postCreateCommand": "chown -R 1000:1000 /var/www/html"
// "forwardPorts": [],
// "runServices": [],
// "shutdownAction": "none",
}
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_VPN_IP=127.0.0.1
APP_PUBLIC_URL=http://localhost
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
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ RUN chown www-data:www-data -R /var/www/html
# install packages for php
#RUN apk add --no-cache bzip2-dev curl-dev libxml2-dev enchant-2

RUN apk add libpq-dev

# install php extensions
RUN docker-php-ext-install bcmath sockets pdo_mysql
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
RUN docker-php-ext-install bcmath sockets pdo_mysql pdo pdo_pgsql pgsql pcntl
# ctype \
# json \
# mbstring \
Expand All @@ -19,7 +22,7 @@ RUN docker-php-ext-install bcmath sockets pdo_mysql
# tokenizer \
# xml

RUN apk add --no-cache pcre-dev $PHPIZE_DEPS && pecl install redis && docker-php-ext-enable redis.so
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS && pecl install redis-5.3.7 && docker-php-ext-enable redis.so

# install composer
RUN EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
Expand All @@ -42,7 +45,7 @@ RUN npm install
# RUN npm run production

# install roadrunner
COPY --from=spiralscout/roadrunner:latest /usr/bin/rr /usr/bin/rr
COPY --from=spiralscout/roadrunner:2023.3.2 /usr/bin/rr /usr/bin/rr

# configure roadrunner
RUN php artisan octane:install --server=roadrunner
Expand All @@ -54,4 +57,4 @@ EXPOSE 8000

CMD php artisan octane:start --server="roadrunner" --host="0.0.0.0" --workers=${ROADRUNNER_WORKERS} --max-requests=${ROADRUNNER_MAX_REQUESTS}

HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD php artisan octane:status --server="roadrunner"
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD php artisan octane:status --server="roadrunner"
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ and the vite dev server:
npm run dev
```

### devcontainer

If you want to use the provided devcontainer via laravel sail you need vscode and the devcontainer extension.

### testing

You can run the tests with:
Expand Down Expand Up @@ -115,7 +119,7 @@ docker exec -it strichlistensystem-web php artisan migrate:fresh --seed
- Website: https://tkirch.dev/
- LinkedIn: [Titus Kirch](https://www.linkedin.com/in/tituskirch/)
- Twitter: [@TitusKirch](https://twitter.com/TitusKirch)
- Github: [@TitusKirch](https://github.com/TitusKirch)
- GitHub: [@TitusKirch](https://github.com/TitusKirch)

👤 **Benedikt Haas (main author)**

Expand All @@ -125,7 +129,11 @@ docker exec -it strichlistensystem-web php artisan migrate:fresh --seed
👤 **Simon Ostendorf**

- LinkedIn: [Simon Ostendorf](https://www.linkedin.com/in/simonostendorf/)
- Github: [@simonostendorf](https://github.com/simonostendorf)
- GitHub: [@simonostendorf](https://github.com/simonostendorf)

👤 **Till Voss**

- GitHub: [@TiltedPhosphor](https://github.com/TiltedPhosphor)

Show here to see the full list of [contributors](https://github.com/fsr5-fhaachen/strichlistensystem/graphs/contributors) who participated in this project.

Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function createCsv(){
['person_id', '=', $person->id],
['article_id', '=', 4]
])->count();

fputcsv($file, array($row['Nachname'], $row['Vorname'], $row['Anz_Bier'], $row['Anz_Radler'], $row['Anz_Softdrink'], $row['Anz_Wasser']), ';');
$array = array($row['Nachname'], $row['Vorname'], $row['Anz_Bier'], $row['Anz_Radler'], $row['Anz_Softdrink'], $row['Anz_Wasser']);
$array = array_map("utf8_decode", $array);
fputcsv($file, $array, ';');
}

fclose($file);
};

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
12 changes: 9 additions & 3 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 Expand Up @@ -117,9 +117,15 @@ public function createAuthLink()
{
$this->generateAuthToken();

return route('person.authWithToken', [
// generate the route
$personAuthWithTokenRoute = route('person.authWithToken', [
'id' => $this->id,
'token' => $this->auth_token
]);

// get only the path
$personAuthWithTokenRoute = parse_url($personAuthWithTokenRoute, PHP_URL_PATH);

return config('app.public_url') . $personAuthWithTokenRoute;
}
}
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
3 changes: 2 additions & 1 deletion 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 All @@ -18,7 +19,7 @@
"require-dev": {
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"laravel/sail": "^1.25",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^5.10",
"phpunit/phpunit": "^9.5.8"
Expand Down
Loading

0 comments on commit f9355dc

Please sign in to comment.