UserController.class.php 8.8 KB

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