Problem with ApiPlatform SearchFilter returning all results - api-platform.com

* #ApiFilter(SearchFilter::class, properties={"serial": "partial"})
I have the following on a products entity. The problem is, when I make API call with ?serial= it returns ALL items in the DB, obviously should have only returned one
EDIT:
This is caused by using a custom controller for the GET method. Looks like it needs refactorring.
* collectionOperations={
* "get"={
* "controller"=DeviceGetCollectionController::class,
* },
Bypasses the searchFilter

Solution was add a check in the custom controller:
if($request->get('serial')){
return $query->findBy(['company' => $user->getCompany(),'serial'=>$request->get('serial')]);
}

Related

Get Info Api Request

currently i'm struggle with the identifier because i not need it.
i only need an get request without anything and returning some system infos.
/**
* #ApiResource(
* itemOperations={
* "info"={
* "method"="GET",
* "path"="/system/info",
* "controller"=GetInfo::class,
* "read"=false
* }
* },
* collectionOperations={
*
* }
* )
*/
thats my current config but it always requires an identifier.
It sounds as if you're after not an ApiPlatform resource, but simply a standard endpoint.
Have a look at this documentation from Symfony on how to define a route.
To me it sounds like you're after something like this:
config/routes.yaml
system_info:
path: /system/info
method: GET
controller: App\System\Info
With controller:
namespace App\System;
class Info
{
public function __invoke(): JsonResponse
{
return new JsonResponse(['pc' => 'master race']);
}
}
Remember that ApiPlatform is supposed to be working with Resources (aka: DTO's and Entities), and as you're use-case does not do either, you end up struggling to make it work.

How can I "validate" DELETE request in api-platform

I want to check the entity variable and check if it is allowed to delete the entity. For example if the owner entity of the association is linked to another entity, I want to make the deletion impossible.
I've looked in the documentation of api-platform bu I could not find any help regarding my problems. Either you give the right to delete or not. I could not find how to control it (equivalent to validation for POST, PUT and PATCH).
You can use the access control feature of Api-Platform and Symfony Expression Language to achieve what you want. This way you can write pretty complex expressions.
I hope this example makes it clear.
user is the currently logged in user.
object is the resource user is trying to delete.
/**
* #ApiResource(
* itemOperations={
* "delete"={
* "access_control"="is_granted('ROLE_USER') and object.getUsers().contains(user),
* }
* }
* )
*/
class Entity
{
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="User", inversedBy="entities")
* #ORM\JoinTable(name="entity_users")
*/
private $users;
/**
* #return ArrayCollection
*/
public function getUsers(): ArrayCollection
{
return $this->users;
}
}
In this case only users who are stored in users Array of Entity can delete this resource.

Joomla! 3.xx *onUserLogout* event not working

I have successfully implemented the onUserAuthenticate event to implement my custom authentication API inside the Joomla! site that I am working on.
Now I want to also have some custom code run on the onUserLogout event.
I have added the following code to the custom authentication plugin file.
But this method is not getting fired/invoked while the previous one(onUserAuthenticate) is working just fine.
/**
* Method to handle the SSO logout
*
* #param array $user Holds the user data.
* #param array $options Array holding options (client, ...).
*
* #return boolean Always returns true.
*
* #since 1.6
*/
public function onUserLogout($user, $options = array()) {
if (JFactory::getApplication()->isSite()) {
// Set the cookie to expired date.
setcookie('customAuth', '123', time() - (60 * 60 * 24 * 365), '/', '.customdomain.org');
}
return true;
}
Okay so I was getting it all wrong.
So I was adding the aforementioned method inside the same plugin file that handled the onUserAuthenticate.
For Joomla! the login is a separate process which has its respective events like onUserAuthenticate.
But it seems like the event onUserLogout has to be inside the plugin with the type of user.
So I created a separate plugin inside the user plugin type directory, installed it, and enabled it....And voila!! it worked.
This had me scratching my head for quite a while.

Laravel - pass parameters through app->bind to model's constructor

Well, code describes it all. I've an Entity Service Provider, which pass an instance of Playlist Model, which supposed to get an array as it's first constructor parameter. How to pass that parameter through app->bind? Knowing that EntityServiceProvider is automagically injected when referenced in the controller.
// Current Code
/**
* Playlist Entity
*
* #return Playlist\PlaylistEntity
*/
$this->app->bind('Playlist\PlaylistEntity', function($app)
{
return new PlaylistEntity($app->make('Playlist\PlaylistRepositoryInterface'));
});
// Should be something like this:
/**
* Playlist Entity
*
* #return Playlist\PlaylistEntity
*/
$this->app->bind('Playlist\PlaylistEntity', function($app)
{
return new PlaylistEntity($app->make('Playlist\PlaylistRepositoryInterface', $parameters));
});
Similar case: Laravel 4: Passing data from make to the service provider
Alex Russell's comment works for me as well.
The answer in the 'similar case' post is correct as far as I can tell. $this->app->bind('Whatever', function ($app, $params) { var_dump($params); }); followed by App::make('Whatever', [1, 2, 3]); var_dumps the [1, 2, 3] array for me.
In Laravel 5.4 the ability to pass config parameters when resolving from the container using App::make() was removed and subsequently re-implemented as App::makeWith().
BTW I tried to make this a comment on the previous answer, but it didn't let me. Maybe due to not enough experience points?
Thanks #yevgeniy-afanasyev for pointing out the problems when mocking. If you need to mock these instances, you could go for Taylor Otwell's early suggestion here: https://github.com/laravel/ideas/issues/391#issuecomment-285197048
I just needed that and it worked well. Just return a closure and call it, when you ::make.
// Service Provider
$this->app->bind(MyClass::class, function ($app) {
return function($param) : MyClass
{
return new MyClass($param);
}
}
// ::make
$myInstance = App::make(MyClass::class)($myParameter);
// mock
$myMock = Mockery::mock(new MyClass($myParameter));
$this->instance(MyClass::class, function($param) use ($myMock) { return $myMock; });

Typo3 extbase validating custom or manual objects

I have created extbase extension, there for some reason I need to create object manually in create action.
My create action looks like this,
/**
* action create
*
* #return void
*/
public function createAction() {
$newObj = new \TYPO3\Myext\Domain\Model\Modelname();
$newObj->setMyval('value');
$this->myrepository->add($newObj);
}
Here the problem is its not validating for require field, captcha etc even if I mention #validate NotEmpty in model.
So how to make the validation of manually created object ?
It should throw error to form like out-of-the-box features.
Thank you.
Out of the box validation is only triggered on constructing model objects from GET/POST parameters according to your controller actions signature.
It should look something like this:
/**
* action create
* #param \TYPO3\Myext\Domain\Model\Modelname $newObject
* #return void
*/
public function createAction(\TYPO3\Myext\Domain\Model\Modelname $newObject) {
$this->myrepository->add($newObj);
}
Take a look at the extension_builder, create a model and let the new/create action be generated for you. That will give you a good example on the create action as well as on the new action where the form is being rendered.

Resources