英文:
Could a complicated premium role function in Firebase Security Rules affect performance?
问题
我正在努力工作,试图集中和避免代码重复,以防止规则文件混乱。然而,我对这个函数的性能有一些担忧,以及这种实现是否被视为反模式。我还不确定所使用的方法是否得到 Firebase 规则引擎的支持,关于为什么有很多角色,是因为每个角色将来都将代表一个层次。
以下是我的函数代码:
function isPremium(requestData) {
// 试用期到期计算
let currentTime = requestData.time.toMillis();
let threeDaysAgo = currentTime - (3 * 24 * 60 * 60 * 1000);
let createdAt = requestData.auth.token.createdAt.toMillis();
// 角色
let stripeRole = requestData.auth.token.stripeRole;
// 所有用户的可用高级角色。
let premiumUserRoles = ['premium-user', 'premium-user-50', 'premium-user-100'];
let premiumSuperUserRoles = ['premium-super-user', 'premium-super-user-50', 'premium-super-user-100'];
let allPremiumRoles = premiumUserRoles.concat(premiumSuperUserRoles);
return (
premiumRoles.includes(stripeRole) ||
(stripeRole === 'trial' && createdAt >= threeDaysAgo)
);
}
希望这对你有所帮助。
英文:
I am working on the following function to centralize and avoid code duplication, in order to prevent clutter in the rules file. However, I have some concerns regarding the performance of this function and whether this implementation could be considered an anti-pattern. I'm also unsure about the methods used and whether they are supported by the Firebase rules engine, regarding why there is many roles its due to the fact that each one will later on represent a tier.
below is my function code :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function isPremium(requestData) {
// trial expiry calclation
let currentTime = requestData.time.toMillis();
let threeDaysAgo = currentTime - (3 * 24 * 60 * 60 * 1000);
let createdAt = requestData.auth.token.createdAt.toMillis();
// Role
let stripeRole = requestData.auth.token.stripeRole;
// available primium for all users.
let premiumUserRoles = ['premium-user', 'premium-user-50', 'premium-user-100'];
let premiumSuperUserRoles = ['premium-super-user', 'premium-super-user-50', 'premium-super-user-100'];
let allPremiumRoles = premiumUserRoles.concat(premiumSuperUserRoles);
return (
premiumRoles.includes(stripeRole) ||
(stripeRole === 'trial' && createdAt >= threeDaysAgo)
);
}
<!-- end snippet -->
答案1
得分: 1
没有已知的安全规则性能问题,对于任何允许的复杂性都没有。它们在能力上是故意受到限制的,原因正是如此。
英文:
There are no known performance problems with security rules for any allowed complexity. They are intentionally limited in capability precisely for this reason.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论