Default Permission Policy
By default, all API endpoints require authentication (configured inconfig/settings.py:167-182):
Built-in Permission Classes
The API uses Django REST Framework’s built-in permission classes:IsAuthenticated
Purpose: Requires user to be authenticated Behavior:- Denies access to unauthenticated users
- Allows access to any authenticated user
- Used as the default for most endpoints
AllowAny
Purpose: Allows unrestricted access Behavior:- Allows access to both authenticated and unauthenticated users
- Used for public endpoints like registration and token generation
POST /api/v1/customers/- Customer registration (seecustomers/views.py:30)POST /auth/token/- Token generationPOST /auth/token/refresh/- Token refresh
Custom Permission Classes
The API implements custom permission classes for resource-specific access control.CustomerOnly
Location:customers/permissions.py:4-7
Purpose: Ensures customers can only access their own data
Implementation:
- Checks object-level permissions
- Returns
Trueonly if the authenticated user matches the object - Used for customer profile operations (GET, PUT, DELETE)
CustomerInstanceView(seecustomers/views.py:53-73)
Allowed: Customer accessing own profile
Allowed: Customer accessing own profile
Denied: Customer accessing another's profile
Denied: Customer accessing another's profile
VendorOnly
Location:stores/permissions.py:4-8
Purpose: Restricts access to vendor-owned resources
Implementation:
- Requires user to be authenticated
- Requires user to have
is_vendor=Trueflag - Requires user to be the owner of the object
- Used for store management operations
- User must be authenticated
- User must have vendor status (
customer.is_vendor == True) - User must own the resource (
request.user == obj.owner)
VendorCreateOnly
Location:stores/permissions.py:11-14
Purpose: Allows only vendors to create certain resources
Implementation:
- Checks view-level permissions (not object-level)
- Requires user to be authenticated and have vendor status
- Used for creating stores or vendor-specific resources
Permission Evaluation Flow
Object-Level Permissions
has_object_permission() methods are checked (e.g., CustomerOnly, VendorOnly)Customer Model Permissions
TheCustomer model includes fields that affect permissions:
is_vendor Flag
Field:customers.models.Customer.is_vendor (see customers/models.py:45)
Type: BooleanField(default=False)
Purpose: Determines if a customer has vendor privileges
Usage:
- Controls access to vendor endpoints
- Required for
VendorOnlyandVendorCreateOnlypermissions - Separate from Django’s built-in staff/superuser flags
is_active Flag
Field: Inherited fromAbstractUser
Purpose: Indicates if the account is active
Behavior:
- Inactive accounts cannot authenticate
- When a customer is “deleted”,
is_activeis set toFalse(seecustomers/views.py:70-73):
Account deletion is a soft delete - the account is deactivated rather than removed from the database.
Combining Permissions
You can require multiple permissions by passing a list:True for access to be granted.
Common Permission Patterns
Public Endpoint
Authenticated Users Only
Resource Owner Only
Vendor-Only Access
Error Responses
401 Unauthorized
Returned when authentication credentials are missing or invalid:- Missing Authorization header
- Invalid or expired JWT token
- Malformed token format
403 Forbidden
Returned when user is authenticated but lacks permission:- Non-vendor trying to access vendor endpoints
- Customer trying to access another customer’s data
- User lacking required flags (
is_vendor,is_active, etc.)
Testing Permissions
Test as Anonymous User
Test as Authenticated User
Test as Vendor
Best Practices
Use Specific Permissions
Use Specific Permissions
Don’t rely solely on
IsAuthenticated. Use custom permissions like CustomerOnly or VendorOnly to enforce proper access control.Check Permissions Early
Check Permissions Early
The
CustomerInstanceView checks permissions in dispatch() to fail fast:Handle Permission Errors Gracefully
Handle Permission Errors Gracefully
Document Permission Requirements
Document Permission Requirements
Always document which permissions are required for each endpoint in your API documentation.
Related Topics
Authentication Overview
Learn about the JWT authentication system
Registration
Create customer accounts with proper permissions