2016-08-16 8 views
2

私はSymfony(始めたばかりです)のブログプロジェクトに取り組んでいます。投稿者の編集と削除を許可する投票者をしようとするまで管理者を除く)。認証がもう動作しません

理由は分かりませんが(ただし、投票者が動作しない点を除きます): 認証はもう機能せず、サインインさえ機能しません。それは今まで働いていましたが、今は「資格情報が無効です。

誰かが助けてくれればとても嬉しいです。ここに私のファイル: ありがとう!

security: 
role_hierarchy: 
    ROLE_ADMIN:  [ROLE_BLOGGER] 

encoders: 
    BlogBundle\Entity\User : 
     algorithm: sha512 
     iterations: 9616 

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers 
providers: 
    our_db_provider: 
     entity: 
      class: BlogBundle:User 
      property: username 

firewalls: 
    # disables authentication for assets and the profiler, adapt it according to your needs 
    dev: 
     pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     security: false 

    main: 
     pattern: ^/ 
     anonymous: ~ 
     form_login: 
      login_path: login 
      check_path: login 
      default_target_path: /post/ 
      always_use_default_target_path: true 

     logout: 
      path: logout 
      target: /login 

     remember_me: 
      secret: '%secret%' 
      lifetime: 604800 # 1 week in seconds 
      path: /
     # activate different ways to authenticate 

     # http_basic: ~ 
     # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate 

     # form_login: ~ 
     # http://symfony.com/doc/current/cookbook/security/form_login_setup.html 


access_control: 
    - { path: ^/post/[0-9]+/edit, roles: [ROLE_ADMIN, ROLE_BLOGGER] } 
    - { path: ^/post/[0-9]+/delete, roles: ROLE_ADMIN } 
    - { path: ^/post/new, roles: [ROLE_ADMIN, ROLE_BLOGGER] } 
    - { path: ^/user/[0-9]+/edit, roles: [ROLE_ADMIN] } 
    - { path: ^/user/[0-9]+/delete, roles: [ROLE_ADMIN] } 

サービス:

parameters: 


services: 
    post_voter: 
     class:  BlogBundle\Security\PostVoter 
     arguments: ['@security.access.decision_manager'] 
     public:  false 
     tags: 
     - { name: security.voter } 

<?php 
namespace AppBundle\DataFixtures\ORM; 

use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
use Symfony\Component\DependencyInjection\ContainerAwareInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use BlogBundle\Entity\User; 

class LoadUserData implements FixtureInterface, ContainerAwareInterface 
{ 
/** 
* @var ContainerInterface 
*/ 
private $container; 

public function setContainer(ContainerInterface $container = null) 
{ 
    $this->container = $container; 
} 

public function load(ObjectManager $manager) 
{ 
    $userBlogger = new User(); 
    $userBlogger->setUsername('Blogger'); 
    $userBlogger->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36)); 
    $encoder = $this->container->get('security.password_encoder'); 
    $password = $encoder->encodePassword($userBlogger, 'fakepassword'); 
    $userBlogger->setPassword($password); 
    $userBlogger->setRoles(array('ROLE_BLOGGER')); 
    $userBlogger->setMail('[email protected]'); 
    $manager->persist($userBlogger); 

    $userAdmin = new User(); 
    $userAdmin->setUsername('Admin'); 
    $userAdmin->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36)); 
    $encoder = $this->container->get('security.password_encoder'); 
    $password = $encoder->encodePassword($userAdmin, 'fakepassword'); 
    $userAdmin->setPassword($password); 
    $userAdmin->setRoles(array('ROLE_ADMIN')); 
    $userAdmin->setMail('[email protected]'); 
    $manager->persist($userAdmin); 
    $manager->flush(); 
} 
} 

ポストコントローラあなたは私を助けるために任意のファイルを見たい場合は

はちょうど私がそれを

セキュリティを追加することは非常に幸せになります頼みます

<?php 

namespace BlogBundle\Controller; 

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use BlogBundle\Entity\Post; 
use BlogBundle\Form\PostType; 
use BlogBundle\Entity\User; 
use AppBundle\Security; 

/** 
* Post controller. 
*/ 
class PostController extends Controller 
{ 
/** 
* Lists all Post entities. 
* 
*/ 
public function indexAction() 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $posts = $em->getRepository('BlogBundle:Post')->findAll(); 

    return $this->render('post/index.html.twig', array(
     'posts' => $posts, 
    )); 
} 

