我为已登录用户设置了安全区域 像这样 - >
firewalls:
members_secured_area:
pattern: ^/
provider: entity_searchers
anonymous: ~
form_login:
check_path: /searchers/login_check
login_path: /searchers/login
default_target_path: /searchers/
logout:
path: /searchers/logout
target: /searchers/login
crm_secured_area:
pattern: ^/crm/
provider: entity_crmusers
anonymous: ~
form_login:
check_path: /crm/login_check
login_path: /crm/login
default_target_path: /crm/customers
logout:
path: /crm/logout
target: /crm/login
remember_me:
#token_provider: entity_crmusers
key: someS3cretKey
name: NameOfTheCookie
lifetime: 3600 # in seconds
secure: false
httponly: true
always_remember_me: false
remember_me_parameter: _remember_me
access_control:
members_login:
path: /searchers/login
roles: IS_AUTHENTICATED_ANONYMOUSLY
members_register:
path: /searchers/register
roles: IS_AUTHENTICATED_ANONYMOUSLY
members_area:
path: /searchers/.*
roles: ROLE_USER
crm_login:
path: /crm/login
roles: IS_AUTHENTICATED_ANONYMOUSLY
crm_register:
path: /crm/register
roles: IS_AUTHENTICATED_ANONYMOUSLY
crm_area:
path: /crm/.*
roles: ROLE_CRM
所以当用户在安全区域时,我可以看到他已被记录, 但当他回到主页或mysite.com/about页面时 我不知道他是否已经登录......
有没有办法知道这个?
答案 0 :(得分:0)
在控制器中,您可以使用$ this-> getUser()获取用户。当用户被记录时,它返回User类的实例,否则返回NULL,因此您可以执行以下操作:
$user = $this->getUser();
$is_logged = isset($user);
$ is_logged是boolean,如果用户已登录则为true,否则为false。
答案 1 :(得分:0)
您仅在安全区域下获得有效用户(估计通过pattern
)。因此,与给定pattern
不匹配的所有内容都不在防火墙下,并且没有用户令牌。您可以通过保护整个站点并使用新的访问规则来更改此设置
firewalls:
crm_secured_area:
pattern: ^/crm/
# ... stuff
members_secured_area:
pattern: ^/
anonymous: ~
# ... all other config
access_control:
# ... all your existing access rules
- { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
最后一条规则允许所有尚未与任何规则匹配的路由匿名访问它们,并且您可以访问所有控制器中的用户令牌(如果经过身份验证)。
答案 2 :(得分:0)
找到了这个解决方案,只需放置在主站点的模式中:^ /(?!crm) 表示不包含文件夹crm。
防火墙: members_secured_area: 模式:^ /(?!crm) 提供者:entity_searchers 匿名:〜 form_login: check_path:/ searchers / login_check login_path:/ searchers / login default_target_path:/ searchers / 登出: 路径:/搜索者/注销 目标:/ searchers / login
crm_secured_area:
pattern: ^/crm/
provider: entity_crmusers
anonymous: ~
form_login:
check_path: /crm/login_check
login_path: /crm/login
default_target_path: /crm/customers
logout:
path: /crm/logout
target: /crm/login
remember_me:
#token_provider: entity_crmusers
key: someS3cretKey
name: NameOfTheCookie
lifetime: 3600 # in seconds
secure: false
httponly: true
always_remember_me: false
remember_me_parameter: _remember_me