Skip to content

Commit

Permalink
remove use var dumper as log viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Mar 28, 2021
1 parent 5a0c10e commit d8f02a5
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* ~/.composer

# Configuring
ADD docker/php/php.ini /etc/php/8.0/cli/php.ini
ADD docker/supervisor/supervisor.conf /etc/supervisord.conf
ADD docker/supervisor/supervisor.dev.conf /etc/supervisord.dev.conf
ADD docker/supervisor/supervisor.prod.conf /etc/supervisord.prod.conf

# Here we go
ADD docker/start.sh /start.sh
Expand Down
2 changes: 1 addition & 1 deletion config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ security:
pattern: ^/admin
anonymous: true
lazy: true
provider: user_provider
switch_user: true
guard:
authenticators:
Expand All @@ -25,6 +24,7 @@ security:
stateless: true
anonymous: true
lazy: true
provider: user_provider
json_login:
check_path: /api/login
username_path: username
Expand Down
15 changes: 9 additions & 6 deletions docker/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ set -e
if [[ ! -d "/semart/var" ]]; then
cd /semart && mkdir var
fi

cd /semart && composer update --prefer-dist -vvv
if [[ "prod" == "${APP_ENV}" ]]; then
cd /semart && composer update --prefer-dist -vvv
composer dump-autoload --optimize --classmap-authoritative
php /semart/bin/console cache:clear --env=prod
fi

if [[ "dev" == "${APP_ENV}" ]]; then
cd /semart && composer update --prefer-dist -vvv
else
composer dump-autoload --optimize
php /semart/bin/console cache:clear --env=dev
chmod 777 -R var/
Expand All @@ -23,4 +21,9 @@ chmod 777 -R storage/
chmod 755 -R config/
chmod 755 -R vendor/

/usr/bin/supervisord -n -c /etc/supervisord.conf
if [[ "prod" == "${APP_ENV}" ]]; then
/usr/bin/supervisord -n -c /etc/supervisord.prod.conf
else
/usr/bin/supervisord -n -c /etc/supervisord.dev.conf
fi

49 changes: 49 additions & 0 deletions docker/supervisor/supervisor.dev.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
nodaemon=true

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket

; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.

[program:cronjob]
command =/usr/bin/php /semart/bin/console semart:cron:start --blocking
autostart=true
autorestart=true
priority=5
stdout_logfile=/var/log/supervisor/cronjob.out.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/supervisor/cronjob.error.log
stderr_logfile_maxbytes=0

[program:server]
command =/semart/bin/rr serve -c /semart/.rr.dev.yaml
autostart=true
autorestart=true
priority=5
stdout_logfile=/var/log/supervisor/server.out.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/supervisor/server.error.log
stderr_logfile_maxbytes=0

[include]
files = /etc/supervisor/conf.d/*.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ stderr_logfile=/var/log/supervisor/cronjob.error.log
stderr_logfile_maxbytes=0

[program:server]
command =/semart/bin/rr serve
command =/semart/bin/rr serve -c /semart/.rr.yaml
autostart=true
autorestart=true
priority=5
Expand Down
2 changes: 2 additions & 0 deletions lib/Admin/AdminContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ final class AdminContext

public const ADMIN_ROUTE = 'admin_home';

public const USER_DEVICE_ID = 'USER_DEVICE_ID';

public static function isAdminContext(Request $request): bool
{
if (static::ADMIN_PATH_PREFIX === substr($request->getPathInfo(), 0, strlen(static::ADMIN_PATH_PREFIX))) {
Expand Down
8 changes: 8 additions & 0 deletions lib/Entity/ApiClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,35 @@ class ApiClientRequest implements ApiClientRequestInterface, EntityInterface
* @Groups({"read"})
*
* @ORM\Column(type="json")
*
* @OA\Property(type="array", @OA\Items(type="string"))
*/
private array $headers;

/**
* @Groups({"read"})
*
* @ORM\Column(type="json")
*
* @OA\Property(type="array", @OA\Items(type="string"))
*/
private array $queries;

/**
* @Groups({"read"})
*
* @ORM\Column(type="json")
*
* @OA\Property(type="array", @OA\Items(type="string"))
*/
private array $requests;

/**
* @Groups({"read"})
*
* @ORM\Column(type="json")
*
* @OA\Property(type="array", @OA\Items(type="string"))
*/
private array $files;

Expand Down
4 changes: 2 additions & 2 deletions lib/EventSubscriber/ApiClientRequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(TokenStorageInterface $tokenStorage, ApiClientReques
$this->userProvider = $userProvider;
}

public function requestLog(ControllerEvent $event): void
public function log(ControllerEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function requestLog(ControllerEvent $event): void
public static function getSubscribedEvents(): array
{
return [
ControllerEvent::class => 'requestLog',
ControllerEvent::class => 'log',
];
}
}
54 changes: 43 additions & 11 deletions lib/EventSubscriber/SingleLoginSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace KejawenLab\ApiSkeleton\EventSubscriber;

