Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

[Add] Support roles #30

Open
migo315 opened this issue May 17, 2018 · 2 comments
Open

[Add] Support roles #30

migo315 opened this issue May 17, 2018 · 2 comments
Assignees
Milestone

Comments

@migo315
Copy link
Contributor

migo315 commented May 17, 2018

Add one standard role provider for active features.

Example:

# For one role
constraint: has_role('ROLE_ADMIN')

# For multiple roles
constraint: "has_role('ROLE_ADMIN') or has_role('ROLE_MOD')"
@migo315 migo315 self-assigned this May 17, 2018
migo315 added a commit that referenced this issue Sep 28, 2018
@migo315 migo315 added this to the 3.2 milestone Sep 28, 2018
migo315 added a commit that referenced this issue Sep 28, 2018
@migo315
Copy link
Contributor Author

migo315 commented Sep 28, 2018

Will be implemented in the next version. However, not with the expression language but as a separate field, as in the symfony security component.

flagception:
    features:      
        # Only one role
        feature_123:
            roles: ROLE_ADMIN
            
        # Multiple roles
        feature_456:
            roles: ROLE_ADMIN,ROLE_MOD
        
        # Multiple roles (alternative formatting)
        feature_789:
            roles:
                - ROLE_ADMIN
                - ROLE_MOD

migo315 added a commit that referenced this issue May 5, 2019
migo315 added a commit that referenced this issue May 5, 2019
@migo315 migo315 changed the title Support roles [Add] Support roles May 6, 2019
@dsentker
Copy link

dsentker commented Mar 3, 2020

Perhaps this will help others who are looking for a solution. I am using this implementation to get a user context:

namespace App\Utils\FeatureToggle;

use App\Entity\User;
use Flagception\Model\Context;
use Flagception\Decorator\ContextDecoratorInterface;
use Symfony\Component\Security\Core\Security;

class UserContextDecorator implements ContextDecoratorInterface
{

    /** @var Security */
    private $security;

    public function __construct(Security $security)
    {

        $this->security = $security;
    }

    public function getName(): string
    {
        return 'user_context_decorator';
    }

    public function decorate(Context $context): Context
    {
        $vars = $this->security->getUser() instanceof User
            ? [
                'user_id'    => $this->security->getUser()->getId(),
                'user_roles' => $this->security->getUser()->getRoles(),
            ]
            : [
                'user_id'    => null,
                'user_roles' => [],
            ];

        foreach ($vars as $key => $var) {
            $context->add($key, $var);
        }

        return $context;
    }
}

It is important that the getUser() method is only called in the decorate() method. Security::getUser() is always null when called too early.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants