欢迎各位兄弟 发布技术文章
这里的技术是共享的
Please sign in or create an account to participate in this conversation.
来自 https://laracasts.com/discuss/channels/general-discussion/prevent-update-empty-password
Auth users can edit their profile and also their password. Although the two password fields can remain blank. In this case the mysql password field should not be updated.
This is my Update method in de UserController:
public function update($userId, UserRequest $request){
$user = User::findOrFail($userId);
if ($request->has('password')){
$this->validate($request, [
'password' => 'required|confirmed|min:6',
]);
$request->request->set('password',bcrypt($request->password));
$request = $request->all();
}
else{
$request = $request->except(['password']);
}
$user->update($request);
Is this considered secure?
And Is this good practice, could the code be cleaner? And how should refactor this? just add private methods or repo pattern?
Thanks in advance!
Just do $request->all()
and in the rules add sometimes
to the password fields. This will allow Laravel to validate the password fields if they're set (in the request to update) but also allow it to pass if it's not present.
来自 https://laracasts.com/discuss/channels/code-review/update-user-allow-blank-password?page=1
Where is the right place to put the check of an empty value? In the model, or in the controller? In my model I have this mutator to hash the password before save it in the database:
public function setPasswordAttribute($password)
{
return $this->attributes['password'] = bcrypt($password);
}
But I need to do this check in the controller to prevent update password with an empty value:
if (trim($request->password) != '') {
$user->password = $request->password;
}
Where is the right place to put this check according to SOLID principles?
Please sign in or create an account to participate in this conversation.
来自 https://laracasts.com/discuss/channels/general-discussion/prevent-update-empty-password
Prevent update empty password
Where is the right place to put the check of an empty value? In the model, or in the controller? In my model I have this mutator to hash the password before save it in the database:
public function setPasswordAttribute($password) { return $this->attributes['password'] = bcrypt($password); }
But I need to do this check in the controller to prevent update password with an empty value:
if (trim($request->password) != '') { $user->password = $request->password; }
Where is the right place to put this check according to SOLID principles?