AuthController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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->v2ray_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. public function token2Login(Request $request)
  130. {
  131. if ($request->input('token')) {
  132. $user = User::where('token', $request->input('token'))->first();
  133. if (!$user) {
  134. return header('Location:' . config('v2board.app_url'));
  135. }
  136. $code = Helper::guid();
  137. $key = 'token2Login_' . $code;
  138. Cache::put($key, $user->id, 600);
  139. $redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  140. if (config('v2board.app_url')) {
  141. $location = config('v2board.app_url') . $redirect;
  142. } else {
  143. $location = url($redirect);
  144. }
  145. return header('Location:' . $location);
  146. }
  147. if ($request->input('verify')) {
  148. $key = 'token2Login_' . $request->input('verify');
  149. $userId = Cache::get($key);
  150. if (!$userId) {
  151. abort(500, '令牌有误');
  152. }
  153. $user = User::find($userId);
  154. if (!$user) {
  155. abort(500, '用户不存在');
  156. }
  157. if ($user->banned) {
  158. abort(500, '该账户已被停止使用');
  159. }
  160. $request->session()->put('email', $user->email);
  161. $request->session()->put('id', $user->id);
  162. if ($user->is_admin) {
  163. $request->session()->put('is_admin', true);
  164. }
  165. Cache::forget($key);
  166. return response([
  167. 'data' => true
  168. ]);
  169. }
  170. }
  171. public function check(Request $request)
  172. {
  173. $data = [
  174. 'is_login' => $request->session()->get('id') ? true : false
  175. ];
  176. if ($request->session()->get('is_admin')) {
  177. $data['is_admin'] = true;
  178. }
  179. return response([
  180. 'data' => $data
  181. ]);
  182. }
  183. public function forget(AuthForget $request)
  184. {
  185. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  186. abort(500, '邮箱验证码有误');
  187. }
  188. $user = User::where('email', $request->input('email'))->first();
  189. if (!$user) {
  190. abort(500, '该邮箱不存在系统中');
  191. }
  192. $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
  193. $user->password_algo = NULL;
  194. if (!$user->save()) {
  195. abort(500, '重置失败');
  196. }
  197. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  198. return response([
  199. 'data' => true
  200. ]);
  201. }
  202. }