ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Firebase Realtime DB Rules
    Android 2021. 9. 29. 11:42

    Rule Types

    • .read - 사용자가 데이터를 읽을 수 있도록 하는 규칙을 정의한다.
    • .write - 사용자가 데이터를 쓸 수 있도록 하는 규칙을 정의한다.
    • .validate - 데이터가 어떤 형태를 가져야 하는지, 자식 속성을 가지는지, 데이터 타입 등을 정의한다.
    • .indexOn - 정렬 및 쿼리를 위해 색인화할 하위 항목을 지정한다.

     

    Firebase Realtime DB Template

    1) No Security

    이 규칙은 어느 앱에서 누구나 DB에 접근할 수 있도록 한다.

     

    개발 단계에서는 간편할 수 있지만, 이 수준의 규칙은 누구나 접근할 수 있기 때문에 앱을 런칭하기 전에는 수정이 필요하다.

    // No Security
    {
     “rules”: {
     “.read”: true,
     “.write”: true
     }

     

    2)Full Security

    이 규칙은 DB 구축시 기본 규칙으로 설정되어 있다. 누구도 이 DB에 접근할 수 없도록 한다.

    Firebase console을 통해서만 접근할 수 있다.

    // Full security
    {
     “rules”: {
     “.read”: false,
     “.write”: false
     }
    }

     

    3) Only authenticated users can access/write data

    앱이나 다른 경로를 통해 Authenticate가 된 사용자만 데이터에 접근할 수 있도록 설정한다.

    // Only authenticated users can access/write data
    {
     “rules”: {
     “.read”: “auth != null”,
     “.write”: “auth != null”
     }
    }

     

    4) User Authentication from a particular domain

    Authenticate가 된 사용자 중에서 특정 도메인(user@google.com, user@naver.com..)을 사용하는 사용자만 접근할 수 있도록 한다.

    // Only authenticated users from a particular domain (example.com) can access/write data
    {
     “rules”: {
     “.read”: “auth.token.email.endsWith(‘@example.com’)”,
     “.write”: “auth.token.email.endsWith(‘@example.com’)”
     }
    }

     

    5) User Data Only

    Authenticate가 된 사용자 중에서 그 사용자에게 할당된 데이터만 사용하도록 설정한다.

    // These rules grant access to a node matching the authenticated
    // user's ID from the Firebase auth token
    {
      "rules": {
        "users": {
          "$uid": {
            ".read": "$uid === auth.uid",
            ".write": "$uid === auth.uid"
          }
        }
      }
    }

    $uid는 users 데이터 속성의 자식 속성을 지칭하는 와일드 카드다

     

    예를 들어 DB가 다음과 같다면

    fir-projectname-readdb-example{
    users{	
    	2meisuUS2jVgw4ZkEIg5xmftA7h1{
    		Email: "femail"
    		Name: "fname"
    		Phone: "fphone"
    		Year: "fyear"
    	}
        }
    }

    여기서 $uid는 2meisuUS2jVgw4ZkEIg5xmftA7h1를 가리킨다. 즉 위 규칙을 사용하려면 사용 전에 미리 uid를 가지는 DB를 구축해야 한다.

     

    6) Validates user is moderator from different database location

    사용자가 다른 DB 의 Moderator인지 확인한다.

    // Validates user is moderator from different database location
    {
     “rules”: {
       “posts”: {
          “$uid”: {
            “.write”: “root.child(‘users’).child(‘moderator’).val() === true”
          }
        }
      }
    }

     

    7) Validates string datatype and length range

    새로 저장되는 데이터의 타입과 길이를 확인한다.

    // Validates string datatype and length range
    {
     “rules”: {
       “posts”: {
          “$uid”: {
            “.validate”: “newData.isString()  && newData.val().length > 0
                && newData.val().length <= 140”
            }
         }
       }
    }

     

    8) Checks presence of child attributes

    새로 저장되는 하위 속성의 존재를 확인한다.

    // Checks presence of child attributes
    {
     “rules”: {
       “posts”: {
         “$uid”: {
           “.validate”: “newData.hasChildren([‘username’, ‘timestamp’])”
          }
        }
      }
    }

     

    9) Validates timestamp is not future value

    새로운 데이터의 timestamp를 확인한다.

    // Validates timestamp is not a future value
    {
     “rules”: {
       “posts”: {
         “$uid”: {
           “timestamp”: { 
             “.validate”: “newData.val() <= now” 
             }
           }
         }
       }
    }

     

    10) Prevents Delete or Update

    데이터 삭제와 갱신을 방지한다.

    //Prevents Delete or Update
    {
     “rules”: {
       “posts”: {
         “$uid”: {
           “.write”: “!data.exists()”
          }
        }
      }
    }

     

    11) Prevents only Delete

    데이터 삭제를 방지한다.

    // Prevents only Delete
    {
     “rules”: {
       “posts”: {
          “$uid”: {
            “.write”: “newData.exists()”
           }
         }
       }
    }

     

    12) Prevents only Update

    데이터 갱신을 방지한다.

    // Prevents only Update
    {
     “rules”: {
       “posts”: {
         “$uid”: {
           “.write”: “!data.exists() || !newData.exists()”
           }
         }
       }
    }

     

     

    13) Prevents Create and Delete

    데이터 추가와 삭제를 방지한다.

    // Prevents Create and Delete
    {
     “rules”: {
       “posts”: {
         “$uid”: {
           “.write”: “data.exists() && newData.exists()”
          }
        }
      }
    }

     

     

     

     

    Reference: https://medium.com/@juliomacr/10-firebase-realtime-database-rule-templates-d4894a118a98

    'Android' 카테고리의 다른 글

    What is Context in Android  (0) 2021.09.27
Designed by Tistory.