Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace AcmeUserBundleEntity;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use SymfonyBridgeDoctrineValidatorConstraints as DoctrineAssert;
- use DoctrineORMMapping as ORM;
- /**
- * @ORMEntity()
- * @ORMHasLifecycleCallbacks()
- *
- * list any fields here that must be unique
- * @DoctrineAssertUniqueEntity(
- * fields = { "email" }
- * )
- */
- class User implements UserInterface
- {
- /**
- * @ORMId
- * @ORMColumn(type="integer")
- * @ORMGeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- * @ORMColumn(type="string", length="255", unique="true")
- */
- protected $email;
- /**
- * @ORMColumn(type="string", length="128")
- */
- protected $password;
- /**
- * @ORMColumn(type="string", length="5")
- */
- protected $salt;
- /**
- * Generate a new salt - can't be done as prepersist because we need it before then
- */
- public function initSalt() {
- $this->salt = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,5);
- }
- /**
- * Is the provided user the same as "this"?
- *
- * @return bool
- */
- public function equals(UserInterface $user) {
- if($user->email !== $this->email) {
- return false;
- }
- return true;
- }
- /**
- * Remove sensitive information from the user object
- */
- public function eraseCredentials() {
- $this->password = "";
- $this->salt = "";
- }
- /**
- * Get the list of roles for the user
- *
- * @return string array
- */
- public function getRoles() {
- return array("ROLE_USER");
- }
- /**
- * Get the user's password
- *
- * @return string
- */
- public function getPassword() {
- return $this->password;
- }
- /**
- * Get the user's username
- *
- * We MUST have this to fulfill the requirements of UserInterface
- *
- * @return string
- */
- public function getUsername() {
- return $this->email;
- }
- /**
- * Get the user's "email"
- *
- * @return string
- */
- public function getEmail() {
- return $this->email;
- }
- /**
- * Get the user's salt
- *
- * @return string
- */
- public function getSalt() {
- return $this->salt;
- }
- /**
- * Convert this user to a string representation
- *
- * @return string
- */
- public function __toString() {
- return $this->email;
- }
- }
- ?>
- namespace AcmeUserBundleFormType;
- use SymfonyComponentFormAbstractType;
- use SymfonyComponentFormFormBuilder;
- class UserType extends AbstractType {
- public function buildForm(FormBuilder $builder, array $options) {
- $builder->add('email');
- /* this field type lets you show two fields that represent just
- one field in the model and they both must match */
- $builder->add('password', 'repeated', array (
- 'type' => 'password',
- 'first_name' => "Password",
- 'second_name' => "Re-enter Password",
- 'invalid_message' => "The passwords don't match!"
- ));
- }
- public function getName() {
- return 'user';
- }
- public function getDefaultOptions(array $options) {
- return array(
- 'data_class' => 'AcmeUserBundleEntityUser',
- );
- }
- }
- ?>
- namespace AcmeUserBundleController;
- use SymfonyBundleFrameworkBundleControllerController;
- use SymfonyComponentHttpFoundationRequest;
- use AcmeUserBundleEntityUser;
- use AcmeUserBundleFormTypeUserType;
- class userController extends Controller
- {
- public function newAction(Request $request) {
- $user = new User();
- $form = $this->createForm(new UserType(), $user);
- if ($request->getMethod() == 'POST') {
- $form->bindRequest($request);
- if ($form->isValid()) {
- // encode the password
- $user->initSalt();
- $factory = $this->get('security.encoder_factory');
- $encoder = $factory->getEncoder($user);
- $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
- $user->setPassword($password);
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($user);
- $em->flush();
- return $this->redirect($this->generateUrl('AcmeUserBundle_submitNewSuccess'));
- }
- }
- return $this->render('AcmeUserBundle:User:new.html.twig', array (
- 'form' => $form->createView()
- ));
- }
- public function submitNewSuccessAction() {
- return $this->render("AcmeUserBundle:User:submitNewSuccess.html.twig");
- }
- security:
- encoders:
- AcmeUserBundleEntityUser:
- algorithm: sha512
- iterations: 1
- encode_as_base64: true
- role_hierarchy:
- ROLE_ADMIN: ROLE_USER
- ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
- providers:
- main:
- entity: { class: AcmeUserBundleEntityUser, property: email }
- firewalls:
- secured_area:
- pattern: ^/
- form_login:
- check_path: /login_check
- login_path: /login
- logout:
- path: /logout
- target: /demo/
- anonymous: ~
- self::PROPERTY_CONSTRAINT
- self::CLASS_CONSTRAINT
- validator.debit_card:
- class: MyValidatorConstraintsDebitCardValidator
- tags:
- - { name: validator.constraint_validator, alias: debit_card }
Add Comment
Please, Sign In to add comment