UserController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. $create_sample = D("Options")->get("create_sample") ;
  27. //获取后台的语言设置
  28. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  29. $config = file_get_contents("./Application/Home/Conf/config.php");
  30. if ($create_sample !== '0' && strstr($config, "'zh-cn',") ) {
  31. //导入示例项目
  32. $this->_importSample($new_uid);
  33. }
  34. //设置自动登录
  35. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  36. unset($ret['password']);
  37. session("login_user" , $ret );
  38. $token = D("UserToken")->createToken($ret['uid']);
  39. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  40. $this->sendResult(array());
  41. }else{
  42. $this->sendError(10101,'register fail');
  43. }
  44. }else{
  45. $this->sendError(10101,L('username_exists'));
  46. }
  47. }else{
  48. $this->sendError(10101,L('code_much_the_same'));
  49. }
  50. }else{
  51. $this->sendError(10206,L('verification_code_are_incorrect'));
  52. }
  53. }
  54. //导入示例项目
  55. private function _importSample($uid){
  56. $this->_importZip("../Public/SampleZip/apidoc.zip" , $uid);
  57. $this->_importZip("../Public/SampleZip/databasedoc.zip" , $uid);
  58. $this->_importZip("../Public/SampleZip/teamdoc.zip" , $uid);
  59. $this->_importZip("../Public/SampleZip/spreadsheet.zip" , $uid);
  60. }
  61. private function _importZip($file , $uid){
  62. $zipArc = new \ZipArchive();
  63. $ret = $zipArc->open($file, \ZipArchive::CREATE);
  64. $info = $zipArc->getFromName("prefix_info.json") ;
  65. if ($info) {
  66. $info_array = json_decode($info ,1 );
  67. if ($info_array) {
  68. D("Item")->import( json_encode($info_array) , $uid );
  69. return true;
  70. }
  71. }
  72. return false ;
  73. }
  74. //登录
  75. public function login(){
  76. $username = trim(I("username"));
  77. $password = I("password");
  78. $v_code = I("v_code");
  79. if (!$password) {
  80. $this->sendError(10206,"no empty password");
  81. return;
  82. }
  83. //检查用户输错密码的次数。如果超过一定次数,则需要验证 验证码
  84. $key= 'login_fail_times_'.$username;
  85. if(!D("VerifyCode")->_check_times($key)){
  86. if (!$v_code || $v_code != session('v_code')) {
  87. $this->sendError(10206,L('verification_code_are_incorrect'));
  88. return;
  89. }
  90. }
  91. session('v_code',null) ;
  92. if(!D("User")->checkDbOk()){
  93. $this->sendError(100100,"数据库连接不上。请确保安装了php-sqlite扩展以及数据库文件Sqlite/showdoc.db.php可用");
  94. return;
  95. }
  96. $ret = D("User")->checkLogin($username,$password);
  97. //如果失败则尝试ldap登录
  98. if (!$ret) {
  99. $ret = D("User")->checkLdapLogin($username,$password);
  100. }
  101. if ($ret) {
  102. //获取后台的语言设置
  103. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  104. $config = file_get_contents("./Application/Home/Conf/config.php");
  105. if (D("Item")->count() < 1 && strstr($config, "'zh-cn',") ) {
  106. //如果项目表是空的,则生成系统示例项目
  107. $this->_importSample(1);
  108. }
  109. unset($ret['password']);
  110. session("login_user" , $ret );
  111. D("User")->setLastTime($ret['uid']);
  112. $token = D("UserToken")->createToken($ret['uid'],60*60*24*180);
  113. cookie('cookie_token',$token,array('expire'=>60*60*24*180,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  114. $this->sendResult(array());
  115. }else{
  116. D("VerifyCode")->_ins_times($key);//输错密码则设置输错次数
  117. if(D("VerifyCode")->_check_times($key)){
  118. $error_code = 10204 ;
  119. }else{
  120. $error_code = 10210 ;
  121. }
  122. $this->sendError($error_code,L('username_or_password_incorrect'));
  123. return;
  124. }
  125. }
  126. //登录2
  127. public function loginByVerify(){
  128. $username = I("username");
  129. $password = I("password");
  130. $captcha_id = I("captcha_id");
  131. $captcha = I("captcha");
  132. if ( !D("Captcha")->check($captcha_id , $captcha) ) {
  133. $this->sendError(10206,L('verification_code_are_incorrect'));
  134. return;
  135. }
  136. $ret = D("User")->checkLogin($username,$password);
  137. //如果失败则尝试ldap登录
  138. if (!$ret) {
  139. $ret = D("User")->checkLdapLogin($username,$password);
  140. }
  141. if ($ret) {
  142. //获取后台的语言设置
  143. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  144. $config = file_get_contents("./Application/Home/Conf/config.php");
  145. if (D("Item")->count() < 1 && strstr($config, "'zh-cn',") ) {
  146. //如果项目表是空的,则生成系统示例项目
  147. $this->_importSample(1);
  148. }
  149. unset($ret['password']);
  150. session("login_user" , $ret );
  151. D("User")->setLastTime($ret['uid']);
  152. $token = D("UserToken")->createToken($ret['uid'], 60*60*24*180);
  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. "email_verify" => $ret['email_verify'] ,
  162. "user_token" => $token ,
  163. ));
  164. }else{
  165. $this->sendError(10204,L('username_or_password_incorrect'));
  166. return;
  167. }
  168. }
  169. //注册2
  170. public function registerByVerify(){
  171. $username = trim(I("username"));
  172. $password = I("password");
  173. $confirm_password = I("confirm_password");
  174. $captcha_id = I("captcha_id");
  175. $captcha = I("captcha");
  176. $register_open = D("Options")->get("register_open" ) ;
  177. if ($register_open === '0') {
  178. $this->sendError(10101,"管理员已关闭注册");
  179. return ;
  180. }
  181. if ( !D("Captcha")->check($captcha_id , $captcha) ) {
  182. $this->sendError(10206,L('verification_code_are_incorrect'));
  183. return;
  184. }
  185. if ( $password != '' && $password == $confirm_password) {
  186. if ( ! D("User")->isExist($username) ) {
  187. $new_uid = D("User")->register($username,$password);
  188. if ($new_uid) {
  189. $create_sample = D("Options")->get("create_sample") ;
  190. //获取后台的语言设置
  191. //这是个历史包袱。因为安装的时候语言设置没有写到API模块的配置下,所以只能读文件读取Home模快的配置文件
  192. $config = file_get_contents("./Application/Home/Conf/config.php");
  193. if ($create_sample !== '0' && strstr($config, "'zh-cn',") ) {
  194. //导入示例项目
  195. $this->_importSample($new_uid);
  196. }
  197. //设置自动登录
  198. $ret = D("User")->where("uid = '$new_uid' ")->find() ;
  199. unset($ret['password']);
  200. session("login_user" , $ret );
  201. $token = D("UserToken")->createToken($ret['uid']);
  202. cookie('cookie_token',$token,array('expire'=>60*60*24*90,'httponly'=>'httponly'));//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
  203. $this->sendResult(array(
  204. "uid" => $ret['uid'] ,
  205. "username" => $ret['username'] ,
  206. "name" => $ret['name'] ,
  207. "groupid" => $ret['groupid'] ,
  208. "avatar" => $ret['avatar'] ,
  209. "avatar_small" => $ret['avatar_small'] ,
  210. "email" => $ret['email'] ,
  211. "user_token" => $token ,
  212. ));
  213. }else{
  214. $this->sendError(10101,'register fail');
  215. }
  216. }else{
  217. $this->sendError(10101,L('username_exists'));
  218. }
  219. }else{
  220. $this->sendError(10101,L('code_much_the_same'));
  221. }
  222. }
  223. //获取用户信息
  224. public function info(){
  225. $login_user = $this->checkLogin();
  226. $uid = $login_user['uid'] ;
  227. $field = "uid,username,email,name,avatar,avatar_small,groupid" ;
  228. $info = D("User")->where(" uid = '$uid' ")->field($field)->find();
  229. $this->sendResult($info);
  230. }
  231. //获取所有用户名
  232. public function allUser(){
  233. $login_user = $this->checkLogin();
  234. $uid = $login_user['uid'] ;
  235. $username = I("username");
  236. $field = "username as value" ;
  237. if ($username) {
  238. $username = \SQLite3::escapeString($username) ;
  239. $where = " username like '%{$username}%'" ;
  240. }else{
  241. $where = ' 1 = 1 ';
  242. }
  243. $info = D("User")->where($where)->field($field)->select();
  244. $this->sendResult($info);
  245. }
  246. //通过旧密码验证来更新用户密码
  247. public function resetPassword(){
  248. $login_user = $this->checkLogin();
  249. $username = $login_user['username'];
  250. $password = I("password");
  251. $new_password = I("new_password");
  252. $ret = D("User")->checkLogin($username,$password);
  253. if ($ret) {
  254. $ret = D("User")->updatePwd($login_user['uid'],$new_password);
  255. if ($ret) {
  256. $this->sendResult(array());
  257. }else{
  258. $this->sendError(10101,L('modify_faild'));
  259. }
  260. }else{
  261. $this->sendError(10101,L('old_password_incorrect'));
  262. }
  263. }
  264. //退出登录
  265. public function logout(){
  266. $login_user = $this->checkLogin();
  267. D("UserToken")->where(" uid = '$login_user[uid]' ")->save(array("token_expire"=>0));
  268. session("login_user" , NULL);
  269. cookie('cookie_token',NULL);
  270. session(null);
  271. $this->sendResult(array());
  272. }
  273. public function updateInfo(){
  274. $user = $this->checkLogin();
  275. $uid = $user['uid'];
  276. $name = I("name");
  277. D("User")->where(" uid = '$uid' ")->save(array("name"=>$name));
  278. $this->sendResult(array());
  279. }
  280. }