ottaviane
324
20
Laravel

quering a ldapserver and synchronize laravel database

Posted 1 month ago by ottaviane

old title: intercepting laravel login error.

Hi to all. I updated the title because the problem eventually widened. if someone has the same problem as me, they can use this code that I developed thanks to the help of this forum, but in particular thanks to Snapey. I summarize the problem-solution:

  • in an already functioning network where authentication takes place on an existing ldap server, I have to insert a new web server (laravel) using the same accounts already present on the said server;
  • I'd like to use login/service already working in Laravel;
  • The best solution is:
    1. Laravel show login view;
    2. User go with his credentials
    3. Laravel query its user table, if is present login is ok
    4. if login is'nt ok, laravel query ldap. If ldap fails laravel show error
    5. if ldap replay that user is valid, Laravel add new user-record in database and attempt login of new user.

All this I made in LoginController :

protected function attemptLogin(Request $request)
    {
      if($this->guard()->attempt($this->credentials($request), $request->filled('remember'))) //if user is valid in Laravel
      {
        return true;
      }
      else
      {
        if(my_ldap_verify($request)) //if user is valid in ldap server
        {
          $mycredentials['email']=$request['email'];
          $mycredentials['password']=Hash::make($request['password']);
          $mycredentials['name']=$request['email'];
          $user = User::updateOrCreate($mycredentials); //add new user with credentials to users table in my database if not exists or update it if password changed
          $this->guard()->attempt($this->credentials($request), $request->filled('remember')); //relogin

          return true;
        }
        else return false;
      }
    }

Thanks all very much.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.