feat: Admin form action handlers
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AskTamMCP\Admin;
|
||||||
|
|
||||||
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles admin-post.php form submissions for key create/revoke from the WP admin UI.
|
||||||
|
*/
|
||||||
|
class AdminActions {
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
add_action('admin_post_asktam_mcp_create_key', [$this, 'handleCreate']);
|
||||||
|
add_action('admin_post_asktam_mcp_revoke_key', [$this, 'handleRevoke']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleCreate(): void {
|
||||||
|
if (!current_user_can('manage_options')) wp_die('Unauthorized');
|
||||||
|
check_admin_referer('asktam_mcp_create_key', '_wpnonce_mcp');
|
||||||
|
|
||||||
|
$name = sanitize_text_field($_POST['key_name'] ?? '');
|
||||||
|
$scopes = array_map('sanitize_text_field', (array) ($_POST['scopes'] ?? ['read']));
|
||||||
|
|
||||||
|
if (empty($name)) {
|
||||||
|
wp_redirect(add_query_arg(['mcp_error' => 'name_required'], admin_url('admin.php?page=asktam-mcp')));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
$table = $wpdb->prefix . 'asktam_mcp_keys';
|
||||||
|
$rawKey = \AskTamMCP\Core\Auth::generateKey();
|
||||||
|
|
||||||
|
$wpdb->insert($table, [
|
||||||
|
'name' => $name,
|
||||||
|
'api_key' => $rawKey,
|
||||||
|
'scopes' => implode(',', $scopes),
|
||||||
|
'created_at' => current_time('mysql'),
|
||||||
|
'is_active' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
wp_redirect(add_query_arg(['mcp_created' => 1], admin_url('admin.php?page=asktam-mcp')));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleRevoke(): void {
|
||||||
|
if (!current_user_can('manage_options')) wp_die('Unauthorized');
|
||||||
|
$id = (int) ($_POST['key_id'] ?? 0);
|
||||||
|
check_admin_referer('asktam_mcp_revoke_' . $id, '_wpnonce_mcp');
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
$table = $wpdb->prefix . 'asktam_mcp_keys';
|
||||||
|
$wpdb->update($table, ['is_active' => 0], ['id' => $id]);
|
||||||
|
|
||||||
|
wp_redirect(add_query_arg(['mcp_revoked' => 1], admin_url('admin.php?page=asktam-mcp')));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user