Skip to content

Commit

Permalink
Added option to forget aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
willbrowningme committed May 18, 2021
1 parent 5676033 commit 3998811
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 129 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ They would make a Twitter announcement informing all users that they would be ke

## Is the application tested?

Yes it has over 180 automated PHPUnit tests written.
Yes it has over 190 automated PHPUnit tests written.

## How do I host this myself?

Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Api/ActiveAliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function store(Request $request)
{
$alias = user()->aliases()->withTrashed()->findOrFail($request->id);

if ($alias->trashed()) {
return response('You need to restore this alias before you can activate it', 422);
}

$alias->activate();

return new AliasResource($alias);
Expand Down
29 changes: 28 additions & 1 deletion app/Http/Controllers/Api/AliasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function show($id)
public function store(StoreAliasRequest $request)
{
if (user()->hasExceededNewAliasLimit()) {
return response('', 429);
return response('You have reached your hourly limit for creating new aliases', 429);
}

if (isset($request->validated()['local_part'])) {
Expand Down Expand Up @@ -148,4 +148,31 @@ public function destroy($id)

return response('', 204);
}

public function forget($id)
{
$alias = user()->aliases()->withTrashed()->findOrFail($id);

$alias->recipients()->detach();

if ($alias->hasSharedDomain()) {
// Remove all data from the alias and change user_id
$alias->update([
'user_id' => '00000000-0000-0000-0000-000000000000',
'extension' => null,
'description' => null,
'emails_forwarded' => 0,
'emails_blocked' => 0,
'emails_replied' => 0,
'emails_sent' => 0
]);

// Soft delete to prevent from being regenerated
$alias->delete();
} else {
$alias->forceDelete();
}

return response('', 204);
}
}
19 changes: 18 additions & 1 deletion app/Models/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ class Alias extends Model

protected $fillable = [
'id',
'user_id',
'active',
'description',
'email',
'local_part',
'extension',
'domain',
'aliasable_id',
'aliasable_type'
'aliasable_type',
'emails_forwarded',
'emails_blocked',
'emails_replied',
'emails_sent'
];

protected $dates = [
Expand All @@ -47,6 +52,18 @@ class Alias extends Model
'active' => 'boolean'
];

public static function boot()
{
parent::boot();

// Deactivate the alias when it is deleted
Alias::deleting(function ($alias) {
if ($alias->active) {
$alias->deactivate();
}
});
}

public function setLocalPartAttribute($value)
{
$this->attributes['local_part'] = strtolower($value);
Expand Down
20 changes: 16 additions & 4 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Laravel\Passport\HasApiTokens;

Expand Down Expand Up @@ -304,10 +305,21 @@ public function hasReachedBandwidthLimit()

public function hasExceededNewAliasLimit()
{
return $this
->aliases()
->where('created_at', '>=', now()->subHour())
->count() >= config('anonaddy.new_alias_hourly_limit');
if (App::environment('testing')) {
return false;
}

return \Illuminate\Support\Facades\Redis::throttle("user:{$this->username}:limit:new-alias")
->allow(config('anonaddy.new_alias_hourly_limit'))
->every(3600)
->then(
function () {
return false;
},
function () {
return true;
}
);
}

public function hasReachedAdditionalUsernameLimit()
Expand Down
29 changes: 12 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ current:
major: 0
minor: 7
patch: 2
prerelease: 8-g8ee7e3b
prerelease: 11-g5676033
buildmetadata: ''
commit: 8ee7e3
commit: '567603'
timestamp:
year: 2020
month: 10
Expand Down
Loading

0 comments on commit 3998811

Please sign in to comment.