انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهJNR-040-L03

فحوص الملكية (Ownership checks)

  • الدرس 3 من 6
  • Junior
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

في هذا الدرس

make sure users reach only their own data.

الموضوعات المخطط لها

  • Take the user's identity from the authenticated principal, never from the request body or a parameter.
  • Option 1: load then compare order.getCustomerId() with the current user.
  • Option 2 (preferred): put ownership in the query: findByIdAndCustomerId(id, customerId).
  • When it does not belong to you, return 404, not 403, so you do not reveal that it exists.
  • Admins may bypass ownership through an explicit, tested path.
تفاصيل المخطط المبدئي

عرض عملي

java
Order order = orders.findByIdAndCustomerId(id, currentUser.id())
    .orElseThrow(() -> new OrderNotFoundException(id));

اختبر فهمك

Why return 404 instead of 403 for another customer's order?

أظهر الإجابة

403 would confirm the order exists.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

المختبر والتمارين الاختبار

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.