dtslobe4694 2014-01-02 08:13
浏览 275
已采纳

Laravel中的User :: create()安全吗?

I'm trying to make my Laravel UserController as lean as possible without using any additional packages, like Ardent (I find it unnecessary; overkill.)

This is my postRegister() function, the function that is routed to when the user clicks "submit" on the registration form.

public function postRegister() {
    $validator = new Services\Validators\RUser;
    if ($validator->passes()) {
        User::create(Input::all());
        return Redirect::to('login');
    }
    return Redirect::to('register')->withInput()->withErrors($validator->getErrors());
}

I sent all the input off to be validated in another class, then I just call the class statically with User::create(). I'm wondering if this is safe or not. I'm having everything but the id and password_confirmation mass-assigned, and the password is hashed in a mutator function.

If this isn't a safe way to handle user creation, how else should I do it? Should I instead create an instance of the object and manually assign values? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duai8153 2014-01-02 09:18
    关注

    Basically, the Model::Create function does these steps:

    1. Instantiate a new model instance
    2. Using the input array, fill in all unguarded/fillable model attributes using set-mutators if available
    3. Save model to DB
    4. Return model

    So as long as you add the fields that you don't want to be mass-assigned to your $guarded array (or excluded them from your $fillable array) there shouldn't be any security risks. The functionality is about the same as building the model gradually and then saving it.

    As a recommendation: The input validation should happen within the model during either the "saving" or "creating" event. If you return false during one of those events, you'll halt the model creation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计