【问题标题】:Symfony Security not see roles for userSymfony Security 看不到用户的角色
【发布时间】:2015-12-10 14:59:05
【问题描述】:

我为用户创建了 ManyToMany 角色,现在 symfony 看不到我的用户的角色,我不知道为什么 现在我可以输入路径下方的每个路由:^/,为什么?以及如何让 Symfony 知道我的用户扮演什么角色,我不明白。 Screen 和当用户有多个角色时的屏幕 - screen

         - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }

并且我尝试的 ROLE_FREELANCER 的行动是被授予的并且都是错误的

    $security = $this->get('security.context');
    $admin = $security->isGranted('ROLE_ADMIN'); // have false
    $freel = $security->isGranted('ROLE_FREELANCER'); //have false

如何更正设置 security.yml 或我做错了什么?

security.yml:

security:
encoders:
    Artel\ProfileBundle\Entity\Users:
        algorithm:        sha1
        encode_as_base64: false
        iterations:       1

    Symfony\Component\Security\Core\User\User: plaintext

role_hierarchy:
    ROLE_CLIENT:   ROLE_CLIENT
    ROLE_COMPANY:  ROLE_COMPANY,
    ROLE_FREELANCER: ROLE_FREELANCER
    ROLE_ADMIN:    ROLE_ADMIN

providers:
    user:
        entity:
            class: ArtelProfileBundle:Users
            property: email
    chain_provider:
        chain:
            providers: [user_db, in_memory]
            providers: [user_dev, in_memory]
    user_db:
        entity: { class: Artel\ProfileBundle\Entity\Users, property: email }
    in_memory:
       memory:
         users:
            admin_tyty: { password: adminpass_tyty, roles: [ 'ROLE_ADMIN' ] }


firewalls:
    default:
        anonymous: ~
        http_basic: ~
        form_login:
            login_path: /login
            check_path: /login_check
        logout:
              path:   /logout
              invalidate_session: false

    main:
        pattern: ^/
        anonymous: ~
        security: true
        form_login:
            provider: user
            login_path: login
            check_path: login_check
            username_parameter: login[email]
            use_referer: true
        logout:
            path: logout
            target: /

access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/sonata-admin/, roles: ROLE_ADMIN }
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/company, roles:  ROLE_COMPANY  }
    - { path: ^/profile, roles:  ROLE_FREELANCER  }
    - { path: ^/clients, roles:  ROLE_CLIENT  }
    - { path: ^/customer/developers/profile/get, roles:  IS_AUTHENTICATED_ANONYMOUSLY  }
    - { path: ^/customer/developers/bit, roles:  IS_AUTHENTICATED_ANONYMOUSLY  }
    - { path: ^/customer/developers/bitGet, roles:  IS_AUTHENTICATED_ANONYMOUSLY  }

实体角色

 class Role implements RoleInterface
{

/**
 * @ORM\ManyToMany(targetEntity="Users", mappedBy="userRoles")
 *
 */
private $users;

public function __construct()
{
    $this->users = new ArrayCollection();
}

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;


/*
 * methods for RoleInterface
*/
public function getRole()
{
    $this->getName();
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Role
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Add users
 *
 * @param \Artel\ProfileBundle\Entity\Users $users
 * @return Role
 */
public function addUser(\Artel\ProfileBundle\Entity\Users $users)
{
    $this->users[] = $users;

    return $this;
}

/**
 * Remove users
 *
 * @param \Artel\ProfileBundle\Entity\Users $users
 */
public function removeUser(\Artel\ProfileBundle\Entity\Users $users)
{
    $this->users->removeElement($users);
}

/**
 * Get users
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUsers()
{
    return $this->users;
}
}

实体用户

   class Users implements UserInterface
  {
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @Expose()
 * @ORM\GeneratedValue(strategy="AUTO")
 * @Groups({"for_vip", "for_all_projects", "for_profile_project"})
 */
protected $id;

/**
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
 * @ORM\JoinTable(name="user_roles")
 *
 */
private $userRoles;

----------------------Method for Userinterface----------------------

/**
 * Get salt
 *
 * @return string
 */
public function getSalt()
{
    return '';
}

/**
 * @inheritDoc
 */
public function eraseCredentials() { }

/**
 * Геттер для ролей пользователя.
 *
 * @return ArrayCollection A Doctrine ArrayCollection
 */
public function getUserRoles()
{
    return $this->userRoles;
}

/**
 * Геттер для массива ролей.
 *
 * @return array An array of Role objects
 */
public function getRoles()
{
    return $this->getUserRoles()->toArray();
}

----------------------End method for Userinterface----------------------


----------------------Additional Method for Role----------------------
/**
 * Add userRoles
 *
 * @param \Artel\ProfileBundle\Entity\Role $userRoles
 * @return Users
 */
public function addUserRole(\Artel\ProfileBundle\Entity\Role $userRoles)
{
    $this->userRoles[] = $userRoles;

    return $this;
}

/**
 * Remove userRoles
 *
 * @param \Artel\ProfileBundle\Entity\Role $userRoles
 */
public function removeUserRole(\Artel\ProfileBundle\Entity\Role $userRoles)
{
    $this->userRoles->removeElement($userRoles);
}




public function setRole(RoleInterface $role)
{
    if (!$this->userRoles->contains($role)) {
        $this->userRoles->add($role);
    }

    return $this;
}

----------------------End additional Method for Role----------------------

}

【问题讨论】:

    标签: php symfony security doctrine-orm


    【解决方案1】:

    嗯,我不知道这是否会对你有所帮助,但因为我是 symfony 的新手并且对此了解不多,所以我使用了 Friends of Symfony 用户包。它可以安全地处理注册、登录和其他一些功能。我用它来扮演更多角色并为我的页面建立一些安全性。如果我错了,请纠正我。

    编辑:

    这是文档,易于使用。 http://symfony.com/doc/current/bundles/FOSUserBundle/index.html

    【讨论】:

    • 我使用来自Security组件的UserInterface,我认为可以用这个组件创建很多角色
    • 是的,FOS UserBundle 也可以使用。使用 FOSUserBundle,您可以通过命令行提升人员,还在 security.yml 中添加新角色,并且只需一行,您就可以保护控制器中的页面。
    猜你喜欢
    • 2021-03-25
    • 2011-07-21
    • 2020-08-24
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2016-09-09
    • 2016-10-14
    • 1970-01-01
    相关资源
    最近更新 更多