【问题标题】:avoid of changing roles on user profile edit in laravel避免在 laravel 中更改用户配置文件编辑的角色
【发布时间】:2017-12-24 02:11:55
【问题描述】:

在我的项目中,我拥有角色和权限,并且我为我的用户创建了一个控制器,以便他们可以编辑他们的个人资料,因为 usercontroller 仅限于管理员角色。

现在我的问题是,当用户更新他的个人资料时,他的角色访问权限将被分离。 我应该怎么做才能让用户能够在不更改角色的情况下编辑自己的个人资料?

这是我的更新函数:

public function update(Request $request, $id)
    {
      $user = User::findOrFail($id); //Get role specified by id

  //Validate name, email and password fields
      $this->validate($request, [
          'name'=>'required|max:120',
          'username'=>'required|max:120',
          'email'=>'required|email|unique:users,email,'.$id,
          'password' => 'nullable|string|min:6|confirmed'
      ]);
      $input = $request->only(['name', 'username', 'email']); //Retreive the name, email and password fields
      if (trim(Input::get('password')) != '') {
        $user->password = Hash::make(trim(Input::get('password')));
      }

      $user->fill($input)->save();
      return view('users.profile')
          ->with('flash_message',
           'Your Profile successfully edited.');
    }

用户模型:

protected $fillable = [
        'name', 'username', 'email', 'password',  'affiliate_id', 'referred_by',
    ];

数据库:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->string('referred_by')->nullable();
            $table->string('affiliate_id')->unique();
            $table->rememberToken();
            $table->timestamps();
        });
    }

【问题讨论】:

  • when user update his profile his role access will be detach 是什么意思?
  • 角色是如何附加到用户的?它是用户模型上的属性吗?连接表?
  • @tompec 这意味着当我的用户在保存时编辑他们的用户名等信息时,他们将失去他们的角色,他们在数据库中的角色列变为空
  • @Cryode 我更新了我的问题,我在数据库一中使用中间件和表作为角色一的权限。
  • 您能否从会话 db 中检索他们现有的用户值,将其添加到 $input 然后重新保存?这是你调用fill的$user对象,为什么不直接在用户对象上操作,或者使用fill之外的东西

标签: php laravel


【解决方案1】:

您可以在编辑视图中传递用户的角色,如果是管理员,您将显示角色复选框或您使用的任何内容,如果不是管理员,则不会显示角色。

public function edit(Request $request, $id)
{   
    $user_role = User::findOrFail($id)->roles->toArray();
    return view('UserCRUD.edit')->with('user_role', $user_role);
}

并且在视图中

@if($user_role == 'admin')
    <label>
        <input type="checkbox" name="role[]" value="value">
    </label>
@endif

如果您显示角色,那么只有它们会在 $request-&gt;all() 数组中
如果我 dd($request-&gt;all()) 没有在视图中显示角色

array:8 [▼
    "_method" => "PATCH"
    "_token" => "8xxRjgKaJNlJOwZZ2vZYecs6GrEWjpYqbCtWULJO"
    "username" => "testcase1"
    "email" => "test1@test.com"
    "password" => ""
    "confirm_password" => ""
    "firstname" => "Test"
    "lastname" => "One"
]

如果我在显示角色时dd($request-&gt;all())

array:9 [▼
    "_method" => "PATCH"
    "_token" => "8xxRjgKaJNlJOwZZ2vZYecs6GrEWjpYqbCtWULJO"
    "username" => "testcase1"
    "email" => "test1@test.com"
    "password" => ""
    "confirm_password" => ""
    "firstname" => "Test"
    "lastname" => "One"
    "role" => array:1 [▼
        0 => "2"
    ]
]

更新时,您只需检查是否设置了角色,否则不更新。

$user = User::findOrFail($id);
$user->update($request->all());
if (isset($request->all()['role'])) {
    $user->roles()->sync($request->all()['role']);
}

【讨论】:

    猜你喜欢
    • 2018-03-26
    • 2018-05-06
    • 2018-05-09
    • 2017-08-10
    • 2013-03-25
    • 2011-05-01
    • 1970-01-01
    • 2021-08-22
    • 2022-01-07
    相关资源
    最近更新 更多