Skip to content

Commit 5bfdbbe

Browse files
New UserController added with views
1 parent 2b455f8 commit 5bfdbbe

File tree

15 files changed

+537
-16
lines changed

15 files changed

+537
-16
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{% extends 'base.html.twig' %}
1+
{% extends '::base.html.twig' %}
22
{% block main %}
3-
<div>
4-
{% block fos_user_content %}
5-
{% endblock fos_user_content %}
6-
</div>
3+
<div>
4+
{% block fos_user_content %}
5+
{% endblock fos_user_content %}
6+
</div>
77
{% endblock %}

app/Resources/views/nav_bootstrap.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
<li class="dropdown">
2828
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ 'action.profile'|trans }}<span class="caret"></span></a>
2929
<ul class="dropdown-menu">
30-
<li><a href="{{ path('fos_user_profile_show') }}">{{ 'action.show_profile'|trans }}</a></li>
31-
<li><a href="{{ path('fos_user_profile_edit') }}">{{ 'action.edit_profile'|trans }}</a></li>
30+
<li><a href="{{ path('user_profile_show', {id: app.user.id }) }}">{{ 'action.show_profile'|trans }}</a></li>
31+
<li><a href="{{ path('user_profile_edit', {id: app.user.id }) }}">{{ 'action.edit_profile'|trans }}</a></li>
3232
</ul>
3333
</li>
3434
{% else %}
3535
<li><a href="{{ path('fos_user_security_login') }}">{{ 'action.sign_in'|trans }}</a></li>
36-
<li><a href="{{ path('fos_user_registration_register') }}">{{ 'action.sign_up'|trans }}</a></li>
36+
<li><a href="{{ path('user_new') }}">{{ 'action.sign_up'|trans }}</a></li>
3737
{% endif %}
3838
{% if is_granted("ROLE_ADMIN") %}
3939
<li class="dropdown">

app/Resources/views/nav_materialize.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
<li>
2020
<a href="#">{{ 'action.profile'|trans }}<span></span></a>
2121
<ul>
22-
<li><a href="{{ path('fos_user_profile_show') }}">{{ 'action.show_profile'|trans }}</a></li>
23-
<li><a href="{{ path('fos_user_profile_edit') }}">{{ 'action.edit_profile'|trans }}</a></li>
22+
<li><a href="{{ path('user_profile_show', {id: app.user.id }) }}">{{ 'action.show_profile'|trans }}</a></li>
23+
<li><a href="{{ path('user_profile_edit', {id: app.user.id }) }}">{{ 'action.edit_profile'|trans }}</a></li>
2424
</ul>
2525
</li>
2626
{% else %}
2727
<li><a href="{{ path('fos_user_security_login') }}">{{ 'action.sign_in'|trans }}</a></li>
28-
<li><a href="{{ path('fos_user_registration_register') }}">{{ 'action.sign_up'|trans }}</a></li>
28+
<li><a href="{{ path('user_new') }}">{{ 'action.sign_up'|trans }}</a></li>
2929
{% endif %}
3030
{% if is_granted("ROLE_ADMIN") %}
3131
<li>

