2014-09-11 21 views
-2
# security.yml 

security: 
    encoders: 
     Cl\AdminBundle\Entity\Admin: 
      algorithm: sha1 
      encode_as_base64: false 
      iterations: 1 


    role_hierarchy: 
     ROLE_ADMIN:  ROLE_ADMIN 

    providers: 
     cl_admin_security: 
      id: cl_admin_security_provider 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     admin_area: 
      pattern: ^/ 
      provider: cl_admin_security 
      anonymous: ~ 
      form_login: 
       login_path: /security 
       check_path: /security_check 
       default_target_path: /admin 
       username_parameter: _useremail 
       password_parameter: _userpassword 
      logout: 
       path: _demo_logout 
       target: _demo 

    access_control: 
     - { path: ^/security, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin, roles: ROLE_ADMIN } 


# routing.yml 
_security_check: 
    path: /security_check 

_security: 
    path: /security 
    defaults: { _controller: ClSecurityBundle:Login:index } 


// Cl\CommonBundle\Entity\Admin\ClSecurityProvider.php 

namespace Cl\SecurityBundle\Security; 
use Symfony\Component\Security\Core\User\UserProviderInterface; 
use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; 
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; 
use Cl\CommonBundle\Entity\Admin; 
use Doctrine\ORM\EntityManager; 
use Doctrine\ORM\NoResultException; 

class ClSecurityProvider implements UserProviderInterface 
{ 
    private $em; 
    public function __construct(EntityManager $em) 
    { 
     $this->em = $em; 
    } 
    /** 
    * Loads the user for the given username. 
    * 
    * This method must throw UsernameNotFoundException if the user is not 
    * found. 
    * 
    * @throws UsernameNotFoundException if the user is not found 
    * @param string $username The username 
    * 
    * @return UserInterface 
    */ 
    public function loadUserByUsername($username) 
    { 
     $admin = $this->findUserBy(array("email" => $username)); 

     if (!$admin) { 
      $message = sprintf(
        'Unable to find an active admin ClCommonBundle:Admin object identified by "%s".', $admin 
      ); 
      throw new UsernameNotFoundException($message); 
     } 
     return $admin; 
    } 
    public function refreshUser(UserInterface $admin) 
    { 
     //return $this->loadUserByUsername($admin->getUsername()); 
     $class = get_class($admin); 
     if (!$this->supportsClass($class)) { //This should be $class not $user 
      $message = sprintf('Unsupported class type : %s', $class); 
      throw new UnsupportedUserException($message); 
     } 
     return $this->find($user->getId()); 
    } 
    /** 
    * Whether this provider supports the given user class 
    * 
    * @param string $class 
    * 
    * @return Boolean 
    */ 
    public function supportsClass($class) 
    { 
     return $class == "Cl\CommonBundle\Entity\Admin"; 
     //return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName()); 
    } 
    /** 
    * findUserBy 
    * 
    * @param array $criteria 
    * 
    * @return mixed 
    */ 
    protected function findUserBy(array $criteria) 
    { 
     $repository = $this->em->getRepository('Cl\CommonBundle\Entity\Admin'); 
     return $repository->findOneBy($criteria); 
    } 
} 

我沒有找到編碼器的問題已配置帳戶「CL \ CommonBundle \實體\管理員」。無編碼器已經配置了帳戶「Collegelife CommonBundle 實體管理員」

我也曾在SecurityBundle \安全\ ClsecurityProvider.php實施ClSecurityProvider

誰能幫我對這個問題我從3-4天來襲。 我還沒有完成自定義驗證模塊。 我想使用我的自定義驗證碼。

+0

你的問題還不清楚,請提供更多信息/規格 – 2014-09-11 12:15:04

+0

我想爲我的應用程序創建管理面板。所以我使用了symfony 2.3並使用自定義用戶提供程序進行身份驗證。現在我收到一個錯誤:沒有編碼器已被配置爲賬戶「Cl \ CommonBundle \ Entity \ Admin」。你可以閱讀我的代碼provider.yml,service.yml,routing.yml。現在我包含service.yml代碼也是.... – 2014-09-11 12:17:37

+0

service.yml代碼。 參數: cl_admin_security_provider.class:CL \ SecurityBundle \安全\ clSecurityProvider 服務: cl_admin_security_provider: 類:%cl_admin_security_provider.class% 參數:[@ doctrine.orm.entity_manager] #arguments:[CL \ CommonBundle \ Entity \ Admin] – 2014-09-11 12:20:32

回答

0

不鬆時編碼輪:) ...嘗試Symfony2的這實在是太棒了這個管理員束....

這裏是一個演示:

http://demo.sonata-project.org/admin/login(登錄:admin /傳遞者:admin)

,這裏是官方文檔

http://sonata-project.org/

,我們在所有的OU使用r項目並使用fosUserBundle管理身份驗證,這也是「必備」捆綁包!

https://github.com/FriendsOfSymfony/FOSUserBundle

+0

是的,我引用了奏鳴曲和fos,但我應該嘗試成功創建一次。我很欣賞你的迴應,但我想自己實現這些東西。 – 2014-09-11 12:35:48

+1

@Dipak chavda Symfony2具有很高的學習曲線,尤其是如果您來自程序編碼並且OOP知識欠佳。因此,如果您有時間需要在Symfony2 Docs(尤其是安全組件)的應用程序研究中使用,那麼也可以使用其他人提供的一些好的軟件包。祝你好運! – 2014-09-11 13:18:20

+0

謝謝你,但我怎麼知道沒有審判,我得到這個錯誤的配置,所以作爲我關心的問題OOPs知識無關緊要。我非常感謝你的回答。 – 2014-09-11 13:41:23

1

你有一個錯字:

encoders: 
    Cl\AdminBundle\Entity\Admin 

應該

encoders: 
    Cl\CommonBundle\Entity\Admin 

我還要指出的是,你的標題有「Collegelife \ CommonBundle \實體\管理員」。

+0

謝謝塞拉德,你指給我正確的地方。我做到了.. – 2014-09-12 05:15:55

+1

沒問題。感謝您接受答案。等一下... – Cerad 2014-09-12 13:13:18