firebase - 如果用户不发送,我可以创建日期属性吗?

标签 firebase firebase-security firebase-realtime-database

我使用 Firebase Bolt 编译器创建了该规则。

// ####### Root

path / {
  read() = true;
  write() = false;
}

// ######## USERS PHOTOS
// Allow anyone to read the list of Photos.
path /users_photos {
  read() = true;
}

// All individual Photos are writable by anyone.
path /users_photos/$id is Photos {
  write() = isSignedIn();
}

type Photos {
  image: String,
  user_id: String,
  removed: Boolean,
  dt_created: InitialTimestamp,
  dt_updated: CurrentTimestamp
}


type CurrentTimestamp extends Number {
    validate() = this == now;
}

type InitialTimestamp extends Number {
  validate() = initial(this, now);
}


//
// Helper Functions
//
isSignedIn() = auth != null;
// Returns true if the value is intialized to init, or retains it's prior
// value, otherwise.
initial(value, init) = value == (prior(value) == null ? init : prior(value));

引用号:https://github.com/firebase/bolt/blob/master/docs/guide.md

我的脚本:

/*Upload*/
VigiApp.controller('UploadController', ['$scope', 'Upload', '$timeout', 'FirebaseURL', function ($scope, Upload, $timeout, FirebaseURL) {
    // upload on file select or drop
    $scope.upload = function (file, id) {
        $('.page-spinner-bar').removeClass('ng-hide hide').addClass('ng-show show');

        id = typeof id !== 'undefined' ? id : null;
        Upload.base64DataUrl(file).then(function(base64){
            //auth
            var fbAuth = FirebaseURL.getAuth();
            //Ref
            var usersPhotosRef = FirebaseURL.child("users_photos");
            usersPhotosRef.push({'image': base64,'removed': true, 'user_id': fbAuth.uid}, function(error){
                if (error) {
                    alert('Error: Something went wrong when creating your post please try again');
                } else {
                  var newID = usersPhotosRef.key();
                  if(id !== null){
                      $('#'+id).css("background-image", "url('"+base64+"')");
                      $('#'+id).css("background-size", "100% 100%");
                  }
                }   

                $('.page-spinner-bar').removeClass('ng-show show').addClass('ng-hide hide');
            });
        });
    }     
}]);

编译...

>firebase-bolt mmgv-vigiapp.bolt -o rules.json
bolt: Generating rules.json...

并部署...

>firebase deploy:rules

=== Deploying to 'vivid-heat-2144'...

i  deploying rules

+  Deploy complete!

Dashboard: https://vivid-heat-2144.firebaseio.com

但我收到错误:

FIREBASE WARNING: set at /users_photos/-K5VL1m04oF8s2xp8oTf failed: permission_denied 

创建的规则:

{
  "rules": {
    ".read": "true",
    "users_photos": {
      ".read": "true",
      "$id": {
        ".validate": "newData.hasChildren(['image', 'user_id', 'removed', 'dt_created', 'dt_updated'])",
        "image": {
          ".validate": "newData.isString()"
        },
        "user_id": {
          ".validate": "newData.isString()"
        },
        "removed": {
          ".validate": "newData.isBoolean()"
        },
        "dt_created": {
          ".validate": "newData.isNumber() && newData.val() == (data.val() == null ? now : data.val())"
        },
        "dt_updated": {
          ".validate": "newData.isNumber() && newData.val() == now"
        },
        "$other": {
          ".validate": "false"
        },
        ".write": "auth != null"
      }
    }
  }
}

当我删除日期时,它就起作用了。

...
type Photos {
  image: String,
  user_id: String,
  removed: Boolean,
}
...

如何生成创建日期和更新? 请问我哪里错了?

最佳答案

当您添加照片时,您传递以下信息:

usersPhotosRef.push({'image': base64,'removed': true, 'user_id': fbAuth.uid}

您的安全规则需要以下属性:

".validate": "newData.hasChildren(['image', 'user_id', 'removed', 'dt_created', 'dt_updated'])",

dt_createddt_updated 没有神奇的“默认值”,因此您需要从应用程序代码中传递这些值:

usersPhotosRef.push({
  'image': base64,
  'removed': true, 
  'user_id': fbAuth.uid,
  'dt_created': Firebase.ServerValue.TIMESTAMP,
  'dt_updated': Firebase.ServerValue.TIMESTAMP
}

由于此代码段要添加新记录,因此 dt_createddt_updated 设置为相同的值。当您更新记录时,您只需设置dt_updated

关于firebase - 如果用户不发送,我可以创建日期属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266879/

相关文章:

javascript - 使用firebase函数将图像上传到云存储错误

Swift Firebase 如何停止进程?

swift - 使用规则与前端在 firebase 数据库中进行数据验证有什么区别?

json - Firebase 错误 : Data requested exceeds the maximum size that can be accessed with a single request

ios - 如何在 Firebase 中检索自动生成的 ID/ key ?

javascript - Firebase 电子邮件/密码创建用户安全性

ios - Firebase 安全规则 - Auth 生成的 UID 是否应该保密?

javascript - Firebase 安全问题/身份验证

javascript - Firebase 数据库安全规则允许匿名访问和 Web 访问

swift - 将 Firebase 中的数据保存在编号列表中