Skip to content

Instantly share code, notes, and snippets.

@j
Created June 4, 2011 10:07
Show Gist options
  • Save j/1007773 to your computer and use it in GitHub Desktop.
Save j/1007773 to your computer and use it in GitHub Desktop.
<?php
[...]
class SignupHandler implements FormHandlerInterface
{
protected $form;
protected $request;
protected $entityManager;
public function buildFormHandler(Form $form, Request $request, EntityManager $entityManager)
{
$this->form = $form;
$this->request = $request;
$this->entityManager = $entityManager;
}
public function process(array $extras)
{
if ('POST' == $this->request->getMethod()) {
// bind form data
$this->form->bindRequest($this->request);
// If form is valid
if ($this->form->isValid() && ($vendor = $this->form->getData()) instanceof Entity\Vendor) {
// If owner is valid, create user
if ($vendor->getOwner() instanceof Entity\User) {
$encoder = $extras['encoderFactory']->getEncoder($vendor->getOwner());
$salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
$vendor->getOwner()->setSalt($salt);
$vendor->getOwner()->setPassword($encoder->encodePassword($vendor->getOwner()->getPlainPassword(), $salt));
$vendor->getOwner()->setEnabled(1);
$vendor->getOwner()->addRole('USER_VENDOR');
}
// persist vendor
$this->entityManager->persist($vendor);
// bind vendor to locations then save
// @TODO: This shouldn't be neccessary.. I would think it would be binded inline with vendor...
foreach ($vendor->getLocations() as $location) {
$location->setVendor($vendor);
$this->entityManager->persist($location);
}
$this->entityManager->flush();
// create acl
$acl = $extras['aclProvider']->createAcl(ObjectIdentity::fromDomainObject($vendor));
// grant owner access
$acl->insertObjectAce(UserSecurityIdentity::fromAccount($vendor->getOwner()), MaskBuilder::MASK_OWNER);
// update acl
$extras['aclProvider']->updateAcl($acl);
return $vendor;
}
}
return false;
}
}
<?php
[...]
class SignupType extends AbstractType
{
protected $loggedIn = false;
public function __constructor($security)
{
$this->loggedIn = $security->isGranted('ROLE_USER');
}
public function buildForm(FormBuilder $builder, array $options)
{
if (false === $this->isLoggedIn()) {
$builder->add('owner', new UserType());
}
$builder
->add('name')
->add('website')
->add('phone')
->add('phoneExt')
->add('description')
->add('locations', new VendorLocationType());
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'JStout\MainBundle\Entity\Vendor'
);
}
protected function isLoggedIn()
{
return $this->loggedIn;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment