From b6680967d9a4fe207917a02cf03bbee4792115e0 Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Tue, 26 Mar 2024 17:20:53 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=84=E3=82=8D=E3=81=84=E3=82=8D=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Web/Auth/Me.php | 19 ++++++- .../Controllers/Web/Auth/MeController.php | 2 - ...oller.php => SwitchCustomerController.php} | 14 ++++-- .../Web/Auth/SwitchEndController.php | 2 +- .../Controllers/Web/Auth/SwitchEndParam.php | 2 +- .../Web/Auth/SwitchShopController.php | 48 ++++++++++++++++++ .../Web/Shop/DepositChargeController.php | 7 +-- .../Web/Shop/DepositChargeParam.php | 2 +- .../Web/Shop/ShopConfigController.php | 49 +++++++++++++++++++ .../Controllers/Web/Shop/ShopConfigParam.php | 28 +++++++++++ .../Controllers/Web/Shop/ShopListParam.php | 18 ++++++- .../Web/Shop/ShopRegisterController.php | 1 + app/Http/Controllers/Web/WebController.php | 2 + app/Http/Middleware/RoleMiddleware.php | 7 ++- app/Logics/QRService/ChargeLogic.php | 5 +- app/Logics/Shop/ShopLogic.php | 2 +- app/Models/Htpms/MstCustomer.php | 13 +++++ app/Providers/AppServiceProvider.php | 13 ++--- app/Repositories/LoginUserRepository.php | 1 + app/Repositories/ShopRepository.php | 8 ++- app/Sessions/SessionUser.php | 16 +++++- app/Util/RouteHelper.php | 4 +- lang/ja/validation.php | 1 + resources/views/index.html | 1 + routes/api.php | 18 ++++--- 25 files changed, 244 insertions(+), 39 deletions(-) rename app/Http/Controllers/Web/Auth/{SwitchController.php => SwitchCustomerController.php} (62%) create mode 100644 app/Http/Controllers/Web/Auth/SwitchShopController.php create mode 100644 app/Http/Controllers/Web/Shop/ShopConfigController.php create mode 100644 app/Http/Controllers/Web/Shop/ShopConfigParam.php create mode 100644 resources/views/index.html diff --git a/app/Http/Controllers/Web/Auth/Me.php b/app/Http/Controllers/Web/Auth/Me.php index 943dc99..b0cee5d 100644 --- a/app/Http/Controllers/Web/Auth/Me.php +++ b/app/Http/Controllers/Web/Auth/Me.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Web\Auth; use App\Exceptions\AppCommonException; +use App\Sessions\SessionUser; use Illuminate\Support\Facades\Auth; trait Me @@ -12,6 +13,22 @@ trait Me if (!Auth::check()) { throw new AppCommonException("Me失敗"); } - return Auth::user()->toArray(); + + $sessionUser = SessionUser::instance(); + + + $ret = Auth::user()->toArray(); + + if ($sessionUser->isSwtiched()) { + $ret['switched_user_id'] = $sessionUser->user()->id; + $ret['switched_role'] = $sessionUser->user()->role; + $ret['switched_name'] = $sessionUser->user()->name; + } else { + $ret['switched_user_id'] = null; + $ret['switched_role'] = null; + $ret['switched_name'] = null; + } + + return $ret; } } diff --git a/app/Http/Controllers/Web/Auth/MeController.php b/app/Http/Controllers/Web/Auth/MeController.php index b89d76b..ac38f26 100644 --- a/app/Http/Controllers/Web/Auth/MeController.php +++ b/app/Http/Controllers/Web/Auth/MeController.php @@ -4,10 +4,8 @@ namespace App\Http\Controllers\Web\Auth; use App\Exceptions\AppCommonException; use App\Http\Controllers\Web\WebController; -use App\Kintone\Models\Customer; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; class MeController extends WebController { diff --git a/app/Http/Controllers/Web/Auth/SwitchController.php b/app/Http/Controllers/Web/Auth/SwitchCustomerController.php similarity index 62% rename from app/Http/Controllers/Web/Auth/SwitchController.php rename to app/Http/Controllers/Web/Auth/SwitchCustomerController.php index d3ccdc5..4b27f28 100644 --- a/app/Http/Controllers/Web/Auth/SwitchController.php +++ b/app/Http/Controllers/Web/Auth/SwitchCustomerController.php @@ -2,17 +2,18 @@ namespace App\Http\Controllers\Web\Auth; +use App\Codes\UserRole; use App\Http\Controllers\Web\WebController; use App\Models\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -class SwitchController extends WebController +class SwitchCustomerController extends WebController { public function name(): string { - return "成り代わり"; + return "成り代わり(顧客)"; } public function description(): string @@ -31,9 +32,16 @@ class SwitchController extends WebController $param = $this->param; $user = User::findOrFail($param->userId); + if ($user->role !== UserRole::CUSTOMER) throw new LogicException(); $this->sessionUser->switch($user); - return $this->successResponse(); + $res = [ + 'user_id' => $user->id, + 'name' => $user->name, + 'role' => $user->role, + ]; + + return $this->successResponse($res); } } diff --git a/app/Http/Controllers/Web/Auth/SwitchEndController.php b/app/Http/Controllers/Web/Auth/SwitchEndController.php index a9ff6ca..5e2b433 100644 --- a/app/Http/Controllers/Web/Auth/SwitchEndController.php +++ b/app/Http/Controllers/Web/Auth/SwitchEndController.php @@ -20,7 +20,7 @@ class SwitchEndController extends WebController } - public function __construct(protected SwitchParam $param) + public function __construct(protected SwitchEndParam $param) { parent::__construct(); } diff --git a/app/Http/Controllers/Web/Auth/SwitchEndParam.php b/app/Http/Controllers/Web/Auth/SwitchEndParam.php index f5bd421..d802282 100644 --- a/app/Http/Controllers/Web/Auth/SwitchEndParam.php +++ b/app/Http/Controllers/Web/Auth/SwitchEndParam.php @@ -4,6 +4,6 @@ namespace App\Http\Controllers\Web\Auth; use App\Http\Controllers\Web\NoneParams; -class SwitchParam extends NoneParams +class SwitchEndParam extends NoneParams { } diff --git a/app/Http/Controllers/Web/Auth/SwitchShopController.php b/app/Http/Controllers/Web/Auth/SwitchShopController.php new file mode 100644 index 0000000..881bc6e --- /dev/null +++ b/app/Http/Controllers/Web/Auth/SwitchShopController.php @@ -0,0 +1,48 @@ +param; + + $user = User::findOrFail($param->userId); + if ($user->role !== UserRole::SHOP) throw new LogicException(); + + $this->sessionUser->switch($user); + + $res = [ + 'user_id' => $user->id, + 'name' => $user->name, + 'role' => $user->role, + ]; + + return $this->successResponse($res); + } +} diff --git a/app/Http/Controllers/Web/Shop/DepositChargeController.php b/app/Http/Controllers/Web/Shop/DepositChargeController.php index ff6a4ae..0fa7afe 100644 --- a/app/Http/Controllers/Web/Shop/DepositChargeController.php +++ b/app/Http/Controllers/Web/Shop/DepositChargeController.php @@ -36,12 +36,7 @@ class DepositChargeController extends WebController throw new LogicException(); } - if ($user->shop_id === null) { - throw new LogicException(); - } - - - $deposit = $this->logic->charge($user->shop_id, $param->amount); + $deposit = $this->logic->charge($param->shopId, $param->amount); $res = [ "shop_id" => $deposit->shop_id, diff --git a/app/Http/Controllers/Web/Shop/DepositChargeParam.php b/app/Http/Controllers/Web/Shop/DepositChargeParam.php index b2f3497..79e2a5d 100644 --- a/app/Http/Controllers/Web/Shop/DepositChargeParam.php +++ b/app/Http/Controllers/Web/Shop/DepositChargeParam.php @@ -15,7 +15,7 @@ class DepositChargeParam extends BaseParam { return [ Deposit::COL_NAME_SHOP_ID => $this->str(), - Deposit::COL_NAME_DEPOSIT => $this->numeric(), + "amount" => $this->numeric(), ]; } } diff --git a/app/Http/Controllers/Web/Shop/ShopConfigController.php b/app/Http/Controllers/Web/Shop/ShopConfigController.php new file mode 100644 index 0000000..67f9c8c --- /dev/null +++ b/app/Http/Controllers/Web/Shop/ShopConfigController.php @@ -0,0 +1,49 @@ +param; + + $shop = Shop::findOrFail($param->shopId); + + $shop->qr_service_expire_min = $param->qrServiceExpireMin; + $shop->under_amount_when_create = $param->underAmountWhenCreate; + $shop->under_amount_when_auth = $param->underAmountWhenAuth; + $shop->under_amount_when_use = $param->underAmountWhenUse; + + $shop->save(); + + $res = [ + "shop_id" => $shop->id, + ]; + + return $this->successResponse($res); + } +} diff --git a/app/Http/Controllers/Web/Shop/ShopConfigParam.php b/app/Http/Controllers/Web/Shop/ShopConfigParam.php new file mode 100644 index 0000000..5fbe60e --- /dev/null +++ b/app/Http/Controllers/Web/Shop/ShopConfigParam.php @@ -0,0 +1,28 @@ + $this->str(), + Shop::COL_NAME_QR_SERVICE_EXPIRE_MIN => $this->numeric(["between:10,10080"]), + Shop::COL_NAME_UNDER_AMOUNT_WHEN_CREATE => $this->numeric(["between:-100000,100000"]), + Shop::COL_NAME_UNDER_AMOUNT_WHEN_AUTH => $this->numeric(["between:-100000,100000"]), + Shop::COL_NAME_UNDER_AMOUNT_WHEN_USE => $this->numeric(["between:-100000,100000"]), + ]; + } +} diff --git a/app/Http/Controllers/Web/Shop/ShopListParam.php b/app/Http/Controllers/Web/Shop/ShopListParam.php index e855bd7..209cb00 100644 --- a/app/Http/Controllers/Web/Shop/ShopListParam.php +++ b/app/Http/Controllers/Web/Shop/ShopListParam.php @@ -2,8 +2,22 @@ namespace App\Http\Controllers\Web\Shop; -use App\Http\Controllers\Web\NoneParams; +use App\Http\Controllers\Web\BaseParam; +use App\Http\Controllers\Web\SortableParam; +use App\Repositories\ShopRepository; -class ShopListParam extends NoneParams +/** + * @property string shopId + * @property string name + */ +class ShopListParam extends BaseParam implements SortableParam { + public function rules(): array + { + return [ + ShopRepository::CONDITION_SHOP_ID => $this->str(true), + ShopRepository::CONDITION_NAME => $this->str(true), + ...$this->sortableRules(), + ]; + } } diff --git a/app/Http/Controllers/Web/Shop/ShopRegisterController.php b/app/Http/Controllers/Web/Shop/ShopRegisterController.php index b323fe0..4df13dd 100644 --- a/app/Http/Controllers/Web/Shop/ShopRegisterController.php +++ b/app/Http/Controllers/Web/Shop/ShopRegisterController.php @@ -41,6 +41,7 @@ class ShopRegisterController extends WebController $shop = new Shop(); $shop->fill($param->toArray()); + $this->logic->create($user, $shop); $res = [ diff --git a/app/Http/Controllers/Web/WebController.php b/app/Http/Controllers/Web/WebController.php index d4f7a9c..441d430 100644 --- a/app/Http/Controllers/Web/WebController.php +++ b/app/Http/Controllers/Web/WebController.php @@ -173,6 +173,8 @@ abstract class WebController extends BaseController $this->validated = $validator->validated(); $this->getParam()->setData($this->validated); + $this->sessionUser->init(); + $this->transaction->beginTransaction(); $ret = $this->run($request); diff --git a/app/Http/Middleware/RoleMiddleware.php b/app/Http/Middleware/RoleMiddleware.php index b74e413..028c70c 100644 --- a/app/Http/Middleware/RoleMiddleware.php +++ b/app/Http/Middleware/RoleMiddleware.php @@ -13,6 +13,7 @@ class RoleMiddleware public function __construct(private SessionUser $sessionUser) { + $sessionUser->init(); } /** * Handle an incoming request. @@ -23,13 +24,15 @@ class RoleMiddleware { $allowRoles = []; - foreach (explode(",", $rolesStr) as $roleSrt) { + foreach (explode("-", $rolesStr) as $roleSrt) { $role = UserRole::from($roleSrt); $allowRoles[] = $role; } + $currentRole = $this->sessionUser->user()->role; - if (in_array($this->sessionUser->user()->role, $allowRoles, true) === false) { + if (in_array($currentRole, $allowRoles, true) === false) { + $path = $request->path(); abort(403); } diff --git a/app/Logics/QRService/ChargeLogic.php b/app/Logics/QRService/ChargeLogic.php index f4b9c61..ab2a41f 100644 --- a/app/Logics/QRService/ChargeLogic.php +++ b/app/Logics/QRService/ChargeLogic.php @@ -20,7 +20,10 @@ class ChargeLogic $deposit->deposit += $amount; - self::makeTransferHistory($shopId, $amount); + $history = self::makeTransferHistory($shopId, $amount); + + $deposit->save(); + $history->save(); return $deposit; } diff --git a/app/Logics/Shop/ShopLogic.php b/app/Logics/Shop/ShopLogic.php index 7f92823..32879e6 100644 --- a/app/Logics/Shop/ShopLogic.php +++ b/app/Logics/Shop/ShopLogic.php @@ -24,7 +24,7 @@ class ShopLogic // デポジット $deposit = new Deposit(); - $deposit->shop_id = $shop->idl; + $deposit->shop_id = $shop->id; $deposit->save(); return $shop; diff --git a/app/Models/Htpms/MstCustomer.php b/app/Models/Htpms/MstCustomer.php index 5d65e1b..b0f4b66 100644 --- a/app/Models/Htpms/MstCustomer.php +++ b/app/Models/Htpms/MstCustomer.php @@ -3,6 +3,8 @@ namespace App\Models\Htpms; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Query\Builder; +use Illuminate\Support\Facades\DB; class MstCustomer extends Model { @@ -19,4 +21,15 @@ class MstCustomer extends Model self::COL_NAME_CUSTOMER_ID, self::COL_NAME_CUSTOMER_NAME, ]; + + public static function getBuilder(string $name = 'main'): Builder + { + $instance = new static(); + return DB::connection($instance->getConnectionName())->table(static::getTableName(), $name); + } + + public static function getTableName(): string + { + return (new static)->getTable(); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 55d9624..2d53f60 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -14,13 +14,7 @@ class AppServiceProvider extends ServiceProvider { // 2019_12_14_000001_create_personal_access_tokens_tableテーブルを作らないようにする Sanctum::ignoreMigrations(); - } - /** - * Bootstrap any application services. - */ - public function boot(): void - { // DB $this->app->singleton(\App\Util\DBUtil::class); @@ -32,4 +26,11 @@ class AppServiceProvider extends ServiceProvider // セッション情報 $this->app->singleton(\App\Sessions\SessionUser::class); } + + /** + * Bootstrap any application services. + */ + public function boot(): void + { + } } diff --git a/app/Repositories/LoginUserRepository.php b/app/Repositories/LoginUserRepository.php index 7f12473..eca6754 100644 --- a/app/Repositories/LoginUserRepository.php +++ b/app/Repositories/LoginUserRepository.php @@ -5,6 +5,7 @@ namespace App\Repositories; use App\Models\Htpms\MstCustomer; use App\Models\User; use App\Repositories\BaseRepository; +use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; diff --git a/app/Repositories/ShopRepository.php b/app/Repositories/ShopRepository.php index 1e00acf..5b775a4 100644 --- a/app/Repositories/ShopRepository.php +++ b/app/Repositories/ShopRepository.php @@ -6,6 +6,7 @@ use App\Models\HtpmsCustomer\Deposit\Deposit; use App\Models\HtpmsCustomer\Mst\Shop; use App\Models\User; use App\Repositories\BaseRepository; +use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; @@ -68,9 +69,14 @@ class ShopRepository extends BaseRepository $shop = static::TABLE_SHOP; $deposit = static::TABLE_DEPOSIT; $columns = [ + $this->makeColumnNameForSelect([$shop, Shop::COL_NAME_NAME]), + $this->makeColumnNameForSelect([$shop, Shop::COL_NAME_MEMO]), + $this->makeColumnNameForSelect([$shop, Shop::COL_NAME_QR_SERVICE_EXPIRE_MIN]), + $this->makeColumnNameForSelect([$shop, Shop::COL_NAME_UNDER_AMOUNT_WHEN_AUTH]), + $this->makeColumnNameForSelect([$shop, Shop::COL_NAME_UNDER_AMOUNT_WHEN_CREATE]), + $this->makeColumnNameForSelect([$shop, Shop::COL_NAME_UNDER_AMOUNT_WHEN_USE]), $this->makeColumnNameForSelect([$deposit, Deposit::COL_NAME_SHOP_ID]), $this->makeColumnNameForSelect([$deposit, Deposit::COL_NAME_DEPOSIT]), - $this->makeColumnNameForSelect([$shop, User::COL_NAME_NAME]), ]; return $columns; diff --git a/app/Sessions/SessionUser.php b/app/Sessions/SessionUser.php index ba1434f..fe877bc 100644 --- a/app/Sessions/SessionUser.php +++ b/app/Sessions/SessionUser.php @@ -7,8 +7,8 @@ use App\Exceptions\AppCommonException; use App\Features\InstanceAble; use App\Models\HtpmsCustomer\HtpmsCustomerConnectionSwitch; use App\Models\User; +use Auth; use Illuminate\Auth\AuthenticationException; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use LogicException; @@ -20,11 +20,23 @@ class SessionUser private User|null $user; private bool $isSwtiched = false; + private bool $isInit = false; - public function __construct() + public function init() + { + if ($this->isInit === true) { + return; + } + $this->initSessionUser(); + $this->isInit = true; + } + private function initSessionUser() { // 認証していない場合はスキップ $this->user = Auth::user(); + + $user = Auth::user(); + $ckeck = Auth::check(); if ($this->user === null) { return; } diff --git a/app/Util/RouteHelper.php b/app/Util/RouteHelper.php index 2da683d..da7591f 100644 --- a/app/Util/RouteHelper.php +++ b/app/Util/RouteHelper.php @@ -55,7 +55,7 @@ class RouteHelper foreach ($roles as $role) { $rolesStrArr[] = $role->value; } - - return "role:" . implode(",", $rolesStrArr); + $ret = "role:" . implode("-", $rolesStrArr); + return $ret; } } diff --git a/lang/ja/validation.php b/lang/ja/validation.php index 9ba9f01..57a8d7a 100644 --- a/lang/ja/validation.php +++ b/lang/ja/validation.php @@ -187,6 +187,7 @@ return array_merge([ 'before_or_equal' => '日付の前後関係が正しくありません', 'between' => [ 'string' => ':min から :max 文字入力してください', + 'numeric' => ':min から :max を入力してください', ], 'date' => '日付を入力してください', 'email' => 'Emailの形式が正しくありません', diff --git a/resources/views/index.html b/resources/views/index.html new file mode 100644 index 0000000..ea32efb --- /dev/null +++ b/resources/views/index.html @@ -0,0 +1 @@ +debug test \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 316b018..a73c1f8 100644 --- a/routes/api.php +++ b/routes/api.php @@ -16,37 +16,41 @@ use Illuminate\Support\Facades\Route; */ RouteHelper::post('/login', App\Http\Controllers\Web\Auth\LoginController::class); -RouteHelper::get('/me', App\Http\Controllers\Web\Auth\MeController::class); RouteHelper::get('/logout', App\Http\Controllers\Web\Auth\LogoutController::class); +RouteHelper::get('/me', App\Http\Controllers\Web\Auth\MeController::class); RouteHelper::get('/qr-service/get-ticket', App\Http\Controllers\Web\QRService\CreateTicketController::class); Route::middleware('auth:sanctum')->group(function () { + // 共通ルート + // 管理者ルート Route::middleware(RouteHelper::role([UserRole::ADMIN]))->group(function () { + RouteHelper::post('/role/switch/customer', App\Http\Controllers\Web\Auth\SwitchCustomerController::class); RouteHelper::get('/customer/list', App\Http\Controllers\Web\Customer\CustomerListController::class); RouteHelper::get('/login-user/customer/list', App\Http\Controllers\Web\LoginUser\CustomerListController::class); - RouteHelper::get('/login-user/customer/register', App\Http\Controllers\Web\LoginUser\CustomerRegisterController::class); - RouteHelper::get('/login-user/shop/register', App\Http\Controllers\Web\LoginUser\CustomerRegisterController::class); + RouteHelper::post('/login-user/customer/register', App\Http\Controllers\Web\LoginUser\CustomerRegisterController::class); + RouteHelper::post('/login-user/shop/register', App\Http\Controllers\Web\LoginUser\CustomerRegisterController::class); }); // 運営会社ルート Route::middleware(RouteHelper::role([UserRole::CUSTOMER]))->group(function () { + RouteHelper::post('/role/switch/shop', App\Http\Controllers\Web\Auth\SwitcShophController::class); RouteHelper::post('/login-user/shop/register', App\Http\Controllers\Web\LoginUser\ShopRegisterController::class); RouteHelper::post('/shop/register', App\Http\Controllers\Web\Shop\ShopRegisterController::class); - RouteHelper::post('/shop/list', App\Http\Controllers\Web\Shop\ShopListController::class); + RouteHelper::get('/shop/list', App\Http\Controllers\Web\Shop\ShopListController::class); + RouteHelper::post('/shop/deposit/charge', App\Http\Controllers\Web\Shop\DepositChargeController::class); + RouteHelper::post('/shop/config', App\Http\Controllers\Web\Shop\ShopConfigController::class); }); // 店舗ルート Route::middleware(RouteHelper::role([UserRole::SHOP]))->group(function () { RouteHelper::get('/shop/deposit', App\Http\Controllers\Web\Shop\MyDepositController::class); - RouteHelper::post('/shop/deposit/charge', App\Http\Controllers\Web\Shop\DepositChargeController::class); }); - // 管理者と運営会社ルート + // 管理者運営会社ルート Route::middleware(RouteHelper::role([UserRole::ADMIN, UserRole::CUSTOMER]))->group(function () { - RouteHelper::post('/role/switch', App\Http\Controllers\Web\Auth\SwitchController::class); RouteHelper::get('/role/switch/end', App\Http\Controllers\Web\Auth\SwitchEndController::class); }); });