UserController.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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")->isExist($username) ) {
  20. $new_uid = D("User")->register($username,$password);
  21. if ($new_uid) {
  22. //设置自动登录
  23. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  24. unset($ret['password']);
  25. session("login_user" , $ret );
  26. $token = D("UserToken")->createToken($ret['uid']);
  27. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  28. $this->sendResult(array());
  29. }else{
  30. $this->sendError(10101,'register fail');
  31. }
  32. }else{
  33. $this->sendError(10101,L('username_exists'));
  34. }
  35. }else{
  36. $this->sendError(10101,L('code_much_the_same'));
  37. }
  38. }else{
  39. $this->sendError(10206,L('verification_code_are_incorrect'));
  40. }
  41. }
  42. //登录
  43. public function login(){
  44. $username = I("username");
  45. $password = I("password");
  46. $v_code = I("v_code");
  47. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  48. $key= 'login_fail_times_'.$username;
  49. if(!D("VerifyCode")->_check_times($key)){
  50. if (!$v_code || $v_code != session('v_code')) {
  51. $this->sendError(10206,L('verification_code_are_incorrect'));
  52. return;
  53. }
  54. }
  55. session('v_code',null) ;
  56. $ret = D("User")->checkLogin($username,$password);
  57. //如果失败则尝试ldap登录
  58. if (!$ret) {
  59. $ret = D("User")->checkLdapLogin($username,$password);
  60. }
  61. if ($ret) {
  62. unset($ret['password']);
  63. session("login_user" , $ret );
  64. D("User")->setLastTime($ret['uid']);
  65. $token = D("UserToken")->createToken($ret['uid']);
  66. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  67. $this->sendResult(array());
  68. }else{
  69. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  70. if(D("VerifyCode")->_check_times($key)){
  71. $error_code = 10204 ;
  72. }else{
  73. $error_code = 10210 ;
  74. }
  75. $this->sendError($error_code,L('username_or_password_incorrect'));
  76. return;
  77. }
  78. }
  79. //获取用户信息
  80. public function info(){
  81. $login_user = $this->checkLogin();
  82. $uid = $login_user['uid'] ;
  83. $field = "uid,username,email,name,avatar,avatar_small,groupid" ;
  84. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  85. $this->sendResult($info);
  86. }
  87. //获取所有用户名
  88. public function allUser(){
  89. $login_user = $this->checkLogin();
  90. $uid = $login_user['uid'] ;
  91. $username = I("username");
  92. $field = "username as value" ;
  93. $username = \SQLite3::escapeString($username) ;
  94. if ($username) {
  95. $where = " username like '%{$username}%'" ;
  96. }else{
  97. $where = ' 1 = 1 ';
  98. }
  99. $info = D("User")->where($where)->field($field)->select();
  100. $this->sendResult($info);
  101. }
  102. //通过旧密码验证来更新用户密码
  103. public function resetPassword(){
  104. $login_user = $this->checkLogin();
  105. $username = $login_user['username'];
  106. $password = I("password");
  107. $new_password = I("new_password");
  108. $ret = D("User")->checkLogin($username,$password);
  109. if ($ret) {
  110. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  111. if ($ret) {
  112. $this->sendResult(array());
  113. }else{
  114. $this->sendError(10101,L('modify_faild'));
  115. }
  116. }else{
  117. $this->sendError(10101,L('old_password_incorrect'));
  118. }
  119. }
  120. //退出登录
  121. public function logout(){
  122. $login_user = $this->checkLogin();
  123. D("UserToken")->where(" uid = '$login_user[uid]' ")->save(array("token_expire"=>0));
  124. session("login_user" , NULL);
  125. cookie('cookie_token',NULL);
  126. session(null);
  127. $this->sendResult(array());
  128. }
  129. public function updateInfo(){
  130. $user = $this->checkLogin();
  131. $uid = $user['uid'];
  132. $name = I("name");
  133. D("User")->where(" uid = '$uid' ")->save(array("name"=>$name));
  134. $this->sendResult(array());
  135. }
  136. }