我正在管理用户对我网站上某些页面的访问权限。我有 1 种完全经过身份验证的用户(role: ROLE_MEMBER, based on ROLE_USER),当然还有匿名用户。
假设我在网站上有 2 个页面 ( /index, /account),两者都带有secure: true. 假设/index需要role: IS_AUTHENTICATED_ANONYMOUSLY和/account需要role: IS_AUTHENTICATED_FULLY(对于ROLE_MEMBER)。
我想在两个页面上为登录成员显示特殊的“帐户菜单”,所以在模板中我检查is_granted('ROLE_MEMBER')。
当然,这对 很有用/account。问题是,当成员被导航到/index(这是“匿名”页面)函数is_granted('ROLE_MEMBER')时返回0,然后is_granted('IS_AUTHENTICATED_ANONYMOUSLY')返回1。当会员再次导航到/account时,一切正常,无需重新输入密码(这意味着会员仍未注销)。
那么,问题是如何检测/index页面模板内登录用户的成员是什么?
更新。@alessandro1997 这是我的app/config/security.yml配置
security:
encoders:
AG\MemberBundle\Entity\Member:
id: member_saltedpassword_encoder
role_hierarchy:
ROLE_MEMBER: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_MEMBER, ROLE_ALLOWED_TO_SWITCH]
providers:
member_db:
entity: { class: AGMemberBundle:Member, property: email }
firewalls:
members:
pattern: ^/account
security: true
form_login:
login_path: /account/login
check_path: /account/login/check
post_only: true
username_parameter: _email
password_parameter: _password
default_target_path: /account/
always_use_default_target_path: true
# csrf token options
csrf_parameter: _csrf_token
intention: authenticate
provider: member_db
logout:
path: /account/logout
target: /
remember_me:
key: "qwewqeqwerwxeweqweqwe"
lifetime: 3600
path: /
domain: ~ # Defaults to the current domain from $_SERVER
pages:
pattern: ^/
security: true
anonymous: ~
access_control:
- { path: ^/account, roles: IS_AUTHENTICATED_FULLY }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }