settings (including profile picture) work now

This commit is contained in:
overflowerror 2021-01-18 17:40:45 +01:00
parent 06a2406e55
commit 517d7ffe79
9 changed files with 377 additions and 290 deletions

View file

@ -43,7 +43,8 @@
"symfony/web-link": "5.2.*",
"symfony/yaml": "5.2.*",
"twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0"
"twig/twig": "^2.12|^3.0",
"ext-gd": "*"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4",

133
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "a08b90e42235f47d768d0ce3aba9a85e",
"content-hash": "508f4e2b22beb09cae7a4c6fc08865f3",
"packages": [
{
"name": "alchemy/binary-driver",
@ -70,16 +70,16 @@
},
{
"name": "amphp/amp",
"version": "v2.5.1",
"version": "v2.5.2",
"source": {
"type": "git",
"url": "https://github.com/amphp/amp.git",
"reference": "ecdc3c476b3ccff02f8e5d5bcc04f7ccfd18751c"
"reference": "efca2b32a7580087adb8aabbff6be1dc1bb924a9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/amphp/amp/zipball/ecdc3c476b3ccff02f8e5d5bcc04f7ccfd18751c",
"reference": "ecdc3c476b3ccff02f8e5d5bcc04f7ccfd18751c",
"url": "https://api.github.com/repos/amphp/amp/zipball/efca2b32a7580087adb8aabbff6be1dc1bb924a9",
"reference": "efca2b32a7580087adb8aabbff6be1dc1bb924a9",
"shasum": ""
},
"require": {
@ -150,7 +150,7 @@
"type": "github"
}
],
"time": "2020-11-03T16:23:45+00:00"
"time": "2021-01-10T17:06:37+00:00"
},
{
"name": "amphp/byte-stream",
@ -2434,16 +2434,16 @@
},
{
"name": "friendsofphp/proxy-manager-lts",
"version": "v1.0.2",
"version": "v1.0.3",
"source": {
"type": "git",
"url": "https://github.com/FriendsOfPHP/proxy-manager-lts.git",
"reference": "4a66e4e0d3279d3bb3722963b4294331fabe15bc"
"reference": "121af47c9aee9c03031bdeca3fac0540f59aa5c3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/FriendsOfPHP/proxy-manager-lts/zipball/4a66e4e0d3279d3bb3722963b4294331fabe15bc",
"reference": "4a66e4e0d3279d3bb3722963b4294331fabe15bc",
"url": "https://api.github.com/repos/FriendsOfPHP/proxy-manager-lts/zipball/121af47c9aee9c03031bdeca3fac0540f59aa5c3",
"reference": "121af47c9aee9c03031bdeca3fac0540f59aa5c3",
"shasum": ""
},
"require": {
@ -2508,7 +2508,7 @@
"type": "tidelift"
}
],
"time": "2021-01-04T11:21:26+00:00"
"time": "2021-01-14T21:52:44+00:00"
},
{
"name": "kelunik/certificate",
@ -6204,16 +6204,16 @@
},
{
"name": "symfony/polyfill-intl-grapheme",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-intl-grapheme.git",
"reference": "c7cf3f858ec7d70b89559d6e6eb1f7c2517d479c"
"reference": "267a9adeb8ecb8071040a740930e077cdfb987af"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/c7cf3f858ec7d70b89559d6e6eb1f7c2517d479c",
"reference": "c7cf3f858ec7d70b89559d6e6eb1f7c2517d479c",
"url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/267a9adeb8ecb8071040a740930e077cdfb987af",
"reference": "267a9adeb8ecb8071040a740930e077cdfb987af",
"shasum": ""
},
"require": {
@ -6225,7 +6225,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6278,33 +6278,32 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/polyfill-intl-icu",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-intl-icu.git",
"reference": "c44d5bf6a75eed79555c6bf37505c6d39559353e"
"reference": "b2b1e732a6c039f1a3ea3414b3379a2433e183d6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-intl-icu/zipball/c44d5bf6a75eed79555c6bf37505c6d39559353e",
"reference": "c44d5bf6a75eed79555c6bf37505c6d39559353e",
"url": "https://api.github.com/repos/symfony/polyfill-intl-icu/zipball/b2b1e732a6c039f1a3ea3414b3379a2433e183d6",
"reference": "b2b1e732a6c039f1a3ea3414b3379a2433e183d6",
"shasum": ""
},
"require": {
"php": ">=7.1",
"symfony/intl": "~2.3|~3.0|~4.0|~5.0"
"php": ">=7.1"
},
"suggest": {
"ext-intl": "For best performance"
"ext-intl": "For best performance and support of other locales than \"en\""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6314,6 +6313,15 @@
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Intl\\Icu\\": ""
},
"classmap": [
"Resources/stubs"
],
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
@ -6354,20 +6362,20 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/polyfill-intl-idn",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-intl-idn.git",
"reference": "3b75acd829741c768bc8b1f84eb33265e7cc5117"
"reference": "0eb8293dbbcd6ef6bf81404c9ce7d95bcdf34f44"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/3b75acd829741c768bc8b1f84eb33265e7cc5117",
"reference": "3b75acd829741c768bc8b1f84eb33265e7cc5117",
"url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/0eb8293dbbcd6ef6bf81404c9ce7d95bcdf34f44",
"reference": "0eb8293dbbcd6ef6bf81404c9ce7d95bcdf34f44",
"shasum": ""
},
"require": {
@ -6381,7 +6389,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6438,20 +6446,20 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/polyfill-intl-normalizer",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-intl-normalizer.git",
"reference": "727d1096295d807c309fb01a851577302394c897"
"reference": "6e971c891537eb617a00bb07a43d182a6915faba"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/727d1096295d807c309fb01a851577302394c897",
"reference": "727d1096295d807c309fb01a851577302394c897",
"url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/6e971c891537eb617a00bb07a43d182a6915faba",
"reference": "6e971c891537eb617a00bb07a43d182a6915faba",
"shasum": ""
},
"require": {
@ -6463,7 +6471,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6519,20 +6527,20 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T17:09:11+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531"
"reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/39d483bdf39be819deabf04ec872eb0b2410b531",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/f377a3dd1fde44d37b9831d68dc8dea3ffd28e13",
"reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13",
"shasum": ""
},
"require": {
@ -6544,7 +6552,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6596,20 +6604,20 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/polyfill-php73",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php73.git",
"reference": "8ff431c517be11c78c48a39a66d37431e26a6bed"
"reference": "a678b42e92f86eca04b7fa4c0f6f19d097fb69e2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/8ff431c517be11c78c48a39a66d37431e26a6bed",
"reference": "8ff431c517be11c78c48a39a66d37431e26a6bed",
"url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/a678b42e92f86eca04b7fa4c0f6f19d097fb69e2",
"reference": "a678b42e92f86eca04b7fa4c0f6f19d097fb69e2",
"shasum": ""
},
"require": {
@ -6618,7 +6626,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6672,20 +6680,20 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/polyfill-php80",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php80.git",
"reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de"
"reference": "dc3063ba22c2a1fd2f45ed856374d79114998f91"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/e70aa8b064c5b72d3df2abd5ab1e90464ad009de",
"reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/dc3063ba22c2a1fd2f45ed856374d79114998f91",
"reference": "dc3063ba22c2a1fd2f45ed856374d79114998f91",
"shasum": ""
},
"require": {
@ -6694,7 +6702,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -6752,7 +6760,7 @@
"type": "tidelift"
}
],
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/process",
@ -9303,16 +9311,16 @@
},
{
"name": "symfony/maker-bundle",
"version": "v1.26.1",
"version": "v1.28.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/maker-bundle.git",
"reference": "0f1d3ed2584349dc8700d7908e8a92b3742b1c99"
"reference": "6f4d27a68c92179c124f5331a27e32d197c9bd59"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/maker-bundle/zipball/0f1d3ed2584349dc8700d7908e8a92b3742b1c99",
"reference": "0f1d3ed2584349dc8700d7908e8a92b3742b1c99",
"url": "https://api.github.com/repos/symfony/maker-bundle/zipball/6f4d27a68c92179c124f5331a27e32d197c9bd59",
"reference": "6f4d27a68c92179c124f5331a27e32d197c9bd59",
"shasum": ""
},
"require": {
@ -9383,7 +9391,7 @@
"type": "tidelift"
}
],
"time": "2020-12-18T17:08:39+00:00"
"time": "2021-01-15T18:19:20+00:00"
},
{
"name": "symfony/phpunit-bridge",
@ -9549,7 +9557,8 @@
"platform": {
"php": ">=7.2.5",
"ext-ctype": "*",
"ext-iconv": "*"
"ext-iconv": "*",
"ext-gd": "*"
},
"platform-dev": [],
"plugin-api-version": "1.1.0"

View file

@ -1,3 +1,5 @@
twig:
default_path: '%kernel.project_dir%/templates'
form_themes: [ 'bootstrap_4_layout.html.twig' ]
globals:
uuidMapper: '@App\Mapper\CustomUuidMapper'

View file

@ -10,6 +10,7 @@ use App\Mapper\CustomUuidMapper;
use App\Service\UserService;
use Doctrine\DBAL\Types\ConversionException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -20,8 +21,10 @@ use Symfony\Component\Routing\Annotation\Route;
class UserController extends AbstractController
{
private const USER_RELATIVE = "../";
private const USER_DIRECTORY = "content/users/";
public const USER_RELATIVE = "../";
public const USER_DIRECTORY = "content/users/";
public const PROFILE_PICTURE_FILE = "/profile.jpg";
public const DELETE_USER_CSRF_TOKEN_ID = "delete-user";
@ -38,16 +41,22 @@ class UserController extends AbstractController
}
/**
* @Route("/user/{username}/picture", name="app_user_profile_picture")
* @Route("/user/{id}/picture", name="app_user_profile_picture")
*/
public function userProfilePicture($username): Response
public function userProfilePicture($id): Response
{
$user = $this->userService->getUserByEmail($username);
try {
$id = $this->uuidMapper->fromString($id);
} catch (ConversionException $e) {
throw new NotFoundHttpException();
}
$user = $this->userService->get($id);
if (!$user) {
throw new NotFoundHttpException();
}
$file = self::USER_RELATIVE . self::USER_DIRECTORY . $user->getId() . "/profile.png";
$file = self::USER_RELATIVE . self::USER_DIRECTORY . $user->getId() . self::PROFILE_PICTURE_FILE;
if (file_exists($file)) {
return new BinaryFileResponse($file);
@ -218,7 +227,8 @@ class UserController extends AbstractController
$form = $this->createForm(UserType::class, $user, [
"password_optional" => true,
"roles" => false
"roles" => false,
"profile_picture" => true,
]);
$okay = false;
@ -231,9 +241,19 @@ class UserController extends AbstractController
$user->setRoles($roles);
$this->userService->update($user);
$okay = true;
$file = $form->get("file")->getData();
if ($file) {
if (!$this->userService->setProfilePicture($user, $file)) {
$form->addError(new FormError("Error while processing profile picture."));
$okay = false;
}
}
if ($okay) {
$this->userService->update($user);
}
}
return $this->render("user/settings.html.twig", [

View file

@ -8,16 +8,39 @@ use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, $options): void
{
if ($options["profile_picture"]) {
$builder->add("file", FileType::class, [
"label" => "Profile Picture",
"mapped" => false,
"required" => false,
"constraints" => [
new File([
"maxSize" => "10Mi",
"mimeTypes" => [
"image/jpeg",
/*"image/gif",
"image/png",
"image/tiff",
"image/webp"*/
]
])
]
]);
}
$builder
->add("name", TextType::class)
->add("email", EmailType::class);
@ -46,6 +69,7 @@ class UserType extends AbstractType
"data_class" => User::class,
"password_optional" => false,
"roles" => true,
"profile_picture" => false
]);
}

View file

@ -4,9 +4,11 @@
namespace App\Service;
use App\Controller\UserController;
use App\Entity\User;
use App\Repository\UserRepository;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security;
@ -75,4 +77,33 @@ class UserService
$this->userRepository->update($user);
}
public function setProfilePicture(User $user, UploadedFile $file): bool
{
$file->move(UserController::USER_RELATIVE . UserController::USER_DIRECTORY . $user->getId(), "/tmp.jpg");
$image = imagecreatefromjpeg(UserController::USER_RELATIVE . UserController::USER_DIRECTORY . $user->getId() . "/tmp.jpg");
$width = imagesx($image);
$height = imagesy($image);
$size = min($width, $height);
$cropped = imagecrop($image, [
"x" => ($width - $size) / 2,
"y" => ($height - $size) / 2,
"width" => $size,
"height" => $size
]);
imagedestroy($image);
unlink(UserController::USER_RELATIVE . UserController::USER_DIRECTORY . $user->getId() . "/tmp.jpg");
if ($cropped !== false) {
$result = imagejpeg($cropped, UserController::USER_RELATIVE . UserController::USER_DIRECTORY . $user->getId() . UserController::PROFILE_PICTURE_FILE);
imagedestroy($cropped);
return $result;
} else {
return false;
}
}
}

View file

@ -70,7 +70,7 @@
>
<img
src="{{ is_granted('ROLE_USER')
? path("app_user_profile_picture", {"username": app.user.username})
? path("app_user_profile_picture", {"id": uuidMapper.toString(app.user.id)})
: asset("images/user.png") }}"
class="rounded-circle avatar"
alt=""
@ -78,7 +78,7 @@
</a>
<ul class="dropdown-menu dropdown-menu-end avatar-dropdown"
aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Settings</a></li>
<li><a class="dropdown-item" href="{{ path("app_settings") }}">Settings</a></li>
<li><a class="dropdown-item" href="{{ path("app_logout") }}">Logout</a></li>
</ul>
</li>

View file

@ -37,7 +37,7 @@
</td>
<td>
<img
src="{{ path("app_user_profile_picture", {"username": user.username}) }}"
src="{{ path("app_user_profile_picture", {"id": user.customId}) }}"
class="rounded-circle avatar"
alt=""
/>

View file

@ -69,7 +69,7 @@
<div id="info">
<div class="user">
<img
src="{{ path("app_user_profile_picture", {"username": video.uploader.username}) }}"
src="{{ path("app_user_profile_picture", {"id": video.uploader.id}) }}"
class="rounded-circle avatar"
alt=""
/>