我想知道是否有更简单的方法来实现这个规则。刚开始尝试使用firestore。
match /emails/{emailId} {
allow write: if request.resource.data.attachments.size() == 0
|| request.resource.data.attachments.size() == 1 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel')
|| request.resource.data.attachments.size() == 2 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[1].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[1].filetype == 'image/png' || request.resource.data.attachments[1].filetype == 'image/jpg' || request.resource.data.attachments[1].filetype == 'application/vnd.ms-excel')
|| request.resource.data.attachments.size() == 3 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[1].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[1].filetype == 'image/png' || request.resource.data.attachments[1].filetype == 'image/jpg' || request.resource.data.attachments[1].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[2].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[2].filetype == 'image/png' || request.resource.data.attachments[2].filetype == 'image/jpg' || request.resource.data.attachments[2].filetype == 'application/vnd.ms-excel')
|| request.resource.data.attachments.size() == 4 && request.resource.data.attachments[0].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[0].filetype == 'image/png' || request.resource.data.attachments[0].filetype == 'image/jpg' || request.resource.data.attachments[0].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[1].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[1].filetype == 'image/png' || request.resource.data.attachments[1].filetype == 'image/jpg' || request.resource.data.attachments[1].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[2].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[2].filetype == 'image/png' || request.resource.data.attachments[2].filetype == 'image/jpg' || request.resource.data.attachments[2].filetype == 'application/vnd.ms-excel') && request.resource.data.attachments[3].fileSize < 3 * 1024 * 1024 && (request.resource.data.attachments[3].filetype == 'image/png' || request.resource.data.attachments[3].filetype == 'image/jpg' || request.resource.data.attachments[3].filetype == 'application/vnd.ms-excel');
}
答案 0 :(得分:6)
我们不允许在规则中使用循环结构,因为我们依赖于几种更复杂结构更难的优化技术。我们也不会为规则执行收取计算时间,这意味着我们不希望它们过于复杂且可能具有滥用性。
随着规则的重复,我强烈建议使用function()功能来简化。例如,这是&lt; 1/3大小:
match /emails/{emailId} {
function attachments (){
return request.resource.data.attachments();
}
function attach_cnt () {
return attachments().size();
}
function valid_size(attach) {
return attachments()[attach].fileSize < 3 * 1024 * 1024;
}
function valid_type(attach) {
return (attachments()[attach].filetype == 'image/png'
|| attachments()[attach].filetype == 'image/jpg'
|| attachments()[attach].filetype == 'application/vnd.ms-excel');
}
allow write: (attach_cnt() < 1 || (valid_size(0) && valid_type(0)))
&& (attach_cnt() < 2 || (valid_size(1) && valid_type(1)))
&& (attach_cnt() < 3 || (valid_size(2) && valid_type(2)))
&& (attach_cnt() < 4 || (valid_size(3) && valid_type(3)))
}
以下是我如何简化它(值得仔细检查,因为我可能输入错误)。
attachments
,因为它使用了一个聚合 - 这使得很容易浏览规则。attach_cnt
,因为经过了大量检查。valid_size
为该测试创建了一个函数attach
。valid_type
,其工作方式相同,但检查确保它是有效类型。