Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ php artisan migrate
To configure message body encryption add these key on `.env` file.

```
CHAT_ENCRYPT_MESSAGE=true #boolen
CHAT_ENCRYPT_MESSAGE=true #boolean
```

## Usage
Expand Down
3 changes: 2 additions & 1 deletion config/chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
'media_folder' => env('CHAT_MEDIA_FOLDER', 'image'),
'pusher_event_trigger' => [
'send_message' => env('CHAT_SEND_MESSAGE_PUSHER_EVENT', true)
]
],
'encrypt_message' => env('CHAT_ENCRYPT_MESSAGE', false),
];
12 changes: 12 additions & 0 deletions src/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Encryption\DecryptException;

class Helper
{
Expand Down Expand Up @@ -63,4 +65,14 @@ public static function fileDelete($disk, $path, $fileName)
return true;
}
}

public static function isEncrypted($value)
{
try {
Crypt::decryptString($value);
return true;
} catch (DecryptException $e) {
return false;
}
}
}
31 changes: 31 additions & 0 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace SevenSpan\Chat\Models;

use App\Models\User;
use SevenSpan\Chat\Helpers\Helper;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Message extends Model
Expand Down Expand Up @@ -73,4 +76,32 @@ public function variables()
{
return $this->hasMany(MessageVariable::class, 'message_id', 'id');
}

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
if(config('chat.encrypt_message')) {
return [
'body' => 'encrypted',
];
}else {
return [
'body' => 'string',
];
}
}


protected function body(): Attribute
{
return Attribute::make(
get: function (string $value) {
return Helper::isEncrypted($value) ? Crypt::decryptString($value) : $value;
}
);
}
}