Installing Ansible on a fresh Mavericks system.

I recently purchased a new Retina Macbook Pro, and was surprised that Ansible did not install as easily as it did before. Looking back through the output on the CLI we see it is trying to use Apples CLANG. Which unfortunately has some deprecated stuff. I guess i had already installed the correct GCC etc manually on my older machines.

No worries though, this is the simplest way i have found to power through this.

Firstly, install apples developer tools. Likely you will just download XCode through the App Store. Then once you have opened it at least once to agree to the terms and conditions, you will also need to go to the xcode preferences and ‘Locations’ tab, to select the latest ‘Command Line Tools’ version. Then you will need to run this on the command line.

This will install some additional command line tools which you will need. It will open a GUI prompt to install them, so just go ahead with that.

With XCode installed we can now install Homebrew, a package manager for mac, which will make installing the non Apple version of GCC much easier. To install Homebrew, run the following on the CLI.

Then run this, which will tell us if Homebrew is working correctly, or if there are any issues.

Now we must install the correct GCC via Homebrew.

With that now installed, we must move Apples GCC aside so that we can use the correct one.

Then, we are good to go ahead and run the steps on Ansibles installation page found here

Problems that were occurring previously with CLANG and the like should now be gone.

Credit where credit is due, these 2 sites documented the steps i followed:
[1] Installing Homebrew on OS X Mavericks 10.9, Package Manager for Unix Apps
[2] Paul Crawford : Installing gcc on Mavericks

Behat and “The current node list is empty.” error.

If your receiving the error “The current node list is empty” while using Behat, then likely your project is returning an HTTP 500 error. This may be because your bundle depends on some other bundle not available in your test environment. This was the case when for me when testing a bundle on travis-ci and i was not able to find documentation on this error.

One solution as suggested on github is to try dumping the contents of the scraper/browser to see what kind of error your getting.

However that will likely not give you any output if the server is returning an HTTP 500 internal server error. Your best bet is to just check your logs and do a search for any errors.

If your trying to run your functional tests on travis-ci then you might want to check out this great article on running Behat scenarios in isolation of your project.

Best of luck.

Simple Method for Checking for Order With Behat

While needing to write a test to check the order of 2 items (within a given CSS query) in Behat, I did a little googling and came across this website here.

The tutorial is great and i just wanted to give a shout out to the author for their great content. I also made a slight change to include testing for the existing of the 2 items.

Here is my slightly updated version:

You can see an example of the test i wrote using this method here.

Git tagging.

Quick cheat sheet for referencing. Tags are a way to bookmark a particular version of your git repository for future referencing. Use them to bookmark stable and dev versions of your project.

Add a tag with inline message (no editor):

Remove a tag:

You can also push tags with a regular push with the tags argument:

Done!

 

Authorisation on Roles in Symfony2

Following a discussion on the IRC room from someone who viewed my post about login/logout handlers in SF2, i wanted to clarify that when dealing with Roles, they should be dealt with in the controller actions.

For off, you need to make sure you got the right hierarchy of roles. In your app/config/security.yml you need something following the structure of:

Where the lowest level of access is at the top and the higher levels of access envelope the last levels of access.

Then you inside your controller actions you would do:

According the order of the role hierarchy if you have the role or higher than specified the controller action will continue, if you have a lower level of access than the minimum required then you will get an AccessDeniedException.

Git workflow.

1) create a branch.

2) add new files and commit.

3) push to github.

4) switch to new branch of github then PR and merge.

5) pull update from github with  merged changes.

DateTime::__construct(): It is not safe to rely on the system’s timezone settings.

If your getting the above error, then you have a misconfiguration in your php.ini file, this is very easy to remedy. Firstly find out which php.ini file your setup is using, you can do this using phpinfo(), just echo that out on its own in a plain php test script, like so:

Then locate where it mentions what php.ini you are using (do a page search on the output), and then load that php.ini file up and look for the line that states:

‘date.timezone’

it may be commented out like this ;date.timezone. Once you find it, set it to your region and capitol, as i have here:

