blessing-skin-server/app/Http/Controllers/AuthController.php

237 lines
7.3 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-21 22:01:57 +08:00
use URL;
2016-11-07 22:34:34 +08:00
use Log;
2016-07-21 22:01:57 +08:00
use Mail;
use View;
2016-08-06 19:38:37 +08:00
use Utils;
2016-10-23 11:41:52 +08:00
use Cookie;
2016-07-21 22:01:57 +08:00
use Option;
2016-08-28 10:05:21 +08:00
use Session;
2016-11-17 17:32:12 +08:00
use App\Events;
use App\Models\User;
2018-07-15 17:42:03 +08:00
use App\Mail\ForgotPassword;
use Illuminate\Http\Request;
use App\Exceptions\PrettyPageException;
2016-10-23 11:41:52 +08:00
use App\Services\Repositories\UserRepository;
2016-07-21 22:01:57 +08:00
2016-09-03 23:50:55 +08:00
class AuthController extends Controller
2016-07-21 22:01:57 +08:00
{
public function login()
{
2016-08-28 10:05:21 +08:00
return view('auth.login');
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
public function handleLogin(Request $request, UserRepository $users)
2016-07-21 22:01:57 +08:00
{
2016-09-03 23:50:55 +08:00
$this->validate($request, [
2016-10-02 20:30:27 +08:00
'identification' => 'required',
'password' => 'required|min:6|max:32'
2016-09-03 23:50:55 +08:00
]);
2016-10-02 20:30:27 +08:00
$identification = $request->input('identification');
2018-02-16 17:31:04 +08:00
// Guess type of identification
$authType = (validate($identification, 'email')) ? "email" : "username";
2016-09-03 23:50:55 +08:00
2018-02-16 17:31:04 +08:00
event(new Events\UserTryToLogin($identification, $authType));
2016-10-17 17:51:51 +08:00
2016-10-23 11:41:52 +08:00
// Get user instance from repository.
// If the given identification is not registered yet,
// it will return a null value.
2018-02-16 17:31:04 +08:00
$user = $users->get($identification, $authType);
2016-07-21 22:01:57 +08:00
2016-08-28 10:05:21 +08:00
if (session('login_fails', 0) > 3) {
2016-09-03 23:50:55 +08:00
if (strtolower($request->input('captcha')) != strtolower(session('phrase')))
2016-09-15 09:20:02 +08:00
return json(trans('auth.validation.captcha'), 1);
2016-07-21 22:01:57 +08:00
}
2018-02-16 17:31:04 +08:00
if (! $user) {
2016-09-15 09:20:02 +08:00
return json(trans('auth.validation.user'), 2);
2016-07-21 22:01:57 +08:00
} else {
if ($user->verifyPassword($request->input('password'))) {
2016-09-03 23:50:55 +08:00
Session::forget('login_fails');
2016-07-21 22:01:57 +08:00
2016-08-28 10:05:21 +08:00
Session::put('uid' , $user->uid);
Session::put('token', $user->getToken());
2016-07-21 22:01:57 +08:00
2018-02-16 17:31:04 +08:00
// Time in minutes
2016-10-23 11:41:52 +08:00
$time = $request->input('keep') == true ? 10080 : 60;
2016-07-21 22:01:57 +08:00
2016-11-17 17:32:12 +08:00
event(new Events\UserLoggedIn($user));
2016-10-17 17:51:51 +08:00
session()->forget('last_requested_path');
2016-10-02 20:30:27 +08:00
return json(trans('auth.login.success'), 0, [
2016-07-21 22:01:57 +08:00
'token' => $user->getToken()
2018-02-16 17:31:04 +08:00
]) // Set cookies
2016-10-23 11:41:52 +08:00
->withCookie('uid', $user->uid, $time)
->withCookie('token', $user->getToken(), $time);
2016-07-21 22:01:57 +08:00
} else {
2016-10-02 20:30:27 +08:00
Session::put('login_fails', session('login_fails', 0) + 1);
2016-08-16 13:27:06 +08:00
2016-10-02 20:30:27 +08:00
return json(trans('auth.validation.password'), 1, [
2016-08-28 10:05:21 +08:00
'login_fails' => session('login_fails')
2016-07-21 22:01:57 +08:00
]);
}
}
}
2016-10-23 11:41:52 +08:00
public function logout(Request $request)
2016-07-21 22:01:57 +08:00
{
2017-10-30 12:40:34 +08:00
if (Session::has('uid') && Session::has('token')) {
2018-02-16 17:31:04 +08:00
// Flush sessions
2016-08-28 10:05:21 +08:00
Session::flush();
2018-02-16 17:31:04 +08:00
// Delete cookies
2016-10-23 11:41:52 +08:00
return json(trans('auth.logout.success'), 0)
->withCookie(Cookie::forget('uid'))
->withCookie(Cookie::forget('token'));
2016-07-21 22:01:57 +08:00
} else {
2016-10-06 17:57:07 +08:00
return json(trans('auth.logout.fail'), 1);
2016-07-21 22:01:57 +08:00
}
}
public function register()
{
2016-10-23 11:41:52 +08:00
if (option('user_can_register')) {
2016-08-28 10:05:21 +08:00
return view('auth.register');
} else {
2016-09-15 09:20:02 +08:00
throw new PrettyPageException(trans('auth.register.close'), 7);
}
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
public function handleRegister(Request $request, UserRepository $users)
2016-07-21 22:01:57 +08:00
{
2018-02-16 17:31:04 +08:00
if (! $this->checkCaptcha($request))
2016-09-15 09:20:02 +08:00
return json(trans('auth.validation.captcha'), 1);
2016-07-21 22:01:57 +08:00
2016-09-03 23:50:55 +08:00
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:8|max:32',
'nickname' => 'required|no_special_chars|max:255'
2016-09-03 23:50:55 +08:00
]);
2018-02-16 17:31:04 +08:00
if (! option('user_can_register')) {
2016-10-23 11:41:52 +08:00
return json(trans('auth.register.close'), 7);
}
// If amount of registered accounts of IP is more than allowed amounts,
// then reject the register.
if (User::where('ip', Utils::getClientIp())->count() < option('regs_per_ip'))
2016-10-23 11:41:52 +08:00
{
// Register a new user.
// If the email is already registered,
// it will return a false value.
$user = User::register(
$request->input('email'),
$request->input('password'), function($user) use ($request)
2016-10-23 11:41:52 +08:00
{
$user->ip = Utils::getClientIp();
2016-10-23 11:41:52 +08:00
$user->score = option('user_initial_score');
$user->register_at = Utils::getTimeFormatted();
$user->last_sign_at = Utils::getTimeFormatted(time() - 86400);
$user->permission = User::NORMAL;
$user->nickname = $request->input('nickname');
});
2018-02-16 17:31:04 +08:00
if (! $user) {
2016-10-23 11:41:52 +08:00
return json(trans('auth.register.registered'), 5);
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
2016-11-17 17:32:12 +08:00
event(new Events\UserRegistered($user));
2016-10-23 11:41:52 +08:00
return json([
'errno' => 0,
'msg' => trans('auth.register.success'),
'token' => $user->getToken(),
2018-02-16 17:31:04 +08:00
]) // Set cookies
2016-10-23 11:41:52 +08:00
->withCookie('uid', $user->uid, 60)
->withCookie('token', $user->getToken(), 60);
2016-07-21 22:01:57 +08:00
} else {
2016-10-23 11:41:52 +08:00
return json(trans('auth.register.max', ['regs' => option('regs_per_ip')]), 7);
2016-07-21 22:01:57 +08:00
}
}
public function forgot()
{
if (config('mail.driver') != "") {
2016-08-28 10:05:21 +08:00
return view('auth.forgot');
} else {
2016-09-15 09:20:02 +08:00
throw new PrettyPageException(trans('auth.forgot.close'), 8);
}
2016-07-21 22:01:57 +08:00
}
2016-10-23 11:41:52 +08:00
public function handleForgot(Request $request, UserRepository $users)
2016-07-21 22:01:57 +08:00
{
2018-02-16 17:31:04 +08:00
if (! $this->checkCaptcha($request))
2016-09-15 09:20:02 +08:00
return json(trans('auth.validation.captcha'), 1);
2016-07-21 22:01:57 +08:00
if (config('mail.driver') == "")
2016-09-15 09:20:02 +08:00
return json(trans('auth.forgot.close'), 1);
2016-09-04 16:15:11 +08:00
if (Session::has('last_mail_time') && (time() - session('last_mail_time')) < 60)
2016-09-15 09:20:02 +08:00
return json(trans('auth.forgot.frequent-mail'), 1);
2016-07-21 22:01:57 +08:00
2018-02-16 17:31:04 +08:00
// Get user instance
2016-10-23 11:41:52 +08:00
$user = $users->get($request->input('email'), 'email');
2016-07-21 22:01:57 +08:00
2018-02-16 17:31:04 +08:00
if (! $user)
2016-09-15 09:20:02 +08:00
return json(trans('auth.forgot.unregistered'), 1);
2016-07-21 22:01:57 +08:00
$url = URL::temporarySignedRoute('auth.reset', now()->addHour(), ['uid' => $user->uid]);
2016-07-21 22:01:57 +08:00
2016-09-04 16:15:11 +08:00
try {
Mail::to($request->input('email'))->send(new ForgotPassword($url));
} catch (\Exception $e) {
// Write the exception to log
app(\Illuminate\Foundation\Exceptions\Handler::class)->report($e);
2016-09-15 09:20:02 +08:00
return json(trans('auth.mail.failed', ['msg' => $e->getMessage()]), 2);
2016-07-21 22:01:57 +08:00
}
2016-09-04 16:15:11 +08:00
Session::put('last_mail_time', time());
2016-09-15 09:20:02 +08:00
return json(trans('auth.mail.success'), 0);
2016-07-21 22:01:57 +08:00
}
public function reset($uid, UserRepository $users)
2016-07-21 22:01:57 +08:00
{
return view('auth.reset')->with('user', $users->get($uid));
2016-07-21 22:01:57 +08:00
}
public function handleReset($uid, Request $request, UserRepository $users)
2016-07-21 22:01:57 +08:00
{
$validated = $this->validate($request, [
'password' => 'required|min:8|max:32',
2016-09-03 23:50:55 +08:00
]);
2016-07-21 22:01:57 +08:00
2018-07-19 10:31:44 +08:00
$users->get($uid)->changePassword($validated['password']);
2016-11-07 22:34:34 +08:00
2016-09-15 09:20:02 +08:00
return json(trans('auth.reset.success'), 0);
2016-07-21 22:01:57 +08:00
}
public function captcha()
{
$builder = new \Gregwar\Captcha\CaptchaBuilder;
$builder->build($width = 100, $height = 34);
2016-08-28 10:05:21 +08:00
Session::put('phrase', $builder->getPhrase());
2017-10-30 12:40:34 +08:00
ob_start();
2016-07-21 22:01:57 +08:00
$builder->output();
2017-10-30 12:40:34 +08:00
$captcha = ob_get_contents();
ob_end_clean();
2016-08-29 23:08:09 +08:00
2017-10-30 12:40:34 +08:00
return \Response::png($captcha);
2016-07-21 22:01:57 +08:00
}
protected function checkCaptcha($request)
2016-10-23 11:41:52 +08:00
{
return (strtolower($request->input('captcha')) == strtolower(session('phrase')));
}
2016-07-21 22:01:57 +08:00
}