简体   繁体   English

在开发过程中如何向用户授予角色?

[英]How to grant a role to a user during development?

Is it possible to grant a role to the current user (me) dynamically? 是否可以动态地将角色授予当前用户(我)?

I'm working on a website on my local computer, a server is hosting the production version of a website. 我正在本地计算机上的网站上工作,一台服务器托管着该网站的生产版本。 The 2 versions ( dev and prod ) use an authentication system which use a CAS server, we changed the firewall to avoid this authentication system in dev environment. 这两个版本( devprod )使用的身份验证系统使用CAS服务器,为了避免在dev环境中使用此身份验证系统,我们更改了防火墙。 But when working locally I am only logged in as an anonymous user, the firewall is configured to allow access to the administration to an anonymous user only if he is connected from the 127.0.0.1 address. 但是,在本地工作时,我仅以匿名用户身份登录,防火墙配置为仅当匿名用户从127.0.0.1地址连接时才允许访问管理。 So I can see any part of the administration but I can't simulate the access from the different roles during the development. 因此,我可以看到管理的任何部分,但是无法在开发过程中模拟来自不同角色的访问。

In other words, I'm looking for something like this: 换句话说,我正在寻找这样的东西:

class AcmeController
{
    public AcmeAction()
    {
        ...
        $user->setRole('ROLE_ADMIN');
        ...
    }
}

Or any other way to set the role. 或任何其他方式设置角色。

Update: 更新:

I tested this: 我测试了这个:

class AcmeController
{
    public AcmeAction()
    {
        ...
        $user = $this->getUser();
        $user->setRole('ROLE_ADMIN');
        ...
    }
}

This brings a fatal error: 这带来了致命错误:

Fatal error: Call to a member function addRole() on a non-object in [...]AcmeController.php on line [...] 致命错误:在第[...]行的[...] AcmeController.php中的非对象上调用成员函数addRole()

I think it's due to the fact the current user is anonymous so it doesn't exist in the DB and I can't access to the User object. 我认为这是由于当前用户是匿名用户,因此它在数据库中不存在,并且我无法访问User对象。

I needed to load any user from the database by using the User entity directly in dev environment and using a SSO/LDAP bundle in prod environment. 我需要通过直接在dev环境中使用User实体,并在prod环境中使用SSO / LDAP捆绑软件从数据库中加载任何用户。

The solution was to change configuration files in order to use different login controllers in dev and prod environments: 解决方案是更改配置文件,以便在devprod环境中使用不同的登录控制器:

app/config/security.yml : where the variables will be loaded app / config / security.yml :将在其中加载变量的位置

security:
    [...]
    providers:
        %security_providers%
    firewalls:
        dev:
            [...]
        user_manager: %security_firewalls_user_management%

app/config/config_dev.yml and app/config/config_test.yml : the variables are declared app / config / config_dev.ymlapp / config / config_test.yml :声明变量

The login use a traditional login form 登录使用传统的登录表单

parameters:
    security_providers:
        user_db:
            entity:
                class: Acme\AcmeBundle\Entity\User
                property: id
    security_firewalls_user_management:
        pattern:    ^/
        anonymous: ~
        form_login: ~
        logout: ~
        switch_user: true
    login_controller: "AcmeDevBundle:Security:login"

app/config/config_prod.yml : the variables are declared app / config / config_prod.yml :声明变量

parameters:
    security_providers:
        user_auth:
            entity:
                class: Acme\AcmeBundle\Entity\User
                property: other_id
   security_firewalls_user_management:
        pattern: ^/
        anonymous: ~
        trusted_sso:
            manager: user_sso
            login_action: false
            logout_action: false
            check_path: /
            login_path: page_login
        logout: true
        switch_user: true
    login_controller: "AcmeBundle:Main:login"

app/AppKernel.php app / AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            [...]
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            [...]
            # Bundle only activated in dev and test environments
            $bundles[] = new Acme\DevBundle\AcmeDevBundle();
        }

        return $bundles;
    }
[...]

The DevBundle is only loaded in dev and test environments, it uses a traditional login form and display a list of users than I can impersonate. DevBundle仅在devtest环境中加载,它使用传统的登录表单并显示我无法模拟的用户列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM