Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. require_once dirname(__FILE__) . "/../www/index.php";
  2.  
  3. class LDAPUserTest extends PHPUnit_Framework_TestCase {
  4.  
  5.     public function testLogin(){
  6.         $username = "ucitel";
  7.         $password = "spravneHeslo2";
  8.         $ldap_user = new LDAPUser($username, $password);
  9.         $this->assertEquals(true, $ldap_user->login());
  10.     }
  11.    
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. use Nette\Environment;
  20.  
  21. class LDAPUser {
  22.  
  23.     private $username;
  24.     private $password;
  25.     private $config;
  26.     private $user_info;
  27.  
  28.     public function __construct($username, $password) {
  29.         $this->username = $username;
  30.         $this->password = $password;
  31.         $this->config = Environment::getConfig("ldap");
  32.     }
  33.  
  34.     public function login() {
  35.         $ldapconn = ldap_connect($this->config["server"]);
  36.         ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
  37.         ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
  38.         $bind = @ldap_bind($ldapconn, $this->username . $this->config["suffix"], $this->password);
  39.         if (!$bind) {
  40.             throw new Exception("Incorrect login");
  41.         }
  42.         $sr = ldap_search($ldapconn, $this->config["base_dn"], "(samaccountname=" . $this->username . ")");
  43.         $this->user_info = ldap_get_entries($ldapconn, $sr);
  44.         return true;
  45.     }
  46.  
  47.     public function getGroups() {
  48.         $groups = array();
  49.         foreach ($this->user_info[0]["memberof"] as $memberof) {
  50.             $tmp = explode(",", $memberof);
  51.             foreach ($tmp as $value) {
  52.                 if (strpos($value, "CN=") === false) {
  53.                     continue;
  54.                 }
  55.                 $groups[] = str_ireplace("CN=", "", $value);
  56.             }
  57.         }
  58.         return $groups;
  59.     }
  60.  
  61.     public function getRoles() {
  62.         $groups = $this->getGroups();
  63.         $roles = array();
  64.         foreach ($groups as $group) {
  65.             if (in_array($group, array($this->config["students_group_name"], $this->config["teachers_group_name"], $this->config["administrators_group_name"]))) {
  66.                 $roles[] = $group;
  67.             }
  68.         }
  69.         return $roles;
  70.     }
  71.  
  72.     public function getId() {
  73.         $sid = "S-";
  74.         $sidinhex = str_split(bin2hex($this->user_info[0]['objectsid'][0]), 2);
  75.         $sid = $sid . hexdec($sidinhex[0]) . "-";
  76.         $sid = $sid . hexdec($sidinhex[6] . $sidinhex[5] . $sidinhex[4] . $sidinhex[3] . $sidinhex[2] . $sidinhex[1]);
  77.         $subauths = hexdec($sidinhex[7]);
  78.         for ($i = 0; $i < $subauths; $i++) {
  79.             $start = 8 + (4 * $i);
  80.             $sid = $sid . "-" . hexdec($sidinhex[$start + 3] . $sidinhex[$start + 2] . $sidinhex[$start + 1] . $sidinhex[$start]);
  81.         }
  82.         return $sid;
  83.     }
  84.  
  85.     public function getFirstName() {
  86.         return isset($this->user_info[0]["givenname"][0]) ? $this->user_info[0]["givenname"][0] : "";
  87.     }
  88.  
  89.     public function getLastName() {
  90.         return isset($this->user_info[0]["sn"][0]) ? $this->user_info[0]["sn"][0] : $this->username;
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement