Reuse dbal connection for db configuration in doctrine migrations - symfony

In a Symfony project, I have these two dependencies installed:
"doctrine/dbal": "^3.3",
"doctrine/migrations": "^3.5",
I have several db connections defined in Symfony, one of them is:
db.connection:
class: Doctrine\DBAL\Connection
public: true
factory: Doctrine\DBAL\DriverManager::getConnection
arguments:
$params:
driver: pdo_pgsql
url: '%env(DB_URL)%'
charset: UTF8
To run the migrations in the application I execute:
vendor/bin/doctrine-migrations migrate --configuration=doctrine-config.php --db-configuration=doctrine-db.php
The content of doctrine-config.php is:
<?php
declare(strict_types=1);
return [
'table_storage' => [
'table_name' => 'doctrine_migration_versions',
'version_column_name' => 'version',
'version_column_length' => 1024,
'executed_at_column_name' => 'executed_at',
'execution_time_column_name' => 'execution_time'
],
'migrations_paths' => [
'Infrastructure\Doctrine\Migrations'
=> 'src/Infrastructure/Doctrine/Migrations'
],
'all_or_nothing' => true,
'transactional' => true,
'check_database_platform' => true,
'organize_migrations' => 'none',
'connection' => null,
'em' => null,
];
The content of doctrine-db.php is:
<?php
declare(strict_types=1);
use Doctrine\DBAL\DriverManager;
return DriverManager::getConnection([
'diver' => 'pdo_pgsql',
'url' => getenv('DB_URL'),
'charset' => 'UTF8'
]);
I wonder if there is a way to reuse the defined connection db.connection when running the migration to avoid writing the file doctrine-db.php.
Thanks!

Related

Api Platform - NotFoundHttpException when calling an itemOperations endpoint

I have created a custom endpoint using API Platform:
#[ApiResource(
collectionOperations: [
'get' => [
'normalization_context' => ['groups' => ['product:list']]
],
'post' => [
'denormalization_context' => ['groups' => ['product:list']]
]
],
itemOperations: [
'get_by_user' => [
'method' => 'GET',
'path' => '/products/user/{id}',
'read' => 'false',
'controller' => ProductController::class,
'normalization_context' => ['groups' => ['product:list']],
'openapi_context' => [
'summary' => 'Retrieves the Product collection for a specific user ID',
'description' => 'Retrieves the Product collection for a specific user ID'
]
],
], )]
and the controller method:
public function __invoke(int $id): array
{
$prod = $this->productRepository
->findBy(
['user' => $id],
);
return $prod;
}
And the error when I try to call this endpoint is:
Not Found - api/vendor/api-platform/core/src/EventListener/ReadListener.php
Has anyone any ideas what might be the issue?

Configuring api-platform to include header in OpenAPI (AKA Swagger) documentation

I am using API-Platform with JWTs and have created a decorator to allow Swagger to display a /authentication_token endpoint as described by https://api-platform.com/docs/core/jwt/ and to a lesser degree https://api-platform.com/docs/core/openapi/. In addition to providing an email and password, I also need to provide a third parameter which is a UUID which identifies the account the user belongs to. I was able to do so by including the UUID in the body, but I would rather have it included as a header such as X-Account-UUID.
How can this be modified so that Swagger-UI includes this uuid property as a header and not in the body's JSON?
<?php
// api/src/OpenApi/JwtDecorator.php
declare(strict_types=1);
namespace App\OpenApi;
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Model;
final class JwtDecorator implements OpenApiFactoryInterface
{
private $decorated;
public function __construct(OpenApiFactoryInterface $decorated)
{
$this->decorated = $decorated;
}
public function __invoke(array $context = []): OpenApi
{
$openApi = ($this->decorated)($context);
$schemas = $openApi->getComponents()->getSchemas();
/*
echo(json_encode(['class'=>get_class($this), 'methods'=>get_class_methods($this)]));
echo(json_encode(['class'=>get_class($this->decorated), 'methods'=>get_class_methods($this->decorated)]));
echo(json_encode(['class'=>get_class($openApi), 'methods'=>get_class_methods($openApi)]));
echo(json_encode(['class'=>get_class($openApi->getComponents()), 'methods'=>get_class_methods($openApi->getComponents())]));
echo(json_encode(['class'=>get_class($schemas), 'properties'=>$schemas]));
$securitySchemas = $openApi->getComponents()->getSecuritySchemes();
echo(json_encode(['class'=>get_class($securitySchemas), 'properties'=>$securitySchemas]));
$headers = $openApi->getComponents()->getHeaders();
exit(json_encode(['class'=>get_class($headers), 'properties'=>$headers]));
*/
$schemas['Token'] = new \ArrayObject([
'type' => 'object',
'properties' => [
'token' => [
'type' => 'string',
'readOnly' => true,
],
],
]);
$schemas['Credentials'] = new \ArrayObject([
'type' => 'object',
'properties' => [
'email' => [
'type' => 'string',
'example' => 'john.doe#bla.com',
],
'password' => [
'type' => 'string',
'example' => 'secret',
],
'uuid' => [
'type' => 'string',
'in' => 'header',
'example' => '1234abcd-abcd-1234-abcd-123412341234',
],
],
'headers' => [
'uuid' => [
'type' => 'string',
'in' => 'header',
'example' => '1234abcd-abcd-1234-abcd-123412341234',
],
],
]);
$responses = [
'200' => [
'description' => 'Get JWT token',
'content' => [
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/Token',
],
],
]
]
];
$content = new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/Credentials',
],
],
]);
$requestBody = new Model\RequestBody('Generate new JWT Token', $content);
$post = new Model\Operation('postCredentialsItem', [], $responses, 'Get JWT token to login.', '', new Model\ExternalDocumentation, [], $requestBody);
$pathItem = new Model\PathItem('JWT Token', null, null, null, null, $post);
$openApi->getPaths()->addPath('/authentication_token', $pathItem);
return $openApi;
}
}

How to override laravel auto redirection if session exists

