UserController.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. //登录2
  80. public function loginByVerify(){
  81. $username = I("username");
  82. $password = I("password");
  83. $captcha_id = I("captcha_id");
  84. $captcha = I("captcha");
  85. if ( !D("Captcha")->check($captcha_id , $captcha) ) {
  86. $this->sendError(10206,L('verification_code_are_incorrect'));
  87. return;
  88. }
  89. $ret = D("User")->checkLogin($username,$password);
  90. //如果失败则尝试ldap登录
  91. if (!$ret) {
  92. $ret = D("User")->checkLdapLogin($username,$password);
  93. }
  94. if ($ret) {
  95. unset($ret['password']);
  96. session("login_user" , $ret );
  97. D("User")->setLastTime($ret['uid']);
  98. $token = D("UserToken")->createToken($ret['uid']);
  99. $this->sendResult(array(
  100. "uid" => $ret['uid'] ,
  101. "username" => $ret['username'] ,
  102. "name" => $ret['name'] ,
  103. "groupid" => $ret['groupid'] ,
  104. "avatar" => $ret['avatar'] ,
  105. "avatar_small" => $ret['avatar_small'] ,
  106. "email" => $ret['email'] ,
  107. "email_verify" => $ret['email_verify'] ,
  108. "user_token" => $token ,
  109. ));
  110. }else{
  111. $this->sendError(10204,L('username_or_password_incorrect'));
  112. return;
  113. }
  114. }
  115. //获取用户信息
  116. public function info(){
  117. $login_user = $this->checkLogin();
  118. $uid = $login_user['uid'] ;
  119. $field = "uid,username,email,name,avatar,avatar_small,groupid" ;
  120. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  121. $this->sendResult($info);
  122. }
  123. //获取所有用户名
  124. public function allUser(){
  125. $login_user = $this->checkLogin();
  126. $uid = $login_user['uid'] ;
  127. $username = I("username");
  128. $field = "username as value" ;
  129. $username = \SQLite3::escapeString($username) ;
  130. if ($username) {
  131. $where = " username like '%{$username}%'" ;
  132. }else{
  133. $where = ' 1 = 1 ';
  134. }
  135. $info = D("User")->where($where)->field($field)->select();
  136. $this->sendResult($info);
  137. }
  138. //通过旧密码验证来更新用户密码
  139. public function resetPassword(){
  140. $login_user = $this->checkLogin();
  141. $username = $login_user['username'];
  142. $password = I("password");
  143. $new_password = I("new_password");
  144. $ret = D("User")->checkLogin($username,$password);
  145. if ($ret) {
  146. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  147. if ($ret) {
  148. $this->sendResult(array());
  149. }else{
  150. $this->sendError(10101,L('modify_faild'));
  151. }
  152. }else{
  153. $this->sendError(10101,L('old_password_incorrect'));
  154. }
  155. }
  156. //退出登录
  157. public function logout(){
  158. $login_user = $this->checkLogin();
  159. D("UserToken")->where(" uid = '$login_user[uid]' ")->save(array("token_expire"=>0));
  160. session("login_user" , NULL);
  161. cookie('cookie_token',NULL);
  162. session(null);
  163. $this->sendResult(array());
  164. }
  165. public function updateInfo(){
  166. $user = $this->checkLogin();
  167. $uid = $user['uid'];
  168. $name = I("name");
  169. D("User")->where(" uid = '$uid' ")->save(array("name"=>$name));
  170. $this->sendResult(array());
  171. }
  172. }