Skip to content

Commit

Permalink
Added basic rules system
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Jun 2, 2020
1 parent a1b7b78 commit 0fcf355
Show file tree
Hide file tree
Showing 28 changed files with 2,689 additions and 216 deletions.
6 changes: 5 additions & 1 deletion SELF-HOSTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,18 @@ Set a couple of variables:

```bash
MODULE_NAME="mailparse"
MODULE_VERSION="3.0.4"
MODULE_VERSION="3.1.0"
```

```bash
cd ~

# download using pecl
pecl download $MODULE_NAME

# or you can use wget if pecl is not found
wget https://pecl.php.net/get/$MODULE_NAME-$MODULE_VERSION.tgz

tar -zxvf $MODULE_NAME-$MODULE_VERSION.tgz

cd $MODULE_NAME-$MODULE_VERSION
Expand Down
4 changes: 1 addition & 3 deletions app/Console/Commands/ReceiveEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public function handle()

$this->size = $this->option('size') / ($recipientCount ? $recipientCount : 1);

foreach ($recipients as $key => $recipient) {
$displayTo = $this->parser->getAddresses('to')[$key]['display'] ?? null;

foreach ($recipients as $recipient) {
$parentDomain = collect(config('anonaddy.all_domains'))
->filter(function ($name) use ($recipient) {
return Str::endsWith($recipient['domain'], $name);
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Controllers/Api/ActiveRuleController.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\RuleResource;
use Illuminate\Http\Request;

class ActiveRuleController extends Controller
{
public function store(Request $request)
{
$rule = user()->rules()->findOrFail($request->id);

$rule->activate();

return new RuleResource($rule);
}

public function destroy($id)
{
$rule = user()->rules()->findOrFail($id);

$rule->deactivate();

return response('', 204);
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/Api/ReorderRuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreReorderRuleRequest;
use App\Rule;

class ReorderRuleController extends Controller
{
public function store(StoreReorderRuleRequest $request)
{
collect($request->ids)->each(function ($id, $key) {
$rule = Rule::findOrFail($id);

$rule->update([
'order' => $key
]);
});

return response('', 200);
}
}
57 changes: 57 additions & 0 deletions app/Http/Controllers/Api/RuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreRuleRequest;
use App\Http\Resources\RuleResource;

class RuleController extends Controller
{
public function index()
{
return RuleResource::collection(user()->rules()->orderBy('order')->get());
}

public function show($id)
{
$rule = user()->rules()->findOrFail($id);

return new RuleResource($rule);
}

public function store(StoreRuleRequest $request)
{
$rule = user()->rules()->create([
'name' => $request->name,
'conditions' => $request->conditions,
'actions' => $request->actions,
'operator' => $request->operator
]);

return new RuleResource($rule->refresh());
}

public function update(StoreRuleRequest $request, $id)
{
$rule = user()->rules()->findOrFail($id);

$rule->update([
'name' => $request->name,
'conditions' => $request->conditions,
'actions' => $request->actions,
'operator' => $request->operator
]);

return new RuleResource($rule->refresh());
}

public function destroy($id)
{
$rule = user()->rules()->findOrFail($id);

$rule->delete();

return response('', 204);
}
}
13 changes: 13 additions & 0 deletions app/Http/Controllers/ShowRuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Controllers;

class ShowRuleController extends Controller
{
public function index()
{
return view('rules.index', [
'rules' => user()->rules()->orderBy('order')->get()
]);
}
}
35 changes: 35 additions & 0 deletions app/Http/Requests/StoreReorderRuleRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Requests;

use App\Rules\ValidRuleId;
use Illuminate\Foundation\Http\FormRequest;

class StoreReorderRuleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'ids' => [
'required',
'array',
new ValidRuleId
]
];
}
}
95 changes: 95 additions & 0 deletions app/Http/Requests/StoreRuleRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreRuleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => [
'required',
'string',
'max:50'
],
'conditions' => [
'required',
'array',
'max:5'
],
'conditions.*.type' => [
'required',
Rule::in([
'subject',
'sender',
'alias'
])
],
'conditions.*.match' => [
'sometimes',
'required',
Rule::in([
'is exactly',
'is not',
'contains',
'does not contain',
'starts with',
'does not start with',
'ends with',
'does not end with'
])
],
'conditions.*.values' => [
'required',
'array',
'min:1',
'max:10'
],
'conditions.*.values.*' => [
'distinct',
],
'actions' => [
'required',
'array',
'max:5'
],
'actions.*.type' => [
'required',
Rule::in([
'subject',
'displayFrom',
'encryption',
'banner',
'block',
'webhook'
]),
],
'actions.*.value' => [
'required',
'max:50'
],
'operator' => [
'required',
'in:AND,OR'
]
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/RuleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class RuleResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'user_id' => $this->user_id,
'name' => $this->name,
'order' => $this->order,
'conditions' => $this->conditions,
'actions' => $this->actions,
'operator' => $this->operator,
'active' => $this->active,
'created_at' => $this->created_at->toDateTimeString(),
'updated_at' => $this->updated_at->toDateTimeString(),
];
}
}
23 changes: 14 additions & 9 deletions app/Mail/ForwardEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Helpers\OpenPGPSigner;
use App\Notifications\GpgKeyExpired;
use App\Recipient;
use App\Traits\CheckUserRules;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
Expand All @@ -19,8 +20,9 @@

class ForwardEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
use Queueable, SerializesModels, CheckUserRules;

protected $email;
protected $user;
protected $alias;
protected $sender;
Expand All @@ -36,6 +38,7 @@ class ForwardEmail extends Mailable implements ShouldQueue
protected $openpgpsigner;
protected $dkimSigner;
protected $encryptedParts;
protected $fromEmail;

/**
* Create a new message instance.
Expand Down Expand Up @@ -87,23 +90,23 @@ public function build()

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

$this->dkimSigner = new Swift_Signers_DKIMSigner(config('anonaddy.dkim_signing_key'), $this->alias->domain, config('anonaddy.dkim_selector'));
$this->dkimSigner->ignoreHeader('List-Unsubscribe');
$this->dkimSigner->ignoreHeader('Return-Path');
} else {
$fromEmail = config('mail.from.address');
$this->fromEmail = config('mail.from.address');
$returnPath = config('anonaddy.return_path');
}
} else {
$fromEmail = $this->alias->email;
$this->fromEmail = $this->alias->email;
$returnPath = 'mailer@'.$this->alias->parentDomain();
}

$email = $this
->from($fromEmail, base64_decode($this->displayFrom)." '".$this->sender."'")
$this->email = $this
->from($this->fromEmail, base64_decode($this->displayFrom)." '".$this->sender."'")
->replyTo($replyToEmail)
->subject($this->user->email_subject ?? base64_decode($this->emailSubject))
->text('emails.forward.text')->with([
Expand Down Expand Up @@ -141,20 +144,22 @@ public function build()
});

if ($this->emailHtml) {
$email->view('emails.forward.html')->with([
$this->email->view('emails.forward.html')->with([
'html' => base64_decode($this->emailHtml)
]);
}

foreach ($this->emailAttachments as $attachment) {
$email->attachData(
$this->email->attachData(
base64_decode($attachment['stream']),
base64_decode($attachment['file_name']),
['mime' => base64_decode($attachment['mime'])]
);
}

return $email;
$this->checkRules();

return $this->email;
}

private function isAlreadyEncrypted()
Expand Down
Loading

0 comments on commit 0fcf355

Please sign in to comment.