Skip to main content

Overview

The RequestResourceAction class handles the creation of pending resource requests. When users don’t have permission to create resources directly, they can submit a request for admin approval. This action creates the pending request, logs it, and notifies administrators.
This action does not have any constructor dependencies. It can be instantiated directly without dependency injection.

Method Signature

public function execute(
    string $name,
    string $address,
    ?string $description,
    bool $enabled,
    User $user
): PendingResource

Parameters

name
string
required
The name of the requested resource
address
string
required
The network address of the requested resource
description
string|null
required
Optional description of the requested resource. Can be null
enabled
bool
required
Whether the resource should be enabled when created
user
User
required
The user submitting the request

Return Value

return
PendingResource
Returns the created PendingResource Eloquent model instance with the following attributes:
id
int
The auto-generated database ID
name
string
The requested resource name
address
string
The requested resource address
description
string|null
The requested resource description
enabled
bool
Whether the resource should be enabled
user_id
int
The ID of the requesting user
requested_by
string
The name of the requesting user
requested_email
string
The email of the requesting user
status
string
The request status (always 'pending' for new requests)

Side Effects

This action performs the following side effects:
  1. Database Insert: Creates a new PendingResource record with status 'pending'
  2. Audit Log: Creates a ResourceLog entry with action type Requested
  3. Email Notification: Sends a ResourceApprovalRequest email to the admin (if configured)
This action does not interact with the NetBird API. The resource is only created in NetBird after admin approval via ApproveResourceAction.

Example Usage

use App\Actions\RequestResourceAction;
use App\Models\User;

// Instantiate the action (no dependencies required)
$action = new RequestResourceAction();

// Execute the action
$user = User::find(1);
$pendingResource = $action->execute(
    name: 'Development Database',
    address: '10.0.2.50',
    description: 'PostgreSQL database for dev environment',
    enabled: true,
    user: $user
);

// Access the created pending request
echo $pendingResource->id; // e.g., 42
echo $pendingResource->status; // "pending"
echo $pendingResource->requested_by; // User's name

Email Notification

The action automatically sends an email notification to the administrator configured in config('netbird.admin_email'). The email uses the ResourceApprovalRequest mailable to inform the admin of the pending request.
If no admin email is configured (netbird.admin_email is null or empty), no notification will be sent. Ensure your application configuration includes this setting.
Email notifications are sent asynchronously if your application is configured to use queues. If the email fails to send, the request creation still succeeds.

Request Lifecycle

After creation, the pending request can be:
  1. Approved via ApproveResourceAction - Creates the resource in NetBird
  2. Denied via DenyResourceAction - Marks the request as denied
  3. Cancelled via CancelResourceRequestAction - Deletes the pending request

Error Scenarios

This action has minimal error scenarios since it primarily performs database inserts:
  • Database errors: Will throw an exception if the database insert fails
  • Email errors: Will not prevent the request creation from succeeding (emails are typically queued)

Source Reference

Implementation: /home/daytona/workspace/source/app/Actions/RequestResourceAction.php:19 Email notification helper: /home/daytona/workspace/source/app/Actions/RequestResourceAction.php:50

Build docs developers (and LLMs) love