app/Resources/views/nav_skeleton.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
<li>
2020
<a href="#">{{ 'action.profile'|trans }}<span></span></a>
2121
<ul>
22-
<li><a href="{{ path('fos_user_profile_show') }}">{{ 'action.show_profile'|trans }}</a></li>
23-
<li><a href="{{ path('fos_user_profile_edit') }}">{{ 'action.edit_profile'|trans }}</a></li>
22+
<li><a href="{{ path('user_profile_show', {id: app.user.id }) }}">{{ 'action.show_profile'|trans }}</a></li>
23+
<li><a href="{{ path('user_profile_edit', {id: app.user.id }) }}">{{ 'action.edit_profile'|trans }}</a></li>
2424
</ul>
2525
</li>
2626
{% else %}
2727
<li><a href="{{ path('fos_user_security_login') }}">{{ 'action.sign_in'|trans }}</a></li>
28-
<li><a href="{{ path('fos_user_registration_register') }}">{{ 'action.sign_up'|trans }}</a></li>
28+
<li><a href="{{ path('user_new') }}">{{ 'action.sign_up'|trans }}</a></li>
2929
{% endif %}
3030
{% if is_granted("ROLE_ADMIN") %}
3131
<li>
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
<?php
2+
3+
namespace ApiBundle\Controller;
4+
5+
use ApiBundle\Entity\User;
6+
use ApiBundle\Form\UserType;
7+
use ApiBundle\Form\UserProfileType;
8+
9+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10+
11+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\HttpFoundation\RedirectResponse;
16+
17+
use Symfony\Component\HttpKernel\Exception\HttpException;
18+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
20+
use Symfony\Component\Security\Core\Exception\AccountStatusException;
21+
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
22+
23+
use Symfony\Component\HttpFoundation\File\File;
24+
use Symfony\Component\HttpFoundation\File\UploadedFile;
25+
26+
use FOS\UserBundle\Model\UserInterface;
27+
28+
/**
29+
* Controller used to manage user contents in the backend.
30+
*
31+
* @Route("/user")
32+
*
33+
* @author Amarendra Kumar Sinha <[email protected]>
34+
*/
35+
class UserController extends Controller
36+
{
37+
/**
38+
* Lists all User entities.
39+
*
40+
* @Route("/", name="user_index")
41+
* @Method("GET")
42+
*/
43+
public function indexAction()
44+
{
45+
return $this->redirectToRoute('homepage');
46+
}
47+
48+
/**
49+
* Creates a new User entity.
50+
*
51+
* @Route("/new", name="user_new")
52+
* @Method({"GET", "POST"})
53+
*/
54+
public function newAction(Request $request)
55+
{
56+
$confirmationEnabled = $this->container->getParameter('registration_requires_email_confirmation'); $userManager = $this->container->get('fos_user.user_manager');
57+
58+
$user = $userManager->createUser();
59+
$user->setRoles(['ROLE_USER']);
60+
61+
$form = $this->createForm(UserType::class, $user);
62+
63+
$locale = $request->getLocale();
64+
65+
$form->handleRequest($request);
66+
67+
if ($form->isSubmitted() && $form->isValid()) {
68+
// $file stores the uploaded Image file
69+
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
70+
$file = $user->getImage();
71+
72+
// If a file has been uploaded
73+
if ( null != $file ) {
74+
// Generate a unique name for the file before saving it
75+
$fileName = md5(uniqid()).'.'.$file->guessExtension();
76+
77+
// Move the file to the directory where images are stored
78+
$file->move($this->getParameter('images_profile_directory'), $fileName );
79+
80+
// Update the 'image' property to store the Image file name
81+
// instead of its contents
82+
$user->setImage($fileName);
83+
}
84+
85+
$this->setUserData($user, $form);
86+
87+
$userManager->updateUser($user);
88+
89+
$authUser = false;
90+
if ($confirmationEnabled) {
91+
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
92+
$route = 'fos_user_registration_check_email';
93+
} else {
94+
$authUser = true;
95+
$route = 'fos_user_registration_confirmed';
96+
}
97+
98+
$this->logMessageAndFlash(200, 'success', 'User successfully created: ', $this->get('translator')->trans('flash.user_created_successfully'), $request->getLocale() );
99+
$url = $this->container->get('router')->generate($route);
100+
$response = new RedirectResponse($url);
101+
102+
if ($authUser) {
103+
$this->authenticateUser($user, $response);
104+
}
105+
106+
return $response;
107+
}
108+
109+
return $this->render('@ApiBundle/Resources/views/user/new.html.twig', [
110+
'form' => $form->createView(),
111+
'attr' => array('enctype' => 'multipart/form-data'),
112+
]);
113+
}
114+
115+
/**
116+
* Authenticate a user with Symfony Security
117+
*
118+
* @param \FOS\UserBundle\Model\UserInterface $user
119+
* @param \Symfony\Component\HttpFoundation\Response $response
120+
*/
121+
protected function authenticateUser(UserInterface $user, Response $response)
122+
{
123+
try {
124+
$this->container->get('fos_user.security.login_manager')->loginUser(
125+
$this->container->getParameter('fos_user.firewall_name'),
126+
$user,
127+
$response);
128+
} catch (AccountStatusException $ex) {
129+
// We simply do not authenticate users which do not pass the user
130+
// checker (not enabled, expired, etc.).
131+
}
132+
}
133+
134+
/**
135+
* Finds and displays a User entity.
136+
*
137+
* @Route("/profile-show/{id}", name="user_profile_show")
138+
* @Method("GET")
139+
*/
140+
public function showAction(User $user)
141+
{
142+
return $this->render('@ApiBundle/Resources/views/user/show.html.twig', [
143+
'user' => $user
144+
]);
145+
}
146+
147+
/**
148+
* Displays a form to edit an existing User entity.
149+
*
150+
* @Route("/profile-edit/{id}", name="user_profile_edit")
151+
* @Method({"GET", "POST"})
152+
*/
153+
public function editAction(User $user, Request $request)
154+
{
155+
$entityManager = $this->getDoctrine()->getManager();
156+
157+
$currentFilename = $user->getImage();
158+
if ($user->getImage()) {
159+
$user->setImage(
160+
new File($this->getParameter('images_profile_directory').'/'.$currentFilename)
161+
);
162+
}
163+
164+
$editForm = $this->createForm(UserProfileType::class, $user);
165+
166+
$locale = $request->getLocale();
167+
168+
$editForm->handleRequest($request);
169+
170+
if ($editForm->isSubmitted() && $editForm->isValid()) {
171+
// $file stores the uploaded Image file
172+
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
173+
$file = $user->getImage();
174+
175+
// If a file has been uploaded
176+
if ( null != $file ) {
177+
// Generate a unique name for the file before saving it
178+
$fileName = md5(uniqid()).'.'.$file->guessExtension();
179+
180+
// Move the file to the directory where images are stored
181+
$file->move($this->getParameter('images_profile_directory'), $fileName );
182+
183+
// Update the 'image' property to store the Image file name
184+
// instead of its contents
185+
$user->setImage($fileName);
186+
} else {
187+
$user->setImage($currentFilename);
188+
}
189+
190+
$this->setUserProfileData($user, $editForm);
191+
192+
$entityManager = $this->getDoctrine()->getManager();
193+
$entityManager->flush();
194+
195+
$this->logMessageAndFlash(200, 'success', 'User successfully updated: ', $this->get('translator')->trans('flash.user_updated_successfully'), $request->getLocale() );
196+
197+
$route = 'user_profile_show';
198+
$url = $this->container->get('router')->generate($route, array('id' => $user->getId()));
199+
$response = new RedirectResponse($url);
200+
201+
return $response;
202+
}
203+
204+
return $this->render('@ApiBundle/Resources/views/user/edit.html.twig', [
205+
'user' => $user,
206+
'current_image' => $currentFilename,
207+
'edit_form' => $editForm->createView(),
208+
'attr' => array('enctype' => 'multipart/form-data'),
209+
]);
210+
}
211+
212+
private function setUserData(User $user, \Symfony\Component\Form\Form $form)
213+
{
214+
$user->setFirstname($form['firstname']->getData());
215+
$user->setLastname($form['lastname']->getData());
216+
$user->setDob($form['dob']->getData());
217+
$user->setEmail($form['email']->getData());
218+
$user->setUsername($form['username']->getData());
219+
$user->setPlainPassword($form['plainPassword']->getData());
220+
$user->setRoles($form['roles']->getData());
221+
$user->setConfirmationToken(null);
222+
$user->setEnabled(true);
223+
$user->setLastLogin(new \DateTime());
224+
}
225+
226+
private function setUserProfileData(User $user, \Symfony\Component\Form\Form $form)
227+
{
228+
$user->setFirstname($form['firstname']->getData());
229+
$user->setLastname($form['lastname']->getData());
230+
$user->setDob($form['dob']->getData());
231+
$user->setConfirmationToken(null);
232+
$user->setEnabled(true);
233+
}
234+
235+
private function logMessageAndFlash($code = 200, $type = 'success', $logMsg = '', $flashMsg = '', $locale = 'en')
236+
{
237+
$this->logMessage($code, $type, $logMsg);
238+
$this->addFlash($type, $flashMsg);
239+
}
240+
241+
private function logMessage($code = 200, $type='success', $logMsg = '') {
242+
$logger = $this->get('logger');
243+
244+
if($type === 'success'){
245+
$logger->info($code . ' ' . $logMsg);
246+
} else if($type === 'warning'){
247+
$logger->warning($code . ' ' . $logMsg);
248+
}
249+
else if($type === 'danger'){
250+
$logger->error($code . ' ' . $logMsg);
251+
}
252+
}
253+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace ApiBundle\Form;
4+
5+
use ApiBundle\Entity\User;
6+
7+
use Symfony\Component\Form\AbstractType;
8+
use Symfony\Component\Form\Extension\Core\Type\FileType;
9+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
10+
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
11+
use Symfony\Component\Form\Extension\Core\Type\TextType;
12+
use Symfony\Component\Form\Extension\Core\Type\DateType;
13+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
14+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15+
16+
use Symfony\Component\Form\FormBuilderInterface;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
19+
class UserProfileType extends AbstractType
20+
{
21+
/**
22+
* @param FormBuilderInterface $builder
23+
* @param array $options
24+
*/
25+
public function buildForm(FormBuilderInterface $builder, array $options)
26+
{
27+
$builder
28+
->add('image', FileType::class, array('data_class' => null,'label' => 'Image, if any ', 'required' => false) )
29+
->add('firstname',TextType::class)
30+
->add('lastname',TextType::class, array('required' => false))
31+
->add('dob', DateType::class, array('widget' => 'single_text', 'format' => 'M/d/y'))
32+
->add('email', EmailType::class)
33+
->add('username', TextType::class)
34+
;
35+
}
36+
37+
/**
38+
* @param OptionsResolver $resolver
39+
*/
40+
public function configureOptions(OptionsResolver $resolver)
41+
{
42+
$resolver->setDefaults(array(
43+
'data_class' => 'ApiBundle\Entity\User',
44+
'csrf_protection' => true,
45+
'intention' => 'profile',
46+
'validation_groups' => array('Profile')
47+
));
48+
}
49+
50+
public function getName()
51+
{
52+
return 'user_profile';
53+
}
54+
}

0 commit comments

Comments
 (0)