I have a Symfony application that uses lexik/LexikJWTAuthenticationBundle. Can I set configuration to force expiration after midnight?
How can I force expiration date at midnight of current day?
The right thing to do is to read the documentation. Where they says to ...
Add a listener
services:
acme_api.event.jwt_created_listener:
class: AppBundle\EventListener\JWTCreatedListener
arguments: [ '#request_stack' ]
tags:
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_jwt_created, method: onJWTCreated }
and put some code inside it
<?php
namespace AppBundle\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
class JWTCreatedListener
{
public function onJWTCreated(JWTCreatedEvent $event)
{
$expiration = new \DateTime(date('d-m-Y'));
$expiration->add(new DateInterval('PT86400S'))
$payload = $event->getData();
$payload['exp'] = $expiration->getTimestamp();
$event->setData($payload);
}
}
Related
I need help I am new in Drupal 9 Ajax development. I have developed a piece of code but it doesn't work.
To sumarise, I want to display an entity form in a popup. The popup is built dynamically with jQuery, I use Ajax to fill the popup and the form is built in an ajax command (the code is : \Drupal::service('entity.form_builder')->getForm($livraison, 'edit');)
The form is well displayed but I think I miss something in ajax developpement because when I submit the form, the entity is not saved. When I look at the javascript console log, I see 3 others command responses plus the command I have created in server side (modifLivraison).
result: Array(4) [ {…}, {…}, {…}, {…} ]
0: Object { command: "settings", settings: {…}, merge: true }
1: Object { command: "insert", method: "prepend", selector: "head", … }
2: Object { command: "insert", method: "append", selector: "body", … }
3: Object { command: "modifLivraison", modif: "...
I think that the Entity form needs some javascript and that's why these 3 commands have been added.
I don't know if my ajax process is correct.
Here my code :
In the command class:
class ModifLivraisonCommand implements CommandInterface
{ protected livraisonId; ...
/**
* Implements Drupal\Core\Ajax\CommandInterface:render().
*/
public function render()
{
...
$entitytype_manager = \Drupal::service('entity_type.manager');
$storage = $entitytype_manager->getStorage('livraison');
$livraison = $storage->load($this->livraisonId);
...
$livraison_form = \Drupal::service('entity.form_builder')->getForm($livraison, 'edit');
$livraison_form['#cache']['max-age'] =0;
$output = \Drupal::service('renderer')->render($livraison_form);
return [
'command' => 'modifLivraison',
'modif' => $output
];
...
The controller :
public function modifLivraison($livraison) {
$response = new AjaxResponse();
$response->addCommand(new ModifLivraisonCommand($livraison));
return $response;
}
and the javascript in the client side :
create_popup('0,0,0,0')
...
jQuery.ajax('/livraisons/modif-livraison/' + livraison, {
success: function(result,textStatus, jqXHR) {
jQuery(".spinner").remove();
console.log(result);
var ajax;
Drupal.AjaxCommands.prototype.settings(ajax,result[0],200)
jQuery(result[1].selector)[result[1].method](result[1].data);
jQuery(result[2].selector)[result[2].method](result[2].data);
...
jQuery("#modif_livraison").append(result[3].modif);
...
}
})
I think the error is in the way of ajax calling of the url (jQuery.ajax('/livraisons/modif-livraison/' + livraison ...)
My problem is the entity is not saved when the form is submitted. What's wrong ?
I'm using Behat to test a REST API my team has developed. When a particular resource is created the API returns a 201 and a Location header in the response.
The API was developed using using Symfony5 and it's using the Symfony HttpKernel as the client:
$kernel->handle($request);
I would like to assert through my behat test that it returns a 201 and the headers contains the Location. However the client is automatically following the Location header and as such I can't verify that.
Is there a way to turn off following redirects using the existing kernel component? I've not been able to find a way to do this.
You can create your own context and check the Response
<?php declare(strict_types=1);
namespace App\Tests\Features\Context;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
final class HttpContext
{
private Kernel $kernel;
private Response $response;
public function __construct(Kernel $kernel)
{
$this->kernel = $kernel;
}
/**
* #When I request :method to :uri
*/
public function iRequest(string $method, string $uri): void
{
$this->request($method, $uri);
}
/**
* #Then the location header should be :location
*/
public function assertLocationHeader(string $location): void
{
// make your assertion
dd($this->response->headers->get('location'), $location);
}
private function request(string $method, string $uri): void
{
$request = Request::create($uri, $method, [], [], [], [], null);
$this->response = $this->kernel->handle($request);
$this->kernel->terminate($request, $this->response);
}
}
I am new to SlimPHP framework. I have been trying to get a container inside route group and here is the error that shows up:
PHP Warning: Missing argument 2 for Slim\App::get()
Here is my code for routes:
$app->group('/api', function() use ($app) {
$jwtMiddleware = $app->get('jwt');
$this->post('/auth/signup', 'RegisterController:signup');
$this->post('/auth/login', 'LoginController:login');
//User routess
$this->get('/user', 'UserController:getUser')->add($jwtMiddleware);
//$this->put('/user');
});
and the code for my container:
// Jwt Middleware
$container['jwt'] = function ($container) {
$jws_settings = $container->get('settings')['jwt'];
return new \Slim\Middleware\JwtAuthentication($jws_settings);
};
please guys, what could be the problem?
You can get the container like this:
$app->group('/api', function(\Slim\App $app) {
/* #var \Slim\App $this */
$jwtMiddleware = $this->getContainer()->get('jwt');
// ...
});
In the group callback $app and $this is already your Slim\App object. You don't need the use ($app).
I am trying to setup a RESTFUL web service using FOSRestBunble, but I have some problem making POST calls, here's my setup:
app/config/routing.yml
rest:
type: rest
resource: "routing_rest.yml"
prefix: /api
app/config/routing_rest.yml
Rest_User:
type: rest
resource: "#AppBundle/Resources/config/routing_rest.yml"
AppBundle/Resources/config/routing_rest.yml
rest_application:
type: rest
resource: "AppBundle:Rest"
name_prefix: api_
AppBundle/Controller/RestController.php
class RestController extends FOSRestController
{
public function testrestAction(Request $request)
{
$r = [
'is' => 'TEST'
];
return $r;
}
public function getArticleAction()
{
$r = [
'is' => 'GET'
];
return $r;
}
public function postArticleAction()
{
$r = [
'is' => 'POST'
];
return $r;
}
}
I also made PUT and DELETE test methods. so when I do some test call
GET /api/testrest
{
"is": "TEST"
}
GET /api/article
{
"is": "GET"
}
POST /api/article
No route found for "POST /api/article": Method Not Allowed (Allow: GET, HEAD) (405 Method Not Allowed)
PUT and DELETE are also fine. Am I missing some configuration?
second problem: if I make a API folder inside Controller folder, I change the namespace for RestController to "namespace AppBundle\Controller\API;" and I update "AppBundle/Resources/config/routing_rest.yml" to
resource: "AppBundle:API:Rest"
then I got this message:
Can't locate "AppBundle:API:Rest" controller in /var/www/ws/app/config/routing_rest.yml (which is being imported from "/var/www/ws/app/config/routing.yml").
any help appreciated
1-option, run app/console debug:router (or bin/console debug:router if v > 2.8), to list generated routes;
2-option, add RouteResource annotation to class (eg. article), rename postArticleAction to postAction and check POST /api/articles is responding or not;
3-option, add article url explicitly with #POST annotation, eg. /** #Post("article") */
Scenario:
I'm using a bundle (FOSFacebookBundle) that allows me to set parameters for exactly one facebook app in my configuration. Everything works perfectly fine, but now I need to set not only one app, but multiple.
My approach:
I've created a AcmeFacebookBundle, which allows multiple apps to be defined (configuration defined in Acme\FacebookBundle\DependencyInjection\Configuration) in an array like so:
acme_facebook:
apps:
some_competition:
server_url: %acme.facebook.some_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook.some_competition.app_id%
secret: % acme .facebook.some_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
some_other_competition:
server_url: %acme.facebook. some_other_competition.server_url%
file: %kernel.root_dir%/../vendor/facebook/php-sdk/src/base_facebook.php
alias: facebook
app_id: %acme.facebook. some_other_competition.app_id%
secret: % acme .facebook. some_other_competition.secret%
cookie: true
permissions: [email, user_birthday, user_location]
In Acme\FacebookBundle\DependencyInjection\AcmeFacebookExtension I am then looping through all apps. The idea is to compare the server_url parameter against the current URL and override the fos_facebook configuration with mine.
class AcmeFacebookExtension extends Extension
{
...
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config['apps'] as $app)
{
// check for matching path here?
foreach (array('file', 'app_id', 'secret', 'cookie', 'domain', 'logging', 'culture', 'permissions') as $attribute)
{
$container->setParameter('fos_facebook.' . $attribute, $app[$attribute]);
}
}
}
Problem:
But this is exactly where I'm stuck. Obviously, I have no access to the Request object or the DiC from within AcmeFacebookExtension to do this comparison.
Am I going completely wrong in my approach? Do you have any better idea on how to tackle this problem?
What you want to create is a CompilerPass so that you can manipulate the Container after all other configuration has loaded. These should get you started:
Symfony2: Service Container Compiler Passes
Symfony2: Manipulating Service Parameters and Definitions
Create a CompilerPass