Adldap2 / Adldap2-Laravel

LDAP Authentication & Management for Laravel
MIT License
911 stars 184 forks source link

Duplicate entry upon login #48

Closed isbkch closed 8 years ago

isbkch commented 8 years ago

The first time I logged in with no issues. dd(Auth::guard('gardName')->user()); is giving all my user session information So I logged out doing Auth::guard('admin')->logout(); now dd(Auth::guard('admin')->user()); is returning null. Which means that user is no longed in.

Now I tried to login again. But I have an SQL error everytime:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'FirstName lastName' for key 'username' (SQL: insert into 'employee' ('username', 'password') values (FirstName lastName, gjkdfgdfgdfs5g4dfg4dfg4dfs6g4d6f5gdf6g4f))

Why am I getting this ? Shouldn't the record be updated if it already exists ?

stevebauman commented 8 years ago

I need more information. How are your users being created? Are you using the default app/Http/Controllers/Auth/AuthController.php?

Can you also post your code for your login method?

isbkch commented 8 years ago

Users are being added to database manually for now..

My login is heavily inspired from the laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

It's written in app/Http/Controllers/Auth/AuthController.php

/**
     * Handle a login request for admin (Ldap)
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function adminPostLogin(HttpRequest $request)
    {
        // TODO: validate the credentials

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);

        if (Auth::guard('admin')->attempt($credentials)) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        if ($throttles && ! $lockedOut) {
            $this->incrementLoginAttempts($request);
        }

        return $this->sendFailedLoginResponse($request);
    }
stevebauman commented 8 years ago

Hmm it's quite possible that it might be a case sensitivity issue (#L113)? Can you try using an account with the same casing for its username and see if you still experience the issue? Also what type of database are you using?

isbkch commented 8 years ago

Hum, here is what I just discovered.

For user "John Doe", the 'username' field in DB contains: "j_doe". But After login, the username is converted into "John Doe". It actually rewrites the row in DB. So the next time I try to login, it tries to insert the full name again "John Doe" but it's already there..

Shouldn't use a temporary table for this instead of writing into the "members" table ?

stevebauman commented 8 years ago

The members table? Are you using multiple authentication database tables?

isbkch commented 8 years ago

Yes. But here is what I did to fix it: in config/adldap_auth.php another developer had added previously

'sync_attributes' => [
        'username' => 'cn',
],

So I removed 'username' so nothing is updated... My bad :) Thank you for your time, you can close this

stevebauman commented 8 years ago

Ah okay so just a configuration issue, no problem :). Good to know, thanks!