基本表是 Users。
另外建了两张表:Profiles 和 Groups。Profiles 存储用户资料,Groups 用来管理权限。
Profiles 表和 users 表是一一对应关系,在 profiles 表内建立一个 foreign key。
1 |
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
然后在 Model 中定义二者关系:
1 2 3 |
public function profiles(){ return $this->hasOne('App\Profile'); } |
1 2 3 |
public function users(){ return $this->belongsTo('App\User'); } |
注册时,会自动在 profiles 表中建立一条记录,默认分组为 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
protected function create(array $data) { $user=User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']) ]); $profile=new Profile(['nickname'=>$data['name'],'group_id'=>'1']); $user->profiles()->save($profile); return $user; } |
为了保证安全性,user_id 为不可 mass assign 的字段:
1 |
protected $fillable = ['nickname', 'photo', 'phone', 'role', 'aboutme']; |
Groups 中暂时建了 6 组权限:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public function run() { DB::table('groups')->insert([ 'group_name'=>'Regsitered User', 'group_class'=>'1' ]); DB::table('groups')->insert([ 'group_name'=>'Data Point Owner', 'group_class'=>'3' ]); DB::table('groups')->insert([ 'group_name'=>'DA Team Lead/Manager', 'group_class'=>'5' ]); DB::table('groups')->insert([ 'group_name'=>'QA Analyst', 'group_class'=>'7' ]); DB::table('groups')->insert([ 'group_name'=>'QA Team Lead/Manager', 'group_class'=>'9' ]); DB::table('groups')->insert([ 'group_name'=>'Administrator', 'group_class'=>'10' ]); } |
然后会开始做一个后台的页面,用来设置用户信息。前台就是再加一个对用户组的判断,去掉部分不可修改的设置。