duanleiliu7373 2014-02-27 08:23
浏览 13
已采纳

以编程方式在Symfony2上登录用户

Version: Symfony2.3

There is many link that shows how to login programmatically on Symfony or Changing user Roles dynamically on login. I referred some sites and got the code working.

Below the Process,

  • User login with username and password
  • User will be redirected to Group list page
  • User will select one group
  • We will change the Role mapped with the selected Group dynamically and redirect user

Every thing works fine.

But problem is: I am using below code to get user id in all Pages, is stop working after changing "Token"

$id = $this->getUser()->getId();

I have a function with dynamically change Current User role based on different Groups

// Save the original token in the session 
$originalToken = $this->get("security.context")->getToken();
$this->getRequest()->getSession()->set('original.security.token', $originalToken);

// Create my new custom token (loading the roles of the user)
$token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($this->getUser()->getUsername(), null, "secured_area", array($dynamic_rolename));

// Update the security context with the new token
$this->get("security.context")->setToken($token);
$this->get('session')->set('security_secured_area',serialize($token));

The line $this->getUser()->getId() not working after the above function executed.

Error: FatalErrorException: Error: Call to a member function getId() on a non-object

Security.yml

security:
encoders:
    Core\Bundle\Entity\login:
        algorithm:        sha1
        encode_as_base64: false
        iterations:       1

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

providers:
    administrators:
        entity: 
            class: Core\Bundle\Entity\login
            property: userName

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

    login:
        pattern:  ^/login$
        security: false
        anonymous:  ~
        context: administration

    secured_area:
        pattern:    ^/
        context: administration
        form_login:
            check_path: _security_check
            login_path: /login
            default_target_path: /admin/setfacility
        logout:
            path:   _demo_logout
            target: _demo
        #anonymous: ~
        #http_basic:
        #    realm: "Secured Demo Area"

Why the getUser() object not been set?

  • 写回答

3条回答 默认 最新

  • duanchan9354 2014-02-27 09:07
    关注
    // Create my new custom token (loading the roles of the user)
    $token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($this->getUser()->getUsername(), null, "secured_area", array($dynamic_rolename));
    

    In the above section you are using UsernamePasswordToken for the token creation.This accepts 4 parameters. The first parameter can be :

    The username (like a nickname, email address, etc.), or a UserInterface instance or an object implementing a __toString method.

    You are just passing the username . Pass the user entity instead so that you can have $this->getUser()->getId() working.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题