From 04d408aee0865b632b6e583140a20f626bde7665 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Tue, 4 Sep 2012 17:21:04 +0200 Subject: [PATCH] Added login and posting functionality Signed-off-by: Gergely Polonkai (W00d5t0ck) --- app/AppKernel.php | 1 + .../Version20120904170638.php | 28 +++++++ app/config/security.yml | 24 +++--- composer.json | 7 +- .../Controller/AdminController.php | 79 +++++++++++++++++++ .../FrontBundle/Entity/Post.php | 31 +++++++- .../FrontBundle/Entity/User.php | 50 +++++++++++- .../FrontBundle/Form/PostType.php | 30 +++++++ .../Resources/views/Admin/login.html.twig | 13 +++ .../views/Admin/newBlogPost.html.twig | 8 ++ .../FrontBundle/Service/CryptEncoder.php | 23 ++++++ .../GeshiBundle/Twig/GeshiHighlight.php | 1 + 12 files changed, 277 insertions(+), 18 deletions(-) create mode 100644 app/DoctrineMigrations/Version20120904170638.php create mode 100644 src/GergelyPolonkai/FrontBundle/Controller/AdminController.php create mode 100644 src/GergelyPolonkai/FrontBundle/Form/PostType.php create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/views/Admin/login.html.twig create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/views/Admin/newBlogPost.html.twig create mode 100644 src/GergelyPolonkai/FrontBundle/Service/CryptEncoder.php diff --git a/app/AppKernel.php b/app/AppKernel.php index eb09665..a166712 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -23,6 +23,7 @@ class AppKernel extends Kernel new Io\TcpdfBundle\IoTcpdfBundle(), new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(), new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(), + new Ivory\CKEditorBundle\IvoryCKEditorBundle(), new GergelyPolonkai\FrontBundle\GergelyPolonkaiFrontBundle(), new GergelyPolonkai\GeshiBundle\GergelyPolonkaiGeshiBundle(), ); diff --git a/app/DoctrineMigrations/Version20120904170638.php b/app/DoctrineMigrations/Version20120904170638.php new file mode 100644 index 0000000..cae25de --- /dev/null +++ b/app/DoctrineMigrations/Version20120904170638.php @@ -0,0 +1,28 @@ +abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + + $this->addSql("ALTER TABLE users ADD password VARCHAR(50) NOT NULL"); + } + + public function down(Schema $schema) + { + // this down() migration is autogenerated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); + + $this->addSql("ALTER TABLE users DROP password"); + } +} diff --git a/app/config/security.yml b/app/config/security.yml index e01c1c2..38b3819 100644 --- a/app/config/security.yml +++ b/app/config/security.yml @@ -4,18 +4,18 @@ jms_security_extra: security: encoders: - Symfony\Component\Security\Core\User\User: plaintext + GergelyPolonkai\FrontBundle\Entity\User: + id: gergely_polonkai_front.service.crypt_encoder role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] providers: - in_memory: - memory: - users: - user: { password: userpass, roles: [ 'ROLE_USER' ] } - admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] } + gergely_polonkai_front.entity.user: + entity: + class: GergelyPolonkai\FrontBundle\Entity\User + property: username firewalls: dev: @@ -23,17 +23,17 @@ security: security: false login: - pattern: ^/demo/secured/login$ + pattern: ^/admin/login.html$ security: false secured_area: - pattern: ^/demo/secured/ + pattern: ^/admin form_login: - check_path: /demo/secured/login_check - login_path: /demo/secured/login + check_path: /admin/login-check.do + login_path: /admin/login.html logout: - path: /demo/secured/logout - target: /demo/ + path: /admin/logout + target: / #anonymous: ~ #http_basic: # realm: "Secured Demo Area" diff --git a/composer.json b/composer.json index 7f47d0b..97a154b 100644 --- a/composer.json +++ b/composer.json @@ -24,9 +24,10 @@ "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*", - "easybook/geshi": "dev-master", - "stof/doctrine-extensions-bundle": "dev-master", - "doctrine/doctrine-migrations-bundle": "dev-master", + "easybook/geshi": "dev-master", + "stof/doctrine-extensions-bundle": "dev-master", + "doctrine/doctrine-migrations-bundle": "dev-master", + "egeloen/ckeditor-bundle": "dev-master", "gergelypolonkai/tcpdfbundle": "dev-master" }, "scripts": { diff --git a/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php b/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php new file mode 100644 index 0000000..df4dc57 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php @@ -0,0 +1,79 @@ +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 array( + 'last_username' => $session->get(SecurityContext::LAST_USERNAME), + 'error' => $error, + ); + } + + /** + * @Route("/login-check.do", name="GergelyPolonkaiFrontBundle_adminLoginCheck") + */ + public function loginCheckAction() + { + } + + /** + * @Route("/blog/post", name="GergelyPolonkaiFrontBundle_adminNewBlogPost") + * @Template + */ + public function newBlogPostAction() + { + $post = new Post(); + $form = $this->createForm(new PostType(), $post); + $request = $this->getRequest(); + $user = $this->get('security.context')->getToken()->getUser(); + + if ($request->getMethod() === 'POST') { + $form->bind($request); + if ($form->isValid()) { + $post->setUser($user); + $em = $this->getDoctrine()->getEntityManager(); + $em->persist($post); + $em->flush(); + + return $this->redirect($this->generateUrl('GergelyPolonkaiFrontBundle_adminNewBlogPost')); + } + } + + return array( + 'form' => $form->createView(), + ); + } +} diff --git a/src/GergelyPolonkai/FrontBundle/Entity/Post.php b/src/GergelyPolonkai/FrontBundle/Entity/Post.php index 82b91b8..866aff3 100644 --- a/src/GergelyPolonkai/FrontBundle/Entity/Post.php +++ b/src/GergelyPolonkai/FrontBundle/Entity/Post.php @@ -4,6 +4,7 @@ namespace GergelyPolonkai\FrontBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as GedmoORM; +use Symfony\Component\Validator\Constraints as Assert; /** * Description of Post @@ -36,6 +37,7 @@ class Post * @var string $title * * @ORM\Column(type="string", length=100) + * @Assert\NotBlank() */ private $title; @@ -51,6 +53,7 @@ class Post * @var string $content * * @ORM\Column(type="text", nullable=false) + * @Assert\NotBlank() */ private $content; @@ -160,4 +163,30 @@ class Post { return $this->createdAt; } -} + + /** + * Set slug + * + * @param string $slug + * @return Post + */ + public function setSlug($slug) + { + $this->slug = $slug; + + return $this; + } + + /** + * Set createdAt + * + * @param \DateTime $createdAt + * @return Post + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + + return $this; + } +} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Entity/User.php b/src/GergelyPolonkai/FrontBundle/Entity/User.php index 0bc5f30..280636c 100644 --- a/src/GergelyPolonkai/FrontBundle/Entity/User.php +++ b/src/GergelyPolonkai/FrontBundle/Entity/User.php @@ -2,6 +2,7 @@ namespace GergelyPolonkai\FrontBundle\Entity; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Security\Core\User\UserInterface; /** * Description of User @@ -11,7 +12,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Entity * @ORM\Table(name="users") */ -class User +class User implements UserInterface { /** * @ORM\Id @@ -30,6 +31,29 @@ class User */ private $name; + /** + * @var string $password + * + * @ORM\Column(type="string", length=50, nullable=false) + */ + private $password; + + public function __toString() + { + return $this->name . '(' . $this->username . ')'; + } + + public function getSalt() { + return $this->password; + } + + public function eraseCredentials() { + } + + public function getRoles() { + return array('ROLE_ADMIN'); + } + /** * Get id * @@ -85,4 +109,26 @@ class User { return $this->name; } -} + + /** + * Set password + * + * @param string $password + * @return User + */ + public function setPassword($password) + { + $this->password = $password; + return $this; + } + + /** + * Get password + * + * @return string + */ + public function getPassword() + { + return $this->password; + } +} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Form/PostType.php b/src/GergelyPolonkai/FrontBundle/Form/PostType.php new file mode 100644 index 0000000..a8a2289 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Form/PostType.php @@ -0,0 +1,30 @@ +add('title') + ->add('content', 'ckeditor') + ; + } + + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'GergelyPolonkai\FrontBundle\Entity\Post' + )); + } + + public function getName() + { + return 'gergelypolonkai_frontbundle_posttype'; + } +} diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/login.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/login.html.twig new file mode 100644 index 0000000..0068e6d --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/login.html.twig @@ -0,0 +1,13 @@ +{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %} + +{% block content %} +

Bejelentkezés

+{% if error %} +
{{ error }}
+{% endif %} +
+ + + +
+{% endblock %} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/newBlogPost.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/newBlogPost.html.twig new file mode 100644 index 0000000..c05de15 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/newBlogPost.html.twig @@ -0,0 +1,8 @@ +{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %} + +{% block content %} +
+{{ form_widget(form) }} + +
+{% endblock content %} \ No newline at end of file diff --git a/src/GergelyPolonkai/FrontBundle/Service/CryptEncoder.php b/src/GergelyPolonkai/FrontBundle/Service/CryptEncoder.php new file mode 100644 index 0000000..700ffa5 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Service/CryptEncoder.php @@ -0,0 +1,23 @@ +geshi->set_source($code); $this->geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $this->geshi->enable_keyword_links(false); + $this->geshi->set_overall_class("code"); $this->geshi->enable_classes(); return $this->geshi->parse_code();