use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\Security\Model\UserInterface;
use KejawenLab\ApiSkeleton\Security\Service\UserProviderFactory;
use KejawenLab\ApiSkeleton\Security\Service\UserService;
Expand All @@ -12,7 +13,10 @@
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @author Muhamad Surya Iksanudin<[email protected]>
Expand All @@ -23,18 +27,45 @@ final class SingleLoginSubscriber implements EventSubscriberInterface

private RequestStack $requestStack;

private UrlGeneratorInterface $urlGenerator;

private UserService $service;

private UserProviderFactory $userProviderFactory;

public function __construct(RequestStack $requestStack, UserService $service, UserProviderFactory $userProviderFactory)
public function __construct(RequestStack $requestStack, UrlGeneratorInterface $urlGenerator, UserService $service, UserProviderFactory $userProviderFactory)
{
$this->requestStack = $requestStack;
$this->urlGenerator = $urlGenerator;
$this->service = $service;
$this->userProviderFactory = $userProviderFactory;
}

public function validate(JWTDecodedEvent $event): void
public function validate(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}

$request = $event->getRequest();
if (!AdminContext::isAdminContext($request)) {
return;
}

$session = $request->getSession();
if (!$deviceId = $session->get(AdminContext::USER_DEVICE_ID)) {
return;
}

$user = $this->service->getByDeviceId($deviceId);
if ($user) {
return;
}

$event->setResponse(new RedirectResponse($this->urlGenerator->generate('admin_logout')));
}

public function decode(JWTDecodedEvent $event): void
{
$payload = $event->getPayload();
if (!isset($payload['deviceId'])) {
Expand All @@ -49,15 +80,15 @@ public function validate(JWTDecodedEvent $event): void
}

$user = $this->service->getByDeviceId($payload['deviceId']);
if (!$user) {
$event->markAsInvalid();
$event->stopPropagation();

if ($user) {
return;
}

$event->markAsInvalid();
$event->stopPropagation();
}

public function sign(JWTCreatedEvent $event): void
public function create(JWTCreatedEvent $event): void
{
$user = $this->userProviderFactory->getRealUser($event->getUser());
$payload = $event->getData();
Expand All @@ -78,10 +109,11 @@ public function sign(JWTCreatedEvent $event): void
public static function getSubscribedEvents(): array
{
return [
JWTCreatedEvent::class => 'sign',
JWTDecodedEvent::class => 'validate',
Events::JWT_CREATED => 'sign',
Events::JWT_DECODED => 'validate',
RequestEvent::class => [['validate', -255]],
JWTCreatedEvent::class => 'create',
JWTDecodedEvent::class => 'decode',
Events::JWT_CREATED => 'create',
Events::JWT_DECODED => 'decode',
];
}
}
19 changes: 17 additions & 2 deletions lib/Security/AdminAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\Security\Service\UserProviderFactory;
use KejawenLab\ApiSkeleton\Security\Service\UserService;
use KejawenLab\ApiSkeleton\Util\Encryptor;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -25,14 +27,17 @@ final class AdminAuthenticator extends AbstractFormLoginAuthenticator implements
{
use TargetPathTrait;

private UserService $userService;

private UserProviderFactory $userProviderFactory;

private UrlGeneratorInterface $urlGenerator;

private UserPasswordEncoderInterface $passwordEncoder;

public function __construct(UserProviderFactory $userProviderFactory, UrlGeneratorInterface $urlGenerator, UserPasswordEncoderInterface $passwordEncoder)
public function __construct(UserService $userService, UserProviderFactory $userProviderFactory, UrlGeneratorInterface $urlGenerator, UserPasswordEncoderInterface $passwordEncoder)
{
$this->userService = $userService;
$this->userProviderFactory = $userProviderFactory;
$this->urlGenerator = $urlGenerator;
$this->passwordEncoder = $passwordEncoder;
Expand Down Expand Up @@ -72,7 +77,17 @@ public function getPassword($credentials): ?string

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
$session = $request->getSession();
$user = $this->userProviderFactory->getRealUser($token->getUser());
if ($user instanceof Model\UserInterface) {
$deviceId = Encryptor::hash(date('YmdHis'));
$user->setDeviceId($deviceId);

$session->set(AdminContext::USER_DEVICE_ID, $deviceId);
$this->userService->save($user);
}

if ($targetPath = $this->getTargetPath($session, $providerKey)) {
return new RedirectResponse($targetPath);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Security/Model/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public function getEmail(): ?string;
public function getPlainPassword(): ?string;

public function getPassword(): ?string;

public function getDeviceId(): ?string;

public function setDeviceId(string $deviceId): void;
}
4 changes: 3 additions & 1 deletion templates/api_client/view.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends 'layout/main.html.twig' %}

{% block stylesheets %}{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('plugins/json-viewer/json-viewer.min.css') }}">
{% endblock %}
{% block content %}
{% apply spaceless %}
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion templates/setting/view.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'layout/main.html.twig' %}

{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('plugins/json-viewer/json-viewer.min.css') }}">
<link rel="stylesheet" href="{{ asset('plugins/json-viewer/json-viewer.min.css') }}">
{% endblock %}%}
{% block content %}
{% apply spaceless %}
Expand Down

0 comments on commit d8f02a5

Please sign in to comment.