/**
 * Update action permit mapping for a user
 * @param string $action_id the id of the user action mapping for which to update a permit.
 * @param string $user_id the id of the user for this permit.  Not necessary to perform update, just included for controlling access based on user_id.
 * @param string $permit the permit expression, a sequence of permission validator function calls joined by '&'.
 * @return boolean true for success, false if failed
 */
function updateUserActionPermit($action_id, $user_id, $permit)
{
    // This block automatically checks this action against the permissions database before running.
    if (!checkActionPermissionSelf(__FUNCTION__, func_get_args())) {
        addAlert("danger", "Sorry, you do not have permission to access this resource.");
        return false;
    }
    // TODO: Check that user exists
    //Check if selected action exists
    $action_permit = fetchActionPermit($action_id, 'user');
    if (!$action_permit) {
        addAlert("danger", "I'm sorry, the action_id you specified is invalid!");
        return false;
    }
    // Check that permission validators exist
    if (!isValidPermitString($permit)) {
        return false;
    }
    // Attempt to create in DB
    if (!dbUpdateActionPermit($action_id, $permit, 'user')) {
        return false;
    } else {
        addAlert("success", "Successfully updated permit for action {$action_permit['action']}");
        return true;
    }
}
} else {
    $populate_fields = false;
    $button_submit_text = "Create action";
    $target = "../api/create_action_permit.php";
    $box_title = "New Action";
    $action_name_disable_str = "";
}
$action_name = "";
// If we're in update mode, load action data
if ($populate_fields) {
    if ($group_id) {
        if (!($action_permit = fetchActionPermit($action_id, "group"))) {
            addAlert("danger", "The specified action id does not exist.");
        }
    } else {
        if (!($action_permit = fetchActionPermit($action_id, "user"))) {
            addAlert("danger", "The specified action id does not exist.");
        }
    }
    $action_name = $action_permit['action'];
    $action_permits = $action_permit['permits'];
    if ($render_mode == "panel") {
        $box_title = $action_name;
    }
}
// Otherwise just load user/group data
if ($group_id) {
    $group = fetchGroupDetails($group_id);
    $group_name = $group['name'];
} else {
    $user = fetchUser($user_id);