我正在用 laravel 创建一个学校管理系统软件,这个项目恰好是我的第一个大项目,用于商业用途。没有人监督这项工作(所以你看我在这里真的需要建议)。所以我遇到的问题是何时信任用户输入以及何时在另一个表中使用模型之前检查模型是否存在。例如,在将 student_id 插入“attendances”表之前检查“users”表中是否存在“student_id”。这个问题也适用于我想在其他表中持久化模型的每隔一段时间。作为说明,我拿出了一部分用于让学生参加的代码。HTML<form method="POST" action="{{route('daily.attendance')}}"> @csrf <input type="hidden" name="section_id" value="{{$section_id}}"> <input type="hidden" name="semester_id" value="{{$semester_id}}">@foreach ($sections as $section) @foreach ($section->users as $student) <input type="checkbox" name = "present[]" value = "{{$student->id}}" class = "present"> <input type="hidden" name = "students[]" value = "{{$student->id}}"> @endforeach @endforeach<button type="submit" class="btn btn-primary" >{{ __('take attendance') }}</button> </form>因此,即使我为 $section_id 和 $semester_id 使用了隐藏输入,我知道用户(管理员)可以通过使用他的浏览器控制台更改这些值,然后发送表单。想象一下管理员有意或无意(显示for的路由是这样的"daily/student/section/{section_id}') 将 $section_id 或 $semester_id 更改为与sections 表和Semesters 表中的任何模型都不对应的值。繁荣!!!他或她(管理员)参加了一个甚至不存在的部分或学期,这并不好,因为它破坏了数据库的完整性。所以问题是我应该信任管理员还是应该在插入“出席”表之前检查相应表中是否存在 $section_id 和 $semester_id(这样做也会增加脚本结束的时间)。还查看 php 代码,student_id 值也可以从 html 中被篡改,并且您会看到代码循环通过标记为在场的学生和标记为缺席的学生的数组,我是否还应该检查 $student_id 是否存在于“用户”中
1 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
规则总是说永远不要信任用户,特别是在谈到数据完整性时。我建议尝试 laravel 的验证方法: https ://laravel.com/docs/6.x/validation#rule-exists
但是,如果性能是您首先关心的问题,我会在首次请求出勤页面时将 userIds 和 sectionIds 缓存在 redis 哈希中,然后检查例如 student_id 是否在缓存的 user_ids 中如果您想查看 Laravel 的缓存: https ://laravel.com/docs/5.7/cache#retrieving-items-from-the-cache
- 1 回答
- 0 关注
- 179 浏览
添加回答
举报
0/150
提交
取消
