Add users.arrive.list permission & refactored LegacyMiddleware test
This commit is contained in:
parent
b2951b7337
commit
f1f5cd7c01
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Migrations;
|
||||
|
||||
use Engelsystem\Database\Migration\Migration;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\Schema\Builder as SchemaBuilder;
|
||||
|
||||
class AddUsersArriveListPermission extends Migration
|
||||
{
|
||||
protected int $voucher = 35;
|
||||
|
||||
protected int $arrive;
|
||||
|
||||
protected Connection $db;
|
||||
|
||||
public function __construct(SchemaBuilder $schema)
|
||||
{
|
||||
parent::__construct($schema);
|
||||
$this->db = $this->schema->getConnection();
|
||||
|
||||
$this->arrive = $this->db->table('privileges')
|
||||
->where('name', 'admin_arrive')
|
||||
->get(['id'])
|
||||
->first()->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$this->db->table('privileges')
|
||||
->insert([
|
||||
'name' => 'users.arrive.list', 'description' => 'View arrive angels list',
|
||||
]);
|
||||
|
||||
$arriveList = $this->db->table('privileges')
|
||||
->where('name', 'users.arrive.list')
|
||||
->get(['id'])
|
||||
->first()->id;
|
||||
|
||||
// Goodie Manager, Shift Coordinator, Voucher Angel, Welcome Angel
|
||||
$groups = [50, 60, 35, 30];
|
||||
foreach ($groups as $group) {
|
||||
$this->db->table('group_privileges')
|
||||
->insertOrIgnore([
|
||||
['group_id' => $group, 'privilege_id' => $arriveList],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->db->table('group_privileges')
|
||||
->where(['group_id' => $this->voucher, 'privilege_id' => $this->arrive])
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migration
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$this->db->table('privileges')
|
||||
->where('name', 'users.arrive.list')
|
||||
->delete();
|
||||
|
||||
$this->db->table('group_privileges')
|
||||
->insertOrIgnore([
|
||||
['group_id' => $this->voucher, 'privilege_id' => $this->arrive],
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ use Engelsystem\Models\User\User;
|
|||
*/
|
||||
function admin_arrive_title()
|
||||
{
|
||||
return __('Arrive angels');
|
||||
return auth()->can('admin_arrive') ? __('Arrive angels') : __('Angels');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,48 +19,51 @@ function admin_arrive()
|
|||
$msg = '';
|
||||
$search = '';
|
||||
$request = request();
|
||||
$admin_arrive = auth()->can('admin_arrive');
|
||||
|
||||
if ($request->has('search')) {
|
||||
$search = strip_request_item('search');
|
||||
$search = trim($search);
|
||||
}
|
||||
|
||||
$action = $request->get('action');
|
||||
if (
|
||||
$action == 'reset'
|
||||
&& preg_match('/^\d+$/', $request->input('user'))
|
||||
&& $request->hasPostData('submit')
|
||||
) {
|
||||
$user_id = $request->input('user');
|
||||
$user_source = User::find($user_id);
|
||||
if ($user_source) {
|
||||
$user_source->state->arrived = false;
|
||||
$user_source->state->arrival_date = null;
|
||||
$user_source->state->save();
|
||||
if ($admin_arrive) {
|
||||
$action = $request->get('action');
|
||||
if (
|
||||
$action == 'reset'
|
||||
&& preg_match('/^\d+$/', $request->input('user'))
|
||||
&& $request->hasPostData('submit')
|
||||
) {
|
||||
$user_id = $request->input('user');
|
||||
$user_source = User::find($user_id);
|
||||
if ($user_source) {
|
||||
$user_source->state->arrived = false;
|
||||
$user_source->state->arrival_date = null;
|
||||
$user_source->state->save();
|
||||
|
||||
engelsystem_log('User set to not arrived: ' . User_Nick_render($user_source, true));
|
||||
success(__('Reset done. Angel has not arrived.'));
|
||||
throw_redirect(user_link($user_source->id));
|
||||
} else {
|
||||
$msg = error(__('Angel not found.'), true);
|
||||
}
|
||||
} elseif (
|
||||
$action == 'arrived'
|
||||
&& preg_match('/^\d+$/', $request->input('user'))
|
||||
&& $request->hasPostData('submit')
|
||||
) {
|
||||
$user_id = $request->input('user');
|
||||
$user_source = User::find($user_id);
|
||||
if ($user_source) {
|
||||
$user_source->state->arrived = true;
|
||||
$user_source->state->arrival_date = new Carbon\Carbon();
|
||||
$user_source->state->save();
|
||||
engelsystem_log('User set to not arrived: ' . User_Nick_render($user_source, true));
|
||||
success(__('Reset done. Angel has not arrived.'));
|
||||
throw_redirect(user_link($user_source->id));
|
||||
} else {
|
||||
$msg = error(__('Angel not found.'), true);
|
||||
}
|
||||
} elseif (
|
||||
$action == 'arrived'
|
||||
&& preg_match('/^\d+$/', $request->input('user'))
|
||||
&& $request->hasPostData('submit')
|
||||
) {
|
||||
$user_id = $request->input('user');
|
||||
$user_source = User::find($user_id);
|
||||
if ($user_source) {
|
||||
$user_source->state->arrived = true;
|
||||
$user_source->state->arrival_date = new Carbon\Carbon();
|
||||
$user_source->state->save();
|
||||
|
||||
engelsystem_log('User set has arrived: ' . User_Nick_render($user_source, true));
|
||||
success(__('Angel has been marked as arrived.'));
|
||||
throw_redirect(user_link($user_source->id));
|
||||
} else {
|
||||
$msg = error(__('Angel not found.'), true);
|
||||
engelsystem_log('User set has arrived: ' . User_Nick_render($user_source, true));
|
||||
success(__('Angel has been marked as arrived.'));
|
||||
throw_redirect(user_link($user_source->id));
|
||||
} else {
|
||||
$msg = error(__('Angel not found.'), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -206,15 +209,17 @@ function admin_arrive()
|
|||
form_text('search', __('form.search'), $search),
|
||||
form_submit('submit', icon('search') . __('form.search')),
|
||||
], url('/admin-arrive')),
|
||||
table([
|
||||
'name' => __('general.name'),
|
||||
'rendered_planned_arrival_date' => __('Planned arrival'),
|
||||
'arrived' => __('Arrived?'),
|
||||
'rendered_arrival_date' => __('Arrival date'),
|
||||
'rendered_planned_departure_date' => __('Planned departure'),
|
||||
'actions' => __('general.actions'),
|
||||
], $users_matched),
|
||||
div('row', [
|
||||
table(array_merge(
|
||||
['name' => __('general.name'),],
|
||||
($admin_arrive ? ['rendered_planned_arrival_date' => __('Planned arrival')] : []),
|
||||
['arrived' => __('Arrived?')],
|
||||
($admin_arrive ? [
|
||||
'rendered_arrival_date' => __('Arrival date'),
|
||||
'rendered_planned_departure_date' => __('Planned departure'),
|
||||
'actions' => __('general.actions'),
|
||||
] : [])
|
||||
), $users_matched),
|
||||
div('row', $admin_arrive ? [
|
||||
div('col-md-4', [
|
||||
heading(__('Planned arrival statistics'), 3),
|
||||
BarChart::render([
|
||||
|
@ -260,6 +265,6 @@ function admin_arrive()
|
|||
'sum' => __('Sum'),
|
||||
], $planned_departure_at_day),
|
||||
]),
|
||||
]),
|
||||
] : []),
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ function make_navigation()
|
|||
// path => name,
|
||||
// path => [name, permission],
|
||||
|
||||
'admin_arrive' => 'Arrive angels',
|
||||
'admin_arrive' => [admin_arrive_title(), 'users.arrive.list'],
|
||||
'admin_active' => 'Active angels',
|
||||
'users' => ['All Angels', 'admin_user'],
|
||||
'admin_free' => 'Free angels',
|
||||
|
|
|
@ -45,16 +45,21 @@ class LegacyMiddleware implements MiddlewareInterface
|
|||
/** @var Request $appRequest */
|
||||
$appRequest = $this->container->get('request');
|
||||
$page = $appRequest->query->get('p');
|
||||
// Support old URL scheme
|
||||
// Support old URL/permission scheme
|
||||
if (empty($page)) {
|
||||
$page = $appRequest->path();
|
||||
$page = str_replace('-', '_', $page);
|
||||
}
|
||||
|
||||
$allowPage = false;
|
||||
if ($page === 'admin_arrive') {
|
||||
$allowPage = $this->auth->can('users.arrive.list');
|
||||
}
|
||||
|
||||
$title = $content = '';
|
||||
if (
|
||||
preg_match('~^\w+$~i', $page)
|
||||
&& (in_array($page, $this->free_pages) || $this->auth->can($page))
|
||||
&& (in_array($page, $this->free_pages) || $this->auth->can($page) || $allowPage)
|
||||
) {
|
||||
list($title, $content) = $this->loadPage($page);
|
||||
}
|
||||
|
|
|
@ -5,16 +5,12 @@ declare(strict_types=1);
|
|||
namespace Engelsystem\Test\Unit\Middleware;
|
||||
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Engelsystem\Helpers\Translation\Translator;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Middleware\LegacyMiddleware;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
class LegacyMiddlewareTest extends TestCase
|
||||
{
|
||||
|
@ -22,79 +18,61 @@ class LegacyMiddlewareTest extends TestCase
|
|||
* @covers \Engelsystem\Middleware\LegacyMiddleware::__construct
|
||||
* @covers \Engelsystem\Middleware\LegacyMiddleware::process
|
||||
*/
|
||||
public function testRegister(): void
|
||||
public function testRegisterNotFound(): void
|
||||
{
|
||||
/** @var ContainerInterface|MockObject $container */
|
||||
$container = $this->getMockForAbstractClass(ContainerInterface::class);
|
||||
/** @var Authenticator|MockObject $auth */
|
||||
$auth = $this->createMock(Authenticator::class);
|
||||
/** @var LegacyMiddleware|MockObject $middleware */
|
||||
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
|
||||
->setConstructorArgs([$container, $auth])
|
||||
->onlyMethods(['loadPage', 'renderPage'])
|
||||
->getMock();
|
||||
/** @var Request|MockObject $defaultRequest */
|
||||
$defaultRequest = $this->createMock(Request::class);
|
||||
/** @var ParameterBag|MockObject $parameters */
|
||||
$parameters = $this->createMock(ParameterBag::class);
|
||||
/** @var ResponseInterface|MockObject $response */
|
||||
$response = $this->getMockForAbstractClass(ResponseInterface::class);
|
||||
/** @var RequestHandlerInterface|MockObject $handler */
|
||||
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
||||
/** @var ServerRequestInterface|MockObject $request */
|
||||
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
||||
/** @var Translator|MockObject $translator */
|
||||
$translator = $this->getMockBuilder(Translator::class)
|
||||
->disableOriginalConstructor()
|
||||
/** @var Authenticator|MockObject $auth */
|
||||
$auth = $this->createMock(Authenticator::class);
|
||||
$response = new Response();
|
||||
$request = new Request(['p' => 'foo']);
|
||||
$this->app->instance('request', $request);
|
||||
$this->mockTranslator();
|
||||
|
||||
/** @var LegacyMiddleware|MockObject $middleware */
|
||||
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
|
||||
->setConstructorArgs([$this->app, $auth])
|
||||
->onlyMethods(['renderPage'])
|
||||
->getMock();
|
||||
|
||||
$middleware->expects($this->once())
|
||||
->method('loadPage')
|
||||
->with('users')
|
||||
->willReturn(['title', 'content']);
|
||||
|
||||
$middleware->expects($this->exactly(2))
|
||||
->method('renderPage')
|
||||
->withConsecutive(
|
||||
['users', 'title', 'content'],
|
||||
[404, 'Page not found', 'It\'s not available!']
|
||||
)
|
||||
->with(404, 'page.404.title', 'page.404.text')
|
||||
->willReturn($response);
|
||||
|
||||
$container->expects($this->exactly(3))
|
||||
->method('get')
|
||||
->withConsecutive(['request'], ['request'], ['translator'])
|
||||
->willReturnOnConsecutiveCalls(
|
||||
$defaultRequest,
|
||||
$defaultRequest,
|
||||
$translator
|
||||
);
|
||||
|
||||
$auth->expects($this->atLeastOnce())
|
||||
->method('can')
|
||||
->willReturn(false);
|
||||
|
||||
$translator->expects($this->exactly(2))
|
||||
->method('translate')
|
||||
->willReturnOnConsecutiveCalls(
|
||||
'Page not found',
|
||||
'It\'s not available!'
|
||||
);
|
||||
|
||||
$defaultRequest->query = $parameters;
|
||||
$defaultRequest->expects($this->once())
|
||||
->method('path')
|
||||
->willReturn('users');
|
||||
|
||||
$parameters->expects($this->exactly(2))
|
||||
->method('get')
|
||||
->with('p')
|
||||
->willReturnOnConsecutiveCalls(
|
||||
null,
|
||||
'foo'
|
||||
);
|
||||
|
||||
$middleware->process($request, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Middleware\LegacyMiddleware::process
|
||||
*/
|
||||
public function testRegisterHasPermission(): void
|
||||
{
|
||||
/** @var RequestHandlerInterface|MockObject $handler */
|
||||
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
||||
/** @var Authenticator|MockObject $auth */
|
||||
$auth = $this->createMock(Authenticator::class);
|
||||
$auth->expects($this->exactly(2))
|
||||
->method('can')
|
||||
->withConsecutive(['users.arrive.list'], ['admin_arrive'])
|
||||
->willReturnOnConsecutiveCalls(true, false);
|
||||
$response = new Response();
|
||||
$request = new Request([], [], [], [], [], ['REQUEST_URI' => 'admin-arrive']);
|
||||
$this->app->instance('request', $request);
|
||||
|
||||
/** @var LegacyMiddleware|MockObject $middleware */
|
||||
$middleware = $this->getMockBuilder(LegacyMiddleware::class)
|
||||
->setConstructorArgs([$this->app, $auth])
|
||||
->onlyMethods(['loadPage', 'renderPage'])
|
||||
->getMock();
|
||||
$middleware->expects($this->once())
|
||||
->method('loadPage')
|
||||
->with('admin_arrive')
|
||||
->willReturn(['title', 'content']);
|
||||
$middleware->expects($this->once())
|
||||
->method('renderPage')
|
||||
->with('admin_arrive', 'title', 'content')
|
||||
->willReturn($response);
|
||||
|
||||
$middleware->process($request, $handler);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue