From b55c6f3d17443bd04af85c897ffc68b3ec3565bf Mon Sep 17 00:00:00 2001 From: Takashi Kanemoto Date: Thu, 19 Nov 2020 17:10:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20impl=20profile=20crud?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/UserController.php | 61 +++++++++++++++++++ src/Form/UserProfileEditType.php | 30 +++++++++ templates/base.html.twig | 19 +++++- templates/user/_form.html.twig | 30 +++++++++ .../user/profile_change_password.html.twig | 17 ++++++ templates/user/profile_edit.html.twig | 16 +++++ templates/user/profile_show.html.twig | 19 ++++++ translations/messages.ja.yaml | 5 ++ 8 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 src/Form/UserProfileEditType.php create mode 100644 templates/user/profile_change_password.html.twig create mode 100644 templates/user/profile_edit.html.twig create mode 100644 templates/user/profile_show.html.twig diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 2217b01..e3ad139 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -7,6 +7,7 @@ use App\Entity\User; use App\Form\UserChangePasswordType; use App\Form\UserEditType; +use App\Form\UserProfileEditType; use App\Form\UserType; use App\Repository\UserRepository; use App\Routing\ReturnToAwareControllerTrait; @@ -66,6 +67,66 @@ public function logout(): Response throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); } + /** + * @Route("/profile", name="profile_show", methods={"GET"}) + */ + public function profileShow(): Response + { + return $this->render('user/profile_show.html.twig', [ + 'user' => $this->getUser(), + ]); + } + + /** + * @Route("/profile/edit", name="profile_edit", methods={"GET", "POST"}) + */ + public function profileEdit(Request $request): Response + { + $form = $this->createForm(UserProfileEditType::class, $user = $this->getUser()); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->em->persist($user); + $this->em->flush(); + + $this->addFlash('success', 'Profile is successfully updated.'); + + return $this->redirectToRouteOrReturn('user_profile_show'); + } + + return $this->render('user/profile_edit.html.twig', [ + 'user' => $user, + 'form' => $form->createView(), + ]); + } + + /** + * @Route("/profile/change_password", name="profile_change_password", methods={"GET", "POST"}) + */ + public function profileChangePassword(Request $request): Response + { + $user = $this->getUser(); + + $form = $this->createForm(UserChangePasswordType::class); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $user->plainPassword = $form->get('newPassword')->getData(); + + $this->em->persist($user); + $this->em->flush(); + + $this->addFlash('success', 'Password is successfully updated.'); + + return $this->redirectToRouteOrReturn('user_profile_show'); + } + + return $this->render('user/profile_change_password.html.twig', [ + 'user' => $user, + 'form' => $form->createView(), + ]); + } + /** * @Route("/", name="index", methods={"GET"}) */ diff --git a/src/Form/UserProfileEditType.php b/src/Form/UserProfileEditType.php new file mode 100644 index 0000000..e7a37e1 --- /dev/null +++ b/src/Form/UserProfileEditType.php @@ -0,0 +1,30 @@ +remove('plainPassword') + ->remove('roles') + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + parent::configureOptions($resolver); + + $resolver->setDefaults([ + 'validation_groups' => [], + ]); + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index b329ba3..81166cc 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -58,7 +58,7 @@ + + {% endif %} diff --git a/templates/user/_form.html.twig b/templates/user/_form.html.twig index adaeed1..0069595 100644 --- a/templates/user/_form.html.twig +++ b/templates/user/_form.html.twig @@ -33,6 +33,26 @@ {{ form_widget(form._token) }} {{ form_end(form, {render_rest: false}) }} +{% elseif type == 'profile_edit' %} + + {{ form_start(form) }} + {{ form_row(form.email) }} +
+ + +
+ {{ form_row(form.displayName) }} +
+ + {{ 'Cancel'|trans }} +
+ {{ form_widget(form._token) }} + {{ form_end(form, {render_rest: false}) }} + {% elseif type == 'change_password' %} {{ form_start(form) }} @@ -43,4 +63,14 @@ {{ form_end(form) }} +{% elseif type == 'profile_change_password' %} + + {{ form_start(form) }} + {{ form_rest(form) }} +
+ + {{ 'Cancel'|trans }} +
+ {{ form_end(form) }} + {% endif %} diff --git a/templates/user/profile_change_password.html.twig b/templates/user/profile_change_password.html.twig new file mode 100644 index 0000000..1cb84ba --- /dev/null +++ b/templates/user/profile_change_password.html.twig @@ -0,0 +1,17 @@ +{% extends 'base.html.twig' %} + +{% block subtitle %}{{ 'Change password'|trans }}{% endblock %} + +{% block content_container %} + {% include 'widgets/breadcrumb.html.twig' with {items: [ + {label: 'Profile'|trans, path: path('user_profile_show')}, + {label: 'Edit'|trans, path: path('user_profile_edit')}, + {label: 'Change password'|trans}, + ]} %} + + {% include 'widgets/content-navbar.html.twig' with { title: block('subtitle') } %} + +
+ {% include 'user/_form.html.twig' with {type: 'profile_change_password'} %} +
+{% endblock %} diff --git a/templates/user/profile_edit.html.twig b/templates/user/profile_edit.html.twig new file mode 100644 index 0000000..86d05ff --- /dev/null +++ b/templates/user/profile_edit.html.twig @@ -0,0 +1,16 @@ +{% extends 'base.html.twig' %} + +{% block subtitle %}{{ 'Edit profile'|trans }}{% endblock %} + +{% block content_container %} + {% include 'widgets/breadcrumb.html.twig' with {items: [ + {label: 'Profile'|trans, path: path('user_profile_show')}, + {label: 'Edit'|trans}, + ]} %} + + {% include 'widgets/content-navbar.html.twig' with { title: block('subtitle') } %} + +
+ {% include 'user/_form.html.twig' with {type: 'profile_edit'} %} +
+{% endblock %} diff --git a/templates/user/profile_show.html.twig b/templates/user/profile_show.html.twig new file mode 100644 index 0000000..c3d0e3e --- /dev/null +++ b/templates/user/profile_show.html.twig @@ -0,0 +1,19 @@ +{% extends 'base.html.twig' %} + +{% block subtitle %}{{ 'Profile'|trans }}{% endblock %} + +{% block content_container %} + {% include 'widgets/breadcrumb.html.twig' with {items: [ + {label: 'Profile'|trans}, + ]} %} + + {% embed 'widgets/content-navbar.html.twig' with { title: block('subtitle') } %} + {% block nav_items %} + + {% endblock %} + {% endembed %} + + {% include 'user/_detail.html.twig' %} + {% endblock %} diff --git a/translations/messages.ja.yaml b/translations/messages.ja.yaml index 66e7e26..9a9e345 100644 --- a/translations/messages.ja.yaml +++ b/translations/messages.ja.yaml @@ -9,6 +9,9 @@ Password: パスワード Remember me: ログイン状態を記憶する Menu: メニュー +Profile: ユーザー情報 +End impersonation: 代理ログイン終了 +Logout: ログアウト User: ユーザー Id: ID @@ -22,6 +25,7 @@ Add user: ユーザー追加 User detail: ユーザー詳細 Edit user: ユーザー編集 Change password: パスワード変更 +Edit profile: ユーザー情報編集 Add: 追加 Show: 詳細 Edit: 編集 @@ -40,3 +44,4 @@ User is successfully updated.: ユーザーの編集が完了しました。 Password is successfully updated.: パスワードの変更が完了しました。 User is successfully deleted.: ユーザーの削除が完了しました。 User cannot be deleted because it owns some related contents.: そのユーザーに紐づいているデータがあるため削除できません。 +Profile is successfully updated.: ユーザー情報の編集が完了しました。