Skip to content

Commit 954ada4

Browse files
Profile pic further updations
1 parent 5bfdbbe commit 954ada4

File tree

10 files changed

+67
-43
lines changed

10 files changed

+67
-43
lines changed

src/ApiBundle/Controller/Admin/UserController.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ApiBundle\Entity\User;
66
use ApiBundle\Form\UserType;
7+
use ApiBundle\Form\UserProfileType;
78

89
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
910
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@@ -15,6 +16,8 @@
1516
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
1617
use Symfony\Component\HttpFoundation\File\File;
1718
use Symfony\Component\HttpFoundation\File\UploadedFile;
19+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
20+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1821

1922
/**
2023
* Controller used to manage user contents in the backend.
@@ -53,10 +56,22 @@ public function newAction(Request $request)
5356
{
5457
$userManager = $this->container->get('fos_user.user_manager');
5558
$user = $userManager->createUser();
56-
$user->setRoles(['ROLE_USER']);
5759

5860
$form = $this->createForm(UserType::class, $user);
5961

62+
// Role added in admin area
63+
$form->add('roles', CollectionType::class, array(
64+
'entry_type' => ChoiceType::class,
65+
'entry_options' => array(
66+
'label' => false,
67+
'choices' => array(
68+
'ROLE_ADMIN' => 'ROLE_ADMIN',
69+
'ROLE_USER' => 'ROLE_USER',
70+
'ROLE_API' => 'ROLE_API',
71+
),
72+
),
73+
));
74+
6075
$locale = $request->getLocale();
6176

6277
$form->handleRequest($request);
@@ -127,10 +142,23 @@ public function editAction(User $user, Request $request)
127142
);
128143
}
129144

130-
$editForm = $this->createForm(UserType::class, $user);
145+
$editForm = $this->createForm(UserProfileType::class, $user);
131146
$deleteForm = $this->createDeleteForm($user);
132147
$locale = $request->getLocale();
133148

149+
// Role added in admin area
150+
$editForm->add('roles', CollectionType::class, array(
151+
'entry_type' => ChoiceType::class,
152+
'entry_options' => array(
153+
'label' => false,
154+
'choices' => array(
155+
'ROLE_ADMIN' => 'ROLE_ADMIN',
156+
'ROLE_USER' => 'ROLE_USER',
157+
'ROLE_API' => 'ROLE_API',
158+
),
159+
),
160+
));
161+
134162
$editForm->handleRequest($request);
135163

136164
if ($editForm->isSubmitted() && $editForm->isValid()) {
@@ -153,7 +181,7 @@ public function editAction(User $user, Request $request)
153181
$user->setImage($currentFilename);
154182
}
155183

156-
$this->setUserData($user, $editForm);
184+
$this->setUserProfileData($user, $editForm);
157185

158186
$entityManager = $this->getDoctrine()->getManager();
159187
$entityManager->flush();
@@ -221,6 +249,17 @@ private function setUserData(User $user, \Symfony\Component\Form\Form $form)
221249
$user->setLastLogin(new \DateTime());
222250
}
223251

252+
private function setUserProfileData(User $user, \Symfony\Component\Form\Form $form)
253+
{
254+
$user->setFirstname($form['firstname']->getData());
255+
$user->setLastname($form['lastname']->getData());
256+
$user->setDob($form['dob']->getData());
257+
$user->setEmail($form['email']->getData());
258+
$user->setUsername($form['username']->getData());
259+
$user->setRoles($form['roles']->getData());
260+
}
261+
262+
224263
private function logMessageAndFlash($code = 200, $type = 'success', $logMsg = '', $flashMsg = '', $locale = 'en')
225264
{
226265
$this->logMessage($code, $type, $logMsg);

src/ApiBundle/Controller/AuthController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ public function newClientAction()
172172
* {"name"="lastname", "dataType"="string", "required"=true, "description"="lastname"},
173173
* {"name"="dob", "dataType"="datetime", "required"=true, "description"="date of birth mm/dd/yyyy"},
174174
* {"name"="email", "dataType"="email", "required"=true, "description"="Email"},
175-
* {"name"="email_confirmation", "dataType"="integer", "required"=true, "description"="0-email confirmation not required, 1-required"},
176175
* {"name"="image", "dataType"="image/jpeg, image/jpg, image/gif, image/png", "required"=false, "description"="Profile Picture within 1024k size"},
177176
* {"name"="_locale", "dataType"="string", "required"=false, "description"="User locale. Will default to en"}
178177
* },
179178
* )
180179
*/
181180
public function postRegisterAction(Request $request)
182181
{
182+
$confirmationEnabled = $this->container->getParameter('registration_requires_email_confirmation');
183183
$request = $this->container->get('request');
184184

185185
$userManager = $this->get('fos_user.user_manager');
@@ -208,7 +208,7 @@ public function postRegisterAction(Request $request)
208208
$msg = 'N.A.';
209209
$grantType = 'password';
210210

211-
if ('1' == $request->request->get('email_confirmation')) {
211+
if (true == $confirmationEnabled ) {
212212
$msg = 'Please check your email to complete the registration.';
213213
} else {
214214
$msg = 'Registration complete. Welcome!';
@@ -451,7 +451,7 @@ public function editProfilePicAction()
451451
$userManager = $this->get('fos_user.user_manager');
452452
$userManager->updateUser($user);
453453

454-
$msg = 'Profile Pic updated successfully'.$user->getUsername();
454+
$msg = 'Profile Pic updated successfully. '.$user->getUsername();
455455
$this->logMessage(201, $msg);
456456

457457
return new JsonResponse(array(
@@ -544,7 +544,7 @@ public function postAccessTokenAction()
544544

545545
$oAuthRtn = $this->fetchAccessToken($request, $grantType);
546546

547-
$msg = 'Access Token successfully fetched for '.$username;
547+
$msg = 'Access Token successfully fetched for '.$data['username'];
548548
$this->logMessage(201, $msg);
549549

550550
$oAuthRtn['code'] = 201;
@@ -747,7 +747,7 @@ private function setUserData(Request $request, User $user)
747747

748748
// Update the 'image' property to store the Image file name
749749
// instead of its contents
750-
$user->setImage('/images/profile/'.$fileName);
750+
$user->setImage($this->getParameter('images_profile_directory').$fileName);
751751
}
752752

753753
$user->setUsername($request->request->get('username'));
@@ -783,7 +783,7 @@ private function setUserPicData(Request $request, User $user)
783783

784784
// Update the 'image' property to store the Image file name
785785
// instead of its contents
786-
$user->setImage($fileName);
786+
$user->setImage($this->getParameter('images_profile_directory').$fileName);
787787
}
788788
}
789789

src/ApiBundle/Controller/UserController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function indexAction()
5353
*/
5454
public function newAction(Request $request)
5555
{
56-
$confirmationEnabled = $this->container->getParameter('registration_requires_email_confirmation'); $userManager = $this->container->get('fos_user.user_manager');
56+
$confirmationEnabled = $this->container->getParameter('registration_requires_email_confirmation');
57+
$userManager = $this->container->get('fos_user.user_manager');
5758

5859
$user = $userManager->createUser();
5960
$user->setRoles(['ROLE_USER']);
@@ -128,6 +129,7 @@ protected function authenticateUser(UserInterface $user, Response $response)
128129
} catch (AccountStatusException $ex) {
129130
// We simply do not authenticate users which do not pass the user
130131
// checker (not enabled, expired, etc.).
132+
$this->logMessageAndFlash(200, 'warning', 'User Authentication failed: '.$user->getUsername(), $this->get('translator')->trans('flash.user_authentication_failed'), $request->getLocale() );
131133
}
132134
}
133135

@@ -217,7 +219,12 @@ private function setUserData(User $user, \Symfony\Component\Form\Form $form)
217219
$user->setEmail($form['email']->getData());
218220
$user->setUsername($form['username']->getData());
219221
$user->setPlainPassword($form['plainPassword']->getData());
220-
$user->setRoles($form['roles']->getData());
222+
223+
// If Roles exist in form as the form is common for both admin and user areas
224+
// Only admin area is allowed to have roles
225+
$roles = array_key_exists('roles', $form) ? $form['roles']->getData() : $user->getRoles();
226+
$user->setRoles($roles);
227+
221228
$user->setConfirmationToken(null);
222229
$user->setEnabled(true);
223230
$user->setLastLogin(new \DateTime());

src/ApiBundle/Form/UserType.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3939
'second_options' => array('label' => 'form.password_confirmation'),
4040
'invalid_message' => 'fos_user.password.mismatch',
4141
))
42-
->add('roles', CollectionType::class, array(
43-
'entry_type' => ChoiceType::class,
44-
'entry_options' => array(
45-
'label' => false,
46-
'choices' => array(
47-
'ROLE_ADMIN' => 'ROLE_ADMIN',
48-
'ROLE_USER' => 'ROLE_USER',
49-
'ROLE_API' => 'ROLE_API',
50-
),
51-
),
52-
));
42+
;
5343
}
5444

5545
/**

src/ApiBundle/Resources/config/config.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ parameters:
22
# This parameter defines the codes of the locales (languages) enabled in the application
33
# app_locales: en|fr|de|es|cs|nl|ru|uk|ro|pt_BR|pl|it|ja|id|ca|sl
44
app_locales: en|fr|hi
5-
images_profile_directory: '%kernel.root_dir%/../web/images/profile'
65
registration_requires_email_confirmation: false
7-
login_confirmation: false
6+
images_profile_directory: '%kernel.root_dir%/../web/images/profile/'
87

98
# Assetic Configuration
109
assetic:
@@ -19,9 +18,9 @@ fos_rest:
1918
routing_loader:
2019
default_format: json # All responses should be JSON formated
2120
include_format: false # We do not include format in request, so that all responses
22-
exception:
23-
enabled: true
24-
exception_controller: 'ApiBundle\Controller\ApiExceptionController::showAction' # will eventually be JSON formated
21+
# exception:
22+
# enabled: true
23+
# exception_controller: 'ApiBundle\Controller\ApiExceptionController::showAction' # will eventually be JSON formated
2524
versioning:
2625
enabled: true
2726
resolvers:

src/ApiBundle/Resources/translations/messages.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ flash:
126126
user_created_successfully: User created successfully!
127127
user_updated_successfully: User updated successfully!
128128
user_deleted_successfully: User deleted successfully!
129+
user_authentication_failed: User Authentication failed!
129130
api:
130131
show_error_client_name: Sorry, Client Name cannot be empty!
131132
show_error_client_name_taken: Sorry, Client Name already taken!

src/ApiBundle/Resources/translations/messages.fr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ flash:
114114
user_created_successfully: Utilisateur créé avec succès!
115115
user_updated_successfully: Utilisateur mis à jour avec succès!
116116
user_deleted_successfully: L'utilisateur a été supprimé avec succès!
117+
user_authentication_failed: L'identification de l'utilisateur a échoué!
117118
api:
118119
show_error_client_name: Désolé, le nom du client ne peut pas être vide!
119120
show_error_client_name_taken: Désolé, Nom du client déjà pris!

src/ApiBundle/Resources/translations/messages.hi.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ flash:
114114
user_created_successfully: प्रयोक्ता सफलतापूर्वक बनाया!
115115
user_updated_successfully: प्रयोक्ता को सफलतापूर्वक अद्यतन!
116116
user_deleted_successfully: प्रयोक्ता को सफलतापूर्वक नष्ट कर दिया!
117+
user_authentication_failed: उपयोगकर्ता प्रमाणीकरण विफल रहा!
117118
api:
118119
show_error_client_name: क्षमा करें, क्लाइंट का नाम रिक्त नहीं हो सकता!
119120
show_error_client_name_taken: क्षमा करें, क्लाइंट का नाम पहले ही ले लिया!

src/ApiBundle/Resources/views/admin/user/edit.html.twig

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,14 @@
4646
<th scope="row">{{ 'label.user_email'|trans }}</th>
4747
<td>
4848
{{ form_errors(edit_form.email) }}
49-
{{ form_widget(edit_form.email, { 'attr':{'readonly':'readonly'} }) }}
49+
{{ form_widget(edit_form.email) }}
5050
</td>
5151
</tr>
5252
<tr>
5353
<th scope="row">{{ 'label.user_username'|trans }}</th>
5454
<td>
5555
{{ form_errors(edit_form.username) }}
56-
{{ form_widget(edit_form.username, { 'attr':{'readonly':'readonly'} }) }}
57-
</td>
58-
</tr>
59-
<tr>
60-
<th scope="row">{{ 'label.user_password'|trans }}</th>
61-
<td>
62-
{{ form_errors(edit_form.plainPassword) }}
63-
{{ form_widget(edit_form.plainPassword) }}
56+
{{ form_widget(edit_form.username) }}
6457
</td>
6558
</tr>
6659
<tr>

src/ApiBundle/Resources/views/user/new.html.twig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@
5858
{{ form_widget(form.plainPassword) }}
5959
</td>
6060
</tr>
61-
<tr>
62-
<th scope="row">{{ 'label.user_roles'|trans }}</th>
63-
<td>
64-
{{ form_errors(form.roles) }}
65-
{{ form_widget(form.roles) }}
66-
</td>
67-
</tr>
6861
</tbody>
6962
</table>
7063
<input type="submit" value="{{ button_label|default('action.create_user'|trans) }}"

0 commit comments

Comments
 (0)