date.timezone = ‘Europe/London’
And ensure that the semicolon is not at the beginning of this line. Save the file then restart your web servers Apache/PHP and the following error you saw should go away.

Enjoy.

Redirecting on login/logout in Symfony2 using LoginHandlers.

Using login handlers or even logout handlers, we can do a number of last minute things before the user is redirected to where they need to go. Being able to intercept the redirect at the last minute and make the user get redirected elsewhere is relatively simple.

Firstly though, i notice a lot of people now who are using Symfony 2 are cheating somewhat by using an EventListener and registering it with the security.interactive_login event listener. This is not the best way to go about this, namely because this event listener is not intended for this purpose. Sometimes event listeners are not the best way to go because they were defined for a specific purpose other than your intention and using them may have undesirable side effects you may not notice right away. Using handlers however allows a little more flexibility in that in all likelihood regardless of changes to Symfony in future revisions, they should still work and were designed specifically for the purposes we will need them for.

Firstly, we need to define our services, personally, i am a fan of YAML, so i define my services as such but feel free to do the same in XML:

Now our services are defined, we need to define the handlers themselves. I like to create a directory named Services in my user bundle for this purpose, then create 2 files. The first file is LoginSuccessHandler, the second is LogoutSuccessHandler. Our handlers will implement the appropriate interface for either our login or logout handlers, which require usually only one method in these instances.

Here is what the login handler should look like:

Note my namespace is CCDNUser, and my bundle is called SecurityBundle, so our service is named ccdn_user_security.component.authentication.handler.login_failure_handler. Change this to your own namespace and bundle name accordingly but ensure to use the same format as used here, underscore your namespace and append the bundle name with an additional underscore without the term ‘bundle’ in it. Failure to get this part correct will mean your namespace will not be loaded and you will get problems later on in this tutorial as exceptions will be thrown regarding the service not existing.

Here is what the logout handler should look like:

Our service definitions inject some of the classes we need to make use of our handlers, namely a Router object and the SecurityContext object, we can store these in member variables for later use. Then we wait for the onAuthenticationSuccess method to be called in our LoginSuccessHandler, once this has happened, we have a request and token interface object at our disposal. Using the security context object we can determine what role they have (i.e; ROLE_USER, ROLE_ADMIN etc) and appropriately redirect them as needed.

I like to redirect members of ROLE_USER role to where they came from prior to logging in. This is achieved by passing the ‘referer’ header into the redirect response object we return. I took this step both in the login and logout handlers.

Last thing we now need to do, is inform our security config that it needs to use these handlers by doing the following:

In this instance i am using FOSUserBundle and extending it in my own UserBundle, though this code is contained in the SecurityBundle. Which is probably the best way to do this. As the handlers used here work in any bundle because they plugin to Symfony2 core and implement their login handlers, and should not be dependant on any user bundle.

You can see a complete implementation of this in my security bundle found on github

Once this is implemented your on your way to a better login/logout system. Enjoy.

Symfony Embedded Forms ‘take 2’

Learnt some new things while working with symfony’s embedded forms, as we know, when they are have relations in the entities that the form is modelled after you want to ensure that when including the other form type that you create an instance of the formType representing the many in the manyToOne relationship. For example in the forum i am working on (which you can find on github here though currently still in development as of the date of this blog entry) a Topic has MANY Posts, as a result, we build our form by instantiating the PostType first and then adding the TopicType to it as a field because the PostType has a related Topic in the database schema.

But following up from the last post, what about when we have unique use cases for our form Types? What if we want to use a PostType without including the TopicType or we wish to conditionally decide wether for the purposes of editing we need to populate the form before presenting it?

To make this easier, in my FormHandlers what i choose to do is pass an array with some options we can use, namely a mode key specifies wether we are to ‘insert’ or ‘update’ an entity, and other keys for holding the entity to populate the form if mode is ‘update’

Here is an example from the top half of my TopicFormHandler:

To view the full source in greater context, explore the source on github here.