Skip to content

Instantly share code, notes, and snippets.

@stoefln
Created January 10, 2012 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoefln/1589491 to your computer and use it in GitHub Desktop.
Save stoefln/1589491 to your computer and use it in GitHub Desktop.
Registration implementation
//....
/**
* @Route("/register",name="_index_register")
* @Template()
*/
public function registerAction() {
if($this->getUser() && $this->getUser() != "anon."){
return $this->redirect($this->generateUrl('_user_events', array('username' => $this->getUser()->getUsername())));
}
$message = '';
// TODO!!
if(!$step = $this->getRequest()->get('step')){
$step = 1;
}
$form = $this->createForm(new RegistrationFormType());
$form->bindRequest($this->getRequest());
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
/* @var $product \Ajado\EventHubBundle\Entity\Product */
if ($this->getRequest()->getMethod() == 'POST' && $form->isValid()) {
$code = $form->get('code')->getClientData();
if($code){
$product = $this->em()->getRepository('AjadoEventHubBundle:Product')->findOneBy(array('code'=>$code, 'usedBy'=>null));
}
if($code && $product && $product->getUsedBy() == null){ // if product was found and has not been assigned to another user so far
$data = $form->getData();
$user = $this->generateUserFromUserData($data['email'], '', '', $data['username'],$data['plainPassword']);
$user->setEnabled(true);
$this->authenticateUser($user);
$product->setUsedBy($user);
$this->persist($user);
$this->persist($product);
$this->flush();
$this->setFlash('notice', 'Ihr Produktcode wurde eingelöst. Sie verfügen nun über einen '.$product->getType().' Account!');
$url = $this->container->get('router')->generate('_index_welcome');
return new RedirectResponse($url);
}else{
$this->setFlash('notice', 'Ihr Produktcode ist ungültig!');
}
}
$this->setData('form', $form->createView());
$this->setData('message', $message);
$this->setData('step', $step);
return $this->getData();
}
//....
<?php
namespace Ajado\EventHubBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class RegistrationFormType extends AbstractType
{
/**
*
* @var FormBuilder
*/
private $builder;
public function buildForm(FormBuilder $builder, array $options)
{
$this->builder = $builder;
$this->builder
->add('code','text', array(
'label' => 'Einladungscode'
))->add('username','text',array(
'label' => 'Benutzername',
))
->add('email', 'email',array(
'label' => 'E-Mail'
))
->add('plainPassword', 'repeated', array('type' => 'password',
'first_name'=>'Passwort',
'second_name'=> 'Passwort wiederholen',
)
);
}
public function showRegistrationFields(){
}
public function getDefaultOptions(array $options)
{
return array(
'csrf_protection' => false,
'validation_groups' => array('Registration'),
);
}
public function getName()
{
return 'registration';
}
}
<?php
namespace Ajado\EventHubBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @UniqueEntity("username")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\generatedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
/**
* @var string $email
*
* @Assert\Email()
*/
protected $email;
/**
* @ORM\ManyToMany(targetEntity="FacebookAccount", inversedBy="administrators")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
protected $facebookAccounts;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $firstname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $lastname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $facebookID;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $facebookAccessToken;
/**
*
* @ORM\Column(type="array", nullable=true)
*/
protected $settings;
/**
* @ORM\ManyToMany(targetEntity="Contact", mappedBy="ownedBy")
*/
protected $ownedContacts;
/**
* @ORM\OneToMany(targetEntity="ContactTag", mappedBy="createdBy")
*/
protected $createdTags;
/**
* @ORM\OneToMany(targetEntity="Visit", mappedBy="createdBy")
*/
protected $visits;
/**
* @ORM\OneToMany(targetEntity="Document", mappedBy="createdBy")
*/
protected $backgroundImages;
/**
* @ORM\ManyToMany(targetEntity="Event", inversedBy="attendees")
* @ORM\JoinTable(name="attendee")
*/
protected $attendingEvents;
/**
* @ORM\ManyToMany(targetEntity="LogEntry", mappedBy="receiver")
*/
protected $logEntries;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="usedBy")
*/
protected $usedProducts;
/**
*
* @ORM\OneToOne(targetEntity="Document", cascade={"persist"})
* @ORM\JoinColumn(onDelete="SET NULL")
*/
protected $profileImage;
/**
* in order to check if user can use special features, we have to know if the user is a publisher
* @return boolean
*/
public function isPublisher(){
return $this->getUsedProducts()->count() > 0;
}
public function isEnabled(){
return $this->enabled;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* @param string $firstname
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
/**
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @param string $lastname
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
/**
* Get the full name of the user (first + last name)
* @return string
*/
public function getFullName()
{
return trim($this->getFirstName() . ' ' . $this->getLastname());
}
public function getName(){
$name = $this->getFullName();
if(!$name){
$name = $this->getUsername();
}
return $name;
}
/**
* @param string $facebookID
* @return void
*/
public function setFacebookID($facebookID)
{
$this->facebookID = $facebookID;
// don't override username, if there was already one set (by forms registration)
if(!$this->getUsername()){
$this->setUsername($facebookID);
$this->salt = '';
}
}
/**
* @return string
*/
public function getFacebookID()
{
return $this->facebookID;
}
/**
* @param Array
*/
public function setFBData($fbdata)
{
if (isset($fbdata['id'])) {
$this->setFacebookID($fbdata['id']);
$this->addRole('ROLE_FACEBOOK');
}
if (isset($fbdata['first_name'])) {
$this->setFirstname($fbdata['first_name']);
}
if (isset($fbdata['last_name'])) {
$this->setLastname($fbdata['last_name']);
}
if (isset($fbdata['email'])) {
$this->setEmail($fbdata['email']);
}
}
/**
* Add administrators
*
* @param Ajado\EventHubBundle\Entity\FacebookAccount $page
*/
public function addFacebookAccount(\Ajado\EventHubBundle\Entity\FacebookAccount $page)
{
// add page only if the page was not already added
foreach($this->facebookAccounts as $aPage){
if($aPage->getFacebookID() == $page->getFacebookID()){
return;
}
}
$this->facebookAccounts[] = $page;
}
/**
* Get facebookAccounts
*
* @return Doctrine\Common\Collections\Collection $facebookAccounts
*/
public function getFacebookAccounts()
{
return $this->facebookAccounts;
}
public function removeSetting($key) {
unset ($this->settings[$key]);
}
public function setSetting($key,$val){
$this->settings[$key] = $val;
}
public function getSetting($key){
if(isset($this->settings[$key])){
return $this->settings[$key];
}else{
return null;
}
}
/**
* Get Tags which were created by the user
*
* @return Doctrine\Common\Collections\Collection $createdTags
*/
public function getCreatedTags()
{
return $this->createdTags;
}
/**
* Set settings
*
* @param array $settings
*/
public function setSettings($settings)
{
$this->settings = $settings;
}
/**
* Get settings
*
* @return array $settings
*/
public function getSettings()
{
return $this->settings;
}
/**
* Add facebookAccount
*
* @param Ajado\EventHubBundle\Entity\FacebookAccount $facebookAccount
*/
public function addFacebookAccounts(\Ajado\EventHubBundle\Entity\FacebookAccount $facebookAccount)
{
$this->facebookAccounts[] = $facebookAccount;
}
/**
* Add createdTags
*
* @param Ajado\EventHubBundle\Entity\ContactTag $createdTags
*/
public function addCreatedTags(\Ajado\EventHubBundle\Entity\ContactTag $createdTags)
{
$this->createdTags[] = $createdTags;
}
/**
* Add attendingEvents
*
* @param Ajado\EventHubBundle\Entity\Event $event
*/
public function addAttendingEvent(\Ajado\EventHubBundle\Entity\Event $event)
{
foreach($this->attendingEvents as $event0){
if($event->getId() == $event0->getId()){
return $this;
}
}
$this->attendingEvents[] = $event;
return $this;
}
/**
* Get attendingEvents
*
* @return Doctrine\Common\Collections\Collection $attendingEvents
*/
public function getAttendingEvents()
{
return $this->attendingEvents;
}
/**
* Add attendingEvents
*
* @param Ajado\EventHubBundle\Entity\Event $attendingEvents
*/
public function addAttendingEvents(\Ajado\EventHubBundle\Entity\Event $attendingEvents)
{
$this->attendingEvents[] = $attendingEvents;
}
/**
* Add visits
*
* @param Ajado\EventHubBundle\Entity\Visit $visits
*/
public function addVisits(\Ajado\EventHubBundle\Entity\Visit $visits)
{
$this->visits[] = $visits;
}
/**
* Get visits
*
* @return Doctrine\Common\Collections\Collection
*/
public function getVisits()
{
return $this->visits;
}
/**
* Add logEntries
*
* @param Ajado\EventHubBundle\Entity\LogEntry $logEntries
*/
public function addLogEntries(\Ajado\EventHubBundle\Entity\LogEntry $logEntries)
{
$this->logEntries[] = $logEntries;
}
/**
* Get logEntries
*
* @return Doctrine\Common\Collections\Collection
*/
public function getLogEntries()
{
return $this->logEntries;
}
/**
* Add backgroundImages
*
* @param Ajado\EventHubBundle\Entity\Document $backgroundImages
*/
public function addBackgroundImages(\Ajado\EventHubBundle\Entity\Document $backgroundImages)
{
$this->backgroundImages[] = $backgroundImages;
}
/**
* Get backgroundImages
*
* @return Doctrine\Common\Collections\Collection
*/
public function getBackgroundImages()
{
return $this->backgroundImages;
}
/**
* Add contacts
*
* @param Ajado\EventHubBundle\Entity\Contact $contacts
*/
public function addContacts(\Ajado\EventHubBundle\Entity\Contact $contacts)
{
$this->contacts[] = $contacts;
}
/**
* Get contacts
*
* @return Doctrine\Common\Collections\Collection
*/
public function getContacts()
{
return $this->contacts;
}
/**
* Add ownedContacts
*
* @param Ajado\EventHubBundle\Entity\Contact $contact
*/
public function addOwnedContacts(\Ajado\EventHubBundle\Entity\Contact $contact)
{
if($this->hasOwnedContact($contact)){
return false;
}
$this->ownedContacts[] = $contact;
}
/**
* Get ownedContacts
*
* @return Doctrine\Common\Collections\Collection
*/
public function getOwnedContacts()
{
return $this->ownedContacts;
}
public function hasOwnedContact(Contact $contact)
{
foreach($this->ownedContacts as $contact0){
if($contact0->getEmail() == $contact->getEmail()){
return true;
}
}
return false;
}
public function hasOwnedContactEmail($email)
{
foreach($this->ownedContacts as $contact0){
if($contact0->getEmail() == $email){
return true;
}
}
return false;
}
public function hasSubscribingUser(User $subscriber) {
foreach($this->getOwnedContacts() as $contact){
/* @var $contact Contact */
if($contact->getSubscribingUser() == $subscriber && $contact->getUnsubscribed() === false){
return true;
}
}
return false;
}
public function __construct()
{
parent::__construct();
$this->facebookAccounts = new \Doctrine\Common\Collections\ArrayCollection();
$this->ownedContacts = new \Doctrine\Common\Collections\ArrayCollection();
$this->createdTags = new \Doctrine\Common\Collections\ArrayCollection();
$this->visits = new \Doctrine\Common\Collections\ArrayCollection();
$this->backgroundImages = new \Doctrine\Common\Collections\ArrayCollection();
$this->attendingEvents = new \Doctrine\Common\Collections\ArrayCollection();
$this->logEntries = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString() {
return $this->username;
}
/**
* Add usedProducts
*
* @param Ajado\EventHubBundle\Entity\Product $usedProducts
*/
public function addUsedProducts(\Ajado\EventHubBundle\Entity\Product $usedProducts)
{
$this->usedProducts[] = $usedProducts;
}
/**
* Get usedProducts
*
* @return Doctrine\Common\Collections\Collection
*/
public function getUsedProducts()
{
return $this->usedProducts;
}
/**
* Set facebookAccessToken
*
* @param string $facebookAccessToken
*/
public function setFacebookAccessToken($facebookAccessToken)
{
$this->facebookAccessToken = $facebookAccessToken;
}
/**
* Get facebookAccessToken
*
* @return string
*/
public function getFacebookAccessToken()
{
return $this->facebookAccessToken;
}
/**
* Add ownedContacts
*
* @param Ajado\EventHubBundle\Entity\Contact $ownedContacts
*/
public function addContact(\Ajado\EventHubBundle\Entity\Contact $ownedContacts)
{
$this->ownedContacts[] = $ownedContacts;
}
/**
* Add createdTags
*
* @param Ajado\EventHubBundle\Entity\ContactTag $createdTags
*/
public function addContactTag(\Ajado\EventHubBundle\Entity\ContactTag $createdTags)
{
$this->createdTags[] = $createdTags;
}
/**
* Add visits
*
* @param Ajado\EventHubBundle\Entity\Visit $visits
*/
public function addVisit(\Ajado\EventHubBundle\Entity\Visit $visits)
{
$this->visits[] = $visits;
}
/**
* Add backgroundImages
*
* @param Ajado\EventHubBundle\Entity\Document $backgroundImages
*/
public function addDocument(\Ajado\EventHubBundle\Entity\Document $backgroundImages)
{
$this->backgroundImages[] = $backgroundImages;
}
/**
* Add attendingEvents
*
* @param Ajado\EventHubBundle\Entity\Event $attendingEvents
*/
public function addEvent(\Ajado\EventHubBundle\Entity\Event $attendingEvents)
{
$this->attendingEvents[] = $attendingEvents;
}
/**
* Add logEntries
*
* @param Ajado\EventHubBundle\Entity\LogEntry $logEntries
*/
public function addLogEntry(\Ajado\EventHubBundle\Entity\LogEntry $logEntries)
{
$this->logEntries[] = $logEntries;
}
/**
* Add usedProducts
*
* @param Ajado\EventHubBundle\Entity\Product $usedProducts
*/
public function addProduct(\Ajado\EventHubBundle\Entity\Product $usedProducts)
{
$this->usedProducts[] = $usedProducts;
}
/**
* Set profileImage
*
* @param Ajado\EventHubBundle\Entity\Document $profileImage
* @return User
*/
public function setProfileImage(\Ajado\EventHubBundle\Entity\Document $profileImage=null)
{
$this->profileImage = $profileImage;
return $this;
}
/**
* Get profileImage
*
* @return Ajado\EventHubBundle\Entity\Document
*/
public function getProfileImage()
{
return $this->profileImage;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phone
*
* @param string $phone
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment