Web Development
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.