Skip to content

Commit

Permalink
Add more functions, simplifying the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Oct 23, 2020
1 parent 53dc2f0 commit 63a4798
Show file tree
Hide file tree
Showing 27 changed files with 491 additions and 402 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -18,6 +18,7 @@
"yiisoft/active-record": "^3.0@dev",
"yiisoft/aliases": "^1.0",
"yiisoft/assets": "^1.0@dev",
"yiisoft/auth": "^1.0",
"yiisoft/cache": "^3.0@dev",
"yiisoft/cache-file": "^3.0@dev",
"yiisoft/csrf": "^3.0@dev",
Expand Down
214 changes: 108 additions & 106 deletions composer.lock

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions config/Component/Auth.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Yii\Component;

use Yii\Params;
use Yiisoft\Auth\AuthenticationMethodInterface;
use Yiisoft\Yii\Web\User\UserAuth;

$params = new Params();

return [
/** component auth */
UserAuth::class => [
'__class' => UserAuth::class,
'withAuthUrl()' => [$params->getLoginUrl()]
],

AuthenticationMethodInterface::class => UserAuth::class,
];
5 changes: 5 additions & 0 deletions config/Params.php
Expand Up @@ -154,6 +154,11 @@ public function getLogFile(): string
return dirname(__DIR__) . '/runtime/logs/app.log';
}

public function getLoginUrl(): string
{
return '/auth/login';
}

public function getLogLevels(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions config/Web.php
Expand Up @@ -16,6 +16,7 @@ public function buildConfig(): array
require(__DIR__ . '/Component/Parameters.php'),
require(__DIR__ . '/Component/EventDispatcher.php'),
require(__DIR__ . '/Component/YiiWeb.php'),
require(__DIR__ . '/Component/Auth.php'),
require(__DIR__ . '/Component/MiddlewareDispatcher.php'),
require(__DIR__ . '/Component/Router.php'),
require(__DIR__ . '/Component/LogTargetFile.php'),
Expand Down
13 changes: 9 additions & 4 deletions src/Module/Rbac/Config/Routes.php
Expand Up @@ -10,6 +10,7 @@
use App\Module\Rbac\Action\ItemEditAction;
use App\Module\Rbac\Action\ItemsApiAction;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\Auth\Middleware\Authentication;
use Yiisoft\Router\Route;

final class Routes
Expand All @@ -19,18 +20,22 @@ public function getRoutes(): array
return [
/** item actions */
Route::methods(['GET', 'POST'], '/item/index', [ItemAction::class, 'index'])
->name('item/index'),
->addMiddleware(Authentication::class)
->name('item/index'),
Route::methods(['GET', 'POST'], '/item/create', [ItemCreateAction::class, 'create'])
->name('item/create'),
->addMiddleware(Authentication::class)
->name('item/create'),
Route::methods(['GET', 'POST'], '/item/edit[/{id}]', [ItemEditAction::class, 'edit'])
->addMiddleware(Authentication::class)
->name('item/edit'),
Route::methods(['GET', 'POST'], '/item/delete[/{id}]', [ItemDeleteAction::class, 'delete'])
->addMiddleware(Authentication::class)
->name('item/delete'),

/** items api actions */
Route::get('/items', [ItemsApiAction::class, 'index'])
->addMiddleware(FormatDataResponseAsJson::class)
->name('items/index'),
->name('items/index')
->addMiddleware(FormatDataResponseAsJson::class),
];
}
}
35 changes: 15 additions & 20 deletions src/Module/User/Action/AdminBlockAction.php
Expand Up @@ -8,6 +8,7 @@
use App\Module\User\Repository\ModuleSettingsRepository;
use App\Module\User\Repository\UserRepository;
use App\Service\View;
use App\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -25,20 +26,16 @@ public function block(
UserRepository $userRepository,
UrlGeneratorInterface $url,
UrlMatcherInterface $urlMatcher,
View $view
View $view,
WebControllerService $webController
): ResponseInterface {
$id = $request->getAttribute('id');

if ($id === null || $identity->getId() === $id) {
$view->addFlash(
'is-danger',
$settings->getMessageHeader(),
$id === null ? 'The requested page does not exist.' : 'You can not block your own account.'
);

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
if ($id === null || $identity->getId() === $id || ($user = $userRepository->findUserById($id)) === null) {
return $webController
->notFoundResponse(
$identity->getId() === $id ? 'You can not block your own account.' : null
);
}

$user = $userRepository->findUserById($id);
Expand All @@ -49,14 +46,12 @@ public function block(
$userRepository->block($user);
}

$view->addFlash(
'is-success',
$settings->getMessageHeader(),
$user->isBlocked() ? 'User has been unblocked.' : 'User has been blocked.'
);

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
return $webController
->withFlash(
$user->isBlocked() ? 'is-danger' : 'is-success',
$settings->getMessageHeader(),
$user->isBlocked() ? 'User has been unblocked.' : 'User has been blocked.'
)
->redirectResponse('admin/index');
}
}
30 changes: 10 additions & 20 deletions src/Module/User/Action/AdminConfirmAction.php
Expand Up @@ -8,6 +8,7 @@
use App\Module\User\Repository\ModuleSettingsRepository;
use App\Module\User\Repository\UserRepository;
use App\Service\View;
use App\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -23,34 +24,23 @@ public function confirm(
UserRepository $userRepository,
UrlGeneratorInterface $url,
UrlMatcherInterface $urlMatcher,
View $view
View $view,
WebControllerService $webController
): ResponseInterface {
$id = $request->getAttribute('id');

if ($id !== null) {
$user = $userRepository->findUserById($id);
if ($id !== null && ($user = $userRepository->findUserById($id)) !== null) {
$userRepository->confirm($user);

if ($userRepository->confirm($user)) {
$view->addFlash(
return $webController
->withFlash(
'is-success',
$settings->getMessageHeader(),
'Your user has been confirmed.'
);

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
}
)
->redirectResponse('admin/index');
}

$view->addFlash(
'is-danger',
$settings->getMessageHeader(),
'The requested page does not exist.'
);

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
return $webController->notFoundResponse();
}
}
20 changes: 10 additions & 10 deletions src/Module/User/Action/AdminCreateAction.php
Expand Up @@ -8,6 +8,7 @@
use App\Module\User\Repository\ModuleSettingsRepository;
use App\Module\User\Repository\UserRepository;
use App\Service\View;
use App\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -22,7 +23,8 @@ public function create(
ModuleSettingsRepository $settings,
UrlGeneratorInterface $url,
UserRepository $userRepository,
View $view
View $view,
WebControllerService $webController
): ResponseInterface {
$body = $request->getParsedBody();
$method = $request->getMethod();
Expand All @@ -40,16 +42,14 @@ public function create(
['html' => 'welcome', 'text' => 'text/welcome']
)
) {
$view->addFlash(
'is-info',
$settings->getMessageHeader(),
'The account has been created.'
);
return $webController
->withFlash(
'is-info',
$settings->getMessageHeader(),
'The account has been created.'
)
->redirectResponse('admin/index');
}

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
}

