donglinxia1541 2016-06-13 01:52 采纳率: 0%
浏览 18

如何确保不会操纵API URL中传递的参数

I have a API url as:

api/classes/{id}

and I have a Class Controller and a show() function defined inside it as below:

public function show($id)
{
    $classes = $this->classesService->findById($id);
    if ($classes) {
        return response()->json(['message' => 'Success', 'success' => true, 'status' => 200, 'data' => $classes]);
    } else {
        return response()->json(['message' => 'Not found', 'success' => false, 'status' => 404, 'data' => null]);
    }
}

Now the question is, if a user is limited to see only in the class he is registered, which I am getting the class_id from the user.info in the front end and pass it to the API call URL, but I was wondering if user may call this api and change the class_id then he may be able to see every class, how to make sure he can only access his class id he belongs to?

My next idea was to initialize the class id from back-end like this:

    public function show($id)
    {
        $classes = $this->classesService->findById(Auth::user()->student->team->class_id);
        if ($classes) {
            return response()->json(['message' => 'Success', 'success' => true, 'status' => 200, 'data' => $classes]);
        } else {
            return response()->json(['message' => 'Not found', 'success' => false, 'status' => 404, 'data' => null]);
        }
    }

but, this is making me problem if I wanna later check each class info from and admin perspective, as admins can see every class details... so this way admin has no class id and it shows null...

Any idea how to achieve such thing that a user can see only his class and admin can see all classes

Will this require two different API call, two functions?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