首页 > 解决方案 > 2个复选框之间的链接问题,不应该插入数据

问题描述

好的,我有一个问题。让我解释一下。

我需要列出一些输入(复选框)中的所有模式,然后我们可以选择我们想要操作的模式,以赋予特定用户特权。

无论如何,我得到了这个,正如你所看到的:

<div id="list-schemas">
    <?php
        foreach ($schemas as $elt) {
        echo '<input type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
        }
    ?>
</div>

然后,我还需要放置一些具有权限的复选框,我这样做了:

<div id="div-privileges">
    <?php
        foreach ($schemas as $elt) {
            echo '<div class="list">';
            echo '<label for="list">' . $elt->getSchema() . ' :</label><br />';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="REVOKE"/> REVOKE ? <br />';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="ALL"/> ALL PRIVILEGES ? <br />';
            echo '<hr>';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="SELECT"/> SELECT ? <br />';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="INSERT"/> INSERT ? <br />';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="UPDATE"/> UPDATE ?  <br />';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="DELETE"/> DELETE ?  <br />';
            echo '<hr>';
            echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="CREATE"/>CREATE ?  <br />';
            echo '</div>';
        }
    ?>
</div>

看起来像这样:https ://image.noelshack.com/fichiers/2019/37/3/1568191628-capture2.png

所以,这就是说,这是我在 UserManager.class.php 中的函数更新:

public static function update(User $newPerso){

    $db = DbConnect::getDb();
    $newLogin= pg_escape_string($newPerso->getLogin());
    $arraySchemas=$newPerso->getSchemas();
    $arrayPrivileges=$newPerso->getPrivileges();

    if (isset($arraySchemas)){

        foreach($arrayPrivileges as $schema => $privileges){

            if (isset($arrayPrivileges)){

                foreach($privileges as $privilege){

                    if($privilege=="REVOKE"){
                        pg_query("{$privilege} ALL ON ALL TABLES IN SCHEMA {$schema} FROM {$newLogin};");
                    }

                    else if($privilege=="CREATE"){
                        pg_query("GRANT {$privilege} ON SCHEMA {$schema} TO {$newLogin};");
                    }

                    else if($privilege=="ALL" || $privilege=="INSERT" || $privilege=="SELECT" || $privilege=="UPDATE" || $privilege=="DELETE"){
                        pg_query("GRANT {$privilege} ON ALL TABLES IN SCHEMA {$schema} TO {$newLogin};");
                    }
                }
            }
        }
    }
}

事实是,当我这样做时:https ://image.noelshack.com/fichiers/2019/37/3/1568193038-capture3.png

它有效,用户将在“公共”上使用 SELECT + INSERT 并在“schematest”上使用 UPDATE + DELETE

但,

当我这样做时:https ://image.noelshack.com/fichiers/2019/37/3/1568186255-capture.png

我的用户将拥有(在该示例中):

这两种模式中的所有特权...

它适用于“公共”是正常的,但我如何才能防止“schematest”..?

标签: phppostgresqlmultidimensional-arraycheckbox

解决方案


伙计们,我添加了一个“&& in_array($schema, $arraySchemas)”

它现在似乎有效,让我告诉你:

public static function update(User $newPerso){

    $db = DbConnect::getDb();
    $newLogin= pg_escape_string($newPerso->getLogin());
    $arraySchemas=$newPerso->getSchemas();
    $arrayPrivileges=$newPerso->getPrivileges();

    if (isset($arraySchemas)){

        foreach($arrayPrivileges as $schema => $privileges){

            if (isset($arrayPrivileges) && in_array($schema, $arraySchemas)){ /****<- RIGHT HERE****/

                foreach($privileges as $privilege){

                    if($privilege=="REVOKE"){
                        pg_query("{$privilege} ALL ON ALL TABLES IN SCHEMA {$schema} FROM {$newLogin};");
                    }

                    else if($privilege=="CREATE"){
                        pg_query("GRANT {$privilege} ON SCHEMA {$schema} TO {$newLogin};");
                        pg_query("GRANT USAGE ON SCHEMA {$schema} TO {$newLogin};");  
                    }

                    else if($privilege=="ALL" || $privilege=="INSERT" || $privilege=="SELECT" || $privilege=="UPDATE" || $privilege=="DELETE"){
                        pg_query("GRANT {$privilege} ON ALL TABLES IN SCHEMA {$schema} TO {$newLogin};");
                        pg_query("GRANT USAGE ON SCHEMA {$schema} TO {$newLogin};"); 
                    }
                }
            }
        }
    }
}

推荐阅读