Skip to content

Instantly share code, notes, and snippets.

@patie
Created September 15, 2012 18:43
Show Gist options
  • Save patie/3729241 to your computer and use it in GitHub Desktop.
Save patie/3729241 to your computer and use it in GitHub Desktop.
<?php
namespace Admin\CoreBundle\Entity;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Role implements RoleInterface
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* @ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @see RoleInterface
*/
public function getRole()
{
return $this->role;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function setRole($role)
{
$this->role = $role;
}
}
<?php
namespace Admin\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use \Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use APY\DataGridBundle\Grid\Mapping as GRID;
/**
* Admin\CoreBundle\Entity\User
*
* @ORM\Table()
* @ORM\Entity
* @UniqueEntity(fields="username", message="Takýto username už existuje")
*/
class User implements UserInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @GRID\Column(visible=false)
*/
private $id;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=100, unique=true)
*
* @GRID\Column(title="Username", visible=true)
*/
private $username;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=100, unique=true)
*
* @GRID\Column(title="Meno", visible=true)
*/
private $name;
/**
* @var string $email
*
* @ORM\Column(name="email", type="string", length=255)
*
* @GRID\Column(title="Email", visible=true)
*/
private $email;
/**
* @var string $password
*
* @ORM\Column(name="password", type="string", length=255)
*
* @GRID\Column(visible=false)
*/
private $password;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="User_Role",
* joinColumns={@ORM\JoinColumn(name="User_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="Role_id", referencedColumnName="id")}
* )
*/
private $roles;
/**
* @var string $salt
*
* @ORM\Column(name="salt", type="string", length=32)
*
* @GRID\Column(visible=false)
*/
private $salt;
/**
* @Assert\File(maxSize="250k")
*/
public $fileSign;
public function __construct()
{
$this->salt = md5(uniqid(null, true));
$this->roles = new ArrayCollection;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set role
*
* @param string $roles
*/
public function setRoles($roles)
{
$this->roles = $roles;
}
/**
* Get role
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRoles()
{
return $this->roles;
}
/**
* Set salt
*
* @param string $salt
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
}
/**
* @Route("/create", name="User_create")
* @Template()
*/ public function acreateAction(Request $request)
{
$user = new \Admin\CoreBundle\Entity\User();
/** @var $em \Doctrine\ORM\EntityManager */
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(new \Admin\CoreBundle\Form\Type\UserCreateType(), new \Admin\CoreBundle\Entity\User());
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
try {
//$data2 = $request->request->get('userCreate');
$formData = $form->getData();
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$pwd = $encoder->encodePassword($formData->getPassword(), $salt);
$user->setUsername($formData->getUsername());
$user->setName($formData->getName());
$user->setEmail($formData->getEmail());
$user->setSalt($salt);
$user->setPassword($pwd);
//$user->setRoles(array($role)); // ????????????????
$em->persist($user);
$em->flush();
$this->get('session')->setFlash('success', 'Užívateľ ' . $user->getUsername() . ' bol úspešne vytvorený');
} catch (\Exception $e) {
$this->get('session')->setFlash('error', 'Užívateľa sa nepodarilo vytvoriť !!!');
}
}
}
return array(
'form' => $form->createView()
);
}
<?php
namespace Admin\CoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class UserCreateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text', array(
'label' => 'Meno a priezvisko',
'error_bubbling' => true
));
$builder->add('username', 'text', array(
'label' => 'Login',
'error_bubbling' => true
));
$builder->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'Heslá sa musia zhodovať',
'required' => true,
'options' => array('attr' => array('class' => 'span12')),
'first_options' => array('label' => 'Heslo'),
'second_options' => array('label' => 'Heslo znova'),
'error_bubbling' => true
));
$builder->add('email', 'email', array(
'error_bubbling' => true
));
$builder->add('role', 'entity', array(
'class' => 'CoreBundle:Role',
'property' => 'name',
'multiple' => false,
'expanded' => false,
'error_bubbling' => true
));
$builder->add('fileSign', 'file', array(
'label' => 'Podpis',
'error_bubbling' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Admin\CoreBundle\Entity\User',
));
}
public function getName()
{
return 'userCreate';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment