Skip to main content

Overview

The PendingResource model represents a resource creation request that is awaiting approval. It stores all the information needed to create a NetBird resource once approved, along with request metadata and status tracking.

Database Table

Table Name: pending_resources The pending_resources table stores resource creation requests in various states (pending, approved, rejected).

Properties

Fillable Attributes

name
string
required
The name of the resource being requested.
address
string
required
The network address or endpoint for the resource (e.g., IP address, hostname).
description
string
Optional description explaining the purpose or details of the resource.
enabled
boolean
default:"true"
Whether the resource should be enabled when created. Cast to boolean.
user_id
integer
required
The ID of the user who will own the resource once approved.
requested_by
string
required
The name of the person requesting the resource.
requested_email
string
required
The email address of the person requesting the resource.
status
string
required
The current status of the request (e.g., ‘pending’, ‘approved’, ‘rejected’).
decided_at
datetime
Timestamp when the request was approved or rejected. Cast to datetime.

Timestamps

created_at
datetime
Timestamp when the request was created
updated_at
datetime
Timestamp when the request was last updated

Casts

The model automatically casts the following attributes:
'enabled' => 'boolean',
'decided_at' => 'datetime'

Relationships

user()

Defines a belongs to relationship with the User model.
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
Returns: The User instance who will own this resource Example:
$pendingResource = PendingResource::find(1);
$futureOwner = $pendingResource->user;
echo $futureOwner->email;

Methods

isOwnedBy()

Checks if the pending resource request belongs to a specific user.
public function isOwnedBy(User $user): bool
Parameters:
user
User
required
The user instance to check ownership against
Returns: bool - True if the pending resource is for the specified user, false otherwise Example:
$pending = PendingResource::find(1);

if ($pending->isOwnedBy(auth()->user())) {
    // User can view/modify this request
}

isPending()

Checks if the resource request is still pending approval.
public function isPending(): bool
Returns: bool - True if status is ‘pending’, false otherwise Example:
$request = PendingResource::find(1);

if ($request->isPending()) {
    // Show approve/reject buttons
}

toLogChanges()

Returns an array of data suitable for activity logging.
public function toLogChanges(): array
Returns: array<string, mixed> - Array containing key resource attributes for logging Example:
$pending = PendingResource::find(1);
$logData = $pending->toLogChanges();

// Returns:
// [
//     'name' => 'My Resource',
//     'address' => '192.168.1.100',
//     'description' => 'Internal server',
//     'enabled' => true,
//     'requested_by' => 'John Doe'
// ]
This method is specifically designed for use with the ResourceLog model to track changes and actions.

Usage Examples

Creating a Pending Resource Request

$pendingResource = PendingResource::create([
    'name' => 'Development Server',
    'address' => '192.168.1.100',
    'description' => 'Internal development server',
    'enabled' => true,
    'user_id' => auth()->user()->id,
    'requested_by' => auth()->user()->name,
    'requested_email' => auth()->user()->email,
    'status' => 'pending',
]);

Querying Pending Resources

// Get all pending requests
$pending = PendingResource::where('status', 'pending')->get();

// Get user's pending requests
$userRequests = PendingResource::where('user_id', $user->id)
    ->where('status', 'pending')
    ->get();

// Get pending requests with user relationship
$requests = PendingResource::with('user')
    ->where('status', 'pending')
    ->orderBy('created_at', 'desc')
    ->get();

Approving a Request

$pending = PendingResource::findOrFail($id);

if ($pending->isPending()) {
    $pending->update([
        'status' => 'approved',
        'decided_at' => now(),
    ]);
    
    // Log the approval
    $changes = $pending->toLogChanges();
    ResourceLog::logAction(
        'approved',
        $pending->name,
        null,
        $pending->address,
        auth()->user()->email,
        $changes
    );
}

Filtering by Status

// Using the isPending() method
$allRequests = PendingResource::all();
$pendingOnly = $allRequests->filter(fn($req) => $req->isPending());

// Or direct query
$pending = PendingResource::where('status', 'pending')->get();
$approved = PendingResource::where('status', 'approved')->get();
$rejected = PendingResource::where('status', 'rejected')->get();

Model Location

Namespace: App\Models\PendingResource File: app/Models/PendingResource.php:9
Use the toLogChanges() method when integrating with the ResourceLog model to maintain consistent activity tracking.

Build docs developers (and LLMs) love