共通化
このコミットが含まれているのは:
コミット
b0810a93b2
|
@ -3,63 +3,18 @@
|
|||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller, Session;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Controllers\Api\CommonController as Common;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Admin;
|
||||
use App\Models\LoginLimits;
|
||||
class AdminController extends Common {
|
||||
private $common;
|
||||
|
||||
class AdminController extends Controller {
|
||||
protected $guard = 'admins';
|
||||
public function __construct () {
|
||||
$this->common = new Common('admin');
|
||||
}
|
||||
|
||||
public function login (Request $r) {
|
||||
// if (null === $r->server('HTTP_USER_AGENT')) {
|
||||
// return ['status_code' => 400, 'error_message' => ['不正なuser_agent。']];
|
||||
// }
|
||||
|
||||
// if (null !== ($ll = LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->first())) {
|
||||
// if ((time() >= strtotime($ll->updated_at) + 600) === false) {
|
||||
// LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->delete();
|
||||
// }
|
||||
// if ($ll->fail_number >= 10) {
|
||||
// return ['status_code' => 400, 'error_message' => ['10回連続で失敗しましたので、10分、ログインロックになりました。']];
|
||||
// }
|
||||
// }
|
||||
|
||||
$validate = Validator::make($r->all(), [
|
||||
'email' => 'required|max:255|email',
|
||||
'password' => 'required|min:8|max:72',
|
||||
]);
|
||||
|
||||
if ($validate->fails()) {
|
||||
// バリデーションエラー
|
||||
return ['status_code' => 422, 'error_messages' => $validate->errors()];
|
||||
}
|
||||
|
||||
// 存在しない場合
|
||||
if (null === ($admin = Admin::select('id', 'email', 'password')->where('email', $r->email)->first())) {
|
||||
return ['status_code' => 400, 'error_message' => ['このアカウントが存在しません。']];
|
||||
}
|
||||
|
||||
// パスワードが異なる場合
|
||||
if (!Hash::check($r->password, $admin->password)) {
|
||||
// if ($ll = LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->first()) {
|
||||
// LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->update(['fail_number' => $ll->fail_number+1]);
|
||||
// }
|
||||
// else {
|
||||
// LoginLimits::create(['user_agent' => $r->server('HTTP_USER_AGENT'), 'fail_number' => 1]);
|
||||
// }
|
||||
return ['status_code' => 400, 'error_message' => ['ログインに失敗しました。10回連続で失敗すると、一定期間ログインできなくなります。']];
|
||||
}
|
||||
|
||||
// セッションを想像する
|
||||
if (Auth::guard('admins')->attempt($r->all())) {
|
||||
Session::put('admin_email', $admin->email);
|
||||
Session::save();
|
||||
}
|
||||
return ['status_code' => 200];
|
||||
return $this->common->login($r);
|
||||
}
|
||||
|
||||
public function checkLogin () {
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller, Session;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Models\Admin;
|
||||
use App\Models\Father;
|
||||
use App\Models\Child;
|
||||
// use App\Models\LoginLimits;
|
||||
|
||||
class CommonController extends Controller {
|
||||
private $guard;
|
||||
private $guards;
|
||||
private $model;
|
||||
|
||||
public function __construct ($guard) {
|
||||
$this->guard = $guard;
|
||||
$this->guards = $guard.'s';
|
||||
|
||||
$select = ['id', 'email', 'password'];
|
||||
|
||||
if ($this->guard == 'admin') {
|
||||
$this->model = Admin::select($select);
|
||||
}
|
||||
else if ($this->guard == 'father') {
|
||||
$this->model = Father::select($select);
|
||||
}
|
||||
else if ($this->guard == 'child') {
|
||||
$this->guards = $guard.'ren';
|
||||
$this->model = Child::select($select);
|
||||
}
|
||||
}
|
||||
|
||||
public function login (Request $r) {
|
||||
// if (null === $r->server('HTTP_USER_AGENT')) {
|
||||
// return ['status_code' => 400, 'error_message' => ['不正なuser_agent。']];
|
||||
// }
|
||||
|
||||
// if (null !== ($ll = LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->first())) {
|
||||
// if ((time() >= strtotime($ll->updated_at) + 600) === false) {
|
||||
// LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->delete();
|
||||
// }
|
||||
// if ($ll->fail_number >= 10) {
|
||||
// return ['status_code' => 400, 'error_message' => ['10回連続で失敗しましたので、10分、ログインロックになりました。']];
|
||||
// }
|
||||
// }
|
||||
|
||||
$validate = Validator::make($r->all(), [
|
||||
'email' => 'required|max:255|email',
|
||||
'password' => 'required|min:8|max:72',
|
||||
]);
|
||||
|
||||
if ($validate->fails()) {
|
||||
// バリデーションエラー
|
||||
return ['status_code' => 422, 'error_messages' => $validate->errors()];
|
||||
}
|
||||
|
||||
// 存在しない場合
|
||||
if (null === ($get = $this->model->where('email', $r->email)->first())) {
|
||||
return ['status_code' => 400, 'error_message' => ['ログインに失敗しました。10回連続で失敗すると、一定期間ログインできなくなります。']];
|
||||
}
|
||||
|
||||
// パスワードが異なる場合
|
||||
if (!Hash::check($r->password, $get->password)) {
|
||||
// if ($ll = LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->first()) {
|
||||
// LoginLimits::where('user_agent', $r->server('HTTP_USER_AGENT'))->update(['fail_number' => $ll->fail_number+1]);
|
||||
// }
|
||||
// else {
|
||||
// LoginLimits::create(['user_agent' => $r->server('HTTP_USER_AGENT'), 'fail_number' => 1]);
|
||||
// }
|
||||
return ['status_code' => 400, 'error_message' => ['ログインに失敗しました。10回連続で失敗すると、一定期間ログインできなくなります。']];
|
||||
}
|
||||
|
||||
// セッションを想像する
|
||||
if (Auth::guard($this->guards)->attempt($r->all())) {
|
||||
Session::put($this->guard.'_email', $get->email);
|
||||
Session::save();
|
||||
}
|
||||
|
||||
return ['status_code' => 200];
|
||||
}
|
||||
}
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Controller, Session;
|
||||
use App\Http\Controllers\Api\CommonController as Common;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Http\Request;
|
||||
|
@ -12,37 +13,15 @@ use App\Models\Father;
|
|||
use App\Models\FatherRelation;
|
||||
use App\Models\EmailActivations;
|
||||
|
||||
class FathersController extends Controller {
|
||||
protected $guard = 'fathers';
|
||||
class FathersController extends Common {
|
||||
private $common;
|
||||
|
||||
public function __construct () {
|
||||
$this->common = new Common('father');
|
||||
}
|
||||
|
||||
public function login (Request $r) {
|
||||
$validate = Validator::make($r->all(), [
|
||||
'email' => 'required|max:255|email',
|
||||
'password' => 'required|min:8|max:72',
|
||||
]);
|
||||
|
||||
if ($validate->fails()) {
|
||||
// バリデーションエラー
|
||||
return ['status_code' => 422, 'error_messages' => $validate->errors()];
|
||||
}
|
||||
|
||||
// 存在しない場合
|
||||
if (null === ($father = Father::select('id', 'email', 'password')->where('email', $r->email)->first())) {
|
||||
return ['status_code' => 400, 'error_message' => ['このアカウントが存在しません。']];
|
||||
}
|
||||
|
||||
// パスワードが異なる場合
|
||||
if (!Hash::check($r->password, $father->password)) {
|
||||
return ['status_code' => 400, 'error_message' => ['ログインに失敗しました。10回連続で失敗すると、一定期間ログインできなくなります。']];
|
||||
}
|
||||
|
||||
// セッションを想像する
|
||||
if (Auth::guard($this->guard)->attempt($r->all())) {
|
||||
Session::put('father_email', $father->email);
|
||||
Session::save();
|
||||
}
|
||||
|
||||
return ['status_code' => 200];
|
||||
return $this->common->login($r);
|
||||
}
|
||||
|
||||
public function requestPassword () {}
|
||||
|
|
新しいイシューから参照