Firebase安全规则,禁用特定节点上的写入

时间:2018-03-08 17:29:11

标签: firebase firebase-realtime-database firebase-security-rules

我的火力架结构看起来像这样:

<input type="submit" value="Submit" class="button">

(价值并不重要......)

我想允许读取和写入每个节点, execpt 指南节点,

所以我尝试了以下规则:

{
"post": {
    "uid": {
        "text": "Name";
    }
},
"games": {
    "id": {
        "title": "buttons",
        "text": "(user id string)"
    },
    "id": {
        "title": "navbars",
        "text": "(id string)"
    }
},
"guides": {
    "1": {
        "title": "guide",
        "text": "unwriteable string"
    }
}
}

但是。不幸的是,因为父亲&#39;津贴,firebase不关心指南特定规则,

知道如何实现我的目标吗?

1 个答案:

答案 0 :(得分:3)

Bacause firebase security rules cascade你不能说某人有权在任何地方写作,后来说但不是在这里。

所以在你的情况下,你必须为你的其他路径添加规则:

private AudioManager audioManager;

private int currentRingerVolume;
private int currentNotificationVolume;
private int currentAudioVolume;

// stream audio just to be sure if some SIP app uses Audio for ringer in stead of Ringer

private boolean silenceMode = false;
private boolean appStart = true;

public void silence() {

    if (!silenceMode) {
        currentRingerVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
        currentNotificationVolume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        currentAudioVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

        silenceMode = true;
    }
}

public void oldVolume() {

    if (silenceMode) {

        audioManager.setStreamVolume(AudioManager.STREAM_RING, currentRingerVolume, 0);
        audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, currentNotificationVolume, 0);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentAudioVolume, 0);

        silenceMode = false;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (appStart) {
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        appStart = false;
    }
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
                silence();
                Toast.makeText(context, "Silence on AC", Toast.LENGTH_SHORT).show();
            } else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
                silence();
                Toast.makeText(context, "Silence on USB", Toast.LENGTH_SHORT).show();
            } else if (plugged == 0) {
                oldVolume();
                Toast.makeText(context, "Old Volume on Battery", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
            }

        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(receiver, filter);


}

正如Kato所说,这也可以通过以下规则来完成:

{
  "rules": {
    ".read": "auth == null",
      "guides": {
        ".write": false
      },
      "games": {
        ".write": "auth == null"
      },
      "post": {
        ".write": "auth == null"
      }
  }
}

第一个示例允许您只在{ "rules": { ".read": "auth == null", ".write": "auth == null && !newData.hasChild('guides')" } } games节点中写入,而第二个示例允许您在除post节点之外的任何地方写入。