Symfony2 won't load custom authentication provider, loads DaoAuthenticationProvider - php

This is a continuation of my last question.
Hi,
I'm implementing, in a Symfony2 application, a custom authentication provider in order to authenticate against the Wordnik REST API.
On application load, no matter what request path, this is the exception I get:
( ! ) Fatal error: Cannot access parent:: when current class scope has no parent in /[..]/WordRot/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php on line 43
You can see the full stacktrace here.
Second to last line in the trace reveals that it is loading the DaoAuthenticationProvider:
18 0.0217 1922792 Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider->__construct( ) ../appDevDebugProjectContainer.php:3071
But none of my configuration refers to that provider, or anything that extends it. My custom provider directly implements the AuthenticationProviderInterface.
So I assume that my configuration is wrong, and somewhere I need to be explicitly setting the WordnikProvider, but I'm not sure where! Research has not provided any clues to this issue.
Any help would be much appreciated!
Files
/app/config/config.yml
/app/config/security.yml
/src/WordRot/PlayBundle/Security/Authentication/Provider/WordnikProvider.php
/src/WordRot/PlayBundle/Security/Authentication/Token/WordnikUserToken.php
/src/WordRot/PlayBundle/Security/Firewall/WordnikListener.php
/src/WordRot/PlayBundle/DependencyInjection/Security/Factory/WordnikFactory.php

the line return $this->authenticationManager->authenticate(new WordnikUserToken($username, $password, $this->providerKey)); in the WordnikListener goes to
Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager (classes.php)
authenticate.
$this->providers are DaoAuthentificationProvider, WordnikProvider and AnonymousAuthentificationProvider.
From the DaoAuthentificationProvider it only uses the method supports($token):
return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey();
which returns false so next in line is WordnikProvider.
Oh..misread: the error is in the constructor:
parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions); seems to fail. Running PHP 5.4.10 or so I DON'T have an error!!
Either rm -rf vendor and run composer install again or try using a different PHP version!!

A had to create something like this a week ago.
At the and, I created a custom user provider, where I simply call the api and with the response i create the user or not.
I would advise to read this:
http://symfony.com/doc/2.0/cookbook/security/custom_provider.html

Related

Service override doesn't work as expected

I create a service inside my module with the name of an existing core service (prestashop.adapter.data_provider.product). It successfully replaces it as seen in php ./bin/console debug:container output.
In yaml:
prestashop.adapter.data_provider.product:
class: PrestaShop\Module\MyModule\Adapter\Product\ProductDataProvider
The problem now is that I have 500 errors in some pages in BO. These errors are type errors like:
Type error: Argument 4 passed to
PrestaShopBundle\Model\Product\AdminModelAdapter::__construct() must
be an instance of
PrestaShop\PrestaShop\Adapter\Product\ProductDataProvider, instance of
PrestaShop\Module\MyModule\Adapter\Product\ProductDataProvider given,
called in....
I understand now that, whenever a constructor has an argument of type ProductDataProvider, the app tries to load my service but find a differentuse statement in the class (i.e in PrestaShopBundle\Model\Product\AdminModelAdapter)
These errors can be fixed by replacing the use statement in each file containing the problem, but as you may know, touching core files must be avoided.
Is there a way of overriding an existing service, but also make the override work all across the app "bypassing the use statements of the old service".

Testing Laravel Service Providers

