Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_once dirname(__FILE__) . "/../www/index.php";
- class LDAPUserTest extends PHPUnit_Framework_TestCase {
- public function testLogin(){
- $username = "ucitel";
- $password = "spravneHeslo2";
- $ldap_user = new LDAPUser($username, $password);
- $this->assertEquals(true, $ldap_user->login());
- }
- }
- use Nette\Environment;
- class LDAPUser {
- private $username;
- private $password;
- private $config;
- private $user_info;
- public function __construct($username, $password) {
- $this->username = $username;
- $this->password = $password;
- $this->config = Environment::getConfig("ldap");
- }
- public function login() {
- $ldapconn = ldap_connect($this->config["server"]);
- ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
- ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
- $bind = @ldap_bind($ldapconn, $this->username . $this->config["suffix"], $this->password);
- if (!$bind) {
- throw new Exception("Incorrect login");
- }
- $sr = ldap_search($ldapconn, $this->config["base_dn"], "(samaccountname=" . $this->username . ")");
- $this->user_info = ldap_get_entries($ldapconn, $sr);
- return true;
- }
- public function getGroups() {
- $groups = array();
- foreach ($this->user_info[0]["memberof"] as $memberof) {
- $tmp = explode(",", $memberof);
- foreach ($tmp as $value) {
- if (strpos($value, "CN=") === false) {
- continue;
- }
- $groups[] = str_ireplace("CN=", "", $value);
- }
- }
- return $groups;
- }
- public function getRoles() {
- $groups = $this->getGroups();
- $roles = array();
- foreach ($groups as $group) {
- if (in_array($group, array($this->config["students_group_name"], $this->config["teachers_group_name"], $this->config["administrators_group_name"]))) {
- $roles[] = $group;
- }
- }
- return $roles;
- }
- public function getId() {
- $sid = "S-";
- $sidinhex = str_split(bin2hex($this->user_info[0]['objectsid'][0]), 2);
- $sid = $sid . hexdec($sidinhex[0]) . "-";
- $sid = $sid . hexdec($sidinhex[6] . $sidinhex[5] . $sidinhex[4] . $sidinhex[3] . $sidinhex[2] . $sidinhex[1]);
- $subauths = hexdec($sidinhex[7]);
- for ($i = 0; $i < $subauths; $i++) {
- $start = 8 + (4 * $i);
- $sid = $sid . "-" . hexdec($sidinhex[$start + 3] . $sidinhex[$start + 2] . $sidinhex[$start + 1] . $sidinhex[$start]);
- }
- return $sid;
- }
- public function getFirstName() {
- return isset($this->user_info[0]["givenname"][0]) ? $this->user_info[0]["givenname"][0] : "";
- }
- public function getLastName() {
- return isset($this->user_info[0]["sn"][0]) ? $this->user_info[0]["sn"][0] : $this->username;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement