Slim 3 Controller Issue: Using $this when not in object context - php

Having an issue using $this->container inside of my controllers.
When I try to access the logger/monolog in the code below, it fails miserably:
$this->container->get('logger')->addInfo('Request: users->get-one');
Here's the code in src/dependancies.php:
<?php
$container = $app->getContainer();
// monolog
$container['logger'] = function ($c) {
$logs = $c->get('settings')['logger'];
$logger = new Monolog\Logger($logs['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($logs['path'], $logs['level']));
return $logger;
};
// database
$container['db'] = function ($c) {
$database = $c->get('settings')['database'];
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection([
'driver' => 'mysql',
'host' => $database['hostname'],
'database' => $database['database'],
'username' => $database['username'],
'password' => $database['password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
return $capsule;
};
// register users controller
require __DIR__ . '/../src/controllers/users.php';
$container['UsersController'] = function($c) {
return new UsersController($c);
};
Below is the code that's in src/controllers/users.php:
<?php
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class UsersController {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function get(Request $request, Response $response, $args) {
$this->container->get('logger')->addInfo('Request: users->get-one');
/**
* TODO: Replace $args['id'] with the id from the current token payload
*/
$data = Users::find($args['id']);
return $response->withJSON($data)->withStatus(200);
}
public function create(Request $request, Response $response, $args) {
$this->logger->addInfo('Request: users->create');
$user = $request->getParsedBody();
$data = Users::create([
'first_name' => $user['first_name'],
'last_name' => $user['last_name'],
'email' => $user['email'],
'password' => password_hash($user['password'], PASSWORD_BCRYPT),
'telephone' => $user['telephone'],
'timezone' => $user['timezone'],
'verification' => '011010'
]);
return $response->withJSON($data)->withStatus(200);
}
public function update(Request $request, Response $response, $args) {
$this->logger->addInfo('Request: users->update');
$user = $request->getParsedBody();
$data = Users::where('id', $args['id'])->update([
'first_name' => $user['first_name'],
'last_name' => $user['last_name'],
'email' => $user['email'],
'password' => password_hash($user['password'], PASSWORD_BCRYPT),
'telephone' => $user['telephone'],
'timezone' => $user['timezone']
]);
return $response->withJSON($data)->withStatus(200);
}
public function delete(Request $request, Response $response, $args) {
$this->logger->addInfo('Request: users->delete');
$data = Users::destroy($args['id']);
return $response->withJSON($data)->withStatus(200);
}
/*
* ==============================
* Manager Functions Routines
* ==============================
*/
public function getAll(Request $request, Response $response, $args) {
$this->logger->addInfo('Request: admin->users->getAll');
$data = Users::all();
return $response->withJSON($data)->withStatus(200);
}
}
I have tried following different tutorials as well as the documentation on the Slim website however, nothing has seemed to fix the issue. I am sure it's something easily fixed that I am just missing.
PHP Slim 3 Framework - Use MonoLog in Custom Class - Using $this when not in object context - The accepted answer here just seems silly to do when I want to be able to access the entire app container and NOT just the logger.
Any help is appreciated.
Thank you!

Well, I figured it out. Apparently using two semicolons when invoking the controller routine is not the proper way of doing things. Oops!
/*
* users::read::one
* method:get
*/
$app->get('/users', '\UsersController::get');
When the code above is adjust to only use one semicolon, it works:
/*
* users::read::one
* method:get
*/
$app->get('/users', '\UsersController:get');

Related

Laravel Tap Formatter: where do I actually format the message?

In my laravel application there's need to format a message I need to send into slack. Hence I set a slack log channel into config/logging.php:
'slack' => [
'driver' => 'slack',
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],
Also as seen on documentation I can do a monolog formater, hence I did the following:
namespace App\Logging;
class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->setFormatter(...);
}
}
}
And specified it as tap into my log:
'slack' => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],
But in my formater where do I process the log entry itself? I mean:
The $handler->setFormatter does not seem to be a method of \Illuminate\Log\Logger class.
I cannot find out what method I need to override when I need to provide a custom format. I mean I have the invoke method then afterwards what?
Dimitrios is almost right (or perhaps this worked on older versions) but as Xavier said, if you copy that code exactly you will get the error
Return value of Monolog\Handler\AbstractProcessingHandler::processRecord() must be of the type array, null returned
You are going to want to do something like this instead :
<?php
namespace App\Logging;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SlackWebhookHandler;
use Request;
class CustomiseFormatter
{
protected $request;
public function __construct(Request $request = null)
{
$this->request = $request;
}
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$handler->setFormatter(new LineFormatter(
'[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
));
$handler->pushProcessor([$this, 'processLogRecord']);
}
}
}
public function processLogRecord(array $record): array
{
$record['extra'] += [
'url' => env("APP_URL"),
];
return $record;
}
}
Equally, as with how extra has be modified, you can change any other value.
The formatter for slack should be the following:
namespace App\Logging;
use Monolog\Formatter\LineFormatter;
class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$format=""; // Look on the Monolog's Line formatter documentation
$formatter= new LineFormatter($format,"Y-m-d H:i:s");
$handler->pushProcessor(function ($record) {
//Append extra info of formatting here
});
$handler->setFormatter($formatter);
}
}
}
}
And config the slack not to send attachment nessesary for the formatter to work:
'slack' => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
'attachment' => FALSE,
],
The setFormatter method takes a new Monolog Formatter as seen in: https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#formatters
Also the pushProcessor allows you to populate extra fields on your message eg. Showing an emoji on your log message:
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$format="%emoji% %message%";
$formatter= new LineFormatter($format,"Y-m-d H:i:s");
$handler->pushProcessor(function ($record) {
$record['emoji']=":poop:";
});
$handler->setFormatter($formatter);
}
}
}