Hello i am new in laravel and i am working on laravel 5.7 i have 2 different auth tables one is users and other is companies first is how can i check that from which table is user logged in and second when i close and open browser laravel auto redirects to home if session exist but i also want to add check their to redirect user on certain page means if user logged in from user table redirect to abc view and if company is logged in from company table redirects to xyz view how can i override ?
Here is what i do for multiple auth tables :
Go to config > auth.php
Add Guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
'hash' => false,
],
'company' => [
'driver' => 'session',
'provider' => 'companies',
'hash' => false,
],
],
Add Provider
'providers' => [
'admins' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'companies' => [
'driver' => 'eloquent',
'model' => App\Companies::class,
],
],
Add Password
'passwords' => [
'admins' => [
'provider' => 'admins',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'companies' => [
'provider' => 'companies',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
GO to App\Http\Middleware\RedirectIfAuthenticated
Update handel method
public function handle($request, Closure $next, $guard = null)
{
if ($guard === 'admin' && Auth::guard($guard)->check()) {
return redirect('/admin');
}
if ($guard === 'company' && Auth::guard($guard)->check()) {
return redirect('/company');
}
return $next($request);
}
Now i'm asumming you've different controller for users and companies in that case you can add middleware in you're controller
public function __construct()
{
// For Admin Users
$this->middleware('auth:admin');
// For Companies Users
//$this->middleware('auth:company');
}
Or you can add middleware in the route as well
Example
Route::group([
'namespace' => 'Companies',
'middleware' => 'auth:company'
], function () {
// Authentication Routes...
Route::get('login', 'Auth\LoginController#showLoginForm')->name('login');
});
I Hope this helps.

Creation of the helper (Helper) in Zend Framework 3 (Is impossible)

I am create class in /module/Application/src/View/Helper/Messages
Code:
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Messages extends AbstractHelper {
public function __construct() {
}
protected function renderErrorMessages($item) {
echo "!!!";
}
}
Add his in module.config.php
Code:
....
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
Controller\UserController::class => InvokableFactory::class,
],
],
'view_helpers' => [
'factories' => [
View\Helper\Messages::class => InvokableFactory::class,
],
'aliases' => [
'messages' => View\Helper\Messages::class
]
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
On page login.phtml i am insert code:
...
<?php echo $this->Messages()->renderErrorMessages(); ?>
...
And get in log error:
[Wed Mar 13 12:18:30.725622 2019] [php7:error] [pid 9697] [client
127.0.0.1:52914] PHP Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: A plugin by
the name "Messages" was not found in the plugin manager
Zend\View\HelperPluginManager in
/var/www/webuseorg4/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:142\nStack
trace:\n#0
/var/www/webuseorg4/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(376):
Zend\ServiceManager\AbstractPluginManager->get('Messages', NULL)\n#1
/var/www/webuseorg4/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(394):
Zend\View\Renderer\PhpRenderer->plugin('Messages')\n#2
/var/www/webuseorg4/module/Application/view/layout/login.phtml(32):
Zend\View\Renderer\PhpRenderer->__call('Messages', Array)\n#3
/var/www/webuseorg4/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(506):
include('/var/www/webuse...')\n#4
/var/www/webuseorg4/vendor/zendframework/zend-view/src/View.php(207):
Zend\View\Renderer\PhpRenderer->render(NULL)\n#5
/var/www/webuseorg4/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrateg
in
/var/www/webuseorg4/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php
on line 142, referer: http://127.0.0.128/user/login
1
What do I do not so?
Update view-helper configuration with Full qualified class name.
'view_helpers' => [
'factories' => [
Application\View\Helper\Messages::class => InvokableFactory::class,
],
'aliases' => [
'messages' => Application\View\Helper\Messages::class
]
],
And On page login.phtml:
<?php echo $this->messages()->renderErrorMessages($item); ?>
...

Cakephp 3.x Saving multiple tables

In my cakephp (3.3) project, How to save multiple tables in a single save. My association is as follows, also I am giving my code here.
Table1 : users
In class UsersTable extends Table
$this->hasOne('ProfileDetails', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
Table2 : profile_details
In class ProfileDetailsTable extends Table
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
What should be the data format to save multiple tables?
In my template I have added the following , for now I have used only few fields
$this->Form->input('User.username',['type' => 'text','class'=>'form-control']) ?>
$this->Form->input('User.password',['type' => 'password','class'=>'form-control']) ?>
$this->Form->input('User.email',['type' => 'text','class'=>'form-control']) ?>
$this->Form->input('ProfileDetail.first_name',['type' => 'text','class'=>'form-control']) ?>
In the controller before saving i have debuged which gives result as follows
debug($this->request->data);
$user = $this->Users->patchEntity($user, $this->request->data);
debug($user);
exit;
$this->Users->save($user)
Result of debug
\src\Controller\UsersController.php (line 39)
[
'User' => [
'username' => 'Tester',
'password' => '43434324',
'email' => 'test#gmail.com',
'role' => 'admin'
],
'ProfileDetail' => [
'first_name' => 'Tester'
]
]
\src\Controller\UsersController.php (line 41)
object(App\Model\Entity\User) {
'User' => [
'username' => 'Tester',
'password' => '43434324',
'email' => 'test#gmail.com',
'role' => 'admin'
],
'ProfileDetail' => [
'first_name' => 'Tester'
],
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'User' => true,
'ProfileDetail' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [
'email' => [
'_required' => 'This field is required'
]
],
'[invalid]' => [],
'[repository]' => 'Users'
}
Could you please suggest me how to save this data in multiple tables with single save with validation?
Looking at your debug, there is an error saying the email field is required. Maybe that is your problem..
If still not working , try this:
replace User by users
and ProjectDetail by project_details
In your form. And also remove joinType from following:
$this->hasOne('ProfileDetails', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);

Resources