<commit_before><?php
namespace Platformsh\Cli\Command\User;

use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Util\ActivityUtil;
use Platformsh\Client\Model\EnvironmentAccess;
use Platformsh\Client\Model\ProjectAccess;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UserRoleCommand extends CommandBase
{

    protected function configure()
    {
        $this
            ->setName('user:role')
            ->setDescription("View or change a user's role")
            ->addArgument('email', InputArgument::REQUIRED, "The user's email address")
            ->addOption('role', 'r', InputOption::VALUE_REQUIRED, "A new role for the user")
            ->addOption('level', 'l', InputOption::VALUE_REQUIRED, "The role level ('project' or 'environment')", 'project')
            ->addOption('pipe', null, InputOption::VALUE_NONE, 'Output the role only');
        $this->addProjectOption()
             ->addEnvironmentOption()
             ->addNoWaitOption();
        $this->addExample("View Alice's role on the project", '<EMAIL>');
        $this->addExample("View Alice's role on the environment", '<EMAIL> --level environment');
        $this->addExample("Give Alice the 'contributor' role on the environment 'test'", '<EMAIL> --level environment --environment test --role contributor');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $level = $input->getOption('level');
        $validLevels = ['project', 'environment'];
        if (!in_array($level, $validLevels)) {
            $this->stdErr->writeln("Invalid level: <error>$level</error>");
            return 1;
        }

        $this->validateInput($input, true);

        $project = $this->getSelectedProject();

        $email = $input->getArgument('email');
        foreach ($project->getUsers() as $user) {
            $account = $user->getAccount();
            if ($account['email'] === $email) {
                $selectedUser = $user;
                break;
            }
        }
        if (empty($selectedUser)) {
            $this->stdErr->writeln("User not found: <error>$email</error>");
            return 1;
        }

        $currentRole = false;
        $environmentAccess = false;
        $validRoles = ProjectAccess::$roles;
        if ($level == 'project') {
            $currentRole = $selectedUser['role'];
        }
        elseif ($level == 'environment') {
            if (!$this->hasSelectedEnvironment()) {
                $this->stdErr->writeln('You must specify an environment');
                return 1;
            }
            $environment = $this->getSelectedEnvironment();
            $environmentAccesses = $environment->getUsers();
            foreach ($environmentAccesses as $candidate) {
                if ($candidate->user === $selectedUser->id) {
                    $environmentAccess = $candidate;
                    $currentRole = $environmentAccess->role;
                    break;
                }<commit_msg>Use account cache in user:role<commit_after><?php
namespace Platformsh\Cli\Command\User;

use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Util\ActivityUtil;
use Platformsh\Client\Model\EnvironmentAccess;
use Platformsh\Client\Model\ProjectAccess;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UserRoleCommand extends CommandBase
{

    protected function configure()
    {
        $this
            ->setName('user:role')
            ->setDescription("View or change a user's role")
            ->addArgument('email', InputArgument::REQUIRED, "The user's email address")
            ->addOption('role', 'r', InputOption::VALUE_REQUIRED, "A new role for the user")
            ->addOption('level', 'l', InputOption::VALUE_REQUIRED, "The role level ('project' or 'environment')", 'project')
            ->addOption('pipe', null, InputOption::VALUE_NONE, 'Output the role only');
        $this->addProjectOption()
             ->addEnvironmentOption()
             ->addNoWaitOption();
        $this->addExample("View Alice's role on the project", '<EMAIL>');
        $this->addExample("View Alice's role on the environment", '<EMAIL> --level environment');
        $this->addExample("Give Alice the 'contributor' role on the environment 'test'", '<EMAIL> --level environment --environment test --role contributor');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $level = $input->getOption('level');
        $validLevels = ['project', 'environment'];
        if (!in_array($level, $validLevels)) {
            $this->stdErr->writeln("Invalid level: <error>$level</error>");
            return 1;
        }

        $this->validateInput($input, true);

        $project = $this->getSelectedProject();

        $email = $input->getArgument('email');
        foreach ($project->getUsers() as $user) {
            $account = $this->getAccount($user);
            if ($account['email'] === $email) {
                $selectedUser = $user;
                break;
            }
        }
        if (empty($selectedUser)) {
            $this->stdErr->writeln("User not found: <error>$email</error>");
            return 1;
        }

        $currentRole = false;
        $environmentAccess = false;
        $validRoles = ProjectAccess::$roles;
        if ($level == 'project') {
            $currentRole = $selectedUser['role'];
        }
        elseif ($level == 'environment') {
            if (!$this->hasSelectedEnvironment()) {
                $this->stdErr->writeln('You must specify an environment');
                return 1;
            }
            $environment = $this->getSelectedEnvironment();
            $environmentAccesses = $environment->getUsers();
            foreach ($environmentAccesses as $candidate) {
                if ($candidate->user === $selectedUser->id) {
                    $environmentAccess = $candidate;
                    $currentRole = $environmentAccess->role;
                    break;
                }
{'fraction_non_alphanumeric': 0.14014552356849097, 'fraction_numerical': 0.0009490667510281556, 'mean_word_length': 2.476085761407367, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5199757', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1613, 'n_words': 374}