AuthController.php 7.8 KB

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