Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
Extend the auth driver using suggested method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhariani committed Jul 17, 2014
1 parent 536339c commit 391c2e6
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 99 deletions.
29 changes: 0 additions & 29 deletions src/GovTribe/LaravelKinvey/Auth/AuthManager.php

This file was deleted.

57 changes: 0 additions & 57 deletions src/GovTribe/LaravelKinvey/Auth/EloquentUserProvider.php

This file was deleted.

120 changes: 120 additions & 0 deletions src/GovTribe/LaravelKinvey/Auth/KinveyUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php namespace GovTribe\LaravelKinvey\Auth;

use Illuminate\Auth\UserProviderInterface;
use Illuminate\Auth\UserInterface;
use GovTribe\LaravelKinvey\Client\Exception\KinveyResponseException;
use GovTribe\LaravelKinvey\Client\KinveyClient;
use GovTribe\LaravelKinvey\Database\Connection;

class KinveyUserProvider implements UserProviderInterface {

protected $connection;
protected $kinvey;
protected $modelClassName;


public function __construct(Connection $connection, KinveyClient $kinvey, $modelClassName)
{
$this->kinvey = $kinvey;
$this->connection = $connection;
$this->modelClassName = $modelClassName;
}

/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveById($identifier)
{
$this->kinvey->setAuthMode('admin');

$result = $this->connection->collection('user')->where('_id', $identifier)->first();

$this->kinvey->setAuthMode('app');

$user = new $this->modelClassName;

$user->setRawAttributes($result);

return $user;
}

/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveByCredentials(array $credentials)
{
$this->kinvey->setAuthMode('admin');

$result = $this->connection->collection('user')->where('username', reset($credentials))->first();

$this->kinvey->setAuthMode('app');

if ($result)
{
$user = new $this->modelClassName;
$user->setRawAttributes($result);

return $user;
}
else return null;
}

/**
* Retrieve a user by by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveByToken($identifier, $token)
{
//
}

/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Auth\UserInterface $user
* @param string $token
* @return void
*/
public function updateRememberToken(UserInterface $user, $token)
{
//
}

/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Auth\UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
{
try
{
$result = $this->kinvey->login($credentials);
$user->_kmd = $result['_kmd'];
}
catch (KinveyResponseException $e)
{
if ($e->getResponse()->getStatusCode() === 401)
{
return false;
}
else
{
throw $e;
}
}

return true;
}
}
41 changes: 28 additions & 13 deletions src/GovTribe/LaravelKinvey/LaravelKinveyAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Session;
use Illuminate\Auth\AuthServiceProvider;
use GovTribe\LaravelKinvey\Auth\AuthManager;
use GovTribe\LaravelKinvey\Database\Eloquent\User;

class LaravelKinveyAuthServiceProvider extends AuthServiceProvider {
use GovTribe\LaravelKinvey\Auth\KinveyUserProvider;

class LaravelKinveyAuthServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
Expand All @@ -18,14 +24,24 @@ public function boot()
{
$app = $this->app;

$this->app->bind('auth', function($app)
$this->app['auth']->extend('eloquent', function($app)
{
return new AuthManager($app);
return new KinveyUserProvider(
$app['db']->connection('kinvey'),
$app['kinvey'],
$app['config']['auth']['model']
);
});
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerEvents();

parent::boot();
}

/**
Expand All @@ -36,15 +52,14 @@ public function boot()
public function registerEvents()
{
//Store the Kinvey auth token in the user's session, and clear it on logout.
Event::listen('auth.login', function($user)
$this->app->events->listen('auth.login', function($user)
{
Session::put('kinvey', $user->_kmd['authtoken']);
$this->app->session->put('kinvey', $user->_kmd['authtoken']);
});

Event::listen('auth.logout', function($user)
$this->app->events->listen('auth.logout', function($user)
{
Session::forget('kinvey');
$this->app->session->forget('kinvey');
});
}

}

0 comments on commit 391c2e6

Please sign in to comment.