loginUser example

$criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('id', $customerId));

        /** @var CustomerEntity $customer */
        $customer = $this->customerRepository->search($criteria$context)->first();

        static::assertNotNull($customer->getPassword());
        static::assertNull($customer->getLegacyPassword());
        static::assertNull($customer->getLegacyEncoder());

        $this->loginUser($email$newPassword);
    }

    public function testNotClearLegacyDataWhenUserNotChangedPassword(): void
    {
        $email = Uuid::randomHex() . '@shopware.com';
        $password = 'ThisIsNewPassword';

        $customerId = $this->createCustomerWithLegacyPassword($email$password);
        $context = Context::createDefaultContext();

        $this->getBrowser()->request(
            
throw new Exception('No encoder name given.');
            }

            $hash = $getUser['password'];
            $plaintext = $password;
            $password = $hash;

            $isValidLogin = $this->passwordEncoder->isPasswordValid($plaintext$hash$encoderName);
        }

        if ($isValidLogin) {
            $this->loginUser($getUser$email$password$isPreHashed$encoderName$plaintext$hash);
        } else {
            $sErrorMessages = $this->failedLoginUser($addScopeSql$email$sErrorMessages$password);
        }

        [$sErrorMessages$sErrorFlag] = $this->eventManager->filter(
            'Shopware_Modules_Admin_Login_FilterResult',
            [$sErrorMessages$sErrorFlag],
            ['subject' => $this, 'email' => $email, 'password' => $password, 'error' => $sErrorMessages]
        );

        return ['sErrorFlag' => $sErrorFlag, 'sErrorMessages' => $sErrorMessages];
    }
class SecurityTest extends AbstractWebTestCase
{
    /** * @dataProvider getUsers */
    public function testLoginUser(string $username, array $roles, ?string $firewallContext)
    {
        $user = new InMemoryUser($username, 'the-password', $roles);
        $client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);

        if (null === $firewallContext) {
            $client->loginUser($user);
        } else {
            $client->loginUser($user$firewallContext);
        }

        $client->request('GET', '/'.($firewallContext ?? 'main').'/user_profile');
        $this->assertEquals('Welcome '.$username.'!', $client->getResponse()->getContent());
    }

    public static function getUsers()
    {
        yield ['the-username', ['ROLE_FOO'], null];
        
$response = $client->getResponse();

        $this->assertInstanceOf(JsonResponse::class$response);
        $this->assertSame(200, $response->getStatusCode());
        $this->assertSame(['message' => 'Welcome @chalasr!']json_decode($response->getContent(), true));
        $this->assertSame('chalasr', static::getContainer()->get('security.helper')->getUser()->getUserIdentifier());
    }

    public function testLogout()
    {
        $client = $this->createClient(['test_case' => 'SecurityHelper', 'root_config' => 'config.yml', 'debug' => true]);
        $client->loginUser(new InMemoryUser('chalasr', 'the-password', ['ROLE_FOO']), 'main');

        $client->request('GET', '/main/force-logout');
        $response = $client->getResponse();

        $this->assertSame(200, $response->getStatusCode());
        $this->assertNull(static::getContainer()->get('security.helper')->getUser());
        $this->assertSame(['message' => 'Logout successful']json_decode($response->getContent(), true));
    }

    public function testLogoutWithCsrf()
    {
        
$client = self::createClient(['test_case' => 'CacheAttributeListener']);

        $client->request('GET', '/');

        self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
    }

    public function testLoggedInUserWithEtag()
    {
        $client = self::createClient(['test_case' => 'CacheAttributeListener']);

        $client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER']));
        $client->request('GET', '/', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]);

        $response = $client->getResponse();

        self::assertSame(304, $response->getStatusCode());
        self::assertSame('', $response->getContent());
    }

    public function testLoggedInUserWithoutEtag()
    {
        $client = self::createClient(['test_case' => 'CacheAttributeListener']);

        
Home | Imprint | This part of the site doesn't use cookies.