PHP中的用户权限系统

时间:2016-10-03 19:05:45

标签: php html mysql checkbox

user privilege to menus

我已从菜单&中填充此内容sub_menu表。现在我想通过复选框选择提供对特定用户的单独访问。 if checked = add privilege,if unchecked =通过批量删除权限。

有人可以提供帮助来建立数据库结构来存储访问详细信息吗?并更新(如果选中)&删除(如果未选中)进程。

我尝试了什么:

User.table:

 id  | Name
 1   | test

menu.table:

id | name
1  |  User
2  |  Category

submenu.table

 id | name            | menu_id
 1  |  Add User       | 1
 2  |  User List      | 1
 3  |  Category List  | 2

menu_assign.table

id  | menu_id  |  sub_menu_id  | user_id
1   | 1        |  1            | 1
2   | 2        |  3            | 1 

我尝试了什么:

HTML:

<input type="checkbox" name="menu[]" value="<?php echo $row2['id'] ?>">
<input type="hidden" name="del[]" value="<?php echo $row2['id'] ?>">

Process.php:

foreach($_POST['del'] as $mid) {
       if(!in_array($mid, $_POST['menu'])){
                $obj->removeAssignMenu("admin_menu_assign","admin_submenu_id",$mid,"user_id",$id); //DELETE FROM $table WHERE $field = '$id' AND $user = $uid
            }
            header("location:../?page=role_assign&id=".$id);
        }

        foreach($_POST['menu'] as $k){

            $get_main_menu = $obj->selectRequiredRow("admin_submenu", "id", $k); //SELECT * FROM $table WHERE $field = '$value'

            $admin_menu_id = $get_main_menu['admin_menu_id'];
            $admin_submenu_id = $get_main_menu['id'];

            $data = array(
                "admin_menu_id" => $admin_menu_id,
                "admin_submenu_id" => $k,
                "user_id" => $id
            );

            $check = $obj->selectMenuAssigned("admin_menu_assign","admin_submenu_id", $admin_submenu_id,"user_id",$id); // SELECT * FROM $table WHERE $field = '$value' AND $user = $id

            if (!isset($check['admin_submenu_id'])) {
                $obj->insert("admin_menu_assign", $data);
            } else {
                $obj->editRow("admin_menu_assign", $data, "admin_submenu_id", $check['admin_submenu_id'],"user_id",$id); // UPDATE $table SET $key ="."'$value' "."WHERE $field = '$id'
            }

            header("location:../?page=role_assign&id=".$id);
        }

如果检查了所有内容,我可以在menu_assign表中指定id,但如果有一些取消选中并且有一些选中,则会在更新时遇到问题。此外,可能会检查多个复选框,或者可能未选中该复选框,因此此更新或删除权限应同时适用于两者(循环中)。

0 个答案:

没有答案