将传统的PHP LDAP登录代码转换为Laravel LDAP代码
我是拉威尔甚至框架的新手。。。我在Laravel中尝试了一些基本的身份验证。现在我的问题来了。我有一个传统PHP中的LDAP登录脚本,我想切换到LaravelLDAP登录。有人能帮我处理代码/捆绑包或其他什么吗?我将非常感谢你的帮助将传统的PHP LDAP登录代码转换为Laravel LDAP代码,php,ldap,laravel,Php,Ldap,Laravel,我是拉威尔甚至框架的新手。。。我在Laravel中尝试了一些基本的身份验证。现在我的问题来了。我有一个传统PHP中的LDAP登录脚本,我想切换到LaravelLDAP登录。有人能帮我处理代码/捆绑包或其他什么吗?我将非常感谢你的帮助 function check_login($username,$password){ if($username=="" || $password=="") return 1 ; $server1="ldap.xyz.com"; /
function check_login($username,$password){
if($username=="" || $password=="")
return 1 ;
$server1="ldap.xyz.com"; //server1 ip or dns
$server2="ldap.xyz.com"; //server2 ip or dns
$firstqry=array("alias,sn,givenname,mail,dn");
$secqry=array("givenname,jobtitledescription,sn,alias,mail,l,employeenumber");
$firstfilter="(alias=$username)";
$searchin="o=XYZ,c=AN";
$ldap=ldap_connect($server1,389);
if (!($res = @ldap_bind($ldap))) {
//echo "<b>Cannt Contact Server 1 :" .$server1 .", Now Contacting Server 2 :".$server2. ".....</b><br>";
$laststatus=ldap_errno($ldap);
if($laststatus==81){
$ldap=ldap_connect($server2,389);
if (!($res = @ldap_bind($ldap))) {
//echo "<b>Cannt Contact Server 2 :" .$server2 ."...</B><br>";
$laststatus=ldap_errno($ldap);
if($laststatus==81){
//echo "<b>Please Contact The Network Adminstartion!!</b><br>";
return 2 ; // server not found
}
}else{
$laststatus=ldap_errno($ldap);
}
}
}else{
$laststatus=ldap_errno($ldap);
}
//echo "Connection :" .ldap_error($ldap)."<br>";
if($laststatus==0){
$sr=ldap_search($ldap,$searchin,$firstfilter,$firstqry);
$info = ldap_get_entries($ldap, $sr);
for ($i=0; $i<$info["count"]; $i++) {
$searchdn=$info[$i]["dn"];
$searchalias=$info[$i]["alias"][0];
}
if($searchdn){
$secfilter=$firstfilter;
$sr2=ldap_search($ldap,$searchin,$secfilter);
$info2 = ldap_get_entries($ldap, $sr2);
$i=0;
if(count($info2)){
if (!($res = @ldap_bind($ldap,$searchdn,addslashes($password)))) {
return 1 ;
}else{
ldap_close($ldap);
return 0 ;
}
}
}else{
return 1 ;
}
}
}
功能检查\u登录($username,$password){
如果($username==“”| |$password==“”)
返回1;
$server1=“ldap.xyz.com”;//server1 ip或dns
$server2=“ldap.xyz.com”;//server2 ip或dns
$firstqry=数组(“别名、序列号、给定名称、邮件、dn”);
$secqry=数组(“givenname,job titledescription,sn,别名,mail,l,employeenumber”);
$firstfilter=“(别名=$username)”;
$searchin=“o=XYZ,c=AN”;
$ldap=ldap\u connect($server1389);
如果(!($res=@ldap\u bind($ldap))){
//echo“无法联系服务器1:“.server1.”,现在正在联系服务器2:“.server2.”………
”;
$laststatus=ldap\u errno($ldap);
如果($laststatus==81){
$ldap=ldap\u connect($server2389);
如果(!($res=@ldap\u bind($ldap))){
//echo“无法联系服务器2:”.$server2.“…
”;
$laststatus=ldap\u errno($ldap);
如果($laststatus==81){
//echo“请联系网络管理员!!
”;
返回2;//找不到服务器
}
}否则{
$laststatus=ldap\u errno($ldap);
}
}
}否则{
$laststatus=ldap\u errno($ldap);
}
//echo“连接:”.ldap_错误($ldap)。“
”;
如果($laststatus==0){
$sr=ldap_search($ldap,$searchin,$firstfilter,$firstqry);
$info=ldap\u get\u条目($ldap,$sr);
对于($i=0;$i如果您在L4上,您可以尝试此捆绑包。我通过这样扩展Auth类解决了这个问题
use Illuminate\Hashing\HasherInterface,
Illuminate\Auth\UserInterface,
Illuminate\Auth\UserProviderInterface;
class XyzUserProvider implements UserProviderInterface {
/**
* The hasher implementation.
*
* @var \Illuminate\Hashing\HasherInterface
*/
protected $hasher;
/**
* The Eloquent user model.
*
* @var string
*/
protected $model;
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveById($identifier)
{
$user = new UserModel;
return $user->newQuery()->find($identifier);
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveByCredentials(array $credentials)
{
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$user = new UserModel;
$query = $user->newQuery();
foreach ($credentials as $key => $value)
{
if ( ! str_contains($key, 'password')) $query->where($key, $value);
}
return $query->first();
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Auth\UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
{
$login_attempt = $this->validateLogin($credentials['username'],$credentials['password']);
if($login_attempt == 0)
return true;
else
return false;
}
public function validateLogin($username,$password)
{
if($username=="" || $password=="")
return 1 ;
$server1="ldap.xyz.com"; //server1 ip or dns
$server2="ldap.xyz.com"; //server2 ip or dns
$firstqry=array("alias,sn,givenname,mail,dn");
$secqry=array("givenname,jobtitledescription,sn,alias,mail,l,employeenumber");
$firstfilter="(alias=$username)";
$searchin="o=XYZ,c=AN";
$ldap=ldap_connect($server1,389);
if (!($res = @ldap_bind($ldap))) {
//echo "<b>Cannt Contact Server 1 :" .$server1 .", Now Contacting Server 2 :".$server2. ".....</b><br>";
$laststatus=ldap_errno($ldap);
if($laststatus==81){
$ldap=ldap_connect($server2,389);
if (!($res = @ldap_bind($ldap))) {
//echo "<b>Cannt Contact Server 2 :" .$server2 ."...</B><br>";
$laststatus=ldap_errno($ldap);
if($laststatus==81){
//echo "<b>Please Contact The Network Adminstartion!!</b><br>";
return 2 ; // server not found
}
}else{
$laststatus=ldap_errno($ldap);
}
}
}else{
$laststatus=ldap_errno($ldap);
}
//echo "Connection :" .ldap_error($ldap)."<br>";
if($laststatus==0){
$sr=ldap_search($ldap,$searchin,$firstfilter,$firstqry);
$info = ldap_get_entries($ldap, $sr);
for ($i=0; $i<$info["count"]; $i++) {
$searchdn=$info[$i]["dn"];
$searchalias=$info[$i]["alias"][0];
}
if($searchdn){
$secfilter=$firstfilter;
$sr2=ldap_search($ldap,$searchin,$secfilter);
$info2 = ldap_get_entries($ldap, $sr2);
$i=0;
if(count($info2)){
if (!($res = @ldap_bind($ldap,$searchdn,addslashes($password)))) {
return 1 ;
}else{
ldap_close($ldap);
return 0 ;
}
}
}else{
return 1 ;
}
}
}
}
使用light\Hashing\HasherInterface,
照亮\Auth\UserInterface,
照亮\Auth\UserProviderInterface;
类XyzUserProvider实现UserProviderInterface{
/**
*hasher实现。
*
*@var\light\Hashing\HasherInterface
*/
受保护的$hasher;
/**
*雄辩的用户模型。
*
*@var字符串
*/
受保护的$model;
/**
*通过用户的唯一标识符检索用户。
*
*@param混合$identifier
*@return\light\Auth\UserInterface | null
*/
公共函数retrieveById($identifier)
{
$user=新用户模型;
返回$user->newQuery()->find($identifier);
}
/**
*通过给定的凭据检索用户。
*
*@param数组$credentials
*@return\light\Auth\UserInterface | null
*/
公共函数retrieveByCredentials(数组$credentials)
{
//首先,我们将把每个凭证元素作为where子句添加到查询中。
//然后我们可以执行查询,如果我们找到了一个用户,就可以在
//将由Guard实例使用的雄辩用户“模型”。
$user=新用户模型;
$query=$user->newQuery();
foreach($key=>$value形式的凭证)
{
如果(!str_包含($key,'password'))$query->where($key,$value);
}
返回$query->first();
}
/**
*根据给定的凭据验证用户。
*
*@param\illighte\Auth\UserInterface$user
*@param数组$credentials
*@returnbool
*/
公共函数validateCredentials(用户界面$user,数组$credentials)
{
$login\u trunt=$this->validateLogin($credentials['username'],$credentials['password']);
如果($login\u尝试==0)
返回true;
其他的
返回false;
}
公共函数validateLogin($username,$password)
{
如果($username==“”| |$password==“”)
返回1;
$server1=“ldap.xyz.com”;//server1 ip或dns
$server2=“ldap.xyz.com”;//server2 ip或dns
$firstqry=数组(“别名、序列号、给定名称、邮件、dn”);
$secqry=数组(“givenname,job titledescription,sn,别名,mail,l,employeenumber”);
$firstfilter=“(别名=$username)”;
$searchin=“o=XYZ,c=AN”;
$ldap=ldap\u connect($server1389);
如果(!($res=@ldap\u bind($ldap))){
//echo“无法联系服务器1:“.server1.”,现在正在联系服务器2:“.server2.”………
”;
$laststatus=ldap\u errno($ldap);
如果($laststatus==81){
$ldap=ldap\u connect($server2389);
如果(!($res=@ldap\u bind($ldap))){
//echo“无法联系服务器2:”.$server2.“…
”;
$laststatus=ldap\u errno($ldap);
如果($laststatus==81){
//echo“请联系网络管理员!!
”;
返回2;//找不到服务器
}
}否则{
$laststatus=ldap\u errno($ldap);
}
}
}否则{
$laststatus=ldap\u errno($ldap);
}
//echo“连接:”.ldap_错误($ldap)。“
”;
如果($laststatus==0){
$sr=ldap_search($ldap,$searchin,$firstfilter,$firstqry);
$info=ldap\u get\u条目($ldap,$sr);
对于($i=0;$i)