cakephp authenticate with ldap and match to local user (or create one)

I'm trying to configure ldap authentication on cakephp 3.8 using the new cakephp/authentication plugin, and I'm not sure how to match the authenticated ldap user with a local entity.
My config closely follows the documentation and is available in full here.
in my Application.php the Application class implements both the AuthenticationServiceProviderInterface and the AuthorizationServiceProviderInterface
public function getAuthenticationService(ServerRequestInterface $request,
ResponseInterface $response)
{
$service = new AuthenticationService();
$service->loadIdentifier('Authentication.Password', [...]),
$service->loadIdentifier('Authentication.Ldap', [
'fields' => [
'username' => 'username',
'password' => 'password'
],
'host' => 'ldap.forumsys.com',
'port' => '389',
'bindDN' => function($username) {
return 'uid='.$username.',DC=example,DC=com';
},
'options' => [LDAP_OPT_PROTOCOL_VERSION => 3]
]);
$service->loadAuthenticator('Authentication.Session');
$service->loadAuthenticator('Authentication.Form', [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'loginUrl' => '/users/login'
]);
return $service;
}
In my middleware, I'm trying decorate the identity with authorization stuff. When authenticating using the regular local system the $identity is a App\Model\Entity\User, but when logging in with a ldap user it's a Authentication\Identity
So when I call setAuthorization
'identityDecorator' => function (AuthorizationServiceInterface $authorization,
ArrayAccess $identity) {
$identity->setAuthorization($authorization);
}
it fails with a Call to undefined method Authentication\Identity::setAuthorization() since all I have in $identity is
object(Authentication\Identity) {
'config' => [
'fieldMap' => [
'id' => 'id'
]
],
'data' => object(ArrayObject) {
username => 'einstein'
}
}
How would I match an authenticated ldap user with their local counterpart, and transform from Authentication\Identity to App\Model\Entity\User?
The final goal is to also optionally generate local users from ldap data if they don't exist.
middleware attempt
Application.php
public function middleware($middlewareQueue)
{
...
$middlewareQueue->add($authentication);
$middlewareQueue->add($ldap_matcher);
$middlewareQueue->add($authorization);
return $middlewareQueue;
}
LdapMatcherMiddleware.php
class LdapMatcherMiddleware
{
public function __invoke(ServerRequestInterface $request,
ResponseInterface $response, $next)
{
$identity = $request->getAttribute('identity');
if ($identity !== null) {
$identity = $this->buildIdentity($identity);
$request = $request->withAttribute('identity', $identity);
}
$response = $next($request, $response);
return $response;
}
public function buildIdentity($identity)
{
$Users = TableRegistry::getTableLocator()->get('Users');
$username = $identity->getOriginalData()['username'];
$user = $Users->find()->where(['username' => $username])->first();
if (is_null($identity)) {
$user = $this->createLocalUserFromLdap($identity);
}
return $user;
}
public function createLocalUserFromLdap($identity)
{
$Users = TableRegistry::getTableLocator()->get('Users');
$user = $Users->newEntity([
'username' => $identity->getOriginalData()['username']
]);
$Users->save($user);
return $user;
}
}
How would I match an authenticated ldap user with their local counterpart, and transform from Authentication\Identity to App\Model\Entity\User?
I would add another middleware after the authentication middleware and do that step there.
$users = TableRegistry::getTableLocator()->get('Users');
$entity = $users->newEntity($identity->getOriginalData());
Then do whatever you need to do for authorization with this entity.
The final goal is to also optionally generate local users from ldap data if they don't exist.
Just implement your logic somewhere and get the entity the same way as shown above from the identity.
$users = TableRegistry::getTableLocator()->get('Users');
$entity = $users->newEntity($identity->getOriginalData());
// Check if it exists inside this method and if not just create it
$users->createLocalUserFromLDAP($entity);

