AdminSettingController.class.php 5.1 KB

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