This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from jonasdekeukelaere/basic-user-bundle-impl…
…ementation Basic user bundle implementation
- Loading branch information
Showing
33 changed files
with
831 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/Resources/SumoCodersFrameworkMultiUserBundle/views/User/base.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{% extends '::base.html.twig' %} | ||
|
||
{% block header_title %} | ||
<h2> | ||
{{ app.request.get('_route')|trans|capitalize }} | ||
</h2> | ||
{% endblock %} | ||
|
||
{% block main %} | ||
{{ form_start(form) }} | ||
{{ form_rest(form) }} | ||
<input type="submit" class="btn btn-success pull-right" value="{{ 'user.form.submit'|trans|capitalize }}"> | ||
{{ form_end(form) }} | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace SumoCodersFramework\Migrations; | ||
|
||
use Doctrine\DBAL\Migrations\AbstractMigration; | ||
use Doctrine\DBAL\Schema\Schema; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
class Version20171012095221 extends AbstractMigration | ||
{ | ||
/** | ||
* @param Schema $schema | ||
*/ | ||
public function up(Schema $schema) | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); | ||
|
||
$this->addSql('CREATE TABLE BaseUser (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, salt VARCHAR(255) NOT NULL, displayName VARCHAR(255) NOT NULL, passwordResetToken VARCHAR(255) DEFAULT NULL, email VARCHAR(255) NOT NULL, status VARCHAR(50) NOT NULL COMMENT \'(DC2Type:user_status)\', discr VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); | ||
$this->addSql('DROP TABLE User'); | ||
$this->addSql('CREATE TABLE User (id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); | ||
$this->addSql('CREATE TABLE Admin (id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); | ||
$this->addSql('ALTER TABLE User ADD CONSTRAINT FK_2DA17977BF396750 FOREIGN KEY (id) REFERENCES BaseUser (id) ON DELETE CASCADE'); | ||
$this->addSql('ALTER TABLE Admin ADD CONSTRAINT FK_49CF2272BF396750 FOREIGN KEY (id) REFERENCES BaseUser (id) ON DELETE CASCADE'); | ||
} | ||
|
||
/** | ||
* @param Schema $schema | ||
*/ | ||
public function down(Schema $schema) | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); | ||
|
||
$this->addSql('ALTER TABLE User DROP FOREIGN KEY FK_A3368EB3BF396750'); | ||
$this->addSql('ALTER TABLE Admin DROP FOREIGN KEY FK_49CF2272BF396750'); | ||
$this->addSql('DROP TABLE User'); | ||
$this->addSql('DROP TABLE Admin'); | ||
$this->addSql('CREATE TABLE User (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, password VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, salt VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, displayName VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, passwordResetToken VARCHAR(255) DEFAULT NULL COLLATE utf8_unicode_ci, email VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, status VARCHAR(50) NOT NULL COLLATE utf8_unicode_ci COMMENT \'(DC2Type:user_status)\', discr VARCHAR(255) NOT NULL COLLATE utf8_unicode_ci, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); | ||
$this->addSql('DROP TABLE BaseUser'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/SumoCoders/FrameworkUserBundle/Controller/EditController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace SumoCoders\FrameworkUserBundle\Controller; | ||
|
||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use SumoCoders\FrameworkMultiUserBundle\Command\Handler; | ||
use SumoCoders\FrameworkMultiUserBundle\Controller\UserController; | ||
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository; | ||
use Symfony\Bundle\FrameworkBundle\Routing\Router; | ||
use Symfony\Component\Form\FormFactoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; | ||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @Route(service="sumo_coders.user.controller.edit_user") | ||
*/ | ||
final class EditController extends UserController | ||
{ | ||
/** @var AuthorizationCheckerInterface */ | ||
private $authorizationChecker; | ||
|
||
/** @var TokenStorageInterface */ | ||
private $tokenStorage; | ||
|
||
public function __construct( | ||
AuthorizationCheckerInterface $authorizationChecker, | ||
TokenStorageInterface $tokenStorage, | ||
FormFactoryInterface $formFactory, | ||
Router $router, | ||
FlashBagInterface $flashBag, | ||
TranslatorInterface $translator, | ||
string $form, | ||
Handler $handler, | ||
UserRepository $userRepository, | ||
$redirectRoute = null | ||
) { | ||
$this->authorizationChecker = $authorizationChecker; | ||
$this->tokenStorage = $tokenStorage; | ||
|
||
parent::__construct( | ||
$formFactory, | ||
$router, | ||
$flashBag, | ||
$translator, | ||
$form, | ||
$handler, | ||
$userRepository, | ||
$redirectRoute | ||
); | ||
} | ||
|
||
/** | ||
* @Template("SumoCodersFrameworkMultiUserBundle:User:base.html.twig") | ||
* | ||
* @param Request $request | ||
* @param int|null $id | ||
* | ||
* @return array | ||
* | ||
* @throws AccessDeniedHttpException if not allowed to edit user | ||
*/ | ||
public function editAction(Request $request, ?int $id): array | ||
{ | ||
if (!$this->authorizationChecker->isGranted('ROLE_ADMIN') | ||
&& $this->tokenStorage->getToken()->getUser()->getId() !== $id | ||
) { | ||
throw new AccessDeniedHttpException('Access denied.'); | ||
} | ||
|
||
return parent::baseAction($request, $id); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/SumoCoders/FrameworkUserBundle/Controller/IndexController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace SumoCoders\FrameworkUserBundle\Controller; | ||
|
||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use SumoCoders\FrameworkMultiUserBundle\User\AbstractUserRepository; | ||
|
||
/** | ||
* @Route(service="sumo_coders.user.controller.index") | ||
*/ | ||
final class IndexController | ||
{ | ||
/** @var AbstractUserRepository */ | ||
private $userRepository; | ||
|
||
public function __construct(AbstractUserRepository $userRepository) | ||
{ | ||
$this->userRepository = $userRepository; | ||
} | ||
|
||
/** | ||
* @Route("/user") | ||
* @Security("has_role('ROLE_ADMIN')") | ||
* @Template() | ||
* | ||
* @return array | ||
*/ | ||
public function indexAction(): array | ||
{ | ||
return ['users' => $this->userRepository->findBy([], ['username' => 'ASC'])]; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/SumoCoders/FrameworkUserBundle/DataTransferObject/AdminDataTransferObject.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace SumoCoders\FrameworkUserBundle\DataTransferObject; | ||
|
||
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\BaseUserDataTransferObject; | ||
use SumoCoders\FrameworkUserBundle\Entity\Admin; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class AdminDataTransferObject extends BaseUserDataTransferObject | ||
{ | ||
/** | ||
* @var string | ||
* | ||
* @Assert\NotBlank(message="forms.not_blank") | ||
*/ | ||
public $displayName; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @Assert\NotBlank(message="forms.not_blank") | ||
* @Assert\Email(message="forms.invalid_email") | ||
*/ | ||
public $email; | ||
|
||
/** | ||
* @var string | ||
* | ||
* @Assert\NotBlank(message="forms.not_blank", groups={"add"}) | ||
*/ | ||
public $plainPassword; | ||
|
||
/** | ||
* @var Admin | ||
*/ | ||
protected $user; | ||
|
||
public function getEntity(): Admin | ||
{ | ||
if ($this->user) { | ||
$this->user->change($this); | ||
|
||
return $this->user; | ||
} | ||
|
||
return new Admin( | ||
$this->plainPassword, | ||
$this->displayName, | ||
$this->email | ||
); | ||
} | ||
} |
Oops, something went wrong.