Skip to content

Commit

Permalink
Fix for alias custom local part with extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Oct 8, 2020
1 parent 1917f0b commit 0fe31fe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion SELF-HOSTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ user = anonaddy
password = your-database-password
hosts = 127.0.0.1
dbname = anonaddy_database
query = SELECT (SELECT 'DISCARD' FROM additional_usernames WHERE (CONCAT(username, '.example.com') = SUBSTRING_INDEX('%s','@',-1)) AND active = 0) AS usernames, (SELECT 'DISCARD' FROM domains WHERE domain = SUBSTRING_INDEX('%s','@',-1) AND active = 0) AS domains LIMIT 1;
query = SELECT (SELECT 'DISCARD' FROM additional_usernames WHERE (CONCAT(username, '.example.com') = SUBSTRING_INDEX('%s','@',-1)) AND active = 0) AS usernames, (SELECT CASE WHEN NOT EXISTS(SELECT NULL FROM aliases WHERE email='%s') AND catch_all = 0 THEN 'REJECT' WHEN active=0 THEN 'DISCARD' ELSE NULL END FROM domains WHERE domain = SUBSTRING_INDEX('%s','@',-1)) AS domains LIMIT 1;
```

This file is responsible for checking whether the alias is for an additional username/custom domain and if so then is that additional username/custom domain set as active. If it is not set as active then the email is discarded.
Expand Down
13 changes: 11 additions & 2 deletions app/Http/Controllers/Api/AliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ public function store(StoreAliasRequest $request)
}

if (isset($request->validated()['local_part'])) {
$localPart = $request->validated()['local_part'];

// Local part has extension
if (Str::contains($localPart, '+')) {
$extension = Str::after($localPart, '+');
$localPart = Str::before($localPart, '+');
}

$data = [
'email' => $request->validated()['local_part'] . '@' . $request->domain,
'local_part' => $request->validated()['local_part'],
'email' => $localPart . '@' . $request->domain,
'local_part' => $localPart,
'extension' => $extension ?? null
];
} else {
if ($request->input('format', 'uuid') === 'random_words') {
Expand Down
1 change: 1 addition & 0 deletions app/Models/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Alias extends Model
'description',
'email',
'local_part',
'extension',
'domain',
'aliasable_id',
'aliasable_type'
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Api/AliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ public function user_can_generate_new_alias_with_local_part()
$this->assertEquals('valid-local-part@'.$this->user->username . '.anonaddy.com', $this->user->aliases[0]->email);
}

/** @test */
public function user_can_generate_new_alias_with_local_part_and_extension()
{
$response = $this->json('POST', '/api/v1/aliases', [
'domain' => $this->user->username . '.anonaddy.com',
'format' => 'custom',
'description' => 'the description',
'local_part' => 'valid-local-part+extension'
]);

$response->assertStatus(201);
$this->assertCount(1, $this->user->aliases);
$this->assertEquals('valid-local-part', $response->getData()->data->local_part);
$this->assertEquals('extension', $response->getData()->data->extension);
$this->assertEquals('valid-local-part@'.$this->user->username . '.anonaddy.com', $this->user->aliases[0]->email);
}

/** @test */
public function user_cannot_generate_new_alias_with_invalid_local_part()
{
Expand Down

0 comments on commit 0fe31fe

Please sign in to comment.