How to mock user creation if password is hidden in Laravel 5.5 unit tests

I have a unit acceptance test where I am mocking the creation of a user.
class UserAcceptanceApiTest extends TestCase
{
use WithoutMiddleware;
public function setUp()
{
parent::setUp();
$this->User = factory(App\Models\User::class)->make([
'id' => '999',
'name' => 'Name',
'email' => 'test#example.com',
'password' => bcrypt('password'),
]);
$this->User = factory(App\Models\User::class)->make([
'id' => '999',
'name' => 'Name',
'email' => 'test#example.com',
'password' => bcrypt('password'),
]);
$user = factory(App\Models\User::class)->make();
$this->actor = $this->actingAs($user);
}
public function testStore()
{
$response = $this->actor->call('POST', 'api/users', $this->User->toArray());
$this->assertEquals(200, $response->getStatusCode());
$this->seeJson(['id' => 999]);
}
}
I get the following exception "Field 'password' doesn't have a default value.
This is because in my User model I have the following:
protected $hidden = ['password', 'remember_token'];
So it automatically removes the password field from the JSON.
Is there a way I can override this only for this test? As I want to keep the password as a hidden attribute.
public function testStore()
{
$this->User->makeVisible(['password']);
$response = $this->actor->call('POST', 'api/users', $this->User->toArray());
$this->assertEquals(200, $response->getStatusCode());
$this->seeJson(['id' => 999]);
}

Symfony 3 Datafixtures using parameter.yml values

I'm using LDAP in my User data fixtures and I don't want to hardcode the LDAP login options. Initially, I tried this:
$options = array(
'host' => '%ldap_host%',
'port' => '%ldap_port%',
'useSsl' => true,
'username' => '%ldap_username%',
'password' => '%ldap_password%',
'baseDn' => '%ldap_baseDn_users%'
);
But that didn't work. I did some research and realized I needed to include the container in my fixtures. However, it's at this point I'm unsure what my next step is.
As I understand it I need to use the container and it's get method to get the service containing the parameters, but I don't know what that is:
$this->container->get('parameters');
Doesn't work, so I'm wondering what I should use.
My full datafixture is as follows:
class LoadFOSUsers extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
* #var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function load(ObjectManager $manager)
{
$this->container->get('parameters');
// Not sure how to access param values.
$options = array(
'host' => '%ldap_host%',
'port' => '%ldap_port%',
'useSsl' => true,
'username' => '%ldap_username%',
'password' => '%ldap_password%',
'baseDn' => '%ldap_baseDn_users%'
);
$ldap = new Ldap($options);
$ldap->bind();
$baseDn = '%ldap_baseDn_users%';
$filter = '(&(&(ObjectClass=user))(samaccountname=*))';
$attributes=['samaccountname', 'dn', 'mail','memberof'];
$result = $ldap->searchEntries($filter, $baseDn, Ldap::SEARCH_SCOPE_SUB, $attributes);
foreach ($result as $item) {
echo $item["dn"] . ': ' . $item['samaccountname'][0] . PHP_EOL;
}
}
public function getOrder()
{
// the order in which fixtures will be loaded
// the lower the number, the sooner that this fixture is loaded
return 8;
}
}
You just have to fetch them from container via getParameter('name') or get them all in a bag via getParameterBag().
So:
$options = array(
'host' => $this->container->getParameter('ldap_host'),
'port' => $this->container->getParameter('ldap_port'),
'useSsl' => true,
'username' => $this->container->getParameter('ldap_username'),
'password' => $this->container->getParameter('ldap_password'),
'baseDn' => $this->container->getParameter('ldap_baseDn_users')
);
etc.

