Правила публикации Firebase Storage Post применяются к правилам удаления

Это мои правила, применяемые к img dir:

match /img {
  match /{fileId} {
    allow read, 
          write: if request.resource.contentType.matches('image/jpeg')
                 || request.resource.contentType.matches('image/png')
                 || request.resource.contentType.matches('image/gif')
                 && request.resource.size < 2 * 1024 * 1024
    }
  }
}

Проблема в том, что эти правила также применяются к delete(), так как это тоже метод записи, поэтому он всегда возвращает ошибку разрешения. Я не смог найти ничего в документации по этому поводу. Как я могу отказаться от правил POST/PUT и DELETE?

2 ответа

Решение

Нашел решение сам. Позволяя правилу применяться, когда ресурс вообще не отправляется (удалять), оно также получает разрешение на запись. Остальная часть кода создания / обновления отправляется в выражение ИЛИ.

match /img {
    match /{fileId} {
        allow read, 
        write: if request.resource == null || 
            (request.resource.contentType.matches('image/jpeg')
            || request.resource.contentType.matches('image/png')
            || request.resource.contentType.matches('image/gif')
            && request.resource.size < 2 * 1024 * 1024)
    }
}

Это для тех, кто хочет конкретного пользователя создавать и удалять.

// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/{bucket}/o {
     // Allow write files to the path "images/*", subject to the constraints:
     // 1) File is less than 10MB
     // 2) Content type is an image or Content type is null for delete operation
    match /user/{userId}/images/{allPaths=**} {
        allow read: if resource.size < 10 * 1024 * 1024
                    && request.auth != null;
        allow write: if request.auth.uid == userId
                    && (
                        request.resource == null 
                        || 
                        (
                        request.resource.contentType.matches('image/.*')
                        && request.resource.size < 10 * 1024 * 1024
                        )
                    )
    }
  }
}
Другие вопросы по тегам