title: LDAP Authentication with Cakephp 3.x
body: <p>I'm new to Cakephp and working on implementation of LDAP-authentication in my application now. Many things in the "bookmarker" tutorial on the official website works automatically, so it didn't give me enough information about how to implement some specific authentication. I've already checked this post <a href="https://stackoverflow.com/questions/32597542/cakephp-3-ldap-authentication-issue-and-clarification">CakePHP 3 Ldap authentication issue and clarification</a> and tried to implement my authentication this way, but still have some understanding problems.</p>
  
  <p>In my database I have a table "Students" which has an attribute "id" as a primary key. 
  My AppController looks as follows:</p>
  
  <pre><code> public function initialize()
  {
      parent::initialize();
  
      $this-&gt;loadComponent('Flash');
      $this-&gt;loadComponent('Auth', ['authenticate' =&gt;
          ['Form' =&gt;
              ['fields' =&gt;
                  ['username' =&gt; 'email',
                      'password' =&gt; 'password']
              ]
          ], 'loginAction' =&gt; [
          'controller' =&gt; 'Students',
          'action' =&gt; 'login'
      ]
      ]);
  
      $this-&gt;Auth-&gt;config('authenticate', ['Ldap']);
  }
  
  public function isAuthorized()
  {
      if($this-&gt;Auth-&gt;user('departmentnumber') == "***") { return true; }
  }
  </code></pre>
  
  <p>The LdapAuthenticate class is like in the post I mentioned above:</p>
  
  <pre><code>namespace App\Auth;
  
  use Cake\Auth\BaseAuthenticate;
  use Cake\Network\Request;
  use Cake\Network\Response;
  
  class LdapAuthenticate extends BaseAuthenticate {
  
  protected $_host = '***' ;
  
  public function authenticate(Request $request, Response $response) {
      $username = $request-&gt;data['username'] ;
      $password = $request-&gt;data['password'] ;
      $ds = @ldap_connect($this-&gt;_host) ;
      if (!$ds) {
          throw \Cake\Error\FatalErrorException ('Unable to connect to LDAP host.') ;
      }
      $basedn = "cn=Users,dc=***";
      $dn = "cn=$username, " . $basedn;
      $ldapbind = @ldap_bind($ds, $dn, $password);
      if (!$ldapbind) {
          return false ;
      }
  
      $entry = ldap_first_entry ($ldapbind) ;
      $attrs = ldap_get_attributes ($ldapbind, $entry) ;
      $user  = [] ;
      // Loop
      for ($i = 0 ; $i &lt; $attrs["count"] ; $i++) {
          $user[$attrs[$i]] = ldap_values ($ldapbind, $entry, $attrs[$i])[0] ;
      }
      // Then close it and return the authenticated user
      ldap_unbind ($ldapbind) ;
      return $user ;
  }
  }
  </code></pre>
  
  <p>In the StudentsController I've implemented functions login and logout like in the "bookmarker" tutorial:</p>
  
  <pre><code>public function login(){
      if ($this-&gt;request-&gt;is('post')){
          $user = $this-&gt;Auth-&gt;identify();
          if ($user) {
              $this-&gt;Auth-&gt;setUser($user);
              return $this-&gt;redirect($this-&gt;Auth-&gt;redirectUrl());
          }
  
          // user is not identified
          $this-&gt;Flash-&gt;error('Your username or password is not correct');
      }
  }
  
  public function logout(){
      $this-&gt;Flash-&gt;success('You are now logged out');
      return $this-&gt;redirect($this-&gt;Auth-&gt;logout());
  }
  </code></pre>
  
  <p>When I open any page, I'm successfully landing in my login.ctp page. After I enter my credentials and click "login", I'm getting an error "SQLSTATE[42S02]: Base table or view not found: 1146 Table '***_db.users' doesn't exist". 
  So I think I made something wrong, but don't have enough understanding to find where - have no idea why it tries to find a "users" table in my database which doesn't exist. </p>
  
  <p>Thank to everyone who helps me with ideas in advance!</p>