Jump to content

help with this code for site permissions


Lily36

Recommended Posts

Hi all,

 

I need a help with this code that I didn't write it , but I need to make it work. The code is for managing site permissions. There are three db tables involved: system_rescources where all the php pages from the site are saved, groups with all the account types, and groups_permissions. 

 

ManageSite.php, should show a list with checkboxes for each php page, from the table system_rescources. With checking the box we can assign permission for that group of users to that php page.

However at the moment the checkboxes are not shown. 

 

Bellow the function that should build the list: 

	// Group Site Permissions builder function
	function buildPermList($parent, $permissions) {
		global $conn;
		
		$html = "";
	
		if ( isset($permissions['parents'][$parent]) ) {
			$html .= "<ul class='unstyled'>";
				
			foreach ( $permissions['parents'][$parent] as $itemId ) {	
				$use_it = false;
				$query = mysqli_query($conn, "SELECT fk_group_id, fk_system_resource_id, groups_permissions_use_it FROM groups_permissions WHERE fk_group_id='".$permissions['items'][$itemId]['gid']."' AND fk_system_resource_id='".$permissions['items'][$itemId]['sid']."'") or die('Unable to issue query: '.mysqli_error($conn));
				if ( mysqli_num_rows($query) > 0 ) {
					$result = mysqli_fetch_assoc($query);
					$use_it = $result['groups_permissions_use_it'];
				}
				
				if( !isset($permissions['parents'][$itemId]) )	{
					$html .= "<li>";
					$html .= "<div class='i-checks'><label><input type='checkbox' name='permissions[".$permissions['items'][$itemId]['gid']."][".$permissions['items'][$itemId]['sid']."]' value='' ".($use_it == '1' ? 'checked' : '').">   ".$permissions['items'][$itemId]['system_resources_path']."</label></div>";
					$html .= "</li>";
				}
	
				if( isset($permissions['parents'][$itemId]) ) {
					$html .= "<li>";
					$html .= "<div class='i-checks'><label><input type='checkbox' name='permissions[".$permissions['items'][$itemId]['gid']."][".$permissions['items'][$itemId]['sid']."]' value='' ".($use_it === '1' ? 'checked' : '').">   ".$permissions['items'][$itemId]['system_resources_path']."</label></div>";
					$html .= buildPermList($itemId, $permissions);
					$html .= "</li>";
				}
			}
				
			$html .= "</ul>";
		}
		
		return $html;
	}

And here is the code from ManageSite.php . The problem is that $permissionStr gives empty result .

	$printStr = "";
	$success = "";
	$error = "";
	
	// Update Group Permissions
	if ( isset($_POST['submit']) ) {
		$permission_arr = $_POST['permissions'];
		if ( is_array($permission_arr) && !empty($permission_arr) ) {
			//foreach ( $permission_arr as $perm => $arr ) {
				$sql_u = "SELECT
								s.id AS sid, s.fk_parent_id, s.system_resources_path, s.system_resources_code, s.system_resources_name,
								g.id AS gid, g.groups_name, g.groups_description
						FROM
								system_resources s, groups g
						WHERE
								g.groups_is_del='0' AND
								s.system_resources_is_del='0'
						ORDER BY g.groups_name, s.system_resources_path";
					
				$query_u = mysqli_query($conn, $sql_u) or die('Unable to issue query: '.mysqli_error($conn));
				while ( $row_u = mysqli_fetch_array($query_u) ) {
					$query_gr_perms = mysqli_query($conn, "SELECT id FROM groups_permissions WHERE fk_group_id='$row_u[gid]' AND fk_system_resource_id='$row_u[sid]'") or die('Unable to issue groups_permissions query: '.mysqli_error($conn));
					if ( mysqli_num_rows($query_gr_perms) > 0 ) {
						if ( isset($permission_arr[$row_u['gid']][$row_u['sid']]) ) {
							$update_sql = "UPDATE groups_permissions SET groups_permissions_use_it='1' WHERE fk_group_id='$row_u[gid]' AND fk_system_resource_id='$row_u[sid]'";
						} else {
							$update_sql = "UPDATE groups_permissions SET groups_permissions_use_it='0' WHERE fk_group_id='$row_u[gid]' AND fk_system_resource_id='$row_u[sid]'";
						}
						
						$update_check = mysqli_query($conn, $update_sql) or die('Unable to issue udpate groups_permissions query: '.mysqli_error($conn));
					} else {
						if ( isset($permission_arr[$row_u['gid']][$row_u['sid']]) ) {
							$insert_sql = "INSERT INTO groups_permissions(fk_group_id, fk_system_resource_id, groups_permissions_use_it, groups_permissions_is_del) VALUES('$row_u[gid]', '$row_u[sid]', '1', '0')";
							$insert_check = mysqli_query($conn, $insert_sql) or die('Unable to issue insert groups_permissions query: '.mysqli_error($conn));
						}
					}
					
					$success = "Group Permissions successfully updated!";
				}
			//}
		} else {
			$error = "Group Permissions NOT updated properly!";
		}
	}
	
	$query_groups = mysqli_query($conn, "SELECT id, groups_name FROM groups WHERE groups_is_del='0' ORDER BY groups_name") or die('Unable to issue groups query: '.mysqli_error($conn));
	if ( mysqli_num_rows($query_groups) > 0 ) {
		while ( $row_gr = mysqli_fetch_array($query_groups) ) {
			$permissions = array(
									'row' => array(),
									'parents' => array()
							);
			
			$sql = "SELECT
						s.id AS sid, s.fk_parent_id, s.system_resources_path, s.system_resources_code, s.system_resources_name,
						g.id AS gid, g.groups_name, g.groups_description
					FROM
						system_resources s, groups g
					WHERE
						g.groups_is_del='0' AND
						s.system_resources_is_del='0' AND
						g.id='$row_gr[id]'
					ORDER BY g.groups_name, s.system_resources_path";
			$query = mysqli_query($conn, $sql) or die('Unable to issue system_resources query: '.mysqli_error($conn));
			while ( $row = mysqli_fetch_array($query) ){
				$permissions['items'][$row['sid']] = $row;
				$permissions['parents'][$row['fk_parent_id']][] = $row['sid'];
			}
			
			$permissionStr = buildPermList(NULL, $permissions);
			$printStr .= '<h3 class="text-navy">'.htmlspecialchars(stripslashes($row_gr['groups_name'])).':</h3>'.$permissionStr;
		}
	}

	

Thanks in advance for your help!

 

Lily

Link to comment
Share on other sites

What debugging attempts have you made? Turn on php error checking to be sure you don't have any errors that could be affecting you. Add some echos at points where critical things are happening to ensure that you have the values that you expect to have at those points.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.