Tutorial Setup - Getting Docker Up and Running


In this video we are going to cover how to impersonate a user when using stateless authentication inside a Symfony 3 JSON API.

This video relies on a new feature in Symfony 3.4, which at the time of recording is still in Beta.

This feature is to allow a user with an appropriate level of permissions (an Admin, most of the time) to 'switch user' to become another valid user, when using stateless authentication. This is an incredibly useful feature that I use a lot, and have covered before on the site.

Up until Symfony 3.4, impersonating a user when using JSON Web Tokens / JWTs has only been possibly by including a third party bundle. As of Symfony 3.4, we get this functionality built right into the framework. Nice.

The setup we will use is:

  • Symfony 3
  • FOS REST Bundle
  • Lexik JWT Authentication Bundle

It should also be noted that we are using the provided Symfony serializer for this demo. You may wish to use a different one.

We won't be covering the setup of the JSON API during this video. Instead, we will solely be covering how to impersonate a user when you already have a working JSON API with stateless authentication.

We will begin by cloning the starting point to this project:

git clone https://github.com/codereviewvideos/docker-symfony3-starting.git
cd stateless-user-impersonation

Once inside the directory, we need to set the permissions up so Docker doesn't complain constantly:

# on Linux
sudo chown -R {your_username_here}:www-data .

# on OSX
sudo chown -R {your_username_here}:1000 .

# in my case
# sudo chown -R chris:www-data .

What this command does is recursively change the owning user and group for every file and folder in this current directory to be {your_username_here}:www-data.

We need to own the files so we can change them as part of our development process.

We need www-data to be the owning group, as the user www-data is a member of the group www-data, and this is the user our Docker container will run as.

Once we've set the permissions on our Symfony files, we can bring up our stack:

make dev

Note that if this doesn't work, you can copy / paste the same command from the Makefile.

This command runs our docker-compose up command, and as such will create a new directory in our local directory: ./volumes.

We also need to set some permissions on this directory.

sudo chown -R {your_username_here}:www-data ./volumes/php

If we don't do this step then our Docker container won't be able to create the cache / logs directories, and that would be sad.

Note that we run this second chown command on one specific sub-directory of volumes, not on every directory.

At this point our stack should be up and running, with our permissions problems sorted.

Code For This Course

Get the code for this course.

Episodes