diff --git a/api/v0/get/user_credentials.php b/api/v0/get/user_credentials.php index 6168639..09e202c 100644 --- a/api/v0/get/user_credentials.php +++ b/api/v0/get/user_credentials.php @@ -14,7 +14,6 @@ $stmt->execute([$userkey, $userkey]); $user_data = $stmt->fetch(); //Define User data $partnerhierarchy = $user_data['partnerhierarchy']; -$permission = userRights($user_data['view']); $profile= getUserPermissions($pdo, $user_data['id']); $username = $user_data['username']; $useremail = $user_data['email']; @@ -22,6 +21,7 @@ $servicekey = $user_data['service']; $partner = json_decode($partnerhierarchy); $language = $user_data['language']; $clientsecret = $user_data['userkey']; +$permission = getHierarchyLevel($partner); //upgrade from userrights(view) //Update Lastlogin $logindate = date('Y-m-d H:i:s'); diff --git a/api/v1/get/application.php b/api/v1/get/application.php index 4504962..0a663ff 100644 --- a/api/v1/get/application.php +++ b/api/v1/get/application.php @@ -179,9 +179,9 @@ switch ($action) { case 'geolocation': if ($whereclause == ''){ - $whereclause = 'WHERE geolocation is not null OR geolocation != "["",""]"'; + $whereclause = 'WHERE geolocation is not null AND geolocation != \'["",""]\''; } else { - $whereclause .= ' AND geolocation is not null OR geolocation != "["",""]'; + $whereclause .= ' AND geolocation is not null AND geolocation != \'["",""]\''; } $sql = 'SELECT distinct(geolocation) FROM equipment e '.$whereclause.''; diff --git a/api/v1/get/user_credentials.php b/api/v1/get/user_credentials.php index f7fa705..082436e 100644 --- a/api/v1/get/user_credentials.php +++ b/api/v1/get/user_credentials.php @@ -16,7 +16,6 @@ if ($stmt->rowCount() == 1) { $user_data = $stmt->fetch(); //Define User data $partnerhierarchy = $user_data['partnerhierarchy']; - $permission = userRights($user_data['view']); $profile= getUserPermissions($pdo, $user_data['id']); $username = $user_data['username']; $useremail = $user_data['email']; @@ -24,6 +23,7 @@ if ($stmt->rowCount() == 1) { $language = $user_data['language']; $partner = json_decode($partnerhierarchy); $clientsecret = $user_data['userkey']; + $permission = getHierarchyLevel($partner); //upgrade from userrights(view) //Update Lastlogin $logindate = date('Y-m-d H:i:s'); diff --git a/api/v1/post/equipments.php b/api/v1/post/equipments.php index 198012c..1765a60 100644 --- a/api/v1/post/equipments.php +++ b/api/v1/post/equipments.php @@ -217,18 +217,18 @@ $clause_insert = substr($clause_insert, 2); //Clean clause - remove first comma $input_insert = substr($input_insert, 1); //Clean clause - remove first comma //QUERY AND VERIFY ALLOWED -if ($command == 'update' && (isAllowed('equipment_manage',$profile,$permission,'U') === 1 || isAllowed('equipments_mass_update',$profile,$permission,'U') === 1 || $owner_equipment === 1)){ +if ($command == 'update' && (isAllowed('equipment',$profile,$permission,'U') === 1 || isAllowed('equipments_mass_update',$profile,$permission,'U') === 1 || $owner_equipment === 1)){ $sql = 'UPDATE equipment SET '.$clause.' WHERE rowID = ? '.$whereclause.''; $execute_input[] = $id; $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); } -elseif ($command == 'insert' && isAllowed('equipment_manage',$profile,$permission,'C') === 1){ +elseif ($command == 'insert' && isAllowed('equipment',$profile,$permission,'C') === 1){ $sql = 'INSERT INTO equipment ('.$clause_insert.') VALUES ('.$input_insert.')'; $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); } -elseif ($command == 'delete' && (isAllowed('equipment_manage',$profile,$permission,'D') === 1 || $owner_equipment === 1)){ +elseif ($command == 'delete' && (isAllowed('equipment',$profile,$permission,'D') === 1 || $owner_equipment === 1)){ //delete equipment $stmt = $pdo->prepare('DELETE FROM equipment WHERE rowID = ? '.$whereclause.''); $stmt->execute([ $id ]); diff --git a/api/v2/get/application.php b/api/v2/get/application.php index dd01c42..7fefbc5 100644 --- a/api/v2/get/application.php +++ b/api/v2/get/application.php @@ -179,9 +179,9 @@ switch ($action) { case 'geolocation': if ($whereclause == ''){ - $whereclause = 'WHERE geolocation is not null OR geolocation != "["",""]"'; + $whereclause = 'WHERE geolocation is not null AND geolocation != \'["",""]\''; } else { - $whereclause .= ' AND geolocation is not null OR geolocation != "["",""]'; + $whereclause .= ' AND geolocation is not null AND geolocation != \'["",""]\''; } $sql = 'SELECT distinct(geolocation) FROM equipment e '.$whereclause.''; diff --git a/api/v2/get/user_credentials.php b/api/v2/get/user_credentials.php index d6ccdba..5301ac4 100644 --- a/api/v2/get/user_credentials.php +++ b/api/v2/get/user_credentials.php @@ -18,7 +18,6 @@ if ($stmt->rowCount() == 1) { $user_data = $stmt->fetch(); //Define User data $partnerhierarchy = $user_data['partnerhierarchy']; - $permission = userRights($user_data['view']); $profile= getUserPermissions($pdo, $user_data['id']); //getProfile($user_data['settings'],$permission); $username = $user_data['username']; $useremail = $user_data['email']; @@ -26,6 +25,7 @@ if ($stmt->rowCount() == 1) { $language = $user_data['language']; $partner = json_decode($partnerhierarchy); $clientsecret = $user_data['userkey']; + $permission = getHierarchyLevel($partner); //upgrade from userrights(view) //Update Lastlogin $logindate = date('Y-m-d H:i:s'); diff --git a/api/v2/post/equipments.php b/api/v2/post/equipments.php index 9057ddb..bca84a5 100644 --- a/api/v2/post/equipments.php +++ b/api/v2/post/equipments.php @@ -224,18 +224,18 @@ $clause_insert = substr($clause_insert, 2); //Clean clause - remove first comma $input_insert = substr($input_insert, 1); //Clean clause - remove first comma //QUERY AND VERIFY ALLOWED -if ($command == 'update' && (isAllowed('equipment_manage',$profile,$permission,'U') === 1 || isAllowed('equipments_mass_update',$profile,$permission,'U') === 1 || $owner_equipment === 1)){ +if ($command == 'update' && (isAllowed('equipment',$profile,$permission,'U') === 1 || isAllowed('equipments_mass_update',$profile,$permission,'U') === 1 || $owner_equipment === 1)){ $sql = 'UPDATE equipment SET '.$clause.' WHERE rowID = ? '.$whereclause.''; $execute_input[] = $id; $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); } -elseif ($command == 'insert' && isAllowed('equipment_manage',$profile,$permission,'C') === 1){ +elseif ($command == 'insert' && isAllowed('equipment',$profile,$permission,'C') === 1){ $sql = 'INSERT INTO equipment ('.$clause_insert.') VALUES ('.$input_insert.')'; $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); } -elseif ($command == 'delete' && (isAllowed('equipment_manage',$profile,$permission,'D') === 1 || $owner_equipment === 1)){ +elseif ($command == 'delete' && (isAllowed('equipment',$profile,$permission,'D') === 1 || $owner_equipment === 1)){ //delete equipment $stmt = $pdo->prepare('DELETE FROM equipment WHERE rowID = ? '.$whereclause.''); $stmt->execute([ $id ]); diff --git a/api/v2/post/user_role_assignments.php b/api/v2/post/user_role_assignments.php index cd663d9..851ea02 100644 --- a/api/v2/post/user_role_assignments.php +++ b/api/v2/post/user_role_assignments.php @@ -17,7 +17,7 @@ $date = date('Y-m-d H:i:s'); //------------------------------------------ // BATCH UPDATE - Update all roles for a user //------------------------------------------ -if (isset($post_content['batch_update']) && isset($post_content['user_id']) && isAllowed('user_manage',$profile,$permission,'U') === 1){ +if (isset($post_content['batch_update']) && isset($post_content['user_id']) && isAllowed('user',$profile,$permission,'U') === 1){ $user_id = $post_content['user_id']; $selected_roles = $post_content['roles'] ?? []; @@ -48,11 +48,11 @@ if (isset($post_content['batch_update']) && isset($post_content['user_id']) && i if ($existing){ //Reactivate existing assignment $stmt = $pdo->prepare('UPDATE user_role_assignments SET is_active = 1, assigned_by = ?, assigned_at = ?, updatedby = ?, updated = ? WHERE rowID = ?'); - $stmt->execute([$username, $date, $username, $date, $existing['rowID']]); + $stmt->execute([$username, $date, $username, $date, $$username]); } else { //Create new assignment $stmt = $pdo->prepare('INSERT INTO user_role_assignments (user_id, role_id, is_active, assigned_by, assigned_at, created, createdby) VALUES (?, ?, 1, ?, ?, ?, ?)'); - $stmt->execute([$user_id, $role_id, $username, $date, $date, $userkey]); + $stmt->execute([$user_id, $role_id, $username, $date, $date, $username]); } } } @@ -105,13 +105,13 @@ else { $input_insert = substr($input_insert, 1); //QUERY AND VERIFY ALLOWED - if ($command == 'update' && isAllowed('user_manage',$profile,$permission,'U') === 1){ + if ($command == 'update' && isAllowed('user',$profile,$permission,'U') === 1){ $sql = 'UPDATE user_role_assignments SET '.$clause.' WHERE rowID = ?'; $execute_input[] = $id; $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); } - elseif ($command == 'insert' && isAllowed('user_manage',$profile,$permission,'C') === 1){ + elseif ($command == 'insert' && isAllowed('user',$profile,$permission,'C') === 1){ //Check if this user-role combination already exists (including inactive ones) $stmt = $pdo->prepare('SELECT rowID, is_active FROM user_role_assignments WHERE user_id = ? AND role_id = ? LIMIT 1'); $stmt->execute([$post_content['user_id'], $post_content['role_id']]); @@ -131,7 +131,7 @@ else { $stmt->execute($execute_input); } } - elseif ($command == 'delete' && isAllowed('user_manage',$profile,$permission,'D') === 1){ + elseif ($command == 'delete' && isAllowed('user',$profile,$permission,'D') === 1){ //Soft delete by setting is_active to 0 $stmt = $pdo->prepare('UPDATE user_role_assignments SET is_active = 0, updatedby = ?, updated = ? WHERE rowID = ?'); $stmt->execute([$username, $date, $id]); diff --git a/api/v2/post/user_roles.php b/api/v2/post/user_roles.php index da38722..035dff2 100644 --- a/api/v2/post/user_roles.php +++ b/api/v2/post/user_roles.php @@ -55,7 +55,7 @@ $clause_insert = substr($clause_insert, 2); $input_insert = substr($input_insert, 1); //QUERY AND VERIFY ALLOWED -if ($command == 'update' && isAllowed('user_role_manage',$profile,$permission,'U') === 1){ +if ($command == 'update' && isAllowed('user_roles',$profile,$permission,'U') === 1){ $sql = 'UPDATE user_roles SET '.$clause.' WHERE rowID = ?'; $execute_input[] = $id; $stmt = $pdo->prepare($sql); @@ -82,7 +82,7 @@ if ($command == 'update' && isAllowed('user_role_manage',$profile,$permission,'U } } } -elseif ($command == 'insert' && isAllowed('user_role_manage',$profile,$permission,'C') === 1){ +elseif ($command == 'insert' && isAllowed('user_roles',$profile,$permission,'C') === 1){ $sql = 'INSERT INTO user_roles ('.$clause_insert.') VALUES ('.$input_insert.')'; $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); @@ -106,7 +106,7 @@ elseif ($command == 'insert' && isAllowed('user_role_manage',$profile,$permissio } } } -elseif ($command == 'delete' && isAllowed('user_role_manage',$profile,$permission,'D') === 1){ +elseif ($command == 'delete' && isAllowed('user_roles',$profile,$permission,'D') === 1){ //Delete role permissions first (foreign key constraint) $stmt = $pdo->prepare('DELETE FROM role_access_permissions WHERE role_id = ?'); $stmt->execute([$id]); diff --git a/api/v2/post/users.php b/api/v2/post/users.php index 175e8f3..c8c1bc2 100644 --- a/api/v2/post/users.php +++ b/api/v2/post/users.php @@ -266,10 +266,17 @@ elseif ($command == 'insert' && isAllowed('user',$profile,$permission,'C') === 1 $stmt = $pdo->prepare($sql); $stmt->execute($execute_input); + // Get the new user ID + $new_user_id = $pdo->lastInsertId(); + //STEP 2- Send to user $mail_location = (file_exists($_SERVER['DOCUMENT_ROOT'].'/custom/'.$domain.'/mail/email_template_new.php') ? $_SERVER['DOCUMENT_ROOT'].'/custom/'.$domain.'/mail/email_template_new.php' : './assets/mail/email_template_new.php'); include_once $mail_location; send_mail($post_content['email'],$subject,$message,'',''); + + // Return the new user ID + $result = json_encode(['id' => (int)$new_user_id], JSON_UNESCAPED_UNICODE); + echo $result; } else { //------------------------------------------ //JSON_ENCODE diff --git a/assets/functions.php b/assets/functions.php index 27d009e..7fb0d5b 100644 --- a/assets/functions.php +++ b/assets/functions.php @@ -1726,6 +1726,7 @@ function getProfile($profile, $permission){ $always_allowed = [ 'com_log' => 'CRU', 'application' => 'CRU', + 'user_roles' => 'R', 'user_role_assignments' => 'R', 'user_permissions' => 'R', 'products_software' => 'R', @@ -2847,28 +2848,37 @@ function serviceReport($history, $request, $country_code) //------------------------------------------ // LIST PARTNER //------------------------------------------ -function listPartner($partnertype, $user_right, $input, $required) +function listPartner($partnertype, $user_right = null, $input, $required) { include dirname(__FILE__,2).'/settings/settings_redirector.php'; -//BASED ON USERRIGHT DEFINE SQL AND DATA RETURNED -if ($user_right != 3 || $user_right !=4) { - //NOT ADMIN USER - $partner = json_decode($_SESSION['authorization']['partnerhierarchy']); - //SoldTo is empty - if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';} - //BUILD CONDITION - $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search; + // Use hierarchy level instead of user_right + $partner = json_decode($_SESSION['authorization']['partnerhierarchy']); + $hierarchyLevel = getHierarchyLevel($partner); + + // Only apply restrictions if hierarchy level is not 0 or 1 + if ($hierarchyLevel != 0 && $hierarchyLevel != 1) { + // Build condition based on hierarchy + $condition = buildHierarchyConditionLvl2($partner, $hierarchyLevel); $whereclause = 'AND salesID like ?'; -} -else {//ADMIN USERS - $whereclause = ''; - } + } else { + // Level 0 or 1: No restrictions or salesid-only level + $whereclause = ''; + $condition = ''; + } $pdo = dbConnect($dbname); - $sql = 'SELECT distinct partnerID, partnername FROM partner WHERE partnertype = ? AND status = 1 '.$whereclause.''; - $stmt = $pdo->prepare($sql); - $stmt->execute([$partnertype, $condition]); + + if ($whereclause != '') { + $sql = 'SELECT distinct partnerID, partnername FROM partner WHERE partnertype = ? AND status = 1 '.$whereclause.''; + $stmt = $pdo->prepare($sql); + $stmt->execute([$partnertype, $condition]); + } else { + $sql = 'SELECT distinct partnerID, partnername FROM partner WHERE partnertype = ? AND status = 1'; + $stmt = $pdo->prepare($sql); + $stmt->execute([$partnertype]); + } + $partners = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($input !='' && !empty($input)){ diff --git a/equipment.php b/equipment.php index e52af6d..126dc18 100644 --- a/equipment.php +++ b/equipment.php @@ -461,13 +461,13 @@ $shipto_id = explode("-",$partner_data->shipto) ?? ''; $partner_users_id = ($shipto_id[0] != '')? $shipto_id[0] : (($soldto_id[0] != '')? $soldto_id[0] : 1); $view_communication = ''; -if ($partner_users_id != 1 && ($_SESSION['authorization']['permission'] == 3 || $_SESSION['authorization']['permission'] == 4)){ +if ($partner_users_id != 1 && (isAllowed('communications',$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'R') === 1)){ $view_communication = ' '.$button_partner_assigned_communication.''; } //DISPLAY RELATED USERS $view_users =''; -if ($partner_users_id != 1 && ($_SESSION['authorization']['permission'] == 3 || $_SESSION['authorization']['permission'] == 4)){ +if ($partner_users_id != 1 && (isAllowed('users',$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'R') === 1)){ $view_users = ' '.$button_partner_assigned_users.''; } diff --git a/equipment_manage.php b/equipment_manage.php index c077f6e..8e8f031 100644 --- a/equipment_manage.php +++ b/equipment_manage.php @@ -7,7 +7,7 @@ if (debug && debug_id == $_SESSION['authorization']['id']){ error_reporting(E_ALL); } -$page = 'equipment_manage'; +$page = 'equipment'; $page_edit = 'equipment_manage_edit'; //Check if allowed if (isAllowed($page,$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'R') === 0){ diff --git a/equipments.php b/equipments.php index 31af2bf..cc999d6 100644 --- a/equipments.php +++ b/equipments.php @@ -22,7 +22,7 @@ if (isAllowed($page,$_SESSION['authorization']['permissions'],$_SESSION['authori exit; } //PAGE Security -$page_manage = 'equipment_manage'; +$page_manage = 'equipment'; $update_allowed = isAllowed($page_manage ,$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'U'); $delete_allowed = isAllowed($page_manage ,$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'D'); $create_allowed = isAllowed($page_manage ,$_SESSION['authorization']['permissions'],$_SESSION['authorization']['permission'],'C'); diff --git a/equipments_mass_update.php b/equipments_mass_update.php index 1bbc639..565043e 100644 --- a/equipments_mass_update.php +++ b/equipments_mass_update.php @@ -68,8 +68,18 @@ if ($update_allowed === 1){ $output_excel[$val]['productname'] = ${$responses->productname} ?? $responses->productname; $output_excel[$val]['order_ref'] = $_POST['order_ref']; $output_excel[$val]['status'] = $_POST['status']; - $output_excel[$val]['salesid'] = $_POST['salesid']; - $output_excel[$val]['soldto'] = $_POST['soldto']; + + // Only include salesid/soldto if form fields were visible (hierarchy 0-1) + $hierarchyLevel = getHierarchyLevel(json_decode($_SESSION['authorization']['partnerhierarchy'])); + if ($hierarchyLevel == 0 || $hierarchyLevel == 1) { + if (isset($_POST['salesid'])) { + $output_excel[$val]['salesid'] = $_POST['salesid']; + } + if (isset($_POST['soldto'])) { + $output_excel[$val]['soldto'] = $_POST['soldto']; + } + } + if (!empty($_POST['shipto']) || $_POST['shipto'] !=''){ $output_excel[$val]['shipto'] = $_POST['shipto']; } @@ -202,7 +212,9 @@ $view .='