Viewed   57 times

I need to run some code prior to checking if a user's credentials are correct. Currently I'm achieving this with a custom event listener that fires on the kernel.request event and checks if the requested URL matches security.yml's check_path setting. But this is inefficient since it runs on every request. I'm aware of the onSecurityInteractiveLogin event, but I believe that fires after a successful login attempt. Does anyone know if there's a pre login event, or where I could dispatch a custom event myself?

 Answers

5

So, there's no 'official' pre-login event. But thankfully it's not hard to set one up since Symfony2 is so extendable. The trick is to use your own service to handle authentication.

Symfony uses this class when using a login form:

SymfonyComponentSecurityHttpFirewallUsernamePasswordFormAuthenticationListener

If you override the security.authentication.listener.form.class parameter (originally defined in SymfonyBundleSecurityBundleResourcesconfigsecurity_listeners.xml) you can use a custom listener that extends UsernamePasswordFormAuthenticationListener.

All that's left to do is override the attemptAuthentication() method to dispatch the custom event.

(Actually you also need to store the event dispatcher as a class property in __construct())

This method should work with other authentication methods - all you'd need to do is modify the appropriate listener (ie BasicAuthenticationListener, X509AuthenticationListener, etc.)

Wednesday, September 7, 2022
2

You need to give the array_shift() the parameter! Look this example:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack); // Here you give the parameter
print_r($fruit);

You give the null parameter on array_shift() and you need to change it!

Update:

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Read here for more

Wednesday, October 19, 2022
 
shark
 
5

file_put_contents creates the file if it doesn't exist, but it fails if you try to put the file in a directory that doesn't exist. So you should try the following:

  • check if the images directory exists
  • check the write permissions of the directory
  • try with an absolute path, so in your case probably $target_dir = '/home/ragiththomas/Sites/asco-forum/images/';
Friday, December 9, 2022
1

As, @dennlinger mentioned in his answer : torch.utils.model_zoo, is being internally called when you load a pre-trained model.

More specifically, the method: torch.utils.model_zoo.load_url() is being called every time a pre-trained model is loaded. The documentation for the same, mentions:

The default value of model_dir is $TORCH_HOME/models where $TORCH_HOME defaults to ~/.torch.

The default directory can be overridden with the $TORCH_HOME environment variable.

This can be done as follows:

import torch 
import torchvision
import os

# Suppose you are trying to load pre-trained resnet model in directory- modelsresnet

os.environ['TORCH_HOME'] = 'models\resnet' #setting the environment variable
resnet = torchvision.models.resnet18(pretrained=True)

I came across the above solution by raising an issue in the PyTorch's GitHub repository: https://github.com/pytorch/vision/issues/616

This led to an improvement in the documentation i.e. the solution mentioned above.

Wednesday, November 30, 2022
 
2

Only when it is necessary to the program logic, such as to prevent form submission or another DOM element's action being triggered. Unless you have a seriously complex DOM tree, there will not be any serious performance hit.

Thursday, August 25, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :