Symfony 3-无法在PROD环境中正确管理我的用户角色

时间:2019-05-22 11:36:37

标签: symfony roles

我在symfony 3上遇到问题。 我正在尝试从创建的页面管理角色。 我使用的角色系统与FOSUserBundle使用的角​​色系统相同。

@(Html.Kendo().Grid<ClinicManagement.Models.Reagent>().Name("PersonGrid")
                            .Name("PersonGrid")
                            .Columns(columns =>
                            {
                                columns.Bound(p => p.Name).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").SuggestionOperator(FilterType.Contains))).Width(90);
                                columns.Bound(p => p.Family).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").SuggestionOperator(FilterType.Contains))).Width(90);
                                columns.Bound(p => p.CardNumber).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").SuggestionOperator(FilterType.Contains))).Width(90);
                                columns.Command(command => command.Custom("ViewDetails").Click("showDetails")).Width(150);
                            })
                           .DataSource(dataSource => dataSource
                               .Ajax()
                               .PageSize(20)
                               .Events(events => events.Error("error_handler"))
                               .Sort(sort => sort.Add(p => p.Name).Ascending())
                               .Model(model => model.Id(p => p.Id))
                               .Create(update => update.Action("Create", "Reagents"))
                               .Read(read => read.Action("ReadReagent", "Reagents"))
                               .Update(update => update.Action("Edit", "Reagents"))
                               .Destroy(destroy => destroy.Action("Delete", "Reagents"))
                  ))

当我处于“ PREPROD”环境中时,一切正常,我的角色变化很好。但是由于我切换到“ PROD”,所以当我的用户没有角色时(因此它自动是ROLE_USER),十分之九(十分之九),我无法分配任何其他角色。但是,如果他具有ROLE_USER以外的其他角色,那么我可以为他分配任何角色。为什么在preprod中不起作用?我不知道...

在树枝上,我有一个带有列表选项的用户列表,我可以在其中选择新角色。然后,它更新页面。我的请求检索了新角色的标签并被分配了

嫩枝:

/**
 * @ORM\Column(type="array")
 */
protected $roles = [];


/**
 * Constructor
 */
public function __construct()
{
    $this->setIsActif(true);
    $this->roles = array();

}

 /**
 * {@inheritdoc}
 */
public function addRole($role)
{
    $role = strtoupper($role);
    if ($role === ['ROLE_USER']) {
        return $this;
    }

    if (!in_array($role, $this->roles, true)) {
        $this->roles[] = $role;
    }

    return $this;
}

/**
 * {@inheritdoc}
 * @return array
 */
public function getRoles()
{
    return array_unique(array_merge(['ROLE_USER'], $this->roles));
}

/**
 * Vérifie si l'utilisateur possède le rôle passé en paramètre
 * {@inheritdoc}
 * 
 * @param string
 * @return bool
 */
public function hasRole($role)
{
    return in_array(strtoupper($role), $this->getRoles(), true);
}

/**
 * Supprimer un rôle
 * {@inheritdoc}
 * 
 * @return UserCas
 */
public function removeRole($role)
{
    if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
        unset($this->roles[$key]);
        $this->roles = array_values($this->roles);
    }

    return $this;
}

/**
 * Set roles
 * {@inheritdoc}
 * 
 * @return UserCas
 */
public function setRoles(array $roles)
{
    $this->roles = array();

    foreach ($roles as $role) {
        $this->addRole($role);
    }

    return $this;
}

/**
 * Réinitialise les rôles de l'utilisateur
 */
public function resetRoles()
{
    $this->roles = [];
}

控制器:

<form action="{{path('user_role_change', {'username': unUser.username })}}" method="POST">
                        <select id="role" name="role" onChange="this.form.submit();">
                            {% for unRole in listeRoles %}
                            <option value="{{unRole}}" {% if unRole == 'User' %} selected {% endif %}>{{unRole}}</option>
                            {% endfor %}
                        </select>
                    </form>

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试修复addRole()方法,如下所示:

 /**
 * {@inheritdoc}
 */
public function addRole($role)
{
    $role = strtoupper($role);
    if ($role === 'ROLE_USER') {
        return $this;
    }

    if (!in_array($role, $this->roles, true)) {
        $this->roles[] = $role;
    }

    return $this;
}