2022-06-19 20:05:45 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhooks controller.
|
|
|
|
*
|
|
|
|
* Handles the webhooks related operations.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Webhooks extends EA_Controller
|
|
|
|
{
|
2024-12-19 22:23:43 +03:00
|
|
|
public array $allowed_webhook_fields = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'url',
|
|
|
|
'actions',
|
|
|
|
'secret_header',
|
|
|
|
'secret_token',
|
|
|
|
'is_ssl_verified',
|
|
|
|
'notes',
|
|
|
|
];
|
2024-05-11 17:42:01 +03:00
|
|
|
|
2024-08-13 14:34:03 +03:00
|
|
|
public array $optional_webhook_fields = [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
/**
|
|
|
|
* Webhooks constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('webhooks_model');
|
|
|
|
$this->load->model('roles_model');
|
|
|
|
|
|
|
|
$this->load->library('accounts');
|
|
|
|
$this->load->library('timezones');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the backend webhooks page.
|
|
|
|
*
|
|
|
|
* On this page admin users will be able to manage webhooks, which are eventually selected by customers during the
|
|
|
|
* booking process.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function index(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
|
|
|
session(['dest_url' => site_url('webhooks')]);
|
|
|
|
|
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (cannot('view', PRIV_WEBHOOKS)) {
|
|
|
|
if ($user_id) {
|
2022-06-19 20:05:45 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
redirect('login');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
|
|
|
|
|
|
|
script_vars([
|
|
|
|
'user_id' => $user_id,
|
2023-12-22 13:35:41 +03:00
|
|
|
'role_slug' => $role_slug,
|
2022-06-19 20:05:45 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
html_vars([
|
|
|
|
'page_title' => lang('webhooks'),
|
|
|
|
'active_menu' => PRIV_SYSTEM_SETTINGS,
|
|
|
|
'user_display_name' => $this->accounts->get_user_display_name($user_id),
|
|
|
|
'timezones' => $this->timezones->to_array(),
|
|
|
|
'privileges' => $this->roles_model->get_permissions_by_slug($role_slug),
|
|
|
|
'available_actions' => [
|
|
|
|
WEBHOOK_APPOINTMENT_SAVE,
|
|
|
|
WEBHOOK_APPOINTMENT_DELETE,
|
|
|
|
WEBHOOK_UNAVAILABILITY_SAVE,
|
|
|
|
WEBHOOK_UNAVAILABILITY_DELETE,
|
2023-11-17 10:06:34 +03:00
|
|
|
WEBHOOK_BLOCKED_PERIOD_SAVE,
|
|
|
|
WEBHOOK_BLOCKED_PERIOD_DELETE,
|
2022-06-19 20:05:45 +03:00
|
|
|
WEBHOOK_CUSTOMER_SAVE,
|
|
|
|
WEBHOOK_CUSTOMER_DELETE,
|
|
|
|
WEBHOOK_SERVICE_SAVE,
|
|
|
|
WEBHOOK_SERVICE_DELETE,
|
2023-10-26 06:12:10 +03:00
|
|
|
WEBHOOK_SERVICE_CATEGORY_SAVE,
|
|
|
|
WEBHOOK_SERVICE_CATEGORY_DELETE,
|
2022-06-19 20:05:45 +03:00
|
|
|
WEBHOOK_PROVIDER_SAVE,
|
|
|
|
WEBHOOK_PROVIDER_DELETE,
|
|
|
|
WEBHOOK_SECRETARY_SAVE,
|
|
|
|
WEBHOOK_SECRETARY_DELETE,
|
|
|
|
WEBHOOK_ADMIN_SAVE,
|
2023-12-22 13:35:41 +03:00
|
|
|
WEBHOOK_ADMIN_DELETE,
|
|
|
|
],
|
2022-06-19 20:05:45 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->load->view('pages/webhooks');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter webhooks by the provided keyword.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function search(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_WEBHOOKS)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$keyword = request('keyword', '');
|
|
|
|
|
2024-05-11 16:02:12 +03:00
|
|
|
$order_by = request('order_by', 'update_datetime DESC');
|
2022-06-19 20:05:45 +03:00
|
|
|
|
|
|
|
$limit = request('limit', 1000);
|
|
|
|
|
2024-05-11 16:02:12 +03:00
|
|
|
$offset = (int) request('offset', '0');
|
2022-06-19 20:05:45 +03:00
|
|
|
|
|
|
|
$webhooks = $this->webhooks_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
|
|
|
json_response($webhooks);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-26 06:41:25 +03:00
|
|
|
* Store a new webhook.
|
2022-06-19 20:05:45 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function store(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('add', PRIV_WEBHOOKS)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook = request('webhook');
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->webhooks_model->only($webhook, $this->allowed_webhook_fields);
|
2022-06-19 20:05:45 +03:00
|
|
|
|
2024-08-13 14:34:03 +03:00
|
|
|
$this->webhooks_model->optional($webhook, $this->optional_webhook_fields);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$webhook_id = $this->webhooks_model->save($webhook);
|
|
|
|
|
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $webhook_id,
|
2022-06-19 20:05:45 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a webhook.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function update(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('edit', PRIV_WEBHOOKS)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook = request('webhook');
|
|
|
|
|
2024-05-11 17:42:01 +03:00
|
|
|
$this->webhooks_model->only($webhook, $this->allowed_webhook_fields);
|
2022-06-19 20:05:45 +03:00
|
|
|
|
2024-08-13 14:34:03 +03:00
|
|
|
$this->webhooks_model->optional($webhook, $this->optional_webhook_fields);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$webhook_id = $this->webhooks_model->save($webhook);
|
|
|
|
|
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $webhook_id,
|
2022-06-19 20:05:45 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a webhook.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function destroy(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('delete', PRIV_WEBHOOKS)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook_id = request('webhook_id');
|
|
|
|
|
|
|
|
$this->webhooks_model->delete($webhook_id);
|
|
|
|
|
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2022-06-19 20:05:45 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a webhook.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function find(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_WEBHOOKS)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook_id = request('webhook_id');
|
|
|
|
|
|
|
|
$webhook = $this->webhooks_model->find($webhook_id);
|
|
|
|
|
|
|
|
json_response($webhook);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|