[認証]全ユーザの強制ログアウト機能を作りました。 - #2222
Merged
Merged
Conversation
授業などで使用している際、強制的に全ユーザをログアウトさせたいときがあります。 管理画面で全ユーザの強制ログアウトをさせることで、ログイン済みのセッションを使用しているユーザも次の画面操作で強制的にログアウトします。 ただし、ログイン中の自分のみは強制ログアウトされないようにします。 Issuesは以下です。 #2221
Contributor
Author
masaton0216
reviewed
Jul 3, 2025
masaton0216
left a comment
Contributor
There was a problem hiding this comment.
ご対応ありがとうございます。
いくつか気になった点をコメントしました。
現状でも十分に動作はしますが、一度、ご確認頂き、ご参考ください。
Comment on lines
+98
to
+109
| // return $this->laravelLogin($request); | ||
|
|
||
| // ログインが成功したら、一旦戻り値を保持しておき、強制ログアウトフラグをクリアする。 | ||
| $login_return = $this->laravelLogin($request); | ||
| if (Auth::check() && Auth::user()->is_force_logout) { | ||
| // 強制ログアウトフラグを処理済みの 0 に戻す | ||
| $user = Auth::user(); | ||
| $user->is_force_logout = 0; | ||
| $user->save(); | ||
| } | ||
| return $login_return; | ||
|
|
Contributor
There was a problem hiding this comment.
ログアウトフラグを処理済みに戻す処理はミドルウェア側にもあるのですが、ログインコントローラにも書いてるのは理由があるのでしょうか?
(ミドルウェアだけあれば事足りるのでは?と捉えています。)
Contributor
Author
There was a problem hiding this comment.
- 一度、強制ログアウトのフラグが立つ。
- そのあとでログインすると、ログイン直後のミドルウェアの動きで、強制ログアウトされてしまう。
- ここでフラグがOFF
という動きをしちゃうんですよ。
Comment on lines
+155
to
+166
| // return $this->laravelLogin($request); | ||
|
|
||
| // ログインが成功したら、一旦戻り値を保持しておき、強制ログアウトフラグをクリアする。 | ||
| $login_return = $this->laravelLogin($request); | ||
| if (Auth::check() && Auth::user()->is_force_logout) { | ||
| // 強制ログアウトフラグを処理済みの 0 に戻す | ||
| $user = Auth::user(); | ||
| $user->is_force_logout = 0; | ||
| $user->save(); | ||
| } | ||
| return $login_return; | ||
|
|
Contributor
There was a problem hiding this comment.
ログアウトフラグを処理済みに戻す処理はミドルウェア側にもあるのですが、ログインコントローラにも書いてるのは理由があるのでしょうか?
(ミドルウェアだけあれば事足りるのでは?と捉えています。)
| public function forceLogoutSubmit($request, $id = null) | ||
| { | ||
| // ユーザデータの更新 | ||
| User::where('id', '!=', Auth::user()->id)->update(['is_force_logout' => 1]); |
Contributor
There was a problem hiding this comment.
管理者複数で運用されているようなサイトがあったとして、他の管理者までログアウトさせなくてもいいのではないかと思いましたが、どうでしょう?例えば下記のようなクエリです。
// ユーザデータの更新(自分以外かつadmin_userロールを持たないユーザーのみ)
User::where('id', '!=', Auth::user()->id)
->whereNotExists(function ($query) {
$query->select('id')
->from('users_roles')
->whereRaw('users_roles.users_id = users.id')
->where('role_name', 'admin_user')
->where('role_value', 1);
})
->update(['is_force_logout' => 1]);
| { | ||
| // ユーザデータの更新 | ||
| User::where('id', '!=', Auth::user()->id)->update(['is_force_logout' => 1]); | ||
|
|
Contributor
There was a problem hiding this comment.
記録用のログを入れてみてはいかがでしょうか?
use Illuminate\Support\Facades\Log;
~略~
Log::info('Force logout executed by admin user ID: ' . Auth::user()->id);
masaton0216
approved these changes
Jul 3, 2025
masaton0216
left a comment
Contributor
There was a problem hiding this comment.
ご対応ありがとうございます。
マージさせて頂きます。
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


概要
授業などで使用している際、強制的に全ユーザをログアウトさせたいときがあります。
管理画面で全ユーザの強制ログアウトをさせることで、ログイン済みのセッションを使用しているユーザも次の画面操作で強制的にログアウトします。 ただし、ログイン中の自分のみは強制ログアウトされないようにします。
関連Pull requests/Issues
Issuesは以下です。
#2221
DB変更の有無
有り
チェックリスト