lundi 3 octobre 2016

user privilege system in PHP

user privilege to menus

I have populate this from menu & sub_menu table. now i want to provide individually access to particular user via checkbox selection. if checked = add privilege, if unchecked = remove privilege through loop in bulk.

Can someone please provide help to make database structure to store access details? and update (if checked) & delete (if unchecked) process.

What i have tried :

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 

What i have tried :

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);
        }

I was able to assign ids in menu_assign table if all is checked but got issues while updating if there is some uncheck and some checked. also that multiple check box might be checked or might be unchecked so this update or remove privilege should work for both at once (in loop).

Aucun commentaire:

Enregistrer un commentaire