UserController.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. //注册2
  116. public function registerByVerify(){
  117. $username = trim(I("username"));
  118. $password = I("password");
  119. $confirm_password = I("confirm_password");
  120. $captcha_id = I("captcha_id");
  121. $captcha = I("captcha");
  122. $register_open = D("Options")->get("register_open" ) ;
  123. if ($register_open === '0') {
  124. $this->sendError(10101,"管理员已关闭注册");
  125. return ;
  126. }
  127. if ( !D("Captcha")->check($captcha_id , $captcha) ) {
  128. $this->sendError(10206,L('verification_code_are_incorrect'));
  129. return;
  130. }
  131. if ( $password != '' && $password == $confirm_password) {
  132. if ( ! D("User")->isExist($username) ) {
  133. $new_uid = D("User")->register($username,$password);
  134. if ($new_uid) {
  135. //设置自动登录
  136. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  137. unset($ret['password']);
  138. session("login_user" , $ret );
  139. $token = D("UserToken")->createToken($ret['uid']);
  140. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  141. $this->sendResult(array(
  142. "uid" => $ret['uid'] ,
  143. "username" => $ret['username'] ,
  144. "name" => $ret['name'] ,
  145. "groupid" => $ret['groupid'] ,
  146. "avatar" => $ret['avatar'] ,
  147. "avatar_small" => $ret['avatar_small'] ,
  148. "email" => $ret['email'] ,
  149. "user_token" => $token ,
  150. ));
  151. }else{
  152. $this->sendError(10101,'register fail');
  153. }
  154. }else{
  155. $this->sendError(10101,L('username_exists'));
  156. }
  157. }else{
  158. $this->sendError(10101,L('code_much_the_same'));
  159. }
  160. }
  161. //获取用户信息
  162. public function info(){
  163. $login_user = $this->checkLogin();
  164. $uid = $login_user['uid'] ;
  165. $field = "uid,username,email,name,avatar,avatar_small,groupid" ;
  166. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  167. $this->sendResult($info);
  168. }
  169. //获取所有用户名
  170. public function allUser(){
  171. $login_user = $this->checkLogin();
  172. $uid = $login_user['uid'] ;
  173. $username = I("username");
  174. $field = "username as value" ;
  175. $username = \SQLite3::escapeString($username) ;
  176. if ($username) {
  177. $where = " username like '%{$username}%'" ;
  178. }else{
  179. $where = ' 1 = 1 ';
  180. }
  181. $info = D("User")->where($where)->field($field)->select();
  182. $this->sendResult($info);
  183. }
  184. //通过旧密码验证来更新用户密码
  185. public function resetPassword(){
  186. $login_user = $this->checkLogin();
  187. $username = $login_user['username'];
  188. $password = I("password");
  189. $new_password = I("new_password");
  190. $ret = D("User")->checkLogin($username,$password);
  191. if ($ret) {
  192. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  193. if ($ret) {
  194. $this->sendResult(array());
  195. }else{
  196. $this->sendError(10101,L('modify_faild'));
  197. }
  198. }else{
  199. $this->sendError(10101,L('old_password_incorrect'));
  200. }
  201. }
  202. //退出登录
  203. public function logout(){
  204. $login_user = $this->checkLogin();
  205. D("UserToken")->where(" uid = '$login_user[uid]' ")->save(array("token_expire"=>0));
  206. session("login_user" , NULL);
  207. cookie('cookie_token',NULL);
  208. session(null);
  209. $this->sendResult(array());
  210. }
  211. public function updateInfo(){
  212. $user = $this->checkLogin();
  213. $uid = $user['uid'];
  214. $name = I("name");
  215. D("User")->where(" uid = '$uid' ")->save(array("name"=>$name));
  216. $this->sendResult(array());
  217. }
  218. }