Skip to content

Commit

Permalink
Add recipient_ids option to create alias
Browse files Browse the repository at this point in the history
  • Loading branch information
willbrowningme committed Feb 24, 2021
1 parent 6920570 commit 9e0ca10
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/AliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public function store(StoreAliasRequest $request)

$alias = user()->aliases()->create($data);

if ($request->recipient_ids) {
$alias->recipients()->sync($request->recipient_ids);
}

return new AliasResource($alias->refresh()->load('recipients'));
}

Expand Down
9 changes: 8 additions & 1 deletion app/Http/Requests/StoreAliasRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Requests;

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

Expand Down Expand Up @@ -32,7 +33,13 @@ public function rules()
Rule::in($this->user()->domainOptions())
],
'description' => 'nullable|max:100',
'format' => 'nullable|in:random_characters,uuid,random_words,custom'
'format' => 'nullable|in:random_characters,uuid,random_words,custom',
'recipient_ids' => [
'nullable',
'array',
'max:10',
new VerifiedRecipientId
]
];
}

Expand Down
4 changes: 2 additions & 2 deletions app/Mail/ReplyToEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public function __construct(User $user, Alias $alias, EmailData $emailData)
*/
public function build()
{
$returnPath = $this->alias->email;

if ($this->alias->isCustomDomain()) {
if ($this->alias->aliasable->isVerifiedForSending()) {
$this->fromEmail = $this->alias->email;
$returnPath = $this->alias->email;

if (config('anonaddy.dkim_signing_key')) {
$this->dkimSigner = new Swift_Signers_DKIMSigner(config('anonaddy.dkim_signing_key'), $this->alias->domain, config('anonaddy.dkim_selector'));
Expand All @@ -77,7 +78,6 @@ public function build()
}
} else {
$this->fromEmail = $this->alias->email;
$returnPath = 'mailer@'.$this->alias->parentDomain();
}

$this->email = $this
Expand Down
4 changes: 2 additions & 2 deletions app/Mail/SendFromEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public function __construct(User $user, Alias $alias, EmailData $emailData)
*/
public function build()
{
$returnPath = $this->alias->email;

if ($this->alias->isCustomDomain()) {
if ($this->alias->aliasable->isVerifiedForSending()) {
$this->fromEmail = $this->alias->email;
$returnPath = $this->alias->email;

if (config('anonaddy.dkim_signing_key')) {
$this->dkimSigner = new Swift_Signers_DKIMSigner(config('anonaddy.dkim_signing_key'), $this->alias->domain, config('anonaddy.dkim_selector'));
Expand All @@ -73,7 +74,6 @@ public function build()
}
} else {
$this->fromEmail = $this->alias->email;
$returnPath = 'mailer@'.$this->alias->parentDomain();
}

$this->email = $this
Expand Down
30 changes: 28 additions & 2 deletions resources/js/pages/Aliases.vue
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,13 @@
</div>
</div>

<label for="alias_domain" class="block text-grey-700 text-sm mt-4 mb-2">
<label for="alias_format" class="block text-grey-700 text-sm mt-4 mb-2">
Alias Format:
</label>
<div class="block relative w-full mb-4">
<select
v-model="generateAliasFormat"
id="alias_domain"
id="alias_format"
class="block appearance-none w-full text-grey-700 bg-grey-100 p-3 pr-8 rounded shadow focus:ring"
required
>
Expand Down Expand Up @@ -501,6 +501,29 @@
autofocus
/>

<label for="alias_recipient_ids" class="block text-grey-700 text-sm my-2">
Recipients:
</label>
<p v-show="errors.generateAliasRecipientIds" class="mb-3 text-red-500 text-sm">
{{ errors.generateAliasRecipientIds }}
</p>
<multiselect
id="alias_recipient_ids"
v-model="generateAliasRecipientIds"
:options="recipientOptions"
:multiple="true"
:close-on-select="true"
:clear-on-select="false"
:searchable="true"
:max="10"
placeholder="Select recipients (optional)..."
label="email"
track-by="email"
:preselect-first="false"
:show-labels="false"
>
</multiselect>

<div class="mt-6">
<button
@click="generateNewAlias"
Expand Down Expand Up @@ -828,6 +851,7 @@ export default {
generateAliasDomain: this.defaultAliasDomain ? this.defaultAliasDomain : this.domain,
generateAliasLocalPart: '',
generateAliasDescription: '',
generateAliasRecipientIds: [],
generateAliasFormat: this.defaultAliasFormat ? this.defaultAliasFormat : 'random_characters',
aliasFormatOptions: [
{
Expand Down Expand Up @@ -1086,6 +1110,7 @@ export default {
local_part: this.generateAliasLocalPart,
description: this.generateAliasDescription,
format: this.generateAliasFormat,
recipient_ids: _.map(this.generateAliasRecipientIds, recipient => recipient.id),
}),
{
headers: { 'Content-Type': 'application/json' },
Expand All @@ -1095,6 +1120,7 @@ export default {
this.generateAliasLoading = false
this.generateAliasLocalPart = ''
this.generateAliasDescription = ''
this.generateAliasRecipientIds = []
this.rows.push(data.data)
this.generateAliasModalOpen = false
this.success('New alias generated successfully')
Expand Down
4 changes: 2 additions & 2 deletions resources/views/settings/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@
<p class="mt-6">If necessary, you may logout of all of your other browser sessions across all of your devices. If you feel your account has been compromised, you should also update your password.</p>

<div class="mt-6 flex flex-wrap mb-4">
<label for="current" class="block text-grey-700 text-sm mb-2">
<label for="current-password-sessions" class="block text-grey-700 text-sm mb-2">
{{ __('Current Password') }}:
</label>

<input id="current" type="password" class="appearance-none bg-grey-100 rounded w-full p-3 text-grey-700 focus:ring{{ $errors->has('current_password_sesssions') ? ' border-red-500' : '' }}" name="current_password_sesssions" placeholder="********" required>
<input id="current-password-sessions" type="password" class="appearance-none bg-grey-100 rounded w-full p-3 text-grey-700 focus:ring{{ $errors->has('current_password_sesssions') ? ' border-red-500' : '' }}" name="current_password_sesssions" placeholder="********" required>

@if ($errors->has('current_password_sesssions'))
<p class="text-red-500 text-xs italic mt-4">
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/Api/AliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\AdditionalUsername;
use App\Models\Alias;
use App\Models\Domain;
use App\Models\Recipient;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

Expand Down Expand Up @@ -107,6 +108,31 @@ public function user_can_generate_new_alias()
$this->assertEquals($this->user->aliases[0]->local_part, $response->getData()->data->local_part);
}

/** @test */
public function user_can_generate_alias_with_recipients()
{
$recipient = Recipient::factory()->create([
'user_id' => $this->user->id
]);

$recipient2 = Recipient::factory()->create([
'user_id' => $this->user->id
]);

$response = $this->json('POST', '/api/v1/aliases', [
'domain' => 'anonaddy.me',
'description' => 'the description',
'recipient_ids' => [
$recipient->id,
$recipient2->id
]
]);

$response->assertStatus(201);
$this->assertCount(2, $this->user->aliases[0]->recipients);
$this->assertEquals($recipient->email, $this->user->aliases[0]->recipients[0]->email);
}

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

0 comments on commit 9e0ca10

Please sign in to comment.