Logging in as another user in CakePHP

Sometimes, we are tasked with features like making an application administrator log in as a different user. This makes it an easy way for the administrator  to do task as  another user, see what another user is able to do and see. CakePHP makes it so easy for developers to implement this feature.

The steps are very simple:
  1. Store your admin details in another session variable
  2. Login user you want to be logged in as
Here is a sample code for implementing this:

    public function admin_login_user($id) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid User'));

        $this->Session->write('Auth.Admin', $this->Session->read('Auth.User'));
        $user = $this->User->findById($id);
        $this->autoRender = false;
Logging out as the user is just as simple:
  1. Check if your session variable indeed has an admin session to log into after you logout as the user. Just a security check.
  2. Login as the Admin
  3. Delete the store where you saved the admin session

    public function logout_user() {
        if ($this->Session->read('Auth.Admin')) {
        $this->autoRender = false;
Frankly, I am loving how CakePHP does this.

 Back to PHP

My current project with Sourcepad is converting a web application with with Drupal to CakePHP. We opted to develop the application with Ruby on Rails but the client wanted it developed in PHP. I would have loved to develop something again in Rails but clients can be persuasive (really persuasive).

With PHP, we had a few choices. We could develop the application using CakePHP, CodeIgniter, Yii or Kohana, frameworks I have had much experience in.

We ended up using CakePHP by writing up pros and cons for CakePHP and CodeIgniter. Yii and Kohana would have been very fine options but my project partner has little experience with them. 

Here's a list of what we had:

  • ( - ) Slow
  • ( + ) Built in themes
  • ( + ) Rails-like relationships
  • ( + ) Better Integration with Testing Suite(PHPUnit)
  • ( +/- ) Convention over Configuration
  • ( + ) Harder to mess up code because of stricter conventions
  • ( + ) Solid performance from the start
  • ( - ) No built in themes
  • ( - ) No ORM
  • ( - ) Built in Unit Test Class is weak
  • ( - ) Can potentially become hard to manage
  • ( +/- ) Configuration over Convention (somehow) 
People can of course argue that there are extensions for the CodeIgniter framework and CakePHP would be just as fast with proper caching.

The CakePHP and CodeIgniter frameworks are great. I've used both extensively (especially CodeIgniter). The thing is, in the end, we didn't choose because of the pros and cons(although CakePHP won). It will be team experience that will affect the framework decision. Only I had experience developing using the CodeIgniter framework, so we chose CakePHP.

Haha. Problem solved!

 Handy Shortcuts Using RubyMine

I have been an avid fan of RubyMine ever since I discovered it. I tried using other tools like Sublime Text 2, Textwrangler and others. The only other lasting editor I used was Vim on my Ubuntu Desktop and MacVim on my Mac.

There are tons of things I miss from Rubymine whenever I develop using other editors. Here are a few:
  1. Image Preview - Do you want to make sure you have the correct image to use in your CSS? Hover over the declaration and press Shift.
  2. Insane Code Inspection - Rubymine helps you be aware of deprecated methods, unneeded methods and overridden methods. 
  3. Checking of Source - A great feature I like about it is Command-clicking. Whenever I am not sure what a method does, I Command-click the method and I am instantly forwarded to the method declaration. This is quite handy if you want to check gem sources.
Here are a few shortcuts I have been using with Rubymine in the span of a year I have been using it. I really hope this helps Rubymine users like myself to ease up development and trim development time.

Joining lines (Shift + Ctrl + J)

I never had the idea that there was such a function in editors. I discovered this when I thought of a way to join lines instead of doing a backspace on every line in a 100+ line file.

Toggle Comments (Command + /)

This will be very familiar since this is used  by Textmate and Sublime Text. It will comment depending on which type of file you have open.

Show / Hide Project Folder (Command + 1)

I work mostly on a small 13" laptop and I need all the space I can get. The project pane can easily be hidden so that I can use up the the full length of my small screen.

 Rewriting FoodScaper

FoodScaper is Social Networking website dedicated for restaurant and food enthusiasts.

After a couple of months of developing our website, we launched FoodScaper. FoodScaper launched due to our need to discover restaurants and menus in greater Manila.

I have been a Ruby on Rails developer for more than 2 years now. Most projects I have worked with was coded in Ruby on Rails 2.3 to  3.x. I code PHP on the side and sometimes if there is a need for a PHP developer, I was the one called upon to handle the project.

I have chosen to rewrite most of FoodScaper from Ruby on Rails to PHP.

PHP is a good language. I would be first to admit that are there a lot of downs in the PHP language. The thing is that it does the job fine. This can be said for most languages.

While I coding FoodScaper, I have missed a lot of testing tools from the Ruby community:
  • RSpec
  • Cucumber
  • Capybara
To my knowledge, PHPUnit is the best testing tool in the PHP community and I feel there is an immediate need to have more just unit testing here.

However, the cost of deploying a Ruby on Rails website is really what got me rewriting FoodScaper to PHP. As of now, an instance from Webbynode or Linode with an allotted 500MB memory costs around 20USD per month. My host right now could handle FoodScaper and a few more websites without running out of memory.

In the end it was cost. I know about the phrase, "Developers are more expensive than hardware". The thing is, right now, I am both the developer and the one paying. 

I believe FoodScaper is going to be a great website and having a good language (not the best) by my side is the best option for me. I’m going to try really hard to make it successful, and I felt in order to do that I need to at least sustain it and pipeline costs to marketing.