AdminSettingController.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class AdminSettingController extends BaseController {
  5. //保存配置
  6. public function saveConfig(){
  7. $login_user = $this->checkLogin();
  8. $this->checkAdmin();
  9. $register_open = intval(I("register_open")) ;
  10. $ldap_open = intval(I("ldap_open")) ;
  11. $ldap_form = I("ldap_form") ;
  12. D("Options")->set("register_open" ,$register_open) ;
  13. if ($ldap_open) {
  14. if( !extension_loaded( 'ldap' ) ) {
  15. $this->sendError(10011,"你尚未安装php-ldap扩展。如果是普通PHP环境,请手动安装之。如果是使用之前官方docker镜像,则需要重新安装镜像。方法是:备份 /showdoc_data 整个目录,然后全新安装showdoc,接着用备份覆盖/showdoc_data 。然后递归赋予777可写权限。");
  16. return ;
  17. }
  18. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  19. if (!$ldap_conn) {
  20. $this->sendError(10011,"Can't connect to LDAP server");
  21. return ;
  22. }
  23. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  24. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  25. if (!$rs) {
  26. $this->sendError(10011,"Can't bind to LDAP server");
  27. return ;
  28. }
  29. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  30. $data = ldap_get_entries($ldap_conn, $result);
  31. for ($i=0; $i<$data["count"]; $i++) {
  32. $ldap_user = $data[$i]["cn"][0] ;
  33. //如果该用户不在数据库里,则帮助其注册
  34. if(!D("User")->isExist($ldap_user)){
  35. D("User")->register($ldap_user,$ldap_user.time());
  36. }
  37. }
  38. D("Options")->set("ldap_form" , json_encode( $ldap_form)) ;
  39. }
  40. D("Options")->set("ldap_open" ,$ldap_open) ;
  41. $this->sendResult(array());
  42. }
  43. //加载配置
  44. public function loadConfig(){
  45. $login_user = $this->checkLogin();
  46. $this->checkAdmin();
  47. $ldap_open = D("Options")->get("ldap_open" ) ;
  48. $register_open = D("Options")->get("register_open" ) ;
  49. $ldap_form = D("Options")->get("ldap_form" ) ;
  50. $ldap_form = json_decode($ldap_form,1);
  51. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  52. if ($register_open === false) {
  53. $this->sendResult(array());
  54. }else{
  55. $array = array(
  56. "ldap_open"=>$ldap_open ,
  57. "register_open"=>$register_open ,
  58. "ldap_form"=>$ldap_form ,
  59. );
  60. $this->sendResult($array);
  61. }
  62. }
  63. public function checkLdapLogin(){
  64. $username = 'admin';
  65. $password = '123456';
  66. $ldap_open = D("Options")->get("ldap_open" ) ;
  67. $ldap_form = D("Options")->get("ldap_form" ) ;
  68. $ldap_form = json_decode($ldap_form,1);
  69. if (!$ldap_open) {
  70. return ;
  71. }
  72. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  73. if (!$ldap_conn) {
  74. $this->sendError(10011,"Can't connect to LDAP server");
  75. return ;
  76. }
  77. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  78. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  79. if (!$rs) {
  80. $this->sendError(10011,"Can't bind to LDAP server");
  81. return ;
  82. }
  83. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  84. $data = ldap_get_entries($ldap_conn, $result);
  85. for ($i=0; $i<$data["count"]; $i++) {
  86. $ldap_user = $data[$i]["cn"][0] ;
  87. $dn = $data[$i]["dn"] ;
  88. if ($ldap_user == $username) {
  89. //如果该用户不在数据库里,则帮助其注册
  90. $userInfo = D("User")->isExist($username) ;
  91. if(!$userInfo){
  92. D("User")->register($ldap_user,$ldap_user.time());
  93. }
  94. $rs2=ldap_bind($ldap_conn, $dn , $password);
  95. if ($rs2) {
  96. D("User")->updatePwd($userInfo['uid'], $password);
  97. $this->sendResult(array());
  98. return ;
  99. }
  100. }
  101. }
  102. $this->sendError(10011,"用户名或者密码错误");
  103. }
  104. }