FIX: Remove login for API

This commit is contained in:
2023-11-25 00:55:31 -03:00
parent 3cadaca746
commit ec7d8e69ab
34 changed files with 140 additions and 71 deletions

View File

@ -10,4 +10,4 @@ $app->group('/api', function($app) {
include_once $file->getRealPath();
}
}
});
})->add($app->getContainer()->get(Incoviba\Middleware\API::class));

View File

@ -3,4 +3,4 @@ use Incoviba\Controller\Inmobiliarias;
$app->group('/inmobiliarias', function($app) {
$app->get('[/]', Inmobiliarias::class);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -4,7 +4,7 @@ use Incoviba\Controller\Proyectos;
$app->group('/proyectos', function($app) {
$app->get('/unidades[/]', [Proyectos::class, 'unidades']);
$app->get('[/]', Proyectos::class);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));
$app->group('/proyecto/{proyecto_id}', function($app) {
$app->get('[/]', [Proyectos::class, 'show']);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -11,10 +11,10 @@ $app->group('/ventas', function($app) {
}
$app->get('/add[/]', [Ventas::class, 'add']);
$app->get('[/]', Ventas::class);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));
$app->group('/venta/{proyecto_nombre:[A-za-zÑñ\+\ %0-9]+}/{unidad_descripcion:[0-9]+}', function($app) {
$app->get('[/]', [Ventas::class, 'showUnidad']);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));
$app->group('/venta/{venta_id:[0-9]+}', function($app) {
$app->group('/propietario', function($app) {
$app->get('[/]', [Ventas::class, 'propietario']);
@ -29,4 +29,4 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) {
});
$app->get('/edit[/]', [Ventas::class, 'edit']);
$app->get('[/]', [Ventas::class, 'show']);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -4,4 +4,4 @@ use Incoviba\Controller\Search;
$app->group('/search', function($app) {
$app->get('[/{query}[/{tipo}[/]]]', Search::class);
$app->post('[/]', Search::class);
});
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -4,5 +4,5 @@ use Incoviba\Controller\Login;
$app->group('/login', function($app) {
$app->post('[/]', [Login::class, 'login']);
$app->get('[/]', [Login::class, 'form']);
});
$app->get('/logout', [Login::class, 'logout']);
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));
$app->get('/logout', [Login::class, 'logout'])->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -2,4 +2,4 @@
use Incoviba\Controller\Base;
$app->get('/construccion', [Base::class, 'construccion'])->setName('construccion');
$app->get('[/]', Base::class);
$app->get('[/]', Base::class)->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -28,7 +28,7 @@
return {
hoy: () => {
const span = $('#cuotas_hoy')
return fetch('{{$urls->api}}/ventas/cuotas/hoy').then(response => {
return fetchAPI('{{$urls->api}}/ventas/cuotas/hoy').then(response => {
span.html('')
if (response.ok) {
return response.json()
@ -48,7 +48,7 @@
},
pendiente: () => {
const span = $('#cuotas_pendientes')
return fetch('{{$urls->api}}/ventas/cuotas/pendiente').then(response => {
return fetchAPI('{{$urls->api}}/ventas/cuotas/pendiente').then(response => {
span.html('')
if (response.ok) {
return response.json()

View File

@ -13,7 +13,7 @@
proyectos: () => {
this.draw().loading()
const url = '{{$urls->api}}/proyectos/escriturando'
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -38,7 +38,7 @@
},
unidades: proyecto_id => {
const url = '{{$urls->api}}/ventas/unidades/disponibles'
return fetch(url, {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
return fetchAPI(url, {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
if (response.ok) {
return response.json()
}
@ -49,7 +49,7 @@
},
promesas: proyecto_id => {
const url = '{{$urls->api}}/ventas/estados/firmar'
return fetch(url, {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
return fetchAPI(url, {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
if (response.ok) {
return response.json()
}
@ -60,7 +60,7 @@
},
escrituras: proyecto_id => {
const url = '{{$urls->api}}/ventas/escrituras/estados';
return fetch(url, {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
return fetchAPI(url, {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -10,7 +10,7 @@
list.append(
$('<div><div>').addClass('ui inline active loader')
)
fetch('{{$urls->api}}/ventas/cierres/vigentes').then(response => {
fetchAPI('{{$urls->api}}/ventas/cierres/vigentes').then(response => {
list.html('')
if (response.ok) {
return response.json()

View File

@ -10,7 +10,7 @@
list.append(
$('<div><div>').addClass('ui inline active loader')
)
return fetch('{{$urls->api}}/ventas/cuotas/vencer').then(response => {
return fetchAPI('{{$urls->api}}/ventas/cuotas/vencer').then(response => {
list.html('')
if (response.ok) {
return response.json()

View File

@ -2,6 +2,18 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.2/semantic.min.js" integrity="sha512-5cguXwRllb+6bcc2pogwIeQmQPXEzn2ddsqAexIBhh7FO1z5Hkek1J9mrK2+rmZCTU6b6pERxI7acnp1MpAg4Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="text/javascript">
function fetchAPI(url, options=null) {
if (options === null) {
options = {}
}
if (!Object.hasOwn(options, 'headers')) {
options['headers'] = {}
}
if (!Object.hasOwn(options['headers'], 'Authorization')) {
options['headers']['Authorization'] = 'Bearer {{md5($API_KEY)}}'
}
return fetch(url, options)
}
const calendar_date_options = {
type: 'date',
firstDayOfWeek: 1,

View File

@ -51,7 +51,7 @@
get() {
return {
start: () => {
return fetch('{{$urls->api}}/proyecto/' + this.id + '/inicio').then(response => {
return fetchAPI('{{$urls->api}}/proyecto/' + this.id + '/inicio').then(response => {
if (response.ok) {
return response.json()
}
@ -60,7 +60,7 @@
})
},
current: () => {
return fetch('{{$urls->api}}/proyecto/' + this.id + '/estado').then(response => {
return fetchAPI('{{$urls->api}}/proyecto/' + this.id + '/estado').then(response => {
if (response.ok) {
return response.json()
}
@ -69,7 +69,7 @@
})
},
recepcion: () => {
return fetch('{{$urls->api}}/proyecto/' + this.id + '/recepcion').then(response => {
return fetchAPI('{{$urls->api}}/proyecto/' + this.id + '/recepcion').then(response => {
if (response.ok) {
if (response.status === 204) {
return null

View File

@ -134,7 +134,7 @@
return {
superficies: () => {
const url = '{{$urls->api}}/proyecto/{{$proyecto->id}}/superficies/vendible'
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -197,7 +197,7 @@
data: {},
get: function() {
const url = '{{$urls->api}}/proyecto/{{$proyecto->id}}/unidades'
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -273,7 +273,7 @@
return {
ventas: () => {
const url = '{{$urls->api}}/ventas'
return fetch(url, {method: 'post', headers: {'Content-Type': 'application/json'},
return fetchAPI(url, {method: 'post', headers: {'Content-Type': 'application/json'},
body: JSON.stringify({proyecto_id: '{{$proyecto->id}}'})}).then(response => {
if (response.ok) {
return response.json()
@ -294,7 +294,7 @@
},
stock: () => {
const url = '{{$urls->api}}/proyecto/{{$proyecto->id}}/unidades/disponibles'
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -315,7 +315,7 @@
},
venta: venta_id => {
const url = '{{$urls->api}}/venta/' + venta_id
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -325,7 +325,7 @@
},
precio: unidad_id => {
const url = '{{$urls->api}}/ventas/precio/unidad/' + unidad_id
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
if (response.status === 204) {
return null

View File

@ -203,7 +203,7 @@
return {
tipos: proyecto_id => {
const url = '{{$urls->api}}/proyecto/' + proyecto_id + '/unidades/tipos'
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -106,7 +106,7 @@
const data = new FormData(document.getElementById('search_form'))
const uri = '{{$urls->api}}/search'
this.data = []
return fetch(uri, {method: 'post', body: data}).then(response => {
return fetchAPI(uri, {method: 'post', body: data}).then(response => {
if (response.ok) {
return response.json()
}
@ -155,7 +155,7 @@
},
unidad: id => {
const url = '{{$urls->api}}/ventas/unidad/' + id
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -163,7 +163,7 @@
},
venta: id => {
const url = '{{$urls->api}}/venta/' + id
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -269,7 +269,7 @@
return {
provincias: () => {
const uri = '{{$urls->api}}/region/' + this.data.region + '/provincias'
return fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}
@ -286,7 +286,7 @@
},
comunas: provincia_id => {
const uri = '{{$urls->api}}/provincia/' + provincia_id + '/comunas'
return fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}
@ -581,7 +581,7 @@
return {
propietario: rut => {
const uri = '{{$urls->api}}/ventas/propietario/' + rut.split('-')[0]
return fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}
@ -664,7 +664,7 @@
return {
unidades: () => {
const uri = '{{$urls->api}}/proyecto/' + this.data.id + '/unidades'
return fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -201,7 +201,7 @@
this.draw().loading()
return fetch('{{$urls->api}}/proyectos').then(response => {
return fetchAPI('{{$urls->api}}/proyectos').then(response => {
if (response.ok) {
return response.json()
}
@ -223,7 +223,7 @@
})
},
cierres: proyecto_id => {
return fetch('{{$urls->api}}/ventas/cierres',
return fetchAPI('{{$urls->api}}/ventas/cierres',
{method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
if (response.ok) {
return response.json()

View File

@ -105,7 +105,7 @@
const cuota_id = button.data('cuota')
const calendar = $(".ui.calendar[data-cuota='" + cuota_id + "']").calendar('get date')
const fecha = [calendar.getFullYear(), calendar.getMonth()+1, calendar.getDate()].join('-')
fetch('{{$urls->api}}/ventas/cuota/abonar', {
return fetchAPI('{{$urls->api}}/ventas/cuota/abonar', {
method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({cuota_id, fecha})
}).then(response => {
if (response.ok) {
@ -125,7 +125,7 @@
const cuota_id = button.data('cuota')
const calendar = $(".ui.calendar[data-cuota='" + cuota_id + "']").calendar('get date')
const fecha = [calendar.getFullYear(), calendar.getMonth()+1, calendar.getDate()].join('-')
fetch('{{$urls->api}}/ventas/cuota/devolver', {
return fetchAPI('{{$urls->api}}/ventas/cuota/devolver', {
method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({cuota_id, fecha})
}).then(response => {
if (response.ok) {

View File

@ -105,7 +105,7 @@
const cuota_id = button.data('cuota')
const calendar = $(".ui.calendar[data-cuota='" + cuota_id + "']").calendar('get date')
const fecha = [calendar.getFullYear(), calendar.getMonth()+1, calendar.getDate()].join('-')
fetch('{{$urls->api}}/ventas/cuota/depositar', {
return fetchAPI('{{$urls->api}}/ventas/cuota/depositar', {
method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({cuota_id, fecha})
}).then(response => {
if (response.ok) {

View File

@ -68,7 +68,7 @@
return
}
const uri = '{{$urls->api}}/venta/{{$venta->id}}'
return fetch(uri,
return fetchAPI(uri,
{method: 'put', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}
).then(response => {
if (response.ok) {

View File

@ -53,7 +53,7 @@
method: 'post',
body: data
}
return this.sent.uf[date.toISOString()] = fetch(url, options).then(response => {
return this.sent.uf[date.toISOString()] = fetchAPI(url, options).then(response => {
if (response.ok) {
return response.json()
}
@ -75,7 +75,7 @@
method: 'post',
body: data
}
return this.sent.ipc[dateKey] = fetch(url, options).then(response => {
return this.sent.ipc[dateKey] = fetchAPI(url, options).then(response => {
if (response.ok) {
return response.json()
}
@ -127,7 +127,7 @@
return {
unidades: () => {
const url = '{{$urls->api}}/venta/' + this.id + '/unidades'
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}
@ -233,7 +233,7 @@
return {
ventas: () => {
const url = '{{$urls->api}}/ventas/facturacion/proyecto/' + this.selected
return fetch(url).then(response => {
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -103,7 +103,7 @@
ventas: proyecto_id => {
this.data.venta_ids = []
this.data.ventas = []
return fetch('{{$urls->api}}/ventas',
return fetchAPI('{{$urls->api}}/ventas',
{method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}
).then(response => {
this.loading.precios = false
@ -130,7 +130,7 @@
})
},
venta: venta_id => {
return fetch('{{$urls->api}}/venta/' + venta_id).then(response => {
return fetchAPI('{{$urls->api}}/venta/' + venta_id).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -34,7 +34,7 @@
return {
pagos: () => {
const uri = '{{$urls->api}}/ventas/pagos/pendientes'
fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}
@ -138,7 +138,7 @@
return {
pendientes: () => {
const uri = '{{$urls->api}}/ventas/pagos/abonar'
fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}
@ -218,7 +218,7 @@
return {
devueltos: () => {
const uri = '{{$urls->api}}/ventas/pagos/rebotes'
fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -339,7 +339,7 @@
$(this.ids.buttons.add).hide()
return fetch('{{$urls->api}}/proyectos').then(response => {
return fetchAPI('{{$urls->api}}/proyectos').then(response => {
if (response.ok) {
return response.json()
}
@ -357,7 +357,7 @@
},
precios: proyecto_id => {
this.data.precios = []
return fetch('{{$urls->api}}/ventas/precios',
return fetchAPI('{{$urls->api}}/ventas/precios',
{method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}
).then(response => {
$('.item.proyecto').css('cursor', 'default')
@ -639,7 +639,7 @@
fecha: $(this.ids.fields.calendar).calendar('get date'),
valor: $(this.ids.fields.valor).val()
}
return fetch('{{$urls->api}}/precios/update',
return fetchAPI('{{$urls->api}}/precios/update',
{method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}
).then(response => {
if (response.ok) {

View File

@ -79,7 +79,7 @@ Editar Propietario
const original_id = $("[name='comuna']").val()
const uri = '{{$urls->api}}/direcciones/comunas/find'
const data = {direccion}
return fetch(uri,
return fetchAPI(uri,
{method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}
).then(response => {
if (response.ok) {
@ -101,7 +101,7 @@ Editar Propietario
const parent = $('#comunas')
parent.hide()
const uri = '{{$urls->api}}/direcciones/region/' + region_id + '/comunas'
return fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}
@ -195,7 +195,7 @@ Editar Propietario
redirect()
return
}
return fetch(uri,
return fetchAPI(uri,
{method: 'put', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}
).then(response => {
if (response.ok) {

View File

@ -48,7 +48,7 @@
return {
comentarios: () => {
const uri = '{{$urls->api}}/venta/{{$venta->id}}/comentarios'
return fetch(uri).then(response => {
return fetchAPI(uri).then(response => {
if (response.ok) {
return response.json()
}

View File

@ -176,7 +176,7 @@
modal.find('.ui.button').click(event => {
modal.modal('hide')
const date = modal.find('#fecha').val()
return fetch(uri,
return fetchAPI(uri,
{method: 'put', body: JSON.stringify({fecha: date}), headers: {'Content-Type': 'application/json'}}
).then(response => {
anchor.css('pointer-events', '')
@ -206,7 +206,7 @@
modal.modal('show')
modal.find('.ui.button').click(event => {
const date = modal.find('#fecha').val()
return fetch(uri,
return fetchAPI(uri,
{method: 'put', body: JSON.stringify({fecha: date}), headers: {'Content-Type': 'application/json'}}
).then(response => {
anchor.css('pointer-events', '')

View File

@ -1,2 +1,2 @@
<?php
$app->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));
//$app->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -2,9 +2,6 @@
use Psr\Container\ContainerInterface;
return [
Psr\Http\Message\ResponseFactoryInterface::class => function(ContainerInterface $container) {
return $container->get(Nyholm\Psr7\Factory\Psr17Factory::class);
},
Incoviba\Middleware\Authentication::class => function(ContainerInterface $container) {
return new Incoviba\Middleware\Authentication(
$container->get(Psr\Http\Message\ResponseFactoryInterface::class),
@ -13,5 +10,11 @@ return [
$container->get(Incoviba\Common\Alias\View::class),
implode('/', [$container->get('APP_URL'), 'login'])
);
},
Incoviba\Middleware\API::class => function(ContainerInterface $container) {
return new Incoviba\Middleware\API(
$container->get(Psr\Http\Message\ResponseFactoryInterface::class),
$container->get('API_KEY')
);
}
];

View File

@ -9,6 +9,7 @@ return [
'money_url' => '',
'login' => $container->get(Incoviba\Service\Login::class),
'format' => $container->get(Incoviba\Service\Format::class),
'API_KEY' => $container->get('API_KEY'),
];
if ($global_variables['login']->isIn()) {
$global_variables['user'] = $global_variables['login']->getUser();

View File

@ -47,20 +47,20 @@ class Money
protected function getValue(Service\Redis $redisService, string $redisKey, Service\Money $moneyService,
DateTimeInterface $date, string $provider): float
{
if (isset($this->data[$date->format('Y-m-d')])) {
return $this->data[$date->format('Y-m-d')];
if (isset($this->data[$provider][$date->format('Y-m-d')])) {
return $this->data[$provider][$date->format('Y-m-d')];
}
try {
$this->data = (array) $this->fetchRedis($redisService, $redisKey);
if (!isset($this->data[$date->format('Y-m-d')])) {
$this->data[$provider] = (array) $this->fetchRedis($redisService, $redisKey);
if (!isset($this->data[$provider][$date->format('Y-m-d')])) {
throw new EmptyRedis($redisKey);
}
} catch (EmptyRedis) {
$result = $moneyService->get($provider, $date);
$this->data[$date->format('Y-m-d')] = $result;
$this->saveRedis($redisService, $redisKey, $this->data, $this->time);
$this->data[$provider][$date->format('Y-m-d')] = $result;
$this->saveRedis($redisService, $redisKey, $this->data[$provider], $this->time);
}
return $this->data[$date->format('Y-m-d')];
return $this->data[$provider][$date->format('Y-m-d')];
}
/*public function uf(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Money $moneyService): ResponseInterface
{

View File

@ -0,0 +1,13 @@
<?php
namespace Incoviba\Exception;
use Throwable;
use Exception;
class MissingAuthorizationHeader extends Exception
{
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Incoviba\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Incoviba\Exception\MissingAuthorizationHeader;
class API
{
public function __construct(protected ResponseFactoryInterface $responseFactory, protected string $key) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$key = $this->getKey($request);
} catch (MissingAuthorizationHeader $exception) {
return $this->responseFactory->createResponse(401);
}
if ($this->validate($key)) {
return $handler->handle($request);
}
return $this->responseFactory->createResponse(403);
}
protected function getKey(ServerRequestInterface $request): string
{
$auth_headers = $request->getHeader('Authorization');
foreach ($auth_headers as $header) {
if (str_contains($header, 'Bearer')) {
return substr($header, strlen('Bearer '));
}
}
throw new MissingAuthorizationHeader();
}
protected function validate($incoming_key): bool
{
return $incoming_key === md5($this->key);
}
}