Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use presigned urls for s3 #93

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions app/Http/Controllers/PortalsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function importUsers(Request $request)
], 401);
}

// if "deleteUsers" url parameter is set to true, all users that are not in the import anymore will be deleted
$deleteUsers = ($request->deleteUsers == 'true');

// call APP_PORTALS_URL
$client = new Client();
Expand All @@ -60,14 +62,31 @@ public function importUsers(Request $request)
// get users
$users = json_decode($body, true)['users'];

// save all removed and added users
$removedUsers = [];
$addedUsers = [];
$updatedUsers = [];
$doNotRemovePersonIds = [];

// loop through users
foreach($users as $user) {
// check if Person with id exists if not create new Person
$person = Person::firstOrNew(['id' => $user['id']]);

array_push($doNotRemovePersonIds, $user['id']);

// set attributes
if($person->id == null) {
// this could be a problem if the id is/was used by a another person that was created/imported before
// because we handle the complete user management via portals import and do not manage persons manually here, this is not a problem
$person->id = $user['id'];
array_push($addedUsers, $user['id'] . " (" . $user['email'] . ")");
} else {
if ($person->email == $user['email']) {
array_push($updatedUsers, $person->id . " (" . $person->email . ")");
} else {
array_push($updatedUsers, $person->id . " (" . $person->email . " - " . $user['email'] . ")");
}
}
$person->firstname = $user['firstname'];
$person->lastname = $user['lastname'];
Expand All @@ -93,10 +112,14 @@ public function importUsers(Request $request)
$person->course = $abbreviation;
}

// import image
$person->img = (!empty($user['avatarUrl']) ? $user['avatarUrl'] : '');
// import image path, if null set empty string
if (isset($user['avatar'])) {
$person->img = $user['avatar'];
} else {
$person->img = "";
}

// cheeck roles
// check roles
$roles = $user['roles'];

// loop through roles
Expand All @@ -116,13 +139,23 @@ public function importUsers(Request $request)

// save Person
$person->save();
}

if ($deleteUsers) {
$deletePersons = Person::whereNotIn('id', $doNotRemovePersonIds)->get();
foreach($deletePersons as $person) {
$person->delete();
array_push($removedUsers, $person->id . " (" . $person->email . ")");
}
}

// return response
return response()->json([
'message' => 'User imported',
'status' => $statusCode
'status' => $statusCode,
'addedUsers' => $addedUsers,
'removedUsers' => $removedUsers,
'updatedUsers' => $updatedUsers
], 200);
}
}
11 changes: 9 additions & 2 deletions app/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;
use NotificationChannels\Telegram\TelegramMessage;

class Person extends Model
Expand Down Expand Up @@ -47,9 +48,15 @@ class Person extends Model
*/
public function getImageAttribute()
{
if (!empty($this->img)) {
return $this->img;
// check if image is set and file exists
if (!empty($this->img) && Storage::disk('s3')->exists($this->img)) {
// generate presigned url for s3 with path stored in "img"
return Storage::disk('s3')->temporaryUrl(
$this->img,
now()->addMinutes(60)
);
} else {
// no image is set, return default image
return '/images/default.jpg';
}
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"laravel/octane": "*",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5",
"league/flysystem-aws-s3-v3": "^1.0",
"spiral/roadrunner": "^2.0"
},
"require-dev": {
Expand Down
Loading
Loading