Method refactoring?

In the TokenRepository you can see 3 similar methods. It create new entry to the tokens table but each method has different fields.
How can I refactor this? Should I merge 3 methods into 1 method or should I use strategy pattern?
TokenRepository Class:
class TokenRepository
{
public function createTokenDigitalOcean(User $user, $name, $accessToken, $refreshToken = null)
{
return $user->tokens()->create([
'name' => $name,
'provider' => 'digital_ocean',
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
]);
}
public function createTokenLinode(User $user, $name, $key)
{
return $user->tokens()->create([
'name' => $name,
'provider' => 'linode',
'linode_key' => $key,
]);
}
public function createTokenAws(User $user, $name, $key, $secret)
{
return $user->tokens()->create([
'name' => $name,
'provider' => 'aws',
'aws_key' => $key,
'aws_secret' => $secret,
]);
}
}
I have 3 classes like DigitalOceanProvider, LinodeProvider and AwsProvider. For example of using LinodeProvider and AwsProvider class.
class LinodeProvider
{
public function callback()
{
$this->tokenRepo->createTokenLinode($user, $name, $key);
}
}
class AwsProvider
{
public function callback()
{
$this->tokenRepo->createTokenAws($user, $name, $key, $secret);
}
}
This may be a bit overkill, but in order to make life a bit easier in the future, you could create separate implementations of each that extend an abstract class. This way you can unify and define the interface and easily add new token types.
<?php namespace Foo\Tokens;
abstract class Token
{
protected $name = '';
protected $key = '';
protected $provider = '';
public function __construct($name, $key)
{
$this->name = $name;
$this->key = $key;
}
public function data()
{
return [
'name' => $this->name,
'provider' => $this->provider,
'token' => $this->key
];
}
}
Next, we create our Digital Ocean token class. This class can either use the default implementation or redefine it.
<?php namespace Foo\Tokens;
use Foo\Tokens\Token;
class DigitalOceanToken extends Token
{
protected $provider = 'digital_ocean';
public function __construct($name, $key, $refreshToken = null)
{
parent::__construct($name, $key);
$this->refreshToken = $refreshToken;
}
public function data()
{
return [
'name' => $this->name,
'provider' => $this->provider,
'key' => $this->key,
'refreshToken' => $this->refreshToken
];
}
}
The TokenRepository now merely cares about attaching a given token to a user.
<?php namespace Foo;
use User;
use Foo\Tokens\Token;
class TokenRepository
{
public function createToken(User $user, Token $token)
{
return $user->tokens()->create(
$token->data()
);
}
}
And your service providers are as simple as...
<?php
use Foo\Tokens\AwsToken;
class AwsProvider
{
public function callback()
{
$this->tokenRepo->createToken(
$user, new AwsToken($name, $key, $secret)
);
}
}
This isn't working code, as I've not attempted to run it however it's just another idea of how you can organize and assign responsibility. Hope it helps, and welcome feedback from others.
According to me you should implement it like this:
class TokenRepository
{
public function createTokenForVendor(User $user, $inputs)
{
return $user->tokens()->create($inputs);
}
}
and inside your callback:
class VendorProvider
{
public function callback()
{
switch($tokenType) {
case 'DigitalOcean':
$inputs = [
'name' => $name,
'provider' => 'digital_ocean',
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
];
break;
case 'Linode':
$inputs = [
'name' => $name,
'provider' => 'linode',
'linode_key' => $key,
];
break;
case 'Aws':
$inputs = [
'name' => $name,
'provider' => 'aws',
'aws_key' => $key,
'aws_secret' => $secret,
];
break;
}
$this->tokenRepo->createTokenForVendor($user, $inputs);
}
}
Hoping you should do some code structure revamp.
Hope this helps!

Categories