return $view
Expand Down
35 changes: 15 additions & 20 deletions src/Module/User/Action/AdminDeleteAction.php
Expand Up @@ -8,6 +8,7 @@
use App\Module\User\Repository\ModuleSettingsRepository;
use App\Module\User\Repository\UserRepository;
use App\Service\View;
use App\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -23,36 +24,30 @@ public function delete(
ModuleSettingsRepository $settings,
UrlGeneratorInterface $url,
UserRepository $userRepository,
View $view
View $view,
WebControllerService $webController
): ResponseInterface {
$body = $request->getParsedBody();
$method = $request->getMethod();
$id = $request->getAttribute('id');

if ($identity->getId() === $id) {
$view->addFlash(
'is-danger',
$settings->getMessageHeader(),
'You cannot delete your own user.'
);

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
if ($id === null || $identity->getId() === $id || ($user = $userRepository->findUserById($id)) === null) {
return $webController
->notFoundResponse(
$identity->getId() === $id ? 'You cannot delete your own user.' : null
);
}

$user = $userRepository->findUserById($id);

$user->delete();

$view->addFlash(
'is-danger',
$settings->getMessageHeader(),
'The data has been delete.'
);

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
return $webController
->withFlash(
'is-danger',
$settings->getMessageHeader(),
'The data has been delete.'
)
->redirectResponse('admin/index');
}
}
27 changes: 17 additions & 10 deletions src/Module/User/Action/AdminEditAction.php
Expand Up @@ -8,6 +8,7 @@
use App\Module\User\Repository\ModuleSettingsRepository;
use App\Module\User\Repository\UserRepository;
use App\Service\View;
use App\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -22,38 +23,44 @@ public function edit(
DataResponseFactoryInterface $responseFactory,
UrlGeneratorInterface $url,
UserRepository $userRepository,
View $view
View $view,
WebControllerService $webController
): ResponseInterface {
$body = $request->getParsedBody();
$method = $request->getMethod();
$id = $request->getAttribute('id');
$registerForm->ip($request->getServerParams()['REMOTE_ADDR']);

$userRepository->loadData($registerForm, $id);
if ($id === null || ($user = $userRepository->findUserById($id)) === null) {
return $webController->notFoundResponse();
}

$userRepository->loadData($user, $registerForm);

if (
$method === 'POST'
&& $registerForm->load($body)
&& $registerForm->validate()
&& $userRepository->update($registerForm, $id)
&& $userRepository->update($user, $registerForm)
) {
if (
$userRepository->sendMailer(
$url,
$settings->getSubjectPassword(),
['html' => 'newpassword', 'text' => 'text/newpassword']
['html' => 'newpassword', 'text' => 'text/newpassword'],
false,
true,
$user
)
) {
$view->addFlash(
return $webController
->withFlash(
'is-info',
$settings->getMessageHeader(),
'The account has been updated.'
);
)
->redirectResponse('admin/index');
}

return $responseFactory
->createResponse(302)
->withHeader('Location', $url->generate('admin/index'));
}

return $view
Expand Down
11 changes: 6 additions & 5 deletions src/Module/User/Action/AdminInfoAction.php
Expand Up @@ -7,6 +7,7 @@
use App\Module\User\ActiveRecord\UserAR;
use App\Module\User\Repository\UserRepository;
use App\Service\View;
use App\Service\WebControllerService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -21,14 +22,14 @@ public function info(
UserRepository $userRepository,
UrlGeneratorInterface $url,
UrlMatcherInterface $urlMatcher,
View $view
View $view,
WebControllerService $webController
): ResponseInterface {
$body = $request->getParsedBody();
$method = $request->getMethod();
$id = $request->getAttribute('id');

/** @var UserAR $user */
$user = $userRepository->findUserById($id);
if ($id === null || ($user = $userRepository->findUserById($id)) === null) {
return $webController->notFoundResponse();
}

return $view
->viewPath('@user/resources/views')
Expand Down

0 comments on commit 63a4798

Please sign in to comment.