AuthController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Http\Controllers\Passport;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Passport\AuthRegister;
  5. use App\Http\Requests\Passport\AuthForget;
  6. use App\Http\Requests\Passport\AuthLogin;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Cache;
  9. use App\Models\Plan;
  10. use App\Models\User;
  11. use App\Models\InviteCode;
  12. use App\Utils\Helper;
  13. use App\Utils\Dict;
  14. use App\Utils\CacheKey;
  15. class AuthController extends Controller
  16. {
  17. public function register(AuthRegister $request)
  18. {
  19. if ((int)config('v2board.email_whitelist_enable', 0)) {
  20. if (!Helper::emailSuffixVerify(
  21. $request->input('email'),
  22. config('v2board.email_whitelist_suffix', Dict::EMAIL_WHITELIST_SUFFIX_DEFAULT))
  23. ) {
  24. abort(500, '邮箱后缀不处于白名单中');
  25. }
  26. }
  27. if ((int)config('v2board.email_gmail_limit_enable', 0)) {
  28. $prefix = explode('@', $request->input('email'))[0];
  29. if (strpos($prefix, '.') !== false || strpos($prefix, '+') !== false) {
  30. abort(500, '不支持Gmail别名邮箱');
  31. }
  32. }
  33. if ((int)config('v2board.stop_register', 0)) {
  34. abort(500, '本站已关闭注册');
  35. }
  36. if ((int)config('v2board.invite_force', 0)) {
  37. if (empty($request->input('invite_code'))) {
  38. abort(500, '必须使用邀请码才可以注册');
  39. }
  40. }
  41. if ((int)config('v2board.email_verify', 0)) {
  42. if (empty($request->input('email_code'))) {
  43. abort(500, '邮箱验证码不能为空');
  44. }
  45. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  46. abort(500, '邮箱验证码有误');
  47. }
  48. }
  49. $email = $request->input('email');
  50. $password = $request->input('password');
  51. $exist = User::where('email', $email)->first();
  52. if ($exist) {
  53. abort(500, '邮箱已存在系统中');
  54. }
  55. $user = new User();
  56. $user->email = $email;
  57. $user->password = password_hash($password, PASSWORD_DEFAULT);
  58. $user->uuid = Helper::guid(true);
  59. $user->token = Helper::guid();
  60. if ($request->input('invite_code')) {
  61. $inviteCode = InviteCode::where('code', $request->input('invite_code'))
  62. ->where('status', 0)
  63. ->first();
  64. if (!$inviteCode) {
  65. if ((int)config('v2board.invite_force', 0)) {
  66. abort(500, '邀请码无效');
  67. }
  68. } else {
  69. $user->invite_user_id = $inviteCode->user_id ? $inviteCode->user_id : null;
  70. if (!(int)config('v2board.invite_never_expire', 0)) {
  71. $inviteCode->status = 1;
  72. $inviteCode->save();
  73. }
  74. }
  75. }
  76. // try out
  77. if ((int)config('v2board.try_out_plan_id', 0)) {
  78. $plan = Plan::find(config('v2board.try_out_plan_id'));
  79. if ($plan) {
  80. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  81. $user->plan_id = $plan->id;
  82. $user->group_id = $plan->group_id;
  83. $user->expired_at = time() + (config('v2board.try_out_hour', 1) * 3600);
  84. }
  85. }
  86. if (!$user->save()) {
  87. abort(500, '注册失败');
  88. }
  89. if ((int)config('v2board.email_verify', 0)) {
  90. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  91. }
  92. $request->session()->put('email', $user->email);
  93. $request->session()->put('id', $user->id);
  94. return response()->json([
  95. 'data' => true
  96. ]);
  97. }
  98. public function login(AuthLogin $request)
  99. {
  100. $email = $request->input('email');
  101. $password = $request->input('password');
  102. $user = User::where('email', $email)->first();
  103. if (!$user) {
  104. abort(500, '用户名或密码错误');
  105. }
  106. if (!Helper::multiPasswordVerify(
  107. $user->password_algo,
  108. $password,
  109. $user->password)
  110. ) {
  111. abort(500, '用户名或密码错误');
  112. }
  113. if ($user->banned) {
  114. abort(500, '该账户已被停止使用');
  115. }
  116. $data = [
  117. 'token' => $user->token
  118. ];
  119. $request->session()->put('email', $user->email);
  120. $request->session()->put('id', $user->id);
  121. if ($user->is_admin) {
  122. $request->session()->put('is_admin', true);
  123. $data['is_admin'] = true;
  124. }
  125. return response([
  126. 'data' => $data
  127. ]);
  128. }
  129. // 准备废弃
  130. public function token2Login(Request $request)
  131. {
  132. if ($request->input('token')) {
  133. $redirect = '/#/login?verify=' . $request->input('token') . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  134. if (config('v2board.app_url')) {
  135. $location = config('v2board.app_url') . $redirect;
  136. } else {
  137. $location = url($redirect);
  138. }
  139. return header('Location:' . $location);
  140. }
  141. if ($request->input('verify')) {
  142. $key = CacheKey::get('TEMP_TOKEN', $request->input('verify'));
  143. $userId = Cache::get($key);
  144. if (!$userId) {
  145. abort(500, '令牌有误');
  146. }
  147. $user = User::find($userId);
  148. if (!$user) {
  149. abort(500, '用户不存在');
  150. }
  151. if ($user->banned) {
  152. abort(500, '该账户已被停止使用');
  153. }
  154. $request->session()->put('email', $user->email);
  155. $request->session()->put('id', $user->id);
  156. if ($user->is_admin) {
  157. $request->session()->put('is_admin', true);
  158. }
  159. Cache::forget($key);
  160. return response([
  161. 'data' => true
  162. ]);
  163. }
  164. }
  165. public function getTempToken(Request $request)
  166. {
  167. $user = User::where('token', $request->input('token'))->first();
  168. if (!$user) {
  169. abort(500, '用户不存在');
  170. }
  171. $code = Helper::guid();
  172. $key = CacheKey::get('TEMP_TOKEN', $code);
  173. Cache::put($key, $user->id, 60);
  174. return response([
  175. 'data' => $code
  176. ]);
  177. }
  178. public function check(Request $request)
  179. {
  180. $data = [
  181. 'is_login' => $request->session()->get('id') ? true : false
  182. ];
  183. if ($request->session()->get('is_admin')) {
  184. $data['is_admin'] = true;
  185. }
  186. return response([
  187. 'data' => $data
  188. ]);
  189. }
  190. public function forget(AuthForget $request)
  191. {
  192. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  193. abort(500, '邮箱验证码有误');
  194. }
  195. $user = User::where('email', $request->input('email'))->first();
  196. if (!$user) {
  197. abort(500, '该邮箱不存在系统中');
  198. }
  199. $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
  200. $user->password_algo = NULL;
  201. if (!$user->save()) {
  202. abort(500, '重置失败');
  203. }
  204. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  205. return response([
  206. 'data' => true
  207. ]);
  208. }
  209. }