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

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

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?


person cerealex    schedule 12.08.2016    source источник


Ответы (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)
    }
}
person cerealex    schedule 12.08.2016

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

// 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
                        )
                    )
    }
  }
}
person choopage - Jek Bao    schedule 02.11.2018