mardi 20 octobre 2015

Not undestanding how to make a custom auth driver. Ultimately will need SOAP.

I am quite new to Laravel and I really need some help. I need to create a simple app for my job, and I think I will not have problem with this as the tutorials here are really excellent.

The issue I have is that for that project I need to authenticate the users against an external DB using a SOAP webservice and if the user does not exist in the local DB I create it and log the user in. I am able to manage this part as I have already written a Joomla plugin that does that.

I have tried to figure out the documentation on how to create a custom driver. http://ift.tt/1FgJQ7H I thought that at first I would replicate the EloquentUserProvider befor modifying it, thus I created:

ErsAuthServiceProvider and ErsUserProvider respectively placed in App\Providers and App\Extensions

But it mysteriously does not work... I get the following error:

ErrorException in ErsUserProvider.php line 33: Argument 1 passed to App\Extensions\ErsUserProvider::__construct() must be an instance of Illuminate\Contracts\Hashing\Hasher, none given, called in /home/vagrant/Code/ERSTools/app/Providers/ErsAuthServiceProvider.php on line 31 and defined

Actually I do not understand much in the documentation what they are doing with the boot() method in the example. I understand that they extend The Auth class in order to add the new driver (ers in my case) but I do not get why they pass the $app['riak.connection']

<?php

namespace App\Providers;

use Auth;
use App\Extensions\ErsUserProvider;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

    class ErsAuthServiceProvider extends ServiceProvider

    {

        protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];


    /**
     * Register any application authentication / authorization services.
     *
     * @param  \Illuminate\Contracts\Auth\Access\Gate  $gate
     * @return void
     */
    public function boot(GateContract $gate)
    {
        parent::registerPolicies($gate);

         Auth::extend('ers', function($app) {
            // Return an instance of Illuminate\Contracts\Auth\UserProvider...
            return new ErsUserProvider;
        });

        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

and

<?php

namespace App\Extensions;

use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class ErsUserProvider implements UserProvider
{
   /**
     * The hasher implementation.
     *
     * @var \Illuminate\Contracts\Hashing\Hasher
     */
    protected $hasher;

    /**
     * The Eloquent user model.
     *
     * @var string
     */
    protected $model;

    /**
     * Create a new database user provider.
     *
     * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
     * @param  string  $model
     * @return void
     */
    public function __construct(HasherContract $hasher, $model)
    {
        $this->model = $model;
        $this->hasher = $hasher;
    }
... the rest is similar to the original file (EloquentUserProvider)

Finally, my plan is to keep the ErsUserprovider quite similar to the EloquentUserProvider and to implement my check with the webservice in the validateCredentials() method as in this method I shoul know if a user exists with the requested username in the local DB, I will know if the user passes validation with the SOAP webservice I can then

  1. Login the user
  2. Login the user and create a new user based on the date returned by the webservice
  3. refuse the login.

Is this a good plan?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire