Is Firestore Security Rule .hasAny(['A', 'B', 'C']) still available for use?

此生再无相见时 提交于 2020-05-26 08:04:31

问题


Can't seemed to use Firestore Security Rule .hasAny(). Is this method deprecated or no longer available?

I am using it like

/// Functions - NOT WORKING!!! ///
function isTeamMember(teamId, userId) {
  return get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId].hasAny(['OWNER', 'ADMIN', 'STANDARD'])
}

The Firestore data is structured as such

- team_access_privilege (COLLECTION)
-- teamId (DOCUMENT)
--- userId1 (KEY) : 'OWNER' (VALUE)
--- userId2 (KEY) : 'ADMIN' (VALUE)
--- userId3 (KEY) : 'ADMIN' (VALUE)
--- userId4 (KEY) : 'STANDARD' (VALUE)

There are 4 users in this team.

I also tested that the below functions and they work well.

/// Functions - Working Well ///
    function isOwnerOfTeam(teamId, userId) {
      return get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId] == 'OWNER'
    }

My goal is to check that as long as userId exists in team_access_privilege he/she can access the data. I tried using exists() but the simulator takes forever to run. I am giving up on using exists(). I am not sure if it is a known issue.

Below this takes forever to run

/// Functions - Not working too ///
    function isTeamMember(teamId, userId) {
      return exists(get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId])
    }

All in all, my goal is to check that the user is a 'OWNER', 'ADMIN', or 'STANDARD' of the team to read / write data.


Edit 1

Tried @Doug Stevenson's answer, and it does NOT work

['OWNER', 'ADMIN', 'STANDARD'].hasAny([get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId]])

This is the error message.

I tried another approach, and it worked.

/// Functions - Working Well ///
    function isOwnerOfTeam(teamId, userId) {
      return get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId] == 'OWNER'
    }

/// Functions - Working Well ///
    function isAdminOfTeam(teamId, userId) {
      return get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId] == 'ADMIN'
    }

/// Functions - Working Well ///
    function isStandardOfTeam(teamId, userId) {
      return get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId] == 'STANDARD'
    }

/// Functions - Then This WOrks ///
    function isTeamMember(teamId, userId) {
      return isOwnerOfTeam(teamId, userId) || isAdminOfTeam(teamId, userId) || isStandardOfTeam(teamId, userId)
    }

['', '', ''].hasAny( get() ) does not work. So is get().hasAny(['', '', '']). Exists does not work as well for my said data structure.

This is just puzzling. Anyway I have a working security rule for now. Hope @Doug Stevenson could help figure out a more efficient way to check that the user is a 'OWNER', 'ADMIN', or 'STANDARD' of the team to read / write data.


回答1:


hasAny is a method on array types, as documented here in the rules reference. You use it to determine if a list type field contains any of the values in the given list. It seems that you're trying to use it on a string.

Perhaps you can use it in the opposite direction to figure out if the string role of the user id is one of the three other roles given:

['OWNER', 'ADMIN', 'STANDARD'].hasAny([get(/databases/$(database)/documents/team_access_privilege/$(teamId)).data[userId]])


来源:https://stackoverflow.com/questions/51073887/is-firestore-security-rule-hasanya-b-c-still-available-for-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!