如果没有做数据控制,A 登录后携带 A 的 token 执行删除的接口localhost/api/payments/4,则会删除 B 的,所以需要对destory方法做数据控制
# 1 删除前鉴权处理
public function destory($id){
$payment = Payment::find($id);
if ($payment->user_id != $this->currentUser->id) {
return ...
}
$payment->delete();
}
# 2 参入id查询删除
public function destory($id){
Payment::whereUserId($this->currentUser->id)->whereId($id)->delete();
}
# 3 模型关联查询
class User extends Model{
public function payments()
{
return $this->hasMany('App\Payment');
}
}
class PaymentController extends Controller{
public function destory($id)
{
$this->currentUser->payments()->whereId($id)->delete();
}
}
class UserController extends Controller{
public function check($data)
{
if (checkEmail($data['email'], $data['code'])) {
return true;
}
...
}
public function findPassword()
{
$user = User::whereEmail($data['email'])->first();
$user->password = bcrypt($data['new_password']);
$user->save();
}
}
正确:在 findPassword 里面再次验证完成邮箱校验的账户是否为当前找回密码的账号
class UserController extends Controller{
public function check($data)
{
if (checkEmail($data['email'], $data['code'])) {
return true;
}
...
}
public function findPassword($data)
{
if (checkEmail($data['email'], $data['code'])) {
$user = User::whereEmail($data['email'])->first();
$user->password = $data['new_password'];
$user->save();
}
...
}
}
限制分页条目范围,防止恶意请求
如获文章列表的接口localhost/api/articles
public function index($params){
$pageId = $params['pageid'] ?? PAGE_ID; //页码
$pageSize = $params['pagesize'] ?? 15; //条码
$articles = Article::where(function ($query) use ($params) {
...
})->take($pageSize)->skip($pageId * $pageSize)->orderby('id', 'desc')->get();
...
...
}