Saturday, 18 June 2016

Temporary User Switching Using Sentry Authentication Laravel

Today we are going to learn how we can achieve switching the users in Laravel.

I recently learned this If we are using Sentry Authentication for your Laravel project.

First we need to register the routes for the admin to navigate from "admin to registered user" & "registered user to admin"

Route::get('user/switch/start/{id}',array('as' => 'user.startswitch', 'uses'=>'UserController@user_switch_start')); 
Route::get('user/switch/stop',array('as' => 'user.stopswitch', 'uses'=>'UserController@user_switch_stop'));

Declare methods in your controller for registered routes:
 /*
  * Assign ther user id that we wants to switch.
  * It will store current admin user id and it will be use to switch back to admin user
  *
  * @param $new_user
  */
  public function user_switch_start( $new_user )
  {
      $admin_id = Sentry::getUser()->id;
      $new_user = User::find( $new_user );
   
      $this->switch_user($new_user->id);
      Session::put('admin_id', $admin_id);
      return Redirect::route('dashboard');
  }
 /*
  * Switch back to the admin user & forget the previous user.  
  */
  public function user_switch_stop()
  {
      $admin_id = Session::get('admin_id');
      $admin_user = User::find( $admin_id );
   
      $this->switch_user($admin_user->id); // back to admin login
      Session::forget('admin_id'); // destroy session
      return Redirect::route('user.index');
  } 

/*
  * Authenticate the user based on passed user_id to this.
  * Gives an error message if necessary.
  *
  * @param $user_id
  */
  public function switch_user($user_id)
  {
    try
    {
      $user = Sentry::findUserById($user_id); // Find the user using the user id
      Sentry::login($user, false); // Log the user in
    }
    catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
    {
        echo 'Login is required.'; exit;
    }
    catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
    {
        echo 'User not found.'; exit;
    }
    catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
    {
        echo 'User not activated.'; exit;
    }
    // Following is only needed if throttle is enabled
    catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
    {
        $time = $throttle->getSuspensionTime();
        echo "User is suspended for [$time] minutes."; exit;
    }
    catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
    {
        echo 'User is banned.'; exit;
    }
  }
Create a link for start and stop launching users:
<a href="http://localhost/myapp/user/switch/start/1">Start</a>
<a href="http://localhost/myapp/user/switch/stop>Stop</a>

That's it!

Happy Coding.

Enjoy :)

Wednesday, 8 June 2016

Load MongoDB extension for WAMP

Let's learn how to load MongoDB extension for WAMP.

It's not hard as I thought, so I would like to share it with you!

Here is steps to follow:
  1. First decide with PHP version you are using right now.
    • In my case I am using 5.5.xx
  2. Then visit MongoDB documentation.
  3. For instance I am using 5.5.xx so I will go through the mongo-1.6 which is latest and supported to my PHP version.
  4. Next it's time to download mongo package.
  5. In my case I will download 1.6 as it's right for me :)
  6. Once you clicked on DLL link under download section? Next you'll get more package information.
    • You will look into DDL list section and choose your right PHP version and right Thread Safe (TS) version for you.

      Note
      : You'll see, there are two architecture x86 and x64. I know what are you going to ask HOW DO I KNOW WHICH ONE I AM USING RIGHT? and answer is, look at your phpinfo() and you'll it from there.
  7. After that you'll download the mongodb package. In package you'll get php_mongodb.dll file, which you have to place under "ext" directory.
    • C:\wamp\bin\php\php5.5.12\ext
  8. Now update your php.ini file, add following line in your php.ini
    • extension=php_mongodb.dll
  9. That's it! fasten your belt and reset your WAMP.
  10. To check extension is loaded or not check phpinfo() and search mongodb, if you able to find it out, then you get your feet in door :)
Happy Coding!