Skip to content

Commit

Permalink
Added ability to disable catch-all for domains
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Oct 8, 2020
1 parent 8ecc246 commit 1917f0b
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 145 deletions.
32 changes: 20 additions & 12 deletions app/Http/Controllers/Api/AliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,31 @@ public function store(StoreAliasRequest $request)
return response('', 429);
}

if ($request->input('format', 'uuid') === 'random_words') {
$localPart = user()->generateRandomWordLocalPart();

if (isset($request->validated()['local_part'])) {
$data = [
'email' => $localPart . '@' . $request->domain,
'local_part' => $localPart,
'email' => $request->validated()['local_part'] . '@' . $request->domain,
'local_part' => $request->validated()['local_part'],
];
} else {
$uuid = Uuid::uuid4();

$data = [
'id' => $uuid,
'email' => $uuid . '@' . $request->domain,
'local_part' => $uuid,
];
if ($request->input('format', 'uuid') === 'random_words') {
$localPart = user()->generateRandomWordLocalPart();

$data = [
'email' => $localPart . '@' . $request->domain,
'local_part' => $localPart,
];
} else {
$uuid = Uuid::uuid4();

$data = [
'id' => $uuid,
'email' => $uuid . '@' . $request->domain,
'local_part' => $uuid,
];
}
}


// Check if domain is for additional username or custom domain
$parentDomain = collect(config('anonaddy.all_domains'))
->filter(function ($name) use ($request) {
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Controllers/Api/CatchAllDomainController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\DomainResource;
use Illuminate\Http\Request;

class CatchAllDomainController extends Controller
{
public function store(Request $request)
{
$domain = user()->domains()->findOrFail($request->id);

$domain->enableCatchAll();

return new DomainResource($domain);
}

public function destroy($id)
{
$domain = user()->domains()->findOrFail($id);

$domain->disableCatchAll();

return response('', 204);
}
}
26 changes: 25 additions & 1 deletion app/Http/Requests/StoreAliasRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use App\Rules\ValidAliasLocalPart;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

Expand Down Expand Up @@ -31,7 +32,30 @@ public function rules()
Rule::in($this->user()->domainOptions())
],
'description' => 'nullable|max:100',
'format' => 'nullable|in:uuid,random_words'
'format' => 'nullable|in:uuid,random_words,custom'
];
}

public function withValidator($validator)
{
$validator->sometimes('local_part', [
'required',
'max:50',
Rule::unique('aliases')->where(function ($query) {
return $query->where('local_part', $this->validationData()['local_part'])
->where('domain', $this->validationData()['domain']);
}),
new ValidAliasLocalPart
], function () {
$format = $this->validationData()['format'] ?? 'uuid';
return $format === 'custom';
});
}

public function messages()
{
return [
'local_part.unique' => 'That alias already exists.',
];
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/DomainResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function toArray($request)
'aliases' => AliasResource::collection($this->whenLoaded('aliases')),
'default_recipient' => new RecipientResource($this->whenLoaded('defaultRecipient')),
'active' => $this->active,
'catch_all' => $this->catch_all,
'domain_verified_at' => $this->domain_verified_at ? $this->domain_verified_at->toDateTimeString() : null,
'domain_sending_verified_at' => $this->domain_sending_verified_at ? $this->domain_sending_verified_at->toDateTimeString() : null,
'created_at' => $this->created_at->toDateTimeString(),
Expand Down
20 changes: 19 additions & 1 deletion app/Models/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Domain extends Model
protected $fillable = [
'domain',
'description',
'active'
'active',
'catch_all'
];

protected $dates = [
Expand All @@ -38,6 +39,7 @@ class Domain extends Model
'id' => 'string',
'user_id' => 'string',
'active' => 'boolean',
'catch_all' => 'boolean',
'default_recipient_id' => 'string',
];

Expand Down Expand Up @@ -107,6 +109,22 @@ public function activate()
$this->update(['active' => true]);
}

/**
* Disable catch-all for the domain.
*/
public function disableCatchAll()
{
$this->update(['catch_all' => false]);
}

/**
* Enable catch-all for the domain.
*/
public function enableCatchAll()
{
$this->update(['catch_all' => true]);
}

/**
* Determine if the domain is verified.
*
Expand Down
40 changes: 40 additions & 0 deletions app/Rules/ValidAliasLocalPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class ValidAliasLocalPart implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))$/', $value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Invalid alias local part.';
}
}
Loading

0 comments on commit 1917f0b

Please sign in to comment.