Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented profile form and controller #12

Merged
merged 3 commits into from
Jun 27, 2016
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
9 changes: 7 additions & 2 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ protected function getWebspaceKey()
*/
protected function setUserPasswordAndSalt(User $user, Form $form)
{
$plainPassword = $form->get('plainPassword')->getData();
if (null === $plainPassword) {
return $user;
}

$salt = $this->get('sulu_security.salt_generator')->getRandomSalt();
$encoder = $this->get('security.encoder_factory')->getEncoder($user);
$password = $encoder->encodePassword($form->get('plainPassword')->getData(), $salt);
$password = $encoder->encodePassword($plainPassword, $salt);

$user->setPassword($password);
$user->setSalt($salt);
Expand Down Expand Up @@ -108,7 +113,7 @@ protected function checkAutoLogin($type)
*
* @return Response
*/
protected function renderTemplate($type, $data)
protected function renderTemplate($type, $data = [])
{
return $this->render(
$this->getCommunityManager($this->getWebspaceKey())->getConfigTypeProperty(
Expand Down
50 changes: 50 additions & 0 deletions Controller/EmailConfirmationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Handle email confirmation.
*/
class EmailConfirmationController extends AbstractController
{
const TYPE = Configuration::TYPE_EMAIL_CONFIRMATION;

/**
* Overwrite user email with contact email.
*
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request)
{
$repository = $this->get('sulu_community.email_confirmation.repository');

$success = false;
$token = $repository->findByToken($request->get('token'));

if ($token !== null) {
$user = $token->getUser();
$user->setEmail($user->getContact()->getMainEmail());
$this->get('doctrine.orm.entity_manager')->remove($token);
$this->saveEntities();

$success = true;
}

return $this->renderTemplate(self::TYPE, ['success' => $success]);
}
}
174 changes: 174 additions & 0 deletions Controller/ProfileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\ContactBundle\Entity\Address;
use Sulu\Bundle\ContactBundle\Entity\AddressType;
use Sulu\Bundle\ContactBundle\Entity\ContactAddress;
use Sulu\Bundle\ContactBundle\Entity\Note;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Handle profile page.
*/
class ProfileController extends AbstractController
{
const TYPE = Configuration::TYPE_PROFILE;

/**
* Handle registration form.
*
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request)
{
$communityManager = $this->getCommunityManager($this->getWebspaceKey());

$user = $this->getUser();

// Create Form
$form = $this->createForm(
$communityManager->getConfigTypeProperty(self::TYPE, Configuration::FORM_TYPE),
$user,
$communityManager->getConfigTypeProperty(self::TYPE, Configuration::FORM_TYPE_OPTIONS)
);

$form->handleRequest($request);
$success = false;

// Handle Form Success
if ($form->isSubmitted() && $form->isValid()) {
// Set Password and Salt
$user = $this->setUserPasswordAndSalt($form->getData(), $form);

if (!$user->getLocale()) {
$user->setLocale($request->getLocale());
}

$this->saveAvatar($form, $user, $request->getLocale());

// Register User
$communityManager->saveProfile($user);
$this->saveEntities();

// Redirect
$redirectTo = $communityManager->getConfigTypeProperty(self::TYPE, Configuration::REDIRECT_TO);

if ($redirectTo) {
return $this->redirect($redirectTo);
}

$success = true;
}

return $this->renderTemplate(
self::TYPE,
[
'form' => $form->createView(),
'success' => $success,
]
);
}

/**
* Save media and set avatar on user.
*
* @param Form $form
* @param User $user
* @param string $locale
*/
private function saveAvatar(Form $form, User $user, $locale)
{
$uploadedFile = $form->get('contact')->get('avatar')->getData();
if (null === $uploadedFile) {
return;
}

$systemCollectionManager = $this->get('sulu_media.system_collections.manager');
$mediaManager = $this->get('sulu_media.media_manager');

$collection = $systemCollectionManager->getSystemCollection('sulu_contact.contact');
$avatar = $user->getContact()->getAvatar();

$apiMedia = $mediaManager->save(
$uploadedFile,
[
'id' => (null !== $avatar ? $avatar->getId() : null),
'locale' => $locale,
'title' => $user->getFullName(),
'collection' => $collection,
],
$user->getId()
);

$user->getContact()->setAvatar($apiMedia->getEntity());
}

/**
* {@inheritdoc}
*
* @return User
*/
public function getUser()
{
/** @var User $user */
$user = parent::getUser();

if (null === $user->getContact()->getMainAddress()) {
$this->addAddress($user);
}

if (0 === count($user->getContact()->getNotes())) {
$this->addNote($user);
}

return $user;
}

/**
* Add address to user.
*
* @param User $user
*/
private function addAddress(User $user)
{
$entityManager = $this->get('doctrine.orm.entity_manager');

$address = new Address();
$address->setPrimaryAddress(true);
$address->setAddressType($entityManager->getRepository(AddressType::class)->find(1));
$contactAddress = new ContactAddress();
$contactAddress->setAddress($address);
$contactAddress->setContact($user->getContact());

$user->getContact()->addContactAddress($contactAddress);
}

/**
* Add note to user.
*
* @param User $user
*/
private function addNote(User $user)
{
$note = new Note();
$user->getContact()->addNote($note);

$this->get('doctrine.orm.entity_manager')->persist($note);
}
}
Loading