Skip to content

Commit

Permalink
Merge pull request #116 from DaniCristante/feature/user-bundle
Browse files Browse the repository at this point in the history
Make RunroomUserBundle work without reset password feature
  • Loading branch information
jordisala1991 committed Dec 7, 2021
2 parents 174d3de + 868709f commit 077d9cf
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
Expand Up @@ -60,6 +60,9 @@ public function load(array $configs, ContainerBuilder $container): void

if (isset($bundles['SonataAdminBundle'])) {
$loader->load('admin.php');

$container->getDefinition('runroom_user.twig.global_variables')
->setArgument('$hasRequestPasswordEnabled', $config['reset_password']['enabled']);
}

if ($this->isConfigEnabled($container, $config['reset_password'])) {
Expand Down
47 changes: 42 additions & 5 deletions packages/user-bundle/src/Entity/ResetPasswordRequest.php
Expand Up @@ -14,15 +14,12 @@
namespace Runroom\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Runroom\UserBundle\Model\ResetPasswordRequestInterface;
use Runroom\UserBundle\Model\UserInterface;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestTrait;

/** @ORM\Entity */
class ResetPasswordRequest implements ResetPasswordRequestInterface
{
use ResetPasswordRequestTrait;

/**
* @ORM\Id
* @ORM\GeneratedValue
Expand All @@ -36,10 +33,25 @@ class ResetPasswordRequest implements ResetPasswordRequestInterface
*/
private UserInterface $user;

/** @ORM\Column(type="string", length=20) */
private string $selector;

/** @ORM\Column(type="string", length=100) */
private string $hashedToken;

/** @ORM\Column(type="datetime_immutable") */
private \DateTimeImmutable $requestedAt;

/** @ORM\Column(type="datetime_immutable") */
private \DateTimeInterface $expiresAt;

public function __construct(UserInterface $user, \DateTimeInterface $expiresAt, string $selector, string $hashedToken)
{
$this->user = $user;
$this->initialize($expiresAt, $selector, $hashedToken);
$this->requestedAt = new \DateTimeImmutable('now');
$this->expiresAt = $expiresAt;
$this->selector = $selector;
$this->hashedToken = $hashedToken;
}

public function getId(): ?int
Expand All @@ -51,4 +63,29 @@ public function getUser(): object
{
return $this->user;
}

public function getSelector(): string
{
return $this->selector;
}

public function getHashedToken(): string
{
return $this->hashedToken;
}

public function getRequestedAt(): \DateTimeInterface
{
return $this->requestedAt;
}

public function getExpiresAt(): \DateTimeInterface
{
return $this->expiresAt;
}

public function isExpired(): bool
{
return $this->expiresAt->getTimestamp() <= time();
}
}
28 changes: 28 additions & 0 deletions packages/user-bundle/src/Model/ResetPasswordRequestInterface.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Runroom package.
*
* (c) Runroom <runroom@runroom.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Runroom\UserBundle\Model;

use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface as SymfonyCastsResetPasswordRequestInterface;

if (interface_exists(SymfonyCastsResetPasswordRequestInterface::class)) {
/** @psalm-suppress UnrecognizedStatement */
interface ResetPasswordRequestInterface extends SymfonyCastsResetPasswordRequestInterface
{
}
} else {
/** @psalm-suppress UnrecognizedStatement */
interface ResetPasswordRequestInterface
{
}
}
3 changes: 2 additions & 1 deletion packages/user-bundle/src/Resources/config/admin.php
Expand Up @@ -34,5 +34,6 @@
->tag('sonata.admin', ['manager_type' => 'orm', 'label' => 'User']);

$services->set('runroom_user.twig.global_variables', GlobalVariables::class)
->arg('$pool', new ReferenceConfigurator('sonata.admin.pool'));
->arg('$pool', new ReferenceConfigurator('sonata.admin.pool'))
->arg('$hasRequestPasswordEnabled', null);
};
Expand Up @@ -60,7 +60,10 @@
</div>
</div>
</form>
<a href="{{ path('runroom_user_forgot_password_request') }}">{{ 'forgotten_password'|trans({}, 'RunroomUserBundle') }}</a>

{% if runroom_user.hasRequestPasswordEnabled %}
<a href="{{ path('runroom_user_forgot_password_request') }}">{{ 'forgotten_password'|trans({}, 'RunroomUserBundle') }}</a>
{% endif %}
</div>
</div>
{% endblock sonata_wrapper %}
10 changes: 9 additions & 1 deletion packages/user-bundle/src/Twig/GlobalVariables.php
Expand Up @@ -20,14 +20,22 @@ final class GlobalVariables
{
private Pool $pool;

public function __construct(Pool $pool)
private bool $hasRequestPasswordEnabled;

public function __construct(Pool $pool, bool $hasRequestPasswordEnabled)
{
$this->pool = $pool;
$this->hasRequestPasswordEnabled = $hasRequestPasswordEnabled;
}

/** @phpstan-return AdminInterface<object> */
public function getUserAdmin(): AdminInterface
{
return $this->pool->getAdminByAdminCode('runroom_user.admin.user');
}

public function getHasRequestPasswordEnabled(): bool
{
return $this->hasRequestPasswordEnabled;
}
}
15 changes: 14 additions & 1 deletion packages/user-bundle/tests/Unit/GlobalVariablesTest.php
Expand Up @@ -28,10 +28,23 @@ public function itGetsAdminByClass(): void
$container->set('runroom_user.admin.user', $this->createStub(AdminInterface::class));

$pool = new Pool($container, ['runroom_user.admin.user']);
$globalVariables = new GlobalVariables($pool);
$globalVariables = new GlobalVariables($pool, false);

$admin = $globalVariables->getUserAdmin();

static::assertInstanceOf(AdminInterface::class, $admin);
}

/** @test */
public function itHasRequestPasswordEnabled(): void
{
$container = new Container();
$container->set('runroom_user.admin.user', $this->createStub(AdminInterface::class));

$pool = new Pool($container, ['runroom_user.admin.user']);

$globalVariables = new GlobalVariables($pool, true);

static::assertTrue($globalVariables->getHasRequestPasswordEnabled());
}
}

0 comments on commit 077d9cf

Please sign in to comment.