I'm (we're) creating a package that acts as a core component for our future CMS and of course that package needs some unit tests.
When the package registeres, the first thing it does is set the back/frontend context like this:
class FoundationServiceProvider extends ServiceProvider
{
// ... stuff ...
public function register()
{
// Switch the context.
// Url's containing '/admin' will get the backend context
// all other urls will get the frontend context.
$this->app['build.context'] = request()->segment(1) === 'admin'
? Context::BACKEND
: Context::FRONTEND;
}
}
So when I visit the /admin url, the app('build.context') variable will be set to backend otherwise it will be set to `frontend.
To test this I've created the following test:
class ServiceProviderTest extends \TestCase
{
public function test_that_we_get_the_backend_context()
{
$this->visit('admin');
$this->assertEquals(Context::BACKEND, app('build.context'));
}
}
When I'm running the code in the browser (navigating to /admin) the context will get picked up and calling app('build.context') will return backend, but when running this test, I always get 'frontend'.
Is there something I did not notice or some incorrect code while using phpunit?
Thanks in advance
Well, this is a tricky situation. As I understand it, laravel initiates two instances of the framework when running tests - one that is running the tests and another that is being manipulated through instructions. You can see it in tests/TestCase.php file.
So in your case you are manipulating one instance, but checking the context of another (the one that did not visit /admin and is just running the tests). I don't know if there's a way to access the manipulated instance directly - there's nothing helpful in documentation on this issue.
One workaround would be to create a route just for testing purposes, something like /admin/test_context, which would output the current context, and the check it with
$this->visit('admin/test_context')->see(Context::BACKEND);
Not too elegant, but that should work. Otherwise, look around in laravel, maybe you will find some undocumented feature.

Apigility "Error saving field" while creating "db-connected" service

When I try to create a new service using db-connected in Apigility, I'm getting the error Error saving field. After receive the error, the service is created but if I select this one, I receive the error Unable to fetch service.
It seems to happen always when I create a db-connected service in a table with name containing "_".
The error that I'm getting in console is:
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (Hookit-V1-Rest-Vitrine_usuario-Controller, line 0)
I checked the module's config and the parameters are have been created.
in module's config in node zf-apigility O removed the parameter resource_class and the run the service... It worked.
but I can't sync the service in admin.
Does someone knows how to solve it to sync the service in Apigility's admin?
Alessandro Garcez is correct. This issue was resolved in this merge https://github.com/zfcampus/zf-apigility-admin-ui/pull/59
However, this merge has been overwritten and in the latest version, you will get the same issue. I have made a pull request to bring back the fix that Alessandro Garcez mentioned.
It seems that when the new service is created, if the table name contains an underscore ( _ ), the API will convert this to what looks like CamelCase.
But when the UI then wants to create the fields for this newly created service, it (the UI) will still use the tablename with the underscore in it, when accessing the api (/apigility/api/module//rest/--Rest--Controller/input-filter)
But the API will this time expect the controller-part being specified with the tablename camedcased:
(/apigility/api/module//rest/--Rest--Controller/input-filter)
It had already been fixed but was undone, I don't know why.
There is a opened discussion https://github.com/zfcampus/zf-apigility-admin-ui/issues/78.
For now is possible follow this way:
Change the function capitalizeFirstLetter in file src/apigility-ui/service/api.service.js adding the line var string = string.replace(/(\w)/g, function(,letter) { return letter.toUpperCase(); }); before the return.

Laravel 5 > Using monolog introspection processor

I have configured Laravel 5 to use a custom logging configuration (default is way too simple). I've added monolog's IntrospectionProcessor to log the file name and line number of the log call.
The problem is that all lines get the same file and line number:
[2015-06-29 17:31:46] local.DEBUG (/home/vagrant/project/vendor/laravel/framework/src/Illuminate/Log/Writer.php#201): Loading view... [192.168.10.1 - GET /loans/create]
Is there a way to config the IntrospectionProcessor to print the actual lines and not the facade ones?
If I do Log::getMonolog()->info('Hello'); it works and prints the correct file and line number... but I don't know how safe is to avoid calling the Writer.writeLog function because it fires a log event (is it safe to not fire that event?).
(Only tried in Laravel 4.2!)
When pushing the Introspection Processor to Monolog it is possible to give an skipClassesPartial array as second parameter in the IntrospectionProcessor contructor. With this array it is possible to skip the Laravel Illuminate classes and the logger logs the class calling the log method.
$log->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, array('Illuminate\\')));
also see: https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/IntrospectionProcessor.php
I know this is an old question but I thought I'd give a quick update because it's pretty easy to get this done now.
I haven't tried with Laravel but My own logging mechanism is within a LoggingService wrapper class. As such the introspection was only giving details about the service rather than the caller.
after reading Matt Topolski's answer, I had a look in the IntrospectionProcessor.php. the constructor looks like this:
__construct($level = Logger::DEBUG, array $skipClassesPartials = array(), $skipStackFramesCount = 0)
All I had to do was add the processor like this:
log->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, array(), 1));
This is actually the expected functionality unless you're having the handler process the logs directly (check out the comments at the top of IntrospectionProcessor.php). My guess is you have a wrapper function around the logger and you're calling it from Writer.php -- BUT
If you look at the code for IntrospectionProcessor.php you'll see a bit of code on lines 81 to 87 that decides how to format that stack trace, and it still has access to the stack. If you bump the $i values for $trace[$i - 1] / $trace[$i] up one (aka $trace[$i]/$trace[$i + 1] respectively) you can 'climb' the stack back to where you want.
It's important to note that the 'class' and 'function' parts of the trace need to be one level of the stack higher than the 'file' and 'line.'
On a personal (plz dont mod me bruhs) note, I'd like to see functionality to include a stack offset when throwing the log in. I know what function I want to blame if an error shoots out when I write the error_log('ut oh') but I might(will) forget that by the time the 'ut oh' comes.

CakePHP Facebook integration tutorial "Fatal error: Call to a member function share() on a non-object" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call to a member function on a non-object
I'm doing this tutorial here:
http://tv.cakephp.org/video/webtechnick/2011/01/12/nick_baker_--_facebook_integration_with_cakephp
I baked a new project with cake bake facebook_app and set the configuration database file to the correct settings and the default cakePHP screen showed that tmp directory was writable, DB setup was good, etc.
I downloaded the CakePHP plugin by WebTechNick here: https://github.com/webtechnick/CakePHP-Facebook-Plugin, and filled out app information (app_secret, app_id, etc), adding it to facebook_app/config/facebook.php
Changed facebook_app/app_controller.php:
class AppController extends Controller {
var $name = 'Facebook';
var $helpers = array('Session', 'Facebook.Facebook');
}
Then just exactly as in the tutorial `facebook_app/views/pages/home.ctp':
<h1>Facebook_App</h1>
<?php $this->Facebook->share(); ?>
returning the error message:
Undefined property: View::$Facebook
I realize that means PHP didn't recognize Facebook as an object. But I installed the plugin!
Also, it seems not MVCish to have something like $this->Facebook->share(); in a view (home.ctp). However, this is exactly how WebTechNick does it in his tutorial (I followed it exactly 3x) and it does not work for me. I'm a complete noob at cakePHP (although I've read the entire documentation) and I'm just trying to learn and understand through examples.
:) To be fair, it's PHP - you didn't install anything. Or if you prefer, "install" != "invoke." PHP is really amazingly easy to debug. I mean, it tells you exactly what's wrong:
Like turning to a channel that's not on the air, the error your getting means that the object you're calling doesn't actually exist, at least not in the scope you're trying to invoke it.
Is that your IDE? Is it set up for your Cake app? Are you sure the instructions were to set your AppController's $name to 'Facebook' instead of $name = Facebook_App in your AppController? It looks like you either replaced your actual app's AppController with the plugin files instead of putting them in the proper directory, or the plugin is not deferring / calling / extending / returning to the application the way it's supposed to. Knee jerk -> typo, naming conflict, path problem, permissions.
Cake's not even rendering. I can tell because your screenshot would show that error with the styled Cake errors. That tells you it's erroring before AppController class makes it to View class.
Create an instance of the Facebook object statically in the view and see what happens. Then, what does
function beforeFilter() {
parent::__construct() ?
}
get you? Anything? What about debug(), var_dump, the object functions will also shed light on what's happening. So will your logfiles.
Btw, if you don't use them already: Firefox + FirePHP + Xdebug = made of win.
I was having this problem and I found that the plugin I was using was for CakePHP 1.3 and I was using Cake 2.0. I found the BETA branch for the upgraded Cake 2.0 and it worked perfect.
Here is the Cake 2.0 BETA Branch

Categories