Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #202 from jonasdekeukelaere/basic-user-bundle-impl…
Browse files Browse the repository at this point in the history
…ementation

Basic user bundle implementation
  • Loading branch information
jonasdekeukelaere authored Oct 23, 2017
2 parents 62a4508 + e0ed819 commit 2faef5f
Show file tree
Hide file tree
Showing 33 changed files with 831 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function registerBundles()
new SumoCoders\FrameworkCoreBundle\SumoCodersFrameworkCoreBundle(),
new SumoCoders\FrameworkSearchBundle\SumoCodersFrameworkSearchBundle(),
new SumoCoders\FrameworkMultiUserBundle\SumoCodersFrameworkMultiUserBundle(),
new SumoCoders\FrameworkUserBundle\SumoCodersFrameworkUserBundle(),
];

if ($this->getEnvironment() === 'prod') {
Expand Down
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 %}
7 changes: 5 additions & 2 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ imports:

services:
multi_user.user_repository.collection:
class: SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection
class: SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection
arguments:
- ["@multi_user.user.repository"]
-
- "@multi_user.base_user.repository"
- "@sumo_coders.user.repository.user"
- "@sumo_coders.user.repository.admin"

sumocoders.user_provider:
class: SumoCoders\FrameworkMultiUserBundle\Security\ObjectUserProvider
Expand Down
7 changes: 7 additions & 0 deletions app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ sumo_coders_framework_multi_user:

fos_js_routing:
resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

sumo_coders_framework_user_bundle_crud:
resource: "@SumoCodersFrameworkUserBundle/Resources/config/routing.yml"

sumo_coders_framework_user_bundle:
resource: "@SumoCodersFrameworkUserBundle/Controller"
type: annotation
6 changes: 4 additions & 2 deletions app/config/security.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
security:
encoders:
SumoCoders\FrameworkMultiUserBundle\Entity\User: sha512
SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser: sha512

role_hierarchy:
ROLE_ADMIN: ROLE_USER
Expand All @@ -14,7 +14,7 @@ security:
main:
provider: sumocoders.multi_user_provider
logout:
path: /%locale%/logout
path: multi_user_logout
target: /
anonymous: ~
guard:
Expand All @@ -30,4 +30,6 @@ security:
- { path: ^/\w\w/reset-password, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/(_(profiler|wdt|tests)|css|js)/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/\w\w/locale.json, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/\w\w/user/\d+/edit-user, role: ROLE_USER }
- { path: ^/\w\w/user, role: ROLE_ADMIN }
- { path: ^/, role: ROLE_USER }
44 changes: 44 additions & 0 deletions app/migrations/Version20171012095221.php
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');
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"jms/di-extra-bundle": "^1.7",
"sumocoders/framework-search-bundle": "^4.0.0",
"sumocoders/framework-example-bundle": "^8.0.0",
"sumocoders/framework-multi-user-bundle": "^6.0.0",
"sumocoders/framework-multi-user-bundle": "^8.0.0",
"simple-bus/symfony-bridge": "^4.1",
"simple-bus/doctrine-orm-bridge": "^4.0",
"gedmo/doctrine-extensions": "^2.4",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/SumoCoders/FrameworkCoreBundle/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,18 @@
<span class="icon icon-angle hidden-xs hidden-sm"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="{{ path('sumocoders_frameworkuser_user_edit', {'id': app.user.id }) }}">{{ 'user.header.actions.settings'|trans|capitalize }}</a></li>
<li><a href="{{ path('fos_user_security_logout') }}">{{ 'user.header.actions.logout'|trans|capitalize }}</a></li>
<li>
<a
{% if app.user is admin %}
href="{{ path('sumo_coders.user.route.edit_admin', {'id': app.user.id}) }}"
{% else %}
href="{{ path('sumo_coders.user.route.edit_user', {'id': app.user.id}) }}"
{% endif %}
>
{{ 'user.actions.settings'|trans|capitalize }}
</a>
</li>
<li><a href="{{ path('multi_user_logout') }}">{{ 'user.actions.logout'|trans|capitalize }}</a></li>
</ul>
</div>
{% endif %}
Expand Down
77 changes: 77 additions & 0 deletions src/SumoCoders/FrameworkUserBundle/Controller/EditController.php
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 src/SumoCoders/FrameworkUserBundle/Controller/IndexController.php
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'])];
}
}
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
);
}
}
Loading

0 comments on commit 2faef5f

Please sign in to comment.