dousonghs58612 2017-03-20 18:09
浏览 46
已采纳

什么是在Laravel 5中存储用户角色权限的最佳方式? [关闭]

I'm building a web app in Laravel 5.2. I'm relatively new to Laravel, and want to follow best practices. I have a table called Roles that have several named user roles (i.e.: admin, editor, etc). I want the admin to be able to edit the permissions for these roles and create new ones. What would be the best way to store the permissions?

  1. Use an integer field with each bit representing one permission? (I think this would quickly get out of hand)
  2. Use a pivot table and a many-many connection to permissions?
  3. Use a string field and just serialize the chosen privileges?

New privileges are likely to be added in the future, and my goal is to be able to easily determine if a user has a certain role or not. I.e.: $user->roles->hasAdmin() or something simirar.

  • 写回答

1条回答 默认 最新

  • doujiu8918 2017-03-20 18:16
    关注

    You may want to borrow best practices for role/permissions table from the Laravel Entrust package:

        // Create table for storing roles
        Schema::create('{{ $rolesTable }}', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });
    
        // Create table for associating roles to users (Many-to-Many)
        Schema::create('{{ $roleUserTable }}', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();
    
            $table->foreign('user_id')->references('{{ $userKeyName }}')->on('{{ $usersTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('{{ $rolesTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
    
            $table->primary(['user_id', 'role_id']);
        });
    
        // Create table for storing permissions
        Schema::create('{{ $permissionsTable }}', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });
    
        // Create table for associating permissions to roles (Many-to-Many)
        Schema::create('{{ $permissionRoleTable }}', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
    
            $table->foreign('permission_id')->references('id')->on('{{ $permissionsTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('{{ $rolesTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
    
            $table->primary(['permission_id', 'role_id']);
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