UserController.class.php 9.3 KB

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