PHP code example of phpcodemaker / ci4-authldap
1. Go to this page and download the library: Download phpcodemaker/ci4-authldap library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpcodemaker / ci4-authldap example snippets
namespace App\Controllers;
use AuthLdap\Libraries\AuthLdap;
use CodeIgniter\View\View;
/**
* Class User
* @package App\Controllers
* @author Karthikeyan C <[email protected]>
*/
class User extends BaseController
{
/**
* @var AuthLdap $authLdap
*/
private $authLdap;
/**
* If Already declared Session in BaseController,
* then comment the below declaration
* @var \CodeIgniter\Session\Session
*/
private $session;
/**
* User constructor.
*/
public function __construct()
{
/**
* If Already declared Session in BaseController,
* then comment the below declaration
*/
$this->session = \Config\Services::session();
}
/**
* @return \CodeIgniter\HTTP\RedirectResponse|View (postlogin redirect | pre-login template)
* @author Karthikeyan C <[email protected]>
*/
public function login()
{
if (null !== $this->request->getPost('username')
&& null !== $this->request->getPost('password'))
{
$this->authLdap = new AuthLdap();
if (is_object($this->authLdap)
&& method_exists($this->authLdap, 'authenticate'))
{
$authenticatedUserData = $this->authLdap->authenticate(
trim($this->request->getPost('username')),
trim($this->request->getPost('password'))
);
if (!empty($authenticatedUserData))
{
$this->session->set($authenticatedUserData);
return redirect()->to('/user/dashboard');
}
else {
// report login failure
}
}
else {
//report about ldap error
}
}
return view('user/login');
}
/**
* @return string
* @author Karthikeyan C <[email protected]>
*/
public function logout()
{
$this->session->destroy();
return view('user/logout');
}
/**
* @author Karthikeyan C <[email protected]>
*/
public function dashboard()
{
// do your own stuff here
}
}