In the Monetizing data apps I introduced a Component that will identify the KNIME user executing a certain Data App, check if he has credits left, and if so, execute the Data App.
This works, but has a small issue: As it is a component within the data app, even when the user does not exist or does not have enough credits, it will always fire up an execution context and deduct YOU (the Data App provider) one or more credits from your KNIME subscription.
I came up with the following solution. I created a 'front end' page with PHP that will first do the checking (user exists and has enough credits) and only then invoke the Data App.
This is meant for Community Hub use. KNIME Business Hub already has mechanisms in place to handle this.
You can find the PHP code below. The things you'll have to change I put between < > tags. The database I use is Postgres, but the code could be easily adapted for other databases.
Here you can see how it works.
<?php
// Database connection parameters
$host = '<hostname>';
$port = '<connection port>';
$user = '<db username>';
$password = '<db password>';
$dbname = '<database name>';
// Connect to the PostgreSQL database
$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn) {
die("Connection failed: " . pg_last_error());
}
// Initialize variables
$message = '';
$username = '';
$url = '';
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$button = $_POST['button']; // Button clicked
// Map button to URL
switch ($button) {
case 'Button1':
$url = 'https://apps.hub.knime.com/d/~data-app:<data app 1 ID>/run';
break;
case 'Button2':
$url = 'https://apps.hub.knime.com/d/~data-app:<data app 2 ID>/run';
break;
case 'Button3':
$url = 'https://apps.hub.knime.com/d/~data-app:<data app 3 ID>/run';
break;
default:
$message = 'Invalid button.';
break;
}
// Look up user in the database
$query = "SELECT credits FROM \"USER_CREDITS\" WHERE user_name = $1";
$result = pg_query_params($conn, $query, array($username));
if ($result) {
$row = pg_fetch_assoc($result);
if ($row) {
$credits = $row['credits'];
// Check if the user has at least 5 credits
if ($credits >= 5) {
// Deduct 5 credits
$new_credits = $credits - 5;
$update_query = "UPDATE \"USER_CREDITS\" SET credits = $1 WHERE user_name = $2";
$update_result = pg_query_params($conn, $update_query, array($new_credits, $username));
if ($update_result) {
// Redirect to the URL
header("Location: $url");
exit();
} else {
$message = 'Error updating credits.';
}
} else {
$message = 'You do not have enough credits.';
}
} else {
$message = 'User not found.';
}
} else {
$message = 'Error checking user credits.';
}
}
pg_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Climate Change Data Apps</title>
</head>
<body>
<h1>Climate Change Data Apps</h1>
<form method="POST">
<label for="username">Please enter your KNIME Hub user name:</label>
<input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username); ?>" required><br><br>
Temperature Data:<br><br>
<button type="submit" name="button" value="Button1">Maximum temperatures by Year - Select Country (CDA81)</button><br>
<button type="submit" name="button" value="Button2">Minimum temperatures by Year - Select Country (CDA82)</button><br>
<button type="submit" name="button" value="Button3">Yearly maximum temperature evolution - Select Country (CDA500)</button>
</form>
<?php if ($message): ?>
<p style="color: red;"><?php echo $message; ?></p>
<?php endif; ?>
</body>
</html>