@@ -35,91 +35,91 @@ |
||
| 35 | 35 | |
| 36 | 36 | class LDAPAuthentificator implements IAuthentificator { |
| 37 | 37 | |
| 38 | - public function __construct() { |
|
| 39 | - // |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * check password of user and import user, if neccessary |
|
| 44 | - * |
|
| 45 | - * @param $username string name of user |
|
| 46 | - * @param $password string password of user |
|
| 47 | - * |
|
| 48 | - * @return userID or -1, if credentials are wrong |
|
| 49 | - */ |
|
| 50 | - public function checkPasswordAndImport(string $username, string $password): int { |
|
| 51 | - //https://samjlevy.com/php-ldap-login/ |
|
| 52 | - |
|
| 53 | - //Free test ldap server: https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ |
|
| 54 | - |
|
| 55 | - //https://www.experts-exchange.com/questions/23969673/Using-PHP-with-LDAP-to-connect-to-Active-Directory-on-another-machine.html |
|
| 56 | - |
|
| 57 | - //http://www.devshed.com/c/a/php/using-php-with-ldap-part-1/3/ |
|
| 58 | - |
|
| 59 | - //check, if username contains a komma (because komma is not allowed here) |
|
| 60 | - if (strpos($username, ",") !== FALSE) { |
|
| 61 | - throw new IllegalArgumentException("',' is not allowed in username."); |
|
| 62 | - return -1; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $ldap_client = new LDAPClient(); |
|
| 66 | - |
|
| 67 | - //try to login user on ldap server |
|
| 68 | - $res = $ldap_client->bind($username, $password); |
|
| 69 | - |
|
| 70 | - if (!$res) { |
|
| 71 | - //user doesnt exists or credentials are wrong |
|
| 72 | - return -1; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - //TODO: set user groups |
|
| 76 | - |
|
| 77 | - //get attributes of user |
|
| 78 | - $attributes = $ldap_client->listAllAttributesOfUser($username); |
|
| 79 | - |
|
| 80 | - $mail = ""; |
|
| 81 | - |
|
| 82 | - //get mail of user |
|
| 83 | - if (isset($attributes['mail'])) { |
|
| 84 | - //get first mail |
|
| 85 | - $mail = $attributes['mail'][0]; |
|
| 86 | - } else { |
|
| 87 | - //generate random local mail |
|
| 88 | - $mail = md5(PHPUtils::randomString(10) . time()) . "@local"; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $common_name = ""; |
|
| 92 | - |
|
| 93 | - if (isset($attributes['cn'])) { |
|
| 94 | - $common_name = $attributes['cn'][0]; |
|
| 95 | - } else { |
|
| 96 | - $common_name = $username; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - //get surname |
|
| 100 | - $surname = ""; |
|
| 101 | - |
|
| 102 | - if (isset($attributes['sn'])) { |
|
| 103 | - $surname = $attributes['sn'][0]; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - //unbind |
|
| 107 | - $ldap_client->unbind(); |
|
| 108 | - |
|
| 109 | - //check, if we have to import user |
|
| 110 | - if (!User::existsUsername($username)) { |
|
| 111 | - //generate random password |
|
| 112 | - $password = md5(PHPUtils::randomString(16) . time()); |
|
| 113 | - |
|
| 114 | - //import user and create user in database |
|
| 115 | - $res = User::create($username, $password, $mail, PHPUtils::getClientIP(), 2, "none", 1, "Plugin\\LDAPLogin\\LDAPAuthentificator"); |
|
| 116 | - |
|
| 117 | - return $res['userID']; |
|
| 118 | - } else { |
|
| 119 | - //return userID |
|
| 120 | - return User::getIDByUsernameFromDB($username); |
|
| 121 | - } |
|
| 122 | - } |
|
| 38 | + public function __construct() { |
|
| 39 | + // |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * check password of user and import user, if neccessary |
|
| 44 | + * |
|
| 45 | + * @param $username string name of user |
|
| 46 | + * @param $password string password of user |
|
| 47 | + * |
|
| 48 | + * @return userID or -1, if credentials are wrong |
|
| 49 | + */ |
|
| 50 | + public function checkPasswordAndImport(string $username, string $password): int { |
|
| 51 | + //https://samjlevy.com/php-ldap-login/ |
|
| 52 | + |
|
| 53 | + //Free test ldap server: https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ |
|
| 54 | + |
|
| 55 | + //https://www.experts-exchange.com/questions/23969673/Using-PHP-with-LDAP-to-connect-to-Active-Directory-on-another-machine.html |
|
| 56 | + |
|
| 57 | + //http://www.devshed.com/c/a/php/using-php-with-ldap-part-1/3/ |
|
| 58 | + |
|
| 59 | + //check, if username contains a komma (because komma is not allowed here) |
|
| 60 | + if (strpos($username, ",") !== FALSE) { |
|
| 61 | + throw new IllegalArgumentException("',' is not allowed in username."); |
|
| 62 | + return -1; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $ldap_client = new LDAPClient(); |
|
| 66 | + |
|
| 67 | + //try to login user on ldap server |
|
| 68 | + $res = $ldap_client->bind($username, $password); |
|
| 69 | + |
|
| 70 | + if (!$res) { |
|
| 71 | + //user doesnt exists or credentials are wrong |
|
| 72 | + return -1; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + //TODO: set user groups |
|
| 76 | + |
|
| 77 | + //get attributes of user |
|
| 78 | + $attributes = $ldap_client->listAllAttributesOfUser($username); |
|
| 79 | + |
|
| 80 | + $mail = ""; |
|
| 81 | + |
|
| 82 | + //get mail of user |
|
| 83 | + if (isset($attributes['mail'])) { |
|
| 84 | + //get first mail |
|
| 85 | + $mail = $attributes['mail'][0]; |
|
| 86 | + } else { |
|
| 87 | + //generate random local mail |
|
| 88 | + $mail = md5(PHPUtils::randomString(10) . time()) . "@local"; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $common_name = ""; |
|
| 92 | + |
|
| 93 | + if (isset($attributes['cn'])) { |
|
| 94 | + $common_name = $attributes['cn'][0]; |
|
| 95 | + } else { |
|
| 96 | + $common_name = $username; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + //get surname |
|
| 100 | + $surname = ""; |
|
| 101 | + |
|
| 102 | + if (isset($attributes['sn'])) { |
|
| 103 | + $surname = $attributes['sn'][0]; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + //unbind |
|
| 107 | + $ldap_client->unbind(); |
|
| 108 | + |
|
| 109 | + //check, if we have to import user |
|
| 110 | + if (!User::existsUsername($username)) { |
|
| 111 | + //generate random password |
|
| 112 | + $password = md5(PHPUtils::randomString(16) . time()); |
|
| 113 | + |
|
| 114 | + //import user and create user in database |
|
| 115 | + $res = User::create($username, $password, $mail, PHPUtils::getClientIP(), 2, "none", 1, "Plugin\\LDAPLogin\\LDAPAuthentificator"); |
|
| 116 | + |
|
| 117 | + return $res['userID']; |
|
| 118 | + } else { |
|
| 119 | + //return userID |
|
| 120 | + return User::getIDByUsernameFromDB($username); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | ?> |