身份验证重定向和安全性做对了吗?

Authentication Redirection and Security done right?

我正在构建一个应用程序,其中除了常规 public 页面之外,还有一个登录页面和每个 3 个角色级别的 3 个仪表板版本。

Table

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');

        $table->string('firstname');
        $table->string('surname');
        $table->string('username');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('role');
        $table->rememberToken();

        $table->datetime('created_at');
        $table->datetime('updated_at');
    });

因此我还有另一个 table 角色:

Schema::create('roles', function (Blueprint $table) {

        $table->increments('id');
        $table->string('role_type');

    });

关系

Users::hasOne('roles');
Roles::hasMany('users');

主要是关于登录功能,我想检查用户role_type,然后根据角色重定向他们。

我想知道这个 table 结构和想法是否被正确考虑。 此外,当用户访问特定路由时,如果没有用户,我将使该路由显示登录,如果用户已登录,则显示 404/503 但与特定 role_type.

不匹配

谢谢大家

在您的用户 table 而不是您拥有的 role 列,应该是这两行:

$table->integer('role_id')->unsigned()->nullable();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('set null');

缺点是它允许用户的 'role_id' 列为空值。它需要这个的原因是为了处理如果分配给用户的角色被删除会发生什么。

但更好的解决方案是使用多对多关系,我个人建议使用 Entrust,因为它内置了您想要的内容,以及您以后可能需要的更多选项。