Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Managing user profile #3

Merged
merged 4 commits into from
Apr 10, 2018
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
52 changes: 52 additions & 0 deletions src/Controller/AbstractSecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Smart\AuthenticationBundle\Controller;

use Smart\AuthenticationBundle\Security\Form\Type\UserProfileType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

Expand All @@ -16,6 +18,18 @@ class AbstractSecurityController extends Controller
* @var string
*/
protected $context;

/**
* @param string $id The message id (may also be an object that can be cast to string)
* @param array $parameters An array of parameters for the message
* @param string|null $domain The domain for the message or null to use the default
*
* @return string
*/
protected function translate($id, array $parameters = array(), $domain = null)
{
return $this->get('translator')->trans($id, $parameters, $domain);
}

/**
* @return Response
Expand All @@ -40,4 +54,42 @@ private function getAuthenticationUtils()
{
return $this->get('security.authentication_utils');
}

/**
* @param Request $request
*
* @return Response
*/
public function profileAction(Request $request)
{
$user = $this->getUser();

$form = $this->createForm(UserProfileType::class, $user, []);

$form->handleRequest($request);

if (!$form->isSubmitted() || !$form->isValid()) {
return $this->render($this->context . '/security/profile.html.twig', [
'base_template' => $this->get('sonata.admin.pool')->getTemplate('layout'),
'admin_pool' => $this->get('sonata.admin.pool'),
'form' => $form->createView(),
'security_profile_url' => $this->generateUrl('admin_security_profile'),
]);
}

if (null !== $user->getPlainPassword()) {
$encoder = $this->get('security.password_encoder');
$user->setPassword(
$encoder->encodePassword($user, $user->getPlainPassword())
);
}

$manager = $this->getDoctrine()->getManager();
$manager->persist($user);
$manager->flush();

$this->addFlash('success', $this->translate('profile_edit.processed', [], 'security'));

return $this->redirectToRoute('sonata_admin_dashboard');
}
}
12 changes: 12 additions & 0 deletions src/Resources/translations/admin.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
<source>form.label_last_name</source>
<target>Lastname</target>
</trans-unit>
<trans-unit id="form.label_password">
<source>form.label_password</source>
<target>Password</target>
</trans-unit>
<trans-unit id="form.label_password_confirmation">
<source>form.label_password_confirmation</source>
<target>Password Confirmation</target>
</trans-unit>
<trans-unit id="fieldset.label_profile">
<source>fieldset.label_profile</source>
<target>My profile</target>
</trans-unit>
</body>
</file>
</xliff>
12 changes: 12 additions & 0 deletions src/Resources/translations/admin.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
<source>form.label_last_name</source>
<target>Nom</target>
</trans-unit>
<trans-unit id="form.label_password">
<source>form.label_password</source>
<target>Mot de passe</target>
</trans-unit>
<trans-unit id="form.label_password_confirmation">
<source>form.label_password_confirmation</source>
<target>Confirmation mot de passe</target>
</trans-unit>
<trans-unit id="fieldset.label_profile">
<source>fieldset.label_profile</source>
<target>Mon compte</target>
</trans-unit>
</body>
</file>
</xliff>
8 changes: 8 additions & 0 deletions src/Resources/translations/security.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<source>user.action.logout</source>
<target>Log out</target>
</trans-unit>
<trans-unit id="profile_edit.title">
<source>profile_edit.title</source>
<target>Edit your informations</target>
</trans-unit>
<trans-unit id="profile_edit.processed">
<source>profile_edit.processed</source>
<target>Informations updated.</target>
</trans-unit>
</body>
</file>
</xliff>
8 changes: 8 additions & 0 deletions src/Resources/translations/security.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<source>user.action.logout</source>
<target>Se déconnecter</target>
</trans-unit>
<trans-unit id="profile_edit.title">
<source>profile_edit.title</source>
<target>Modifier vos informations</target>
</trans-unit>
<trans-unit id="profile_edit.processed">
<source>profile_edit.processed</source>
<target>Vos informations ont été mise à jour.</target>
</trans-unit>
</body>
</file>
</xliff>
5 changes: 2 additions & 3 deletions src/Resources/views/block/user.html.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% trans_default_domain 'security' %}

<li role="presentation" class="user-header bg-light-blue">
<img src="{{ asset('design/admin/images/default_avatar.png') }}" class="img-circle" />
<li role="presentation" class="user-header">
<p>{{ app.user }}</p>
<p class="hidden">{{ app.user.email }}</p>
<p>{{ app.user.email }}</p>
</li>
<li role="presentation" class="user-footer">
<div class="pull-left">
Expand Down
68 changes: 68 additions & 0 deletions src/Resources/views/security/profile.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{% extends '@SonataAdmin/CRUD/edit.html.twig' %}

{% import "@SonataAdmin/CRUD/base_edit_form_macro.html.twig" as form_helper %}
{% form_theme form "@SonataAdmin/Form/form_admin_fields.html.twig" %}

{% block sonata_head_title %}
{% block title %}
{{ 'profile_edit.title'|trans({}, 'security') }}
{% endblock%}
{% endblock%}

{% block tab_menu %}{% endblock %}

{% block actions %}{% endblock %}

{% block form %}

{% set options = { action: security_profile_url, method: 'post' } %}
{% if admin_pool.getOption('form_type') == 'horizontal' %}
{% set options = options|merge({ attr: { class: 'form-horizontal' } }) %}
{% endif %}

{{ form_start(form, options|default({})) }}

{% if form.vars.errors|length > 0 %}
<div class="sonata-ba-form-error">
{{ form_errors(form) }}
</div>
{% endif %}

{% block sonata_pre_fieldsets %}
<div class="row">
{% endblock %}

{% block sonata_tab_content %}
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header">
<h4 class="box-title">
{{ 'fieldset.label_profile'|trans({}, 'admin') }}
</h4>
</div>
<div class="box-body">
<div class="sonata-ba-collapsed-fields">
{{ form_widget(form) }}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

{% block sonata_post_fieldsets %}
</div>
{% endblock %}

{{ form_rest(form) }}

{% block formactions %}
<div class="well well-small form-actions">
<button type="submit" class="btn btn-success" name="btn_update_and_edit"><i class="fa fa-save"></i> {{ 'btn_update'|trans({}, 'SonataAdminBundle') }}</button>
</div>
{% endblock formactions %}
</form>
{% endblock %}
56 changes: 56 additions & 0 deletions src/Security/Form/Type/UserProfileType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Smart\AuthenticationBundle\Security\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Mathieu Ducrot <mathieu.ducrot@pia-production.fr>
*/
class UserProfileType extends AbstractType
{
/**
* @inheritdoc
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'label' => 'form.label_email'
])
->add('firstName', null, [
'label' => 'form.label_first_name'
])
->add('lastName', null, [
'label' => 'form.label_last_name'
])
->add(
'plainPassword',
RepeatedType::class,
[
'type' => 'password',
'required' => false,
'first_options' => ['label' => 'form.label_password'],
'second_options' => ['label' => 'form.label_password_confirmation'],
'translation_domain' => $options['translation_domain'],
]
);
;
}

/**
* @inheritdoc
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'translation_domain' => 'admin'
])
;
}
}