Added login and posting functionality
Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace GergelyPolonkai\FrontBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
||||
use Symfony\Component\Security\Core\SecurityContext;
|
||||
|
||||
use GergelyPolonkai\FrontBundle\Form\PostType;
|
||||
use GergelyPolonkai\FrontBundle\Entity\Post;
|
||||
|
||||
/**
|
||||
* Description of AdminController
|
||||
*
|
||||
* @author polonkai.gergely
|
||||
*
|
||||
* @Route("/admin")
|
||||
*/
|
||||
class AdminController extends Controller
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* @Route("/login.html")
|
||||
* @Template
|
||||
*/
|
||||
public function loginAction()
|
||||
{
|
||||
$request = $this->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(),
|
||||
);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
30
src/GergelyPolonkai/FrontBundle/Form/PostType.php
Normal file
30
src/GergelyPolonkai/FrontBundle/Form/PostType.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace GergelyPolonkai\FrontBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
|
||||
class PostType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->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';
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %}
|
||||
|
||||
{% block content %}
|
||||
<h3>Bejelentkezés</h3>
|
||||
{% if error %}
|
||||
<div id="error">{{ error }}</div>
|
||||
{% endif %}
|
||||
<form method="post" action="{{ path('GergelyPolonkaiFrontBundle_adminLoginCheck') }}">
|
||||
<input type="text" name="_username" vale="{{ last_username }}" />
|
||||
<input type="password" name="_password" />
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
{% endblock %}
|
@@ -0,0 +1,8 @@
|
||||
{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post" action="{{ path('GergelyPolonkaiFrontBundle_adminNewBlogPost') }}">
|
||||
{{ form_widget(form) }}
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
{% endblock content %}
|
23
src/GergelyPolonkai/FrontBundle/Service/CryptEncoder.php
Normal file
23
src/GergelyPolonkai/FrontBundle/Service/CryptEncoder.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace GergelyPolonkai\FrontBundle\Service;
|
||||
|
||||
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
||||
use JMS\DiExtraBundle\Annotation as DI;
|
||||
|
||||
/**
|
||||
* Description of CryptEncoder
|
||||
*
|
||||
* @author polonkai.gergely
|
||||
*
|
||||
* @DI\Service("gergely_polonkai_front.service.crypt_encoder")
|
||||
*/
|
||||
class CryptEncoder implements PasswordEncoderInterface
|
||||
{
|
||||
public function encodePassword($raw, $salt) {
|
||||
return crypt($raw);
|
||||
}
|
||||
|
||||
public function isPasswordValid($encoded, $raw, $salt) {
|
||||
return (crypt($raw, $encoded) === $encoded);
|
||||
}
|
||||
}
|
@@ -35,6 +35,7 @@ class GeshiHighlight extends \Twig_Extension
|
||||
$this->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();
|
||||
|
Reference in New Issue
Block a user