We have found a way around it for now:
In main.inc, we check the user & it’s group along with the ip from where they have logged in. We have removed all individual permissions and made different groups (which was the preferred way anyway). Now we have set a few permissions which we want people to have from outside as well into groups and defined those groups into $onlyAllowedGroups.
If the IP is not allowed, then all extra user groups assigned are stored into another temporary table except the allowed permission. When they are back on the allowed list, we add them back to the system.
Hope this helps anyone looking for a solution similar. I think this is much more easier (not upgrade safe but GUI friendly).
The code is still very raw just finished testing it with dummy data.
$allowedIPs = array(“111.11.111.11”); // inside
$bypassUsers = array(“1”, “2”); // these are user IDs (as strings or integers)
$onlyAllowedGroups = array(7, 8, 14); // this group will be kept for non-technical users
// Get the current IP address.
$currentIP = $_SERVER[‘REMOTE_ADDR’];
// User group object
$usergroup = new UserGroup($db);
$groupslist = $usergroup->listGroupsForUser($user->id);
// User object
$userobject = new User($db);
$userresult = $userobject->fetch($user->id);
$newGroups = array();
$removedGroups = array();
// GT Added allowed module restricted based on ip address — KTI — Start
// --- Check if the current IP is allowed or not ---
if (!in_array($currentIP, $allowedIPs)) {
// First, check if the user is a bypass user.
if (in_array($user->id, $bypassUsers)) {
// Bypass user: Do nothing. They keep full access.
// You can add a debug message if needed.
// dol_syslog("Bypass user {$user->id}: no group changes applied.");
}
else {
$newGroups = array();
foreach ($groupslist as $index=>$groupId) {
if (in_array($index, $onlyAllowedGroups)) {
$newGroups[] = $index;
} else {
// Save this removed group into the temporary storage table.
$sql = "INSERT INTO llx_user_groups_temporary_storage (fk_user, fk_groups, ip_address)
VALUES (" . intval($user->id) . ", " . intval($index) . ", '$currentIP')";
$res = $db->query($sql);
if (!$res) {
dol_syslog("Error inserting group " . $index . " into temporary storage for user " . $user->id . ": " . $db->error());
}
}
// Not to remove allowed groups
if(!in_array($index, $onlyAllowedGroups)) {
$removedGroups[] = $index;
}
}
// Remove those usergroup whos are not accessible outside --- KTI
foreach($removedGroups as $groups) {
$removegroup = $userobject->RemoveFromGroup($groups, 1);
}
}
} else { // Inside office fully accessible mudule
// Look for temporary stored groups in llx_user_groups_temporary_storage.
$sql = "SELECT fk_groups FROM llx_user_groups_temporary_storage WHERE fk_user = " . intval($user->id);
$res = $db->query($sql);
if ($res) {
while ($obj = $db->fetch_object($res)) {
// Add the group back if it is not already in the user's groups.
if (!in_array($obj->fk_groups, $groupslist)) {
// Add users in groups those are removed
$result = $userobject->SetInGroup($obj->fk_groups, 1);
}
}
// Now that groups have been restored, delete the records.
$sql_del = "DELETE FROM llx_user_groups_temporary_storage WHERE fk_user = " . intval($user->id);
$db->query($sql_del);
} else {
dol_syslog("Error selecting temporary storage for user " . $user->id . ": " . $db->error());
}
} // else end