/** 
* Creates a new Post entity. 
* 
*/ 
public function newAction(Request $request) 
{ 
    $post = new Post(); 
    $form = $this->createForm('BlogBundle\Form\PostType', $post); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $post->setCreated(new \DateTime()); 
     $post->setUpdated(NULL); 
     $user = $this->getUser(); 
     $post->setAuthor($user); 
     $em->persist($post); 
     $em->flush(); 

     return $this->redirectToRoute('post_show', array('id' => $post->getId())); 
    } 

    return $this->render('post/new.html.twig', array(
     'post' => $post, 
     'form' => $form->createView(), 
    )); 
} 

/** 
* Finds and displays a Post entity. 
* 
*/ 
public function showAction(Post $post) 
{ 
    $deleteForm = $this->createDeleteForm($post); 

    return $this->render('post/show.html.twig', array(
     'post' => $post, 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

/** 
* Displays a form to edit an existing Post entity. 
*/ 
public function editAction(Request $request, Post $post) 
{ 
    $this->denyAccessUnlessGranted('edit', $post); 
    $deleteForm = $this->createDeleteForm($post); 
    $editForm = $this->createForm('BlogBundle\Form\PostType', $post); 
    $editForm->handleRequest($request); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $post->setUpdated(new \DateTime()); 
     $em->persist($post); 
     $em->flush(); 

     return $this->redirectToRoute('post_edit', array('id' => $post->getId())); 
    } 

    return $this->render('post/edit.html.twig', array(
     'post' => $post, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

/** 
* Deletes a Post entity. 
* 
*/ 
public function deleteAction(Request $request, Post $post) 
{ 
    $form = $this->createDeleteForm($post); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->remove($post); 
     $em->flush(); 
    } 

    return $this->redirectToRoute('post_index'); 
} 

/** 
* Creates a form to delete a Post entity. 
* 
* @param Post $post The Post entity 
* 
* @return \Symfony\Component\Form\Form The form 
*/ 
private function createDeleteForm(Post $post) 
{ 
    return $this->createFormBuilder() 
    ->setAction($this->generateUrl('post_delete', array('id' => $post->getId()))) 
    ->setMethod('DELETE') 
    ->getForm() 
    ; 
} 
} 

セッションコントローラ

<?php 

namespace BlogBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

class SessionController extends Controller 
{ 
/** 
* @Route("/login") 
*/ 

public function loginAction() 
{ 
    $authenticationUtils = $this->get('security.authentication_utils'); 
    $error = $authenticationUtils->getLastAuthenticationError(); 

    if  ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) 
    { 
     // redirect authenticated users to homepage 
     return $this->redirect($this->generateUrl('post_index')); 
    } 
    return $this->render('BlogBundle:Session:login.html.twig', array(
     'error'   => $error 
    )); 
} 

} 

投票者:

<?php 
namespace BlogBundle\Security; 

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; 
use Symfony\Component\Security\Core\Authorization\Voter\Voter; 
use Symfony\Component\Security\Core\User\UserInterface; 
use AppBundle\Entity\Post; 

// Voter class requires Symfony 2.8 or higher version 
class PostVoter extends Voter 
{ 
const CREATE = 'create'; 
const EDIT = 'edit'; 

/** 
* @var AccessDecisionManagerInterface 
*/ 
private $decisionManager; 

public function __construct(AccessDecisionManagerInterface $decisionManager) 
{ 
    $this->decisionManager = $decisionManager; 
} 

protected function supports($attribute, $subject) 
{ 
    if (!in_array($attribute, array(self::CREATE, self::EDIT))) { 
     return false; 
    } 

    if (!$subject instanceof Post) { 
     return false; 
    } 

    return true; 
} 

protected function voteOnAttribute($attribute, $subject, TokenInterface $token) 
{ 
    $user = $token->getUser(); 
    /** @var Post */ 
    $post = $subject; // $subject must be a Post instance, thanks to the supports method 

    if (!$user instanceof UserInterface) { 
     return false; 
    } 

    switch ($attribute) { 
     case self::CREATE: 
      // if the user is an admin, allow them to create new posts 
      if ($this->decisionManager->decide($token, array('ROLE_ADMIN'))) { 
       return true; 
      } 

      break; 
     case self::EDIT: 
      // if the user is the author of the post, allow them to edit the posts 
      if ($user === $post->getAuthor()) { 
       return true; 
      } 

      break; 
    } 

    return false; 
} 
} 
+0

そして、あなたが '無効credentials'が使用されている場所で、私はここのどこに表示されていない'ソース・バージョンControl' – Hackerman

+0

を使うべき理由です。これはMVCE、http://stackoverflow.com/help/mcveのようにも見えません。 – chris85

+0

@Hackermanそれは私が一日中言っていたことです:( –

答えて

0

回答ありがとうございました。 幸いにも、教義:備品:負荷が私の問題を解決しました。メールの場合は、 "[email protected]"を設定していたので、 "test"は有効なメールではないと考えていました。

おかげ

関連する問題