You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.0 KiB

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