0

I have a login controller that has a function that connects to LDAP using my domain credentials.

class Login extends MX_Controller {

const USER = "DOMAINACCOUNT";
const PASS = "DoM@inP@ssw0rd";

public function checkWW930() {
    $ldapserver = "ldap://ww930.sampledomain.net";
    $dn = 'dc=ww930,dc=sampledomain,dc=net';
    $ldapconn = ldap_connect($ldapserver) or die("Could not connect to $ldaphost");
    //ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
    //ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
    $user = self::USER;
    $pass = self::PASS;
    $bind = @ldap_bind($ldapconn, 'ww930\\'.$user, $pass);
    $filter = "(samaccountname=". $this->input->post('username') .")";
    $result = ldap_search($ldapconn, $dn, $filter);
    $info = ldap_get_entries($ldapconn, $result);
    if($info["count"] > 0) {
        return TRUE;    // account exists in ww930 domain
    } else {
        return FALSE;   // account does not exist in ww930 domain
    }

This works fine but I would like to save my credentials in a separate file so I can use it in other controllers if needed. Also I want to save it in a single file so that if my password expires, I only have to update one file. I am thinking to put my credentials in credentials.php file then add include('credentials.php'); Can someone help me how to implement it? Many thanks.

2
  • in CI structure, there is constants.php, in that you can define all the constants. Commented Mar 2, 2016 at 10:53
  • can you show me how? and how to access the variables from the controller? Commented Mar 2, 2016 at 10:59

2 Answers 2

2

You can set your constants in /config/constants.php

by adding this two lines at the end of the file

defined('USER')      OR define('USER', "DOMAINACCOUNT");
defined('PASS')      OR define('PASS', "DoM@inP@ssw0rd");

Now you can use your constants where ever you want in your project.

Sign up to request clarification or add additional context in comments.

Comments

2

you can try something like this in order to abstract and organize your structure

create in your application/config/ folder a new file named ldap.php

and put the following code in it

$config["ldap_default"] = array(
    "server"  =>  "ldap://ww930.sampledomain.net",
    "dn"  =>  "dc=ww930,dc=sampledomain,dc=net",
    "user" => "DOMAINACCOUNT",
    "password"  => "DoM@inP@ssw0rd",
);

create a library called CustomLDAPConnection.php (put it in your application/libraries folder)

class CustomLDAPConnection
{
    private $ci;
    private $arrConfig = array();

    public function __construct()
    {
        $this->ci = &get_instance();
        $this->ci->load->config("ldap");
    }

    public function checkWW930($userName = false, $configGroup = "default") 
    {
        if (!$userName) return false;

        $this->arrConfig = $this->ci->config->item("ldap_".$configGroup);

        $ldapconn = ldap_connect($this->arrConfig['server']) or die("Could not connect to $ldaphost");
        $bind = @ldap_bind($ldapconn, 'ww930\\'.$this->arrConfig['user'], $arrConfig['password']);
        $filter = "(samaccountname=". $userName .")";

        //ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
        //ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
        $result = ldap_search($ldapconn, $this->arrConfig['dn'], $filter);
        $info = ldap_get_entries($ldapconn, $result);
        return ($info["count"] > 0) ?   true    :   false;
    }
}

and then in your Login Controller

class Login extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library("CustomLDAPConnection");
    }

    public function checkLDAPForUser()
    {
        return $this->CustomLDAPConnection->checkWW930($this->input->post("username"));
    }
}

its very basic and simple but it should give you the hint you need

3 Comments

it can be worked, but no need to do this long process. just define constants in constants.php file
since user credentials or any other authentication data are pretty much always dynamic i think its really bad practice to define constants here
then what you did?? created a library and all!!!!!!! but the simple solution is just define constant and give values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.