Skip to content

[認証]全ユーザの強制ログアウト機能を作りました。 - #2222

Merged
masaton0216 merged 3 commits into
masterfrom
forceLogout
Jul 3, 2025
Merged

[認証]全ユーザの強制ログアウト機能を作りました。#2222
masaton0216 merged 3 commits into
masterfrom
forceLogout

Conversation

@nagasheep

Copy link
Copy Markdown
Contributor

概要

授業などで使用している際、強制的に全ユーザをログアウトさせたいときがあります。
管理画面で全ユーザの強制ログアウトをさせることで、ログイン済みのセッションを使用しているユーザも次の画面操作で強制的にログアウトします。 ただし、ログイン中の自分のみは強制ログアウトされないようにします。

関連Pull requests/Issues

Issuesは以下です。
#2221

DB変更の有無

有り

チェックリスト

授業などで使用している際、強制的に全ユーザをログアウトさせたいときがあります。
管理画面で全ユーザの強制ログアウトをさせることで、ログイン済みのセッションを使用しているユーザも次の画面操作で強制的にログアウトします。
ただし、ログイン中の自分のみは強制ログアウトされないようにします。

Issuesは以下です。
#2221
@nagasheep

Copy link
Copy Markdown
Contributor Author

強制ログアウトはユーザ管理の強制ログアウト画面で指示します。
image

@masaton0216 masaton0216 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご対応ありがとうございます。
いくつか気になった点をコメントしました。
現状でも十分に動作はしますが、一度、ご確認頂き、ご参考ください。

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ログアウトフラグを処理済みに戻す処理はミドルウェア側にもあるのですが、ログインコントローラにも書いてるのは理由があるのでしょうか?
(ミドルウェアだけあれば事足りるのでは?と捉えています。)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 一度、強制ログアウトのフラグが立つ。
  • そのあとでログインすると、ログイン直後のミドルウェアの動きで、強制ログアウトされてしまう。
  • ここでフラグが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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ログアウトフラグを処理済みに戻す処理はミドルウェア側にもあるのですが、ログインコントローラにも書いてるのは理由があるのでしょうか?
(ミドルウェアだけあれば事足りるのでは?と捉えています。)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

理由は上記と同じですね。

public function forceLogoutSubmit($request, $id = null)
{
// ユーザデータの更新
User::where('id', '!=', Auth::user()->id)->update(['is_force_logout' => 1]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

管理者複数で運用されているようなサイトがあったとして、他の管理者までログアウトさせなくてもいいのではないかと思いましたが、どうでしょう?例えば下記のようなクエリです。

        // ユーザデータの更新(自分以外かつ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]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに。
使わせていただきます。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

画面のメッセージも合わせます。
image

{
// ユーザデータの更新
User::where('id', '!=', Auth::user()->id)->update(['is_force_logout' => 1]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

記録用のログを入れてみてはいかがでしょうか?

use Illuminate\Support\Facades\Log;
~略~
Log::info('Force logout executed by admin user ID: ' . Auth::user()->id);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

素晴らしい!

@masaton0216 masaton0216 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご対応ありがとうございます。
マージさせて頂きます。

@masaton0216
masaton0216 merged commit 3497c54 into master Jul 3, 2025
@masaton0216
masaton0216 deleted the forceLogout branch July 3, 2025 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants