Firestore规则 - 动态路径

时间:2018-06-15 07:14:28

标签: javascript firebase firebase-authentication google-cloud-firestore

我正在尝试在Firestore的规则中创建动态连接。

我有一个用户集合和一个角色集合,users集合中的每个用户都有一个'string'字段,该字段与firebase为roles集合中相应角色生成的ID相匹配。

为了实现这一点,我试图动态获取ID,然后从角色中检索适当的数据。

我的代码如下:

**index.php**



     <?php include("dbConfig.php");
    error_reporting(0);
     ?>
    <?php 
    session_start();
    $companyname=$_SESSION['companyName'];
    $tablename=$_SESSION['tableName'];
    $ids = $_SESSION["user_id"];
    $name=$_GET['name'];
    $id = $_GET["id"];


       if($_SESSION["user_id"]=="")
     {
      header("Location:index.php");
     }
     ?>


     <link href="css/lib/sweetalert/sweetalert.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


    <?php include("links.php");?>



    <?php include("header.php");?>

            <!-- End header header -->
            <!-- Left Sidebar  -->
            <?php include("sidebar.php"); ?>
            <!-- End Left Sidebar  -->
            <!-- Page wrapper  -->
            <div class="page-wrapper">
                <!-- Bread crumb -->
                <div class="row page-titles">
                    <div class="col-md-5 align-self-center">
                        <h3 class="headtext">Select CSV</h3> </div>
                    <div class="col-md-7 align-self-center">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item active">Select CSV</li>
                        </ol>
                    </div>
                </div>
                <!-- End Bread crumb -->
                <!-- Container fluid  -->
                <div class="container-fluid">
                    <!-- Start Page Content -->
        <?php
        $sqllink=mysqli_query($conn,"SELECT * FROM `Table_Test_CSV_ramanora` WHERE `CompanyName`='".$companyname."'");
     $blink=mysqli_fetch_array($sqllink);
     $idlink = $blink['id'];
        ?>



                    <div class="row">
    <?php $select = mysqli_query($conn,"SELECT u.Id, u.Name, u.Mobile, u.email, u.Organization, u.Designation, u.CompanyKey, ac.CompanyId, ac.Facility, ac.CompanyKey
    FROM ProductDetails u LEFT JOIN CompanyKeys ac ON ac.CompanyKey = u.CompanyKey WHERE CompanyId ='$companyname' ");

            while($row = mysqli_fetch_array($select)){
            // $editid = $row['u.Id'];

            ?>
    <?php  

    $select=mysqli_query($conn,"SELECT * FROM  `csvname` WHERE  `CompanyName` ='".$companyname."' ORDER BY  `csvname`.`Id` DESC ");
    while($rownew = mysqli_fetch_array($select)){

    ?>
                        <div class="col-lg-4">
                            <div class="card">
                                <div class="card-body">

                                    <h4 class="card-title text-center"><?php echo $rownew['CsvName'];   ?></h4>

                                    <div class="card-content">
                                        <div class="toastr m-t-15">
                                            <div class="text-left text-center">
                                                 <a href="assigncallr.php?id=<?php echo $id;?>&&key=<?php echo $name;?>&&name=<?php echo $rownew['CsvName'];?>"><button type="button" class="btn btn-success m-b-10 m-l-5"><i class="fa fa-eye"></i> View CSV</button></a>

                                                <button class="btn btn-danger btn btn m-b-10" id="<?php echo $rownew['CsvName'];?>" data-id="<?php echo $rownew['CsvName'];?>row" href="javascript:void(0)">Delete CSV<i class="fa fa-trash"></i> </button>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- /# card -->
                        </div>
    <?php } ?>
    <?php } ?>
                        <!-- /# column -->
                      </div>

                    <!-- End PAge Content -->
                </div>
                <!-- End Container fluid  -->


    <?php  

    $selectdatarow=mysqli_query($conn,"SELECT * FROM  `csvname` WHERE  `CompanyName` ='".$companyname."' ORDER BY  `csvname`.`Id` DESC ");
    while($rownewdatarow = mysqli_fetch_array($selectdatarow)){

    ?>

    <script>
    $(document).ready(function(){

      readProducts(); /* it will load products when document loads */

      $(document).on('click', '#<?php echo $rownewdatarow['CsvName'];?>', function(e){

       var productId = $(this).data('id');
       SwalDelete(productId);
       e.preventDefault();
      });

     });

     function SwalDelete(productId){

      swal({
       title: 'Are you sure?',
       text: "You won't be able to revert this!",
       type: 'warning',
       showCancelButton: true,
       confirmButtonColor: '#3085d6',
       cancelButtonColor: '#d33',
       confirmButtonText: 'Yes, delete it!',
       showLoaderOnConfirm: true,

       preConfirm: function() {
         return new Promise(function(resolve) {

            $.ajax({
            url: 'delete.php',
            type: 'POST',
               data: 'delete='+productId,
               dataType: 'json'
            })
            .done(function(response){
             swal('Deleted!', response.message, response.status);
         readProducts();
            })
            .fail(function(){
             swal('Oops...', 'Something went wrong with ajax !', 'error');
            });
         });
          },
       allowOutsideClick: false     
      }); 

     }

     function readProducts(){
      $('#load-products').load('read.php'); 
     }

    </script>
    <?php } ?>
    <script src="js/lib/sweetalert/sweetalert.min.js"></script>
        <!-- scripit init-->

    <?php include("footer.php"); ?> 

这不起作用的一个潜在原因是get()函数是异步的,不要等待前一个函数完成,因此它不能使用它的数据。

非常感谢任何帮助。打开解决方法,数据结构更改等......

1 个答案:

答案 0 :(得分:0)

对您的示例进行了测试,它对我有用

service cloud.firestore {
    match /databases/{database}/documents {

        function getRole() {
            return get(/databases/$(database)/documents/test-user/$(request.auth.uid)).data.role;
        }

        function getPermissions(role) {
            return get(/databases/$(database)/documents/test-role/$(role)).data.rights;
        }

        match /test-user/{userid} {
            allow read: if true; 
            allow write: if getPermissions(getRole()) == 'all';
        }
    }
}

我的测试集的内容是

/test-user/0fludECgWKg88Gn5SjNYPc3Opmx2 (id of my test user) has: 
    {"role": "tr"}

/test-role/tr has: 
    {"rights": "all"}

如果/test-role/tr不存在或rights没有值all,则写入请求被拒绝

在您的情况下,也许函数getPermissions的类型是字符串,并且测试需要布尔值。