The following code allows you to change a user's group membership by PHP code. Attention: There is a problem probably due to AppGini's caching implementation. See my annotation at the end.
Code
Code: Select all
function TABLENAME_after_update($data, $memberInfo, &$args)
{
// As recommended, I have added an additional checkbox named "agreement"
// which seems easiest to me. But you may work out a different solution
// your specific condition here, for example...
$user_has_agreed = $data["agreement"] === "1";
// decision: if agreed, move to group (1) else move to group (2)
$targetGroupName = $user_has_agreed ? "geschäftsführung" : "assistenz";
$username = $memberInfo["username"];
// see function below
return membership_setGroup($username, $targetGroupName, true);
}
Code: Select all
// new function
function membership_setGroup($username, $groupName, $skipPerm = false)
{
if (!$skipPerm && !getLoggedAdmin()) die("Permission denied.");
$memberInfo = getMemberInfo($username);
if (!$memberInfo["username"] || !$memberInfo["approved"] || $memberInfo["banned"]) die("Invalid, un-approved or banned user.");
$memberIDSafe = makeSafe(trim($memberInfo["username"]));
$currentGroupNameSafe = makeSafe(trim($memberInfo["group"]));
$currentGroupID = sqlValue("SELECT groupID FROM `membership_groups` WHERE `name` = '{$currentGroupNameSafe}' LIMIT 1");
$newGroupNameSafe = makeSafe(trim($groupName));
$newGroupID = sqlValue("SELECT groupID FROM `membership_groups` WHERE `name` = '{$newGroupNameSafe}' LIMIT 1");
if (!$currentGroupID) die("unknown source group");
if (!$newGroupID) die("unknown target group");
$sql = "UPDATE membership_users SET groupID='{$newGroupID}' WHERE memberID='{$memberIDSafe}' LIMIT 1";
return sql($sql, $eo);
}
(1) user is logged in as "assistenz"
- chrome_S28Wq6RaQJ.png (9.33 KiB) Viewed 2685 times
(2) she belongs to group "Assistenz"
- chrome_RKLJE7Uyv5.png (2.14 KiB) Viewed 2685 times
(3) check the "agreement" checkbox and save record
- chrome_RlygxXIvYv.png (2.3 KiB) Viewed 2685 times
(4) now she is in the more privileged group
- chrome_w6DtxaFlvT.png (2.46 KiB) Viewed 2685 times
The database change works fine...
- chrome_Q9rjlh5Tm9.png (5.33 KiB) Viewed 2685 times
...but there is a problem!
Problem
I guess due to
AppGini's caching, the membership info will not be reloaded after database change. This means, even if there is the new groupID in the
membership_users record, the permission stays the same.
I do not see any way to reset AppGini's cache, because memberinfo-caching has been implemented as a local static variable and therefore I must not touch it from the outside.
So, I'm afraid you will have to
force users to log out and log in again.
Regards,
Jan