AdminSettingController.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $oss_open = intval(I("oss_open")) ;
  11. $home_page = intval(I("home_page")) ;
  12. $home_item = intval(I("home_item")) ;
  13. $oss_setting = I("oss_setting") ;
  14. D("Options")->set("register_open" ,$register_open) ;
  15. D("Options")->set("home_page" ,$home_page) ;
  16. D("Options")->set("home_item" ,$home_item) ;
  17. if ($oss_open) {
  18. D("Options")->set("oss_setting" , json_encode( $oss_setting)) ;
  19. }
  20. D("Options")->set("oss_open" ,$oss_open) ;
  21. $this->sendResult(array());
  22. }
  23. //加载配置
  24. public function loadConfig(){
  25. $login_user = $this->checkLogin();
  26. $this->checkAdmin();
  27. $oss_open = D("Options")->get("oss_open" ) ;
  28. $register_open = D("Options")->get("register_open" ) ;
  29. $oss_setting = D("Options")->get("oss_setting" ) ;
  30. $home_page = D("Options")->get("home_page" ) ;
  31. $home_item = D("Options")->get("home_item" ) ;
  32. $ldap_form = json_decode($ldap_form,1);
  33. $oss_setting = json_decode($oss_setting,1);
  34. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  35. if ($register_open === false) {
  36. $this->sendResult(array());
  37. }else{
  38. $array = array(
  39. "oss_open"=>$oss_open ,
  40. "register_open"=>$register_open ,
  41. "home_page"=>$home_page ,
  42. "home_item"=>$home_item ,
  43. "oss_setting"=>$oss_setting ,
  44. );
  45. $this->sendResult($array);
  46. }
  47. }
  48. //保存Ldap配置
  49. public function saveLdapConfig(){
  50. $login_user = $this->checkLogin();
  51. $this->checkAdmin();
  52. $ldap_open = intval(I("ldap_open")) ;
  53. $ldap_form = I("ldap_form") ;
  54. if ($ldap_open) {
  55. if (!$ldap_form['user_field']) {
  56. $ldap_form['user_field'] = 'cn';
  57. }
  58. if( !extension_loaded( 'ldap' ) ) {
  59. $this->sendError(10011,"你尚未安装php-ldap扩展。如果是普通PHP环境,请手动安装之。如果是使用之前官方docker镜像,则需要重新安装镜像。方法是:备份 /showdoc_data 整个目录,然后全新安装showdoc,接着用备份覆盖/showdoc_data 。然后递归赋予777可写权限。");
  60. return ;
  61. }
  62. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  63. if (!$ldap_conn) {
  64. $this->sendError(10011,"Can't connect to LDAP server");
  65. return ;
  66. }
  67. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  68. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  69. if (!$rs) {
  70. $this->sendError(10011,"Can't bind to LDAP server");
  71. return ;
  72. }
  73. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  74. $data = ldap_get_entries($ldap_conn, $result);
  75. for ($i=0; $i<$data["count"]; $i++) {
  76. $ldap_user = $data[$i][$ldap_form['user_field']][0] ;
  77. if (!$ldap_user) {
  78. continue ;
  79. }
  80. //如果该用户不在数据库里,则帮助其注册
  81. if(!D("User")->isExist($ldap_user)){
  82. D("User")->register($ldap_user,$ldap_user.time());
  83. }
  84. }
  85. D("Options")->set("ldap_form" , json_encode( $ldap_form)) ;
  86. }
  87. D("Options")->set("ldap_open" ,$ldap_open) ;
  88. $this->sendResult(array());
  89. }
  90. //加载Ldap配置
  91. public function loadLdapConfig(){
  92. $login_user = $this->checkLogin();
  93. $this->checkAdmin();
  94. $ldap_open = D("Options")->get("ldap_open" ) ;
  95. $ldap_form = D("Options")->get("ldap_form" ) ;
  96. $ldap_form = json_decode($ldap_form,1);
  97. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  98. if ($register_open === false) {
  99. $this->sendResult(array());
  100. }else{
  101. $array = array(
  102. "ldap_open"=>$ldap_open ,
  103. "ldap_form"=>$ldap_form ,
  104. );
  105. $this->sendResult($array);
  106. }
  107. }
  108. //保存Oauth2配置
  109. public function saveOauth2Config(){
  110. $login_user = $this->checkLogin();
  111. $this->checkAdmin();
  112. $oauth2_open = intval(I("oauth2_open")) ;
  113. $oauth2_form = I("oauth2_form") ;
  114. D("Options")->set("oauth2_form" , json_encode( $oauth2_form)) ;
  115. D("Options")->set("oauth2_open" ,$oauth2_open) ;
  116. $this->sendResult(array());
  117. }
  118. //加载Oauth2配置
  119. public function loadOauth2Config(){
  120. $login_user = $this->checkLogin();
  121. $this->checkAdmin();
  122. $oauth2_open = D("Options")->get("oauth2_open" ) ;
  123. $oauth2_form = D("Options")->get("oauth2_form" ) ;
  124. $oauth2_form = json_decode($oauth2_form,1);
  125. //如果强等于false,那就是尚未有数据。关闭注册应该是有数据且数据为字符串0
  126. if ($register_open === false) {
  127. $this->sendResult(array());
  128. }else{
  129. $array = array(
  130. "oauth2_open"=>$oauth2_open ,
  131. "oauth2_form"=>$oauth2_form ,
  132. );
  133. $this->sendResult($array);
  134. }
  135. }
  136. public function getLoginSecretKey(){
  137. $login_user = $this->checkLogin();
  138. $this->checkAdmin();
  139. $login_secret_key = D("Options")->get("login_secret_key") ;
  140. if(!$login_secret_key){
  141. $login_secret_key = md5("rgrsfsrfsrf".time().rand());
  142. D("Options")->set("login_secret_key",$login_secret_key) ;
  143. }
  144. $this->sendResult(array("login_secret_key"=>$login_secret_key));
  145. }
  146. public function resetLoginSecretKey(){
  147. $login_user = $this->checkLogin();
  148. $this->checkAdmin();
  149. $login_secret_key = md5("rgrsfsrfsrf".time().rand());
  150. D("Options")->set("login_secret_key",$login_secret_key) ;
  151. $this->sendResult(array("login_secret_key"=>$login_secret_key));
  152. }
  153. public function checkLdapLogin(){
  154. $username = 'admin';
  155. $password = '123456';
  156. $ldap_open = D("Options")->get("ldap_open" ) ;
  157. $ldap_form = D("Options")->get("ldap_form" ) ;
  158. $ldap_form = json_decode($ldap_form,1);
  159. if (!$ldap_open) {
  160. return ;
  161. }
  162. if (!$ldap_form['user_field']) {
  163. $ldap_form['user_field'] = 'cn';
  164. }
  165. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  166. if (!$ldap_conn) {
  167. $this->sendError(10011,"Can't connect to LDAP server");
  168. return ;
  169. }
  170. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  171. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  172. if (!$rs) {
  173. $this->sendError(10011,"Can't bind to LDAP server");
  174. return ;
  175. }
  176. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  177. $data = ldap_get_entries($ldap_conn, $result);
  178. for ($i=0; $i<$data["count"]; $i++) {
  179. $ldap_user = $data[$i][$ldap_form['user_field']][0] ;
  180. $dn = $data[$i]["dn"] ;
  181. if ($ldap_user == $username) {
  182. //如果该用户不在数据库里,则帮助其注册
  183. $userInfo = D("User")->isExist($username) ;
  184. if(!$userInfo){
  185. D("User")->register($ldap_user,$ldap_user.time());
  186. }
  187. $rs2=ldap_bind($ldap_conn, $dn , $password);
  188. if ($rs2) {
  189. D("User")->updatePwd($userInfo['uid'], $password);
  190. $this->sendResult(array());
  191. return ;
  192. }
  193. }
  194. }
  195. $this->sendError(10011,"用户名或者密码错误");
  196. }
  197. }