UserController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class UserController extends BaseController {
  5. //注册
  6. public function register(){
  7. $username = trim(I("username"));
  8. $password = I("password");
  9. $confirm_password = I("confirm_password");
  10. $v_code = I("v_code");
  11. $register_open = D("Options")->get("register_open" ) ;
  12. if ($register_open === '0') {
  13. $this->sendError(10101,"管理员已关闭注册");
  14. return ;
  15. }
  16. if (C('CloseVerify') || $v_code && $v_code == session('v_code') ) {
  17. session('v_code',null) ;
  18. if ( $password != '' && $password == $confirm_password) {
  19. if(!D("User")->checkDbOk()){
  20. $this->sendError(100100,"数据库连接不上。请确保安装了php-sqlite扩展以及数据库文件Sqlite/showdoc.db.php可用");
  21. return;
  22. }
  23. if ( ! D("User")->isExist($username) ) {
  24. $new_uid = D("User")->register($username,$password);
  25. if ($new_uid) {
  26. $create_sample = D("Options")->get("create_sample") ;
  27. //获取后台的语言设置
  28. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  29. $config = file_get_contents("./Application/Home/Conf/config.php");
  30. if ($create_sample !== '0' && strstr($config, "'zh-cn',") ) {
  31. //导入示例项目
  32. $this->_importSample($new_uid);
  33. }
  34. //设置自动登录
  35. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  36. unset($ret['password']);
  37. session("login_user" , $ret );
  38. $token = D("UserToken")->createToken($ret['uid']);
  39. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  40. $this->sendResult(array());
  41. }else{
  42. $this->sendError(10101,'register fail');
  43. }
  44. }else{
  45. $this->sendError(10101,L('username_exists'));
  46. }
  47. }else{
  48. $this->sendError(10101,L('code_much_the_same'));
  49. }
  50. }else{
  51. $this->sendError(10206,L('verification_code_are_incorrect'));
  52. }
  53. }
  54. //导入示例项目
  55. private function _importSample($uid){
  56. $this->_importZip("../Public/SampleZip/apidoc.zip" , $uid);
  57. $this->_importZip("../Public/SampleZip/databasedoc.zip" , $uid);
  58. $this->_importZip("../Public/SampleZip/teamdoc.zip" , $uid);
  59. }
  60. private function _importZip($file , $uid){
  61. $zipArc = new \ZipArchive();
  62. $ret = $zipArc->open($file, \ZipArchive::CREATE);
  63. $info = $zipArc->getFromName("prefix_info.json") ;
  64. if ($info) {
  65. $info_array = json_decode($info ,1 );
  66. if ($info_array) {
  67. D("Item")->import( json_encode($info_array) , $uid );
  68. return true;
  69. }
  70. }
  71. return false ;
  72. }
  73. //登录
  74. public function login(){
  75. $username = trim(I("username"));
  76. $password = I("password");
  77. $v_code = I("v_code");
  78. if (!$password) {
  79. $this->sendError(10206,"no empty password");
  80. return;
  81. }
  82. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  83. $key= 'login_fail_times_'.$username;
  84. if(!D("VerifyCode")->_check_times($key)){
  85. if (!$v_code || $v_code != session('v_code')) {
  86. $this->sendError(10206,L('verification_code_are_incorrect'));
  87. return;
  88. }
  89. }
  90. session('v_code',null) ;
  91. if(!D("User")->checkDbOk()){
  92. $this->sendError(100100,"数据库连接不上。请确保安装了php-sqlite扩展以及数据库文件Sqlite/showdoc.db.php可用");
  93. return;
  94. }
  95. $ret = D("User")->checkLogin($username,$password);
  96. //如果失败则尝试ldap登录
  97. if (!$ret) {
  98. $ret = D("User")->checkLdapLogin($username,$password);
  99. }
  100. if ($ret) {
  101. //获取后台的语言设置
  102. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  103. $config = file_get_contents("./Application/Home/Conf/config.php");
  104. if (D("Item")->count() < 1 && strstr($config, "'zh-cn',") ) {
  105. //如果项目表是空的,则生成系统示例项目
  106. $this->_importSample(1);
  107. }
  108. unset($ret['password']);
  109. session("login_user" , $ret );
  110. D("User")->setLastTime($ret['uid']);
  111. $token = D("UserToken")->createToken($ret['uid'],60*60*24*180);
  112. cookie('cookie_token',$token,array('expire'=>60*60*24*180,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  113. $this->sendResult(array());
  114. }else{
  115. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  116. if(D("VerifyCode")->_check_times($key)){
  117. $error_code = 10204 ;
  118. }else{
  119. $error_code = 10210 ;
  120. }
  121. $this->sendError($error_code,L('username_or_password_incorrect'));
  122. return;
  123. }
  124. }
  125. //登录2
  126. public function loginByVerify(){
  127. $username = I("username");
  128. $password = I("password");
  129. $captcha_id = I("captcha_id");
  130. $captcha = I("captcha");
  131. if ( !D("Captcha")->check($captcha_id , $captcha) ) {
  132. $this->sendError(10206,L('verification_code_are_incorrect'));
  133. return;
  134. }
  135. $ret = D("User")->checkLogin($username,$password);
  136. //如果失败则尝试ldap登录
  137. if (!$ret) {
  138. $ret = D("User")->checkLdapLogin($username,$password);
  139. }
  140. if ($ret) {
  141. //获取后台的语言设置
  142. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  143. $config = file_get_contents("./Application/Home/Conf/config.php");
  144. if (D("Item")->count() < 1 && strstr($config, "'zh-cn',") ) {
  145. //如果项目表是空的,则生成系统示例项目
  146. $this->_importSample(1);
  147. }
  148. unset($ret['password']);
  149. session("login_user" , $ret );
  150. D("User")->setLastTime($ret['uid']);
  151. $token = D("UserToken")->createToken($ret['uid'], 60*60*24*180);
  152. $this->sendResult(array(
  153. "uid" => $ret['uid'] ,
  154. "username" => $ret['username'] ,
  155. "name" => $ret['name'] ,
  156. "groupid" => $ret['groupid'] ,
  157. "avatar" => $ret['avatar'] ,
  158. "avatar_small" => $ret['avatar_small'] ,
  159. "email" => $ret['email'] ,
  160. "email_verify" => $ret['email_verify'] ,
  161. "user_token" => $token ,
  162. ));
  163. }else{
  164. $this->sendError(10204,L('username_or_password_incorrect'));
  165. return;
  166. }
  167. }
  168. //注册2
  169. public function registerByVerify(){
  170. $username = trim(I("username"));
  171. $password = I("password");
  172. $confirm_password = I("confirm_password");
  173. $captcha_id = I("captcha_id");
  174. $captcha = I("captcha");
  175. $register_open = D("Options")->get("register_open" ) ;
  176. if ($register_open === '0') {
  177. $this->sendError(10101,"管理员已关闭注册");
  178. return ;
  179. }
  180. if ( !D("Captcha")->check($captcha_id , $captcha) ) {
  181. $this->sendError(10206,L('verification_code_are_incorrect'));
  182. return;
  183. }
  184. if ( $password != '' && $password == $confirm_password) {
  185. if ( ! D("User")->isExist($username) ) {
  186. $new_uid = D("User")->register($username,$password);
  187. if ($new_uid) {
  188. $create_sample = D("Options")->get("create_sample") ;
  189. //获取后台的语言设置
  190. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  191. $config = file_get_contents("./Application/Home/Conf/config.php");
  192. if ($create_sample !== '0' && strstr($config, "'zh-cn',") ) {
  193. //导入示例项目
  194. $this->_importSample($new_uid);
  195. }
  196. //设置自动登录
  197. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  198. unset($ret['password']);
  199. session("login_user" , $ret );
  200. $token = D("UserToken")->createToken($ret['uid']);
  201. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  202. $this->sendResult(array(
  203. "uid" => $ret['uid'] ,
  204. "username" => $ret['username'] ,
  205. "name" => $ret['name'] ,
  206. "groupid" => $ret['groupid'] ,
  207. "avatar" => $ret['avatar'] ,
  208. "avatar_small" => $ret['avatar_small'] ,
  209. "email" => $ret['email'] ,
  210. "user_token" => $token ,
  211. ));
  212. }else{
  213. $this->sendError(10101,'register fail');
  214. }
  215. }else{
  216. $this->sendError(10101,L('username_exists'));
  217. }
  218. }else{
  219. $this->sendError(10101,L('code_much_the_same'));
  220. }
  221. }
  222. //获取用户信息
  223. public function info(){
  224. $login_user = $this->checkLogin();
  225. $uid = $login_user['uid'] ;
  226. $field = "uid,username,email,name,avatar,avatar_small,groupid" ;
  227. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  228. $this->sendResult($info);
  229. }
  230. //获取所有用户名
  231. public function allUser(){
  232. $login_user = $this->checkLogin();
  233. $uid = $login_user['uid'] ;
  234. $username = I("username");
  235. $field = "username as value" ;
  236. $username = \SQLite3::escapeString($username) ;
  237. if ($username) {
  238. $where = " username like '%{$username}%'" ;
  239. }else{
  240. $where = ' 1 = 1 ';
  241. }
  242. $info = D("User")->where($where)->field($field)->select();
  243. $this->sendResult($info);
  244. }
  245. //通过旧密码验证来更新用户密码
  246. public function resetPassword(){
  247. $login_user = $this->checkLogin();
  248. $username = $login_user['username'];
  249. $password = I("password");
  250. $new_password = I("new_password");
  251. $ret = D("User")->checkLogin($username,$password);
  252. if ($ret) {
  253. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  254. if ($ret) {
  255. $this->sendResult(array());
  256. }else{
  257. $this->sendError(10101,L('modify_faild'));
  258. }
  259. }else{
  260. $this->sendError(10101,L('old_password_incorrect'));
  261. }
  262. }
  263. //退出登录
  264. public function logout(){
  265. $login_user = $this->checkLogin();
  266. D("UserToken")->where(" uid = '$login_user[uid]' ")->save(array("token_expire"=>0));
  267. session("login_user" , NULL);
  268. cookie('cookie_token',NULL);
  269. session(null);
  270. $this->sendResult(array());
  271. }
  272. public function updateInfo(){
  273. $user = $this->checkLogin();
  274. $uid = $user['uid'];
  275. $name = I("name");
  276. D("User")->where(" uid = '$uid' ")->save(array("name"=>$name));
  277. $this->sendResult(array());
  278. }
  279. }