Created a new API key product type (#1272)

* Created a new API key product type

* Make the CI happy + a few minor fixes

* Improvements to the generation options

Plus generalized improvements to the config pages

* Fix my typo

* Final touches

At least, I hope they are the final touches. We will see after reviews :P

* Cleanup and minor improvements

* Minor fix

* Move to HTML email templates + add logo

* This doesn't / shouldn't return the ID anymore

It's no longer used as things were switched to the order ID

* Update the model to reflect DB changes

* Misc. cleanup + added some missing types
This commit is contained in:
Belle Aerni authored and GitHub committed 2023-06-28 11:14:20 -07:00
1 parent e0906d15cd
commit 6548ae92e1
16 files changed
+1096 -1

No files matched your search

+2 -1
View File
@@ -340,7 +340,8 @@
"subkeys",
"httponly",
"samesite",
"IPCOUNTRY"
"IPCOUNTRY",
"Serviceapikey"
],
"ignorePaths": [
"tests/**",
+19
View File
@@ -0,0 +1,19 @@
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
class Model_ServiceApiKey extends \RedBeanPHP\SimpleModel
{
public int $id;
public int $client_id;
public string $api_key;
public string $config;
public string $created_at;
public string $updated_at;
}
+38
View File
@@ -0,0 +1,38 @@
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
namespace Box\Mod\Serviceapikey\Api;
class Admin extends \Api_Abstract
{
/**
* Update an API key. Can be used to change the config, but not to reset / regenerate the key itself.
*
* @param array $data - An associative array
* - int 'order_id' (required) The order ID of the API key to reset.
* - array 'config' (optional) The new configuration for the API key. Overrides the previous one, so you must send the complete config rather than only the parameters you want to change.
*/
public function update($data): bool
{
return $this->getService()->updateApiKey($data);
}
/**
* Used to reset / regenerate an API key. Useful in the event one is accidentally leaked.
*
* @param array $data - An associative array containing either the key or ID of whatever API key you want to reset.
* - string 'key' The API key to reset.
* - int 'order_id' The order ID of the API key to reset.
*/
public function reset($data): bool
{
return $this->getService()->resetApiKey($data);
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
namespace Box\Mod\Serviceapikey\Api;
class Client extends \Api_Abstract
{
/**
* Used to reset / regenerate an API key. Useful in the event one is accidentally leaked.
*
* @param array $data - An associative array containing either the key or ID of whatever API key you want to reset.
* - string 'key' The API key to reset.
* - int 'order_id' The order ID of the API key to reset.
*/
public function reset($data): bool
{
return $this->getService()->resetApiKey($data);
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
namespace Box\Mod\Serviceapikey\Api;
class Guest extends \Api_Abstract
{
/**
* Checks if an API key is valid or not.
*
* @param array $data
* - 'key' What API key to check.
*/
public function check($data)
{
return $this->getService()->isValid($data);
}
/**
* Gets the information tied to an API key such as it's validity and any custom parameters tied to it.
*
* @param array $data
* - 'key' What API key to check & get custom parameters for.
*/
public function get_info($data)
{
return $this->getService()->getInfo($data);
}
}
+353
View File
@@ -0,0 +1,353 @@
<?php
/**
* Copyright 2022-2023 FOSSBilling
* Copyright 2011-2021 BoxBilling, Inc.
* SPDX-License-Identifier: Apache-2.0
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
*/
/**
* This file is a delegate for module. Class does not extend any other class.
*
* All methods provided in this example are optional, but function names are
* still reserved.
*/
namespace Box\Mod\Serviceapikey;
use \FOSSBilling\InjectionAwareInterface;
use \RedBeanPHP\OODBBean;
class Service implements InjectionAwareInterface
{
protected ?\Pimple\Container $di;
public function setDi(\Pimple\Container $di): void
{
$this->di = $di;
}
public function getDi(): ?\Pimple\Container
{
return $this->di;
}
public function attachOrderConfig(\Model_Product $product, array $data): array
{
!empty($product->config) ? $config = json_decode($product->config, true) : $config = [];
return array_merge($config, $data);
}
public function create(OODBBean $order)
{
$model = $this->di['db']->dispense('service_apikey');
$model->client_id = $order->client_id;
$model->config = $order->config;
$model->created_at = date('Y-m-d H:i:s');
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return $model;
}
public function activate(OODBBean $order, OODBBean $model): bool
{
$config = json_decode($order->config, 1);
if (!is_object($model)) {
throw new \Box_Exception('Order does not exist.');
}
$model->api_key = $this->generateKey($config);
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
public function suspend(OODBBean $order, OODBBean $model): bool
{
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
public function unsuspend(OODBBean $order, OODBBean $model): bool
{
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
public function cancel(OODBBean $order, OODBBean $model): bool
{
return $this->suspend($order, $model);
}
public function uncancel(OODBBean $order, OODBBean $model): bool
{
return $this->unsuspend($order, $model);
}
public function delete(OODBBean $order, OODBBean $model): void
{
if (is_object($model)) {
$this->di['db']->trash($model);
}
}
public function toApiArray(OODBBean $model): array
{
return [
'id' => $model->id,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
'api_key' => $model->api_key,
'config' => json_decode($model->config, true),
];
}
/**
* Checks if an API key is valid or not.
*
* @param array $data
* - 'key' What API key to check.
*/
public function isValid(array $data): bool
{
if (empty($data['key'])) {
throw new \Box_Exception('You must provide an API key to check it\'s validity.');
}
$model = $this->di['db']->findOne('service_apikey', 'api_key = :api_key', [':api_key' => $data['key']]);
if (is_null($model)) {
throw new \Box_Exception("API key does not exist");
}
return $this->isActive($model);
}
/**
* Checks if an API key is valid or not and returns that + any configured custom parameters.
*
* @param array $data
* - 'key' What API key to check.
*/
public function getInfo(array $data): array
{
if (empty($data['key'])) {
throw new \Box_Exception('You must provide an API key to check it\'s validity.');
}
$model = $this->di['db']->findOne('service_apikey', 'api_key = :api_key', [':api_key' => $data['key']]);
if (is_null($model)) {
throw new \Box_Exception("API key does not exist");
}
// Load the stored JSON config from the DB
$rawConfig = json_decode($model->config, true);
$strippedConfig = [];
if (!is_array($rawConfig)) {
$rawConfig = [];
}
// Then loop through it and only select the custom parameters, removing the 'custom_' prefix & converting numerical strings to a float.
foreach ($rawConfig as $key => $value) {
if (str_starts_with($key, 'custom_')) {
$name = substr($key, 7);
if (is_numeric($value)) {
$strippedConfig[$name] = floatval($value);
} else {
$strippedConfig[$name] = $value;
}
}
}
$data = [
'valid' => $this->isActive($model),
'config' => $strippedConfig,
];
return $data;
}
/**
* Used to reset an API key using the API key generator.
*
* @param array $data An array containing what API key to reset. At least one of the possible identification methods must be provided.
* - int 'order_id' (optional) The ID of the API key to rest.
* - string 'key' (optional) The API key to reset.
*/
public function resetApiKey(array $data): bool
{
if (empty($data['key']) && empty($data['order_id'])) {
throw new \Box_Exception("You must provide either the API key or API key order ID in order to reset it.");
} elseif (!empty($data['order_id'])) {
$order = $this->di['db']->getExistingModelById('ClientOrder', $data['order_id'], 'Order not found');
$orderService = $this->di['mod_service']('order');
$model = $orderService->getOrderService($order);
} else {
$model = $this->di['db']->findOne('service_apikey', 'api_key = :api_key', [':api_key' => $data['key']]);
}
if (is_null($model)) {
throw new \Box_Exception("API key does not exist");
}
try {
$this->di['is_client_logged'];
$client = $this->di['loggedin_client'];
} catch (\Exception) {
$client = null;
}
if (!is_null($client) && $client->id !== $model->client_id) {
throw new \Box_Exception("API key does not exist");
}
$config = json_decode($model->config, true);
$model->api_key = $this->generateKey($config);
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
/**
* Used to update an API key, but prevents changing the API key so we can ensure they use the reset function.
*
* @param array $data An array containing what API key to update and what info to update.
* - int 'order_id' The order ID of the API key to update.
* - array 'config' (optional) The new config to attach to the API key.
*/
public function updateApiKey(array $data): bool
{
if (empty($data['order_id'])) {
throw new \Box_Exception("You must provide the API key order ID in order to update it.");
}
$order = $this->di['db']->getExistingModelById('ClientOrder', $data['order_id'], 'Order not found');
$orderService = $this->di['mod_service']('order');
$model = $orderService->getOrderService($order);
if (is_null($model)) {
throw new \Box_Exception("API key does not exist");
}
if (isset($data['api_key']) && $model->api_key !== $data['api_key']) {
throw new \Box_Exception('To change the API key, please use the reset function rather than updating it.');
}
$config = !empty($data['config']) ? json_encode($data['config']) : $model->config;
//ID and client ID should remain constant so we don't try to update those here.
$model->config = $config;
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
/**
* Creates the database structure to store the API keys in.
*/
public function install(): bool
{
$sql = "
CREATE TABLE IF NOT EXISTS `service_apikey` (
`id` bigint(20) NOT NULL AUTO_INCREMENT UNIQUE,
`client_id` bigint(20) NOT NULL,
`api_key` varchar(255),
`config` text NOT NULL,
`created_at` datetime,
`updated_at` datetime,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
$this->di['db']->exec($sql);
return true;
}
/**
* Removes the API keys from the database.
*/
public function uninstall(): bool
{
$this->di['db']->exec("DROP TABLE IF EXISTS `service_apikey`");
return true;
}
/**
* Generates a new API using PHP's built-in cryptographically secure random_bytes to ensure the API keys are truly random and not predictable.
*
* @param array $config (optional) An array of configuration options. All configuration keys are optional.
* - int 'length' How long of an API key to generate.
* - bool 'split' True to enable splitting of the API key using dashes. Does not count towards the total key length.
* - int 'split_interval' How often the API key should be split with dashes. Example: 8 for every 8 characters.
* - string 'case' What capitalization should be used for the generated API key. 'lower', 'upper', or 'mixed'.
*/
private function generateKey(array $config = []): string
{
$length = $config['length'] ?? 32;
$split = $config['split'] ?? true;
$splitLength = $config['split_interval'] ?? 8;
$case = $config['case'] ?? 'upper';
$i = 0;
do {
// Try 10 times to generate a unique API key. Fail if we are unable to.
if ($i++ >= 10) {
throw new \Box_Exception('Maximum number of iterations reached while generating API key');
}
// Generate random bytes half the length of the configured length, as the length will doubled when converted to a hex string.
$randomBytes = random_bytes(ceil($length / 2));
$apiKey = substr(bin2hex($randomBytes), 0, $length);
if ($split) {
$apiKey = chunk_split($apiKey, $splitLength, '-');
$apiKey = rtrim($apiKey, '-');
}
switch ($case) {
case 'lower':
//Do nothing, the API key generated will be lowercase by default.
break;
case 'upper':
$apiKey = strtoupper($apiKey);
break;
case 'mixed':
$characters = str_split($apiKey);
$result = '';
foreach ($characters as $character) {
if (mt_rand(0, 1) <= 0.5) {
$character = strtoupper($character);
}
$result .= $character;
}
$apiKey = $result;
break;
default:
throw new \Box_Exception("Unknown uppercase option ':case:'. API generator only accepts 'lower', 'upper', or 'mixed'.", [':case:' => $case]);
}
} while (null !== $this->di['db']->findOne('service_apikey', 'api_key = :api_key', [':api_key' => $apiKey]));
return $apiKey;
}
private function isActive(\RedBeanPHP\OODBBean $model): bool
{
$order = $this->di['db']->findOne('ClientOrder', 'service_id = :id AND service_type = "apikey"', [':id' => $model->id]);
if (is_null($order)) {
throw new \Box_Exception("API key does not exist");
}
return $order->status === 'active';
}
}
@@ -0,0 +1,148 @@
{% import 'macro_functions.html.twig' as mf %}
<div class="card-body">
<h1>{{ 'API Key settings'|trans }}</h1>
<form method="post" action="{{ 'api/admin/product/update_config'|link }}" class="api-form save" data-api-msg="{{ 'API key settings updated'|trans }}">
<input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
<div class="mb-3 row">
<P>{{ 'Here you can configured the length of the API keys that will be generated. We recommend making this no less than 16 at a minimum. When in doubt, leave this at 32.'|trans }}</p>
<label class="form-label col-3 col-form-label">{{ 'Length'|trans }}:</label>
<div class="col">
<input type="number" class="form-control" name="config[length]" value="{{ product.config.length|default(32) }}">
</div>
</div>
<hr>
<div class="mb-3 row">
<P>{{ 'FOSSBilling can split the generated API key with dashes depending on the following options. The dashes will not count towards the API key length. If any API key is generated with dashes, they must be included in order for it to be considered valid.'|trans }}</p>
<label class="form-label col-3 col-form-label">{{ 'Split API key with dashes'|trans }}:</label>
<div class="col">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="config[split]" id="split_yes" value="1" {% if product.config.split is not defined or product.config.split == 1 %} checked {% endif %}>
<label class="form-check-label" for="split_yes">{{ 'Yes'|trans }}</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="config[split]" id="split_no" value="0" {% if product.config.split is defined and product.config.split == 0 %} checked {% endif %}>
<label class="form-check-label" for="split_no">{{ 'No'|trans }}</label>
</div>
</div>
</div>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'Split Interval'|trans }}:</label>
<div class="col">
<input type="number" class="form-control" name="config[split_interval]" value="{{ product.config.split_interval|default(8) }}">
</div>
</div>
<hr>
<div class="mb-3 row">
<P>{{ 'Here you can configure what case you want the API keys to be generated with. "Uppercase" makes all letters uppercase, "lowercase" makes them all lowercase, and "mixed" does a combination of the two options.'|trans }}</p>
<label class="form-label col-3 col-form-label">{{ 'API key capitalization'|trans }}:</label>
<div class="col">
<select class="form-select" aria-label="API key capitalization options" name="config[case]">
<option {% if product.config.case is not defined or product.config.case == "upper" %} selected {% endif %} value="upper">{{ 'Uppercase'|trans }}</option>
<option {% if product.config.case == "lower" %} selected {% endif %} value="lower">{{ 'Lowercase'|trans }}</option>
<option {% if product.config.case == "mixed" %} selected {% endif %} value="mixed">{{ 'Mixed'|trans }}</option>
</select>
</div>
</div>
<hr>
<div class="mb-3 row">
<h2>{{ 'Custom Parameters'|trans }}</h2>
<p>{{ 'Here, you have the option to include custom parameters that can be requested from the API. This is particularly useful if you want to set specific limits for API keys. Your application can connect to the FOSSBilling API and retrieve the custom parameters you have defined for an API key.'|trans }}</p>
<P>{{ 'FOSSBilling will remove the "custom_" prefix from the value name when requesting them from the API. IE: custom_monthlyLimit will be named monthlyLimit when fetched through the API.'|trans}}
<div class="col" id="customParameters">
<span class="btn btn-primary" onclick="addNewField()">{{ 'Add new field'|trans }}</span>
{% for key, value in product.config %}
{% if key starts with 'custom_' %}
<div class="field-container row" id="{{key}}">
<label class="form-label col-3 col-form-label">{{key}}:</label>
<div class="col d-flex align-items-center">
<input type="text" class="form-control me-2" value="{{value}}" name="config[{{key}}]"/><span
class="btn btn-primary ml-2" onclick='deleteField("{{key}}")'>Delete</span>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
<input type="hidden" name="id" value="{{ product.id }}">
<button class="btn btn-primary w-100" type="submit">{{ 'Update'|trans }}</button>
</form>
</div>
<script>
function addNewField() {
Modals.create({
type: 'prompt',
title: 'New field',
label: 'Give the new field a name',
value: 'monthlyLimit',
promptConfirmButton: 'Create',
promptConfirmCallback: function (name) {
// We use camel case to ensure it'll be programming-friendly & append 'custom_' to the start of it so they are easily identifiable as custom parameters.
name = 'custom_' + toCamelCase(name);
var container = document.getElementById('customParameters');
// Create the container div
var fieldContainer = document.createElement('div');
fieldContainer.classList.add('field-container', 'row');
fieldContainer.setAttribute('id', name);
// Create the label element
var label = document.createElement('label');
label.classList.add('form-label', 'col-3', 'col-form-label');
label.textContent = name + ':';
// Create the div container for the input and button
var div = document.createElement('div');
div.classList.add('col', 'd-flex', 'align-items-center');
// Create the input element
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.classList.add('form-control');
input.classList.add('me-2');
input.setAttribute('name', 'config[' + name + ']');
// Create the button element
var button = document.createElement('span');
button.classList.add('btn', 'btn-primary', 'ml-2');
button.textContent = 'Delete';
button.setAttribute('onclick', 'deleteField("' + name + '")');
// Append elements
div.appendChild(input);
div.appendChild(button);
// Append the label and div to the container div
fieldContainer.appendChild(label);
fieldContainer.appendChild(div);
// Append the container div to the main container
container.appendChild(fieldContainer);
}
});
}
function deleteField(id){
const element = document.getElementById(id);
element.remove();
}
function toCamelCase(str) {
str = str.replace(/[^a-zA-Z0-9]/g, ' ');
var words = str.split(' ');
// Capitalize the first letter of each word except the first word
for (var i = 1; i < words.length; i++) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
return words.join('');
}
</script>
@@ -0,0 +1,174 @@
{% import 'macro_functions.html.twig' as mf %}
{% set service = admin.order_service({ "id": order.id }) %}
<div class="card-body overflow-auto">
<h3>{{ 'Details'|trans }}</h3>
<table class="table card-table table-vcenter table-striped text-nowrap">
<tbody>
<tr>
<td class="text-end">{{ 'API Key'|trans }}:</td>
<td>{{ service.api_key }}</td>
</tr>
</tbody>
</table>
</div>
<div class="card-footer text-center">
{{ order_actions }}
<a class="btn btn-primary api-link"
href="{{ 'api/admin/serviceapikey/reset'|link({ 'id': service.id, 'CSRFToken': CSRFToken }) }}"
data-api-confirm="{{ 'Are you sure?'|trans }}"
data-api-reload="1">
<svg class="icon" width="24" height="24">
<use xlink:href="#refresh" />
</svg>
<span>{{ 'Reset API key'|trans }}</span>
</a>
</div>
<div class="card-body">
<h2>{{ 'API key configuration'|trans }}</h2>
<P>{{ 'Changing the API key generation options will not automatically regenerate the API key. You must do so manually.'|trans }}</p>
<form action="{{ 'api/admin/serviceapikey/update'|link }}" method="post" class="api-form" data-api-msg="{{ 'API key updated'|trans }}">
<input type="hidden" name="CSRFToken" value="{{ CSRFToken }}"/>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'Length'|trans }}:</label>
<div class="col">
<input type="number" class="form-control" name="config[length]" value="{{ request.config.length|default(service.config.length) }}">
</div>
</div>
<hr>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'Split API key with dashes'|trans }}:</label>
<div class="col">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="config[split]" id="split_yes" value="1" {% if request.config.split|default(service.config.split) == 1 %} checked {% endif %}>
<label class="form-check-label" for="split_yes">{{ 'Yes'|trans }}</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="config[split]" id="split_no" value="0" {% if request.config.split|default(service.config.split) == 0 %} checked {% endif %}>
<label class="form-check-label" for="split_no">{{ 'No'|trans }}</label>
</div>
</div>
</div>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'Split Interval'|trans }}:</label>
<div class="col">
<input type="number" class="form-control" name="config[split_interval]" value="{{ request.split_interval|default(service.config.split_interval) }}">
</div>
</div>
<hr>
<div class="mb-3 row">
<label class="form-label col-3 col-form-label">{{ 'API key capitalization'|trans }}:</label>
<div class="col">
<select class="form-select" aria-label="API key capitalization options" name="config[case]">
<option {% if request.config.case|default(service.config.case) == "upper" %} selected {% endif %} value="upper">{{ 'Uppercase'|trans }}</option>
<option {% if request.config.case|default(service.config.case) == "lower" %} selected {% endif %} value="lower">{{ 'Lowercase'|trans }}</option>
<option {% if request.config.case|default(service.config.case) == "mixed" %} selected {% endif %} value="mixed">{{ 'Mixed'|trans }}</option>
</select>
</div>
</div>
<hr>
<div class="mb-3 row">
<h2>{{ 'Custom Parameters'|trans }}</h2>
<div class="col" id="customParameters">
<span class="btn btn-primary" onclick="addNewField()">{{ 'Add new field'|trans }}</span>
{% for key, value in service.config %}
{% if key starts with 'custom_' %}
<div class="field-container row" id="{{key}}">
<label class="form-label col-3 col-form-label">{{key}}:</label>
<div class="col d-flex align-items-center">
<input type="text" class="form-control me-2" value="{{value}}" name="config[{{key}}]"/><span
class="btn btn-primary ml-2" onclick='deleteField("{{key}}")'>Delete</span>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
<input type="hidden" name="order_id" value="{{ order.id }}">
<input type="submit" value="{{ 'Update'|trans }}" class="btn btn-primary w-100">
</form>
</div>
<script>
function addNewField() {
Modals.create({
type: 'prompt',
title: 'New field',
label: 'Give the new field a name',
value: 'monthlyLimit',
promptConfirmButton: 'Create',
promptConfirmCallback: function (name) {
// We use camel case to ensure it'll be programming-friendly & append 'custom_' to the start of it so they are easily identifiable as custom parameters.
name = 'custom_' + toCamelCase(name);
var container = document.getElementById('customParameters');
// Create the container div
var fieldContainer = document.createElement('div');
fieldContainer.classList.add('field-container', 'row');
fieldContainer.setAttribute('id', name);
// Create the label element
var label = document.createElement('label');
label.classList.add('form-label', 'col-3', 'col-form-label');
label.textContent = name + ':';
// Create the div container for the input and button
var div = document.createElement('div');
div.classList.add('col', 'd-flex', 'align-items-center');
// Create the input element
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.classList.add('form-control');
input.classList.add('me-2');
input.setAttribute('name', 'config[' + name + ']');
// Create the button element
var button = document.createElement('span');
button.classList.add('btn', 'btn-primary', 'ml-2');
button.textContent = 'Delete';
button.setAttribute('onclick', 'deleteField("' + name + '")');
// Append elements
div.appendChild(input);
div.appendChild(button);
// Append the label and div to the container div
fieldContainer.appendChild(label);
fieldContainer.appendChild(div);
// Append the container div to the main container
container.appendChild(fieldContainer);
}
});
}
function deleteField(id){
const element = document.getElementById(id);
element.remove();
}
function toCamelCase(str) {
str = str.replace(/[^a-zA-Z0-9]/g, ' ');
var words = str.split(' ');
// Capitalize the first letter of each word except the first word
for (var i = 1; i < words.length; i++) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
return words.join('');
}
</script>
@@ -0,0 +1,41 @@
{% if order.status == 'active' %}
<div class="row">
<article class="span12 data-block">
<div class="data-container">
<header>
<h1>{{ order.title }}</h1>
</header>
<section>
<h2>{{ 'API key details'|trans }}</h2>
<table class="table table-striped table-bordered table-condensed">
<tbody>
<tr>
<td>{{ 'API key'|trans }}</td>
<td>
<strong>{{ service.api_key }}</strong>
</td>
</tr>
</tbody>
</table>
<h2>{{ 'Reset API key'|trans }}</h2>
<div class="block">
<p><button class="btn btn-primary" type="button" id="license-apikey">{{ 'Reset'|trans }}</button></p>
</div>
</section>
</div>
</article>
</div>
<script type="text/javascript">
document.querySelector('#license-apikey').addEventListener('click', (event) => {
event.preventDefault();
if (confirm("{{ 'Are you sure?'|trans }}")) {
API.client.post('serviceapikey/reset', { order_id: {{ order.id }} }, function(result) {
bb.reload();
});
}
});
</script>
{% endif %}
@@ -0,0 +1,49 @@
{% block subject %}[{{ guest.system_company.name }}] {{ order.title }} Activated{% endblock %}
{% block content %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial, sans-serif;
font-size: 14px;
color: #333333;
}
h1 {
font-size: 24px;
font-weight: bold;
margin: 0 0 20px;
}
p {
margin: 0 0 10px;
}
strong {
font-weight: bold;
}
.signature {
font-style: italic;
color: #999999;
margin-top: 20px;
border-top: 1px solid #cccccc;
padding-top: 10px;
}
</style>
</head>
<body>
<h1>Order is now active</h1>
<p>Hello {{ c.first_name }} {{ c.last_name }},</p>
<p>Your <strong>{{ order.title }}</strong> is now activated.</p>
<p><strong>API key:</strong> {{ service.api_key }}</p>
<p>You may <a href="{{'login'|link({'email' : c.email }) }}" target="_blank">login</a> or <a href="{{ 'order/service/manage'|link }}/{{ order.id }}" target="_blank">manage your order.</a>
<p class="signature">{{ guest.system_company.signature }}</p>
</body>
</html>
{% endblock %}
@@ -0,0 +1,52 @@
{% block subject %}[{{ guest.system_company.name }}] {{ order.title }} Canceled{% endblock %}
{% block content %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial, sans-serif;
font-size: 14px;
color: #333333;
}
h1 {
font-size: 24px;
font-weight: bold;
margin: 0 0 20px;
}
p {
margin: 0 0 10px;
}
strong {
font-weight: bold;
}
.signature {
font-style: italic;
color: #999999;
margin-top: 20px;
border-top: 1px solid #cccccc;
padding-top: 10px;
}
</style>
</head>
<body>
<h1>Order canceled</h1>
<p>Hello {{ c.first_name }} {{ c.last_name }},</p>
<p>Your <strong>{{ order.title }}</strong> that was activated on <strong>{{ order.activated_at|format_date }}</strong> is now canceled.</p>
{% if order.reason %}
<p><strong>Reason:</strong> {{ order.reason }}</p>
{% endif %}
<p>If you have any questions regarding this, please login to the members area and submit a support ticket.</p>
<p>You may <a href="{{'login'|link({'email' : c.email }) }}" target="_blank">login</a> or <a href="{{ 'support'|link({'ticket' : 1}) }}" target="_blank">submit a ticket.</a>
<p class="signature">{{ guest.system_company.signature }}</p>
</body>
</html>
{% endblock %}
@@ -0,0 +1,50 @@
{% block subject %}[{{ guest.system_company.name }}] {{ order.title }} Renewed{% endblock %}
{% block content %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial, sans-serif;
font-size: 14px;
color: #333333;
}
h1 {
font-size: 24px;
font-weight: bold;
margin: 0 0 20px;
}
p {
margin: 0 0 10px;
}
strong {
font-weight: bold;
}
.signature {
font-style: italic;
color: #999999;
margin-top: 20px;
border-top: 1px solid #cccccc;
padding-top: 10px;
}
</style>
</head>
<body>
<h1>Order renewed</h1>
<p>Hello {{ c.first_name }} {{ c.last_name }},</p>
<p>Your <strong>{{ order.title }}</strong> has been renewed.</p>
{% if order.expires_at %}
<p><strong>Next renewal date:</strong> {{ order.expires_at|format_date }}</p>
{% endif %}
<p>You may <a href="{{'login'|link({'email' : c.email }) }}" target="_blank">login</a> or <a href="{{ 'order/service/manage'|link }}/{{ order.id }}" target="_blank">manage your order.</a>
<p class="signature">{{ guest.system_company.signature }}</p>
</body>
</html>
{% endblock %}
@@ -0,0 +1,52 @@
{% block subject %}[{{ guest.system_company.name }}] {{ order.title }} Suspended{% endblock %}
{% block content %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial, sans-serif;
font-size: 14px;
color: #333333;
}
h1 {
font-size: 24px;
font-weight: bold;
margin: 0 0 20px;
}
p {
margin: 0 0 10px;
}
strong {
font-weight: bold;
}
.signature {
font-style: italic;
color: #999999;
margin-top: 20px;
border-top: 1px solid #cccccc;
padding-top: 10px;
}
</style>
</head>
<body>
<h1>Order suspended</h1>
<p>Hello {{ c.first_name }} {{ c.last_name }},</p>
<p>Your <strong>{{ order.title }}</strong> that was activated on <strong>{{ order.activated_at|format_date }}</strong> is now suspended.</p>
{% if order.reason %}
<p><strong>Reason:</strong> {{ order.reason }}</p>
{% endif %}
<p>If you have any questions regarding this, please login to the members area and submit a support ticket.</p>
<p>You may <a href="{{'login'|link({'email' : c.email }) }}" target="_blank">login</a> or <a href="{{ 'support'|link({'ticket' : 1}) }}" target="_blank">submit a ticket.</a>
<p class="signature">{{ guest.system_company.signature }}</p>
</body>
</html>
{% endblock %}
@@ -0,0 +1,47 @@
{% block subject %}[{{ guest.system_company.name }}] {{ order.title }} Reactivated{% endblock %}
{% block content %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: Arial, sans-serif;
font-size: 14px;
color: #333333;
}
h1 {
font-size: 24px;
font-weight: bold;
margin: 0 0 20px;
}
p {
margin: 0 0 10px;
}
strong {
font-weight: bold;
}
.signature {
font-style: italic;
color: #999999;
margin-top: 20px;
border-top: 1px solid #cccccc;
padding-top: 10px;
}
</style>
</head>
<body>
<h1>Order unsuspended</h1>
<p>Hello {{ c.first_name }} {{ c.last_name }},</p>
<p>Your <strong>{{ order.title }}</strong> is now unsuspended.</p>
<p>You may <a href="{{'login'|link({'email' : c.email }) }}" target="_blank">login</a> or <a href="{{ 'order/service/manage'|link }}/{{ order.id }}" target="_blank">manage your order.</a>
<p class="signature">{{ guest.system_company.signature }}</p>
</body>
</html>
{% endblock %}
+1
View File
@@ -0,0 +1 @@
<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12.66 13.67C12.32 14 11.93 14.29 11.5 14.5V21L9.5 23L7.5 21L9.5 19.29L8 18L9.5 16.71L7.5 15V14.5C6 13.77 5 12.26 5 10.5C5 8 7 6 9.5 6C9.54 6 9.58 6 9.61 6C9.59 6.07 9.54 6.12 9.5 6.18C9.23 6.79 9.08 7.43 9.03 8.08C8.43 8.28 8 8.84 8 9.5C8 10.33 8.67 11 9.5 11C9.53 11 9.57 11 9.6 11C10.24 12.25 11.34 13.2 12.66 13.67M16 6C16 5.37 15.9 4.75 15.72 4.18C17.06 4.56 18.21 5.55 18.73 6.96C19.33 8.62 18.89 10.39 17.75 11.59L20 17.68L18.78 20.25L16.22 19.05L17.5 16.76L15.66 16.06L16.63 14.34L14.16 13.41L14 12.95C12.36 12.77 10.88 11.7 10.27 10.04C9.42 7.71 10.63 5.12 12.96 4.27C13.14 4.21 13.33 4.17 13.5 4.13C12.84 2.87 11.53 2 10 2C7.79 2 6 3.79 6 6C6 6.09 6 6.17 6.03 6.26C5.7 6.53 5.4 6.82 5.15 7.15C5.06 6.78 5 6.4 5 6C5 3.24 7.24 1 10 1S15 3.24 15 6C15 7.16 14.6 8.21 13.94 9.06C16.08 8.88 16 6 16 6M12.81 8.1C12.87 8.27 12.96 8.41 13.06 8.54C13.62 7.88 13.97 7.04 14 6.11C13.89 6.13 13.8 6.15 13.7 6.18C12.92 6.47 12.5 7.33 12.81 8.1Z" /></svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

+8
View File
@@ -0,0 +1,8 @@
{
"id": "serviceapikey",
"type": "mod",
"name": "API Key Product",
"description": "A simple product type to allow you to generate and sell API keys for use in your applications",
"icon_url": "icon.svg",
"version": "1.0.0"
}