Skip to content

Commit

Permalink
Add random_characters alias format
Browse files Browse the repository at this point in the history
  • Loading branch information
willbrowningme committed Jan 5, 2021
1 parent f782a39 commit 660bf18
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
9 changes: 8 additions & 1 deletion app/Http/Controllers/Api/AliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ public function store(StoreAliasRequest $request)
'extension' => $extension ?? null
];
} else {
if ($request->input('format', 'uuid') === 'random_words') {
if ($request->input('format', 'random_characters') === 'random_words') {
$localPart = user()->generateRandomWordLocalPart();

$data = [
'email' => $localPart . '@' . $request->domain,
'local_part' => $localPart,
];
} elseif ($request->input('format', 'random_characters') === 'random_characters') {
$localPart = user()->generateRandomCharacterLocalPart(8);

$data = [
'email' => $localPart . '@' . $request->domain,
'local_part' => $localPart,
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreAliasRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function rules()
Rule::in($this->user()->domainOptions())
],
'description' => 'nullable|max:100',
'format' => 'nullable|in:uuid,random_words,custom'
'format' => 'nullable|in:random_characters,uuid,random_words,custom'
];
}

Expand All @@ -47,7 +47,7 @@ public function withValidator($validator)
}),
new ValidAliasLocalPart
], function () {
$format = $this->validationData()['format'] ?? 'uuid';
$format = $this->validationData()['format'] ?? 'random_characters';
return $format === 'custom';
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/UpdateDefaultAliasFormatRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function rules()
'format' => [
'required',
'string',
'in:uuid,random_words,custom'
'in:random_characters,uuid,random_words,custom'
]
];
}
Expand Down
14 changes: 14 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ public function generateRandomWordLocalPart()
->implode('.').mt_rand(0, 999);
}

public function generateRandomCharacterLocalPart(int $length) : string
{
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';

$str = '';

for ($i = 0; $i < $length; $i++) {
$index = random_int(0, 35);
$str .= $alphabet[$index];
}

return $str;
}

public function domainOptions()
{
$customDomains = $this->verifiedDomains()->pluck('domain')->toArray();
Expand Down
8 changes: 6 additions & 2 deletions resources/js/pages/Aliases.vue
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,12 @@ export default {
generateAliasDomain: this.defaultAliasDomain ? this.defaultAliasDomain : this.domain,
generateAliasLocalPart: '',
generateAliasDescription: '',
generateAliasFormat: this.defaultAliasFormat ? this.defaultAliasFormat : 'uuid',
generateAliasFormat: this.defaultAliasFormat ? this.defaultAliasFormat : 'random_characters',
aliasFormatOptions: [
{
value: 'random_characters',
label: 'Random Characters',
},
{
value: 'uuid',
label: 'UUID',
Expand Down Expand Up @@ -909,7 +913,7 @@ export default {
}
},
watch: {
showAliases(value) {
showAliases() {
this.updateAliases()
},
},
Expand Down
1 change: 1 addition & 0 deletions resources/views/settings/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@

<div class="block relative w-full">
<select id="default-alias-format" class="block appearance-none w-full text-grey-700 bg-grey-100 p-3 pr-8 rounded shadow focus:ring" name="format" required>
<option value="random_characters" {{ $user->default_alias_format === 'random_characters' ? 'selected' : '' }}>Random Characters</option>
<option value="uuid" {{ $user->default_alias_format === 'uuid' ? 'selected' : '' }}>UUID</option>
<option value="random_words" {{ $user->default_alias_format === 'random_words' ? 'selected' : '' }}>Random Words</option>
<option value="custom" {{ $user->default_alias_format === 'custom' ? 'selected' : '' }}>Custom</option>
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/Api/AliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public function user_can_generate_new_alias()
'local_part' => 'not-required-for-shared-alias'
]);

$response->assertStatus(201);
$this->assertCount(1, $this->user->aliases);
$this->assertEquals($this->user->aliases[0]->local_part, $response->getData()->data->local_part);
}

/** @test */
public function user_can_generate_new_uuid_alias()
{
$response = $this->json('POST', '/api/v1/aliases', [
'domain' => 'anonaddy.me',
'format' => 'uuid',
'description' => 'the description',
'local_part' => 'not-required-for-shared-alias'
]);

$response->assertStatus(201);
$this->assertCount(1, $this->user->aliases);
$this->assertEquals($this->user->aliases[0]->id, $response->getData()->data->local_part);
Expand Down

0 comments on commit 660bf18

Please sign in to comment.