UserController.class.php 10 KB

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