Created
July 5, 2017 20:45
-
-
Save tomilsinszki/203f25db505b14fab311230954b434df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\UserBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Acme\UserBundle\Entity\User; | |
use Acme\RatingBundle\Entity\Rating; | |
use Acme\RatingBundle\Entity\Image; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Security\Core\SecurityContext; | |
use Acme\UserBundle\Utility\CurrentUser; | |
class DefaultController extends Controller | |
{ | |
public function profileAction(Request $request) | |
{ | |
$user = $this->getUserFromContext(); | |
$imageURL = $this->getImageURLForUser($user); | |
$ratings = $this->getDoctrine()->getRepository('AcmeRatingBundle:Rating')->findBy(array('ratingUser' => $user), array('created' => 'DESC')); | |
return $this->render('AcmeUserBundle:Default:profile.html.twig', array( | |
'user' => $user, | |
'ratingCount' => count($ratings), | |
'ratingAverage' => $this->getRatingsAverageWithTwoDecimals($ratings), | |
'ratings' => $ratings, | |
'imageURL' => $imageURL, | |
)); | |
} | |
private function getImageURLForUser($user) | |
{ | |
$imageURL = null; | |
$image = $user->getImage(); | |
if ( empty($image) === FALSE ) | |
$imageURL = $image->getWebPath(); | |
return $imageURL; | |
} | |
public function profileEditAction() | |
{ | |
$user = $this->getUserFromContext(); | |
$emailForm = $this->createFormBuilder($user)->add('username', 'email', array('label' => 'E-mail', 'attr' => array('autocomplete' => 'off', 'autocapitalize' => 'off')))->getForm(); | |
return $this->renderProfileEditView($emailForm); | |
} | |
public function uploadImageAction() | |
{ | |
$user = $this->getUserFromContext(); | |
$image = new Image(); | |
$imageUploadForm = $this->createFormBuilder($image)->add('file')->getForm(); | |
if ( $this->getRequest()->isMethod('POST') ) { | |
$imageUploadForm->bind($this->getRequest()); | |
if ( $imageUploadForm->isValid() ) { | |
$entityManager = $this->getDoctrine()->getManager(); | |
$user->setImage($image); | |
$entityManager->persist($image); | |
$entityManager->persist($user); | |
$entityManager->flush(); | |
return $this->redirect($this->generateUrl('acme_user_profile_edit')); | |
} | |
} | |
} | |
public function updateUserDataAction() | |
{ | |
$tmpUser = new User(); | |
$emailForm = $this->createFormBuilder($tmpUser)->add('username', 'email', array('label' => 'E-mail', 'attr' => array('autocomplete' => 'off', 'autocapitalize' => 'off')))->getForm(); | |
if ( $this->getRequest()->isMethod('POST') ) { | |
$emailForm->bind($this->getRequest()); | |
if ( $emailForm->isValid() ) { | |
$entityManager = $this->getDoctrine()->getManager(); | |
$user = $this->getUserFromContext(); | |
$tmpUser = $emailForm->getData(); | |
$user->setUsername($tmpUser->getUsername()); | |
$entityManager->persist($user); | |
$entityManager->flush(); | |
return $this->redirect($this->generateUrl('acme_user_profile_edit')); | |
} | |
} | |
return $this->renderProfileEditView($emailForm); | |
} | |
private function renderProfileEditView($emailForm) { | |
$user = $this->getUserFromContext(); | |
$imageURL = $this->getImageURLForUser($user); | |
$image = new Image(); | |
$imageUploadForm = $this->createFormBuilder($image)->add('file')->getForm(); | |
return $this->render('AcmeUserBundle:Default:profileEdit.html.twig', array( | |
'user' => $user, | |
'imageUploadForm' => $imageUploadForm->createView(), | |
'emailForm' => $emailForm->createView(), | |
'imageURL' => $imageURL, | |
)); | |
} | |
private function getUserFromContext() | |
{ | |
$user = $this->get('security.context')->getToken()->getUser(); | |
if ( empty($user) === TRUE ) | |
throw $this->createNotFoundException('Current user could not be found.'); | |
return $user; | |
} | |
private function getRatingsAverageWithTwoDecimals($ratings) | |
{ | |
$ratingSum = 0.0; | |
foreach($ratings AS $rating) | |
$ratingSum += $rating->getStars(); | |
if ( count($ratings) == 0 ) | |
return 0.0; | |
$average = (float)$ratingSum / (float)count($ratings); | |
$average = round($average, 2); | |
return number_format($average, 2, ',', ' '); | |
} | |
public function registerAction(Request $request) | |
{ | |
$user = new User(); | |
$form = $this->createFormBuilder($user) | |
->add('username', 'email', array('attr' => array('placeholder' => 'E-mail', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->add('password', 'password', array('attr' => array('placeholder' => 'Jelszó', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->getForm(); | |
return $this->render('AcmeUserBundle:Default:registration.html.twig', array( | |
'form' => $form->createView(), | |
)); | |
} | |
public function registrationDoneAction(Request $request) | |
{ | |
return $this->render('AcmeUserBundle:Default:registrationDone.html.twig', array()); | |
} | |
public function newAction(Request $request) | |
{ | |
$user = new User(); | |
$form = $this->createFormBuilder($user) | |
->add('username', 'email', array('attr' => array('placeholder' => 'E-mail', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->add('password', 'password', array('attr' => array('placeholder' => 'Jelszó', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->getForm(); | |
if ( $request->isMethod('POST') ) { | |
$form->bind($request); | |
if ( $form->isValid() ) { | |
$factory = $this->get('security.encoder_factory'); | |
$user = $form->getData(); | |
$raterGroup = $this->getDoctrine()->getRepository('AcmeUserBundle:Group')->findOneByName('rater'); | |
$encoder = $factory->getEncoder($user); | |
$password = $encoder->encodePassword($user->getPassword(), $user->getSalt()); | |
$user->setPassword($password); | |
$user->addGroup($raterGroup); | |
$entityManager = $this->getDoctrine()->getManager(); | |
$entityManager->persist($user); | |
$entityManager->flush(); | |
return $this->redirect($this->generateUrl('acme_user_registration_done')); | |
} | |
} | |
} | |
public function changePasswordAction() | |
{ | |
$defaultData = array(); | |
$form = $this->createFormBuilder($defaultData) | |
->add('oldPassword', 'password', array('attr' => array('placeholder' => 'Régi jelszó', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->add('newPassword1', 'password', array('attr' => array('placeholder' => 'Új jelszó', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->add('newPassword2', 'password', array('attr' => array('placeholder' => 'Új jelszó megerősítése', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->getForm(); | |
return $this->render('AcmeUserBundle:Default:changePassword.html.twig', array( | |
'form' => $form->createView(), | |
)); | |
} | |
public function updatePasswordAction() | |
{ | |
$defaultData = array(); | |
$form = $this->createFormBuilder($defaultData) | |
->add('oldPassword', 'password', array('attr' => array('placeholder' => 'Régi jelszó', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->add('newPassword1', 'password', array('attr' => array('placeholder' => 'Új jelszó', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->add('newPassword2', 'password', array('attr' => array('placeholder' => 'Új jelszó megerősítése', 'autocomplete' => 'off', 'autocapitalize' => 'off'))) | |
->getForm(); | |
if ( $this->getRequest()->isMethod('POST') === TRUE ) { | |
$form->bind($this->getRequest()); | |
$user = $this->getUserFromContext(); | |
$data = $form->getData(); | |
$isOldPasswordValid = ( $this->isPasswordValidForCurrentUser($data['oldPassword']) === TRUE ); | |
$doNewPasswordsMatch = ( $data['newPassword1'] === $data['newPassword1'] ); | |
$isNewPasswordNonEmpty = ( empty($data['newPassword1']) === FALSE ); | |
$isNewPasswordString = ( is_string($data['newPassword1']) === TRUE ); | |
$isNewPasswordLongEnough = ( 4 < strlen($data['newPassword1']) ); | |
if ( $isOldPasswordValid AND $doNewPasswordsMatch AND $isNewPasswordNonEmpty AND $isNewPasswordString AND $isNewPasswordLongEnough ) { | |
$factory = $this->get('security.encoder_factory'); | |
$encoder = $factory->getEncoder($user); | |
$password = $encoder->encodePassword($data['newPassword1'], $user->getSalt()); | |
$user->setPassword($password); | |
$entityManager = $this->getDoctrine()->getManager(); | |
$entityManager->persist($user); | |
$entityManager->flush(); | |
} | |
} | |
return $this->redirect($this->generateUrl('acme_user_profile')); | |
} | |
public function doesExistAction() | |
{ | |
$request = $this->getRequest(); | |
$username = $request->get('username'); | |
$user = $this->getDoctrine()->getRepository('AcmeUserBundle:User')->findOneByUsername($username); | |
if ( empty($user) === FALSE ) | |
return new Response(json_encode(TRUE)); | |
else | |
return new Response(json_encode(FALSE)); | |
} | |
public function loginAction() | |
{ | |
$request = $this->getRequest(); | |
$session = $request->getSession(); | |
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { | |
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); | |
} | |
else { | |
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR); | |
$session->remove(SecurityContext::AUTHENTICATION_ERROR); | |
} | |
return $this->render('AcmeUserBundle:Default:login.html.twig', array( | |
'last_username' => $session->get(SecurityContext::LAST_USERNAME), | |
'error' => $error, | |
)); | |
} | |
public function isPasswordValidAction() | |
{ | |
$isPasswordValid = FALSE; | |
$request = $this->getRequest(); | |
if ( $request->isXmlHttpRequest() !== TRUE ) { | |
return new Response(json_encode($isPasswordValid), 200, array('Content-Type' => 'application/json')); | |
} | |
if ( $this->isPasswordValidForCurrentUser($request->request->get('password')) === TRUE ) { | |
$isPasswordValid = TRUE; | |
} | |
return new Response(json_encode($isPasswordValid), 200, array('Content-Type' => 'application/json')); | |
} | |
private function isPasswordValidForCurrentUser($possiblePassword) | |
{ | |
$user = $this->get('security.context')->getToken()->getUser(); | |
$factory = $this->get('security.encoder_factory'); | |
$encoder = $factory->getEncoder($user); | |
$possiblePasswordEncoded = $encoder->encodePassword($possiblePassword, $user->getSalt()); | |
if ( $possiblePasswordEncoded === $user->getPassword() ) { | |
return TRUE; | |
} | |
return FALSE; | |
} | |
public function loadWelcomePageAction() | |
{ | |
$securityContext = $this->get('security.context'); | |
if ( CurrentUser::isOfRole($securityContext, 'ROLE_CUSTOMERSERVICE') ) { | |
return $this->redirect($this->generateUrl('quiz_entrance')); | |
} | |
else if ( CurrentUser::isOfRole($securityContext, 'ROLE_MANAGER') ) { | |
return $this->redirect($this->generateUrl('acme_manager_welcome')); | |
} | |
return $this->redirect($this->generateUrl('identifier_main')); | |
} | |
public function managerWelcomeAction() | |
{ | |
return $this->render('AcmeUserBundle:Default:managerWelcome.html.twig', array( | |
'ownedCollections' => $this->getUser()->getOwnedCollections() | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment