F
Filament9mo ago
130413

How can I encrypt and decrypt an ID

How can I encrypt and decrypt an ID in a URL like http://localhost:4444/admin/users/1/edit for enhanced security, ensuring that the ID is not directly exposed?
No description
No description
5 Replies
130413
1304139mo ago
thank you, I've tried this before, I understand how to use it, only I use the shield plugin, there are lots of ID relationships to users. I'm confused about using it.
toeknee
toeknee9mo ago
It should be possible with Shield if memory serves, it's just changing a few to be more string based opposed to auto-incremementing integers
130413
1304139mo ago
I can't use the ID because in the shield, the ID must be set as the default, but as far as I know, UUIDs shouldn't be set as default.
No description
toeknee
toeknee8mo ago
You need to use a UUID trait to set a default on the usermodel. i.,e.
<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait UUID
{
protected static function boot()
{
// Boot other traits on the Model
parent::boot();

/**
* Listen for the creating event on the user model.
* Sets the 'id' to a UUID using Str::uuid() on the instance being created
*/
static::creating(function ($model) {
if ($model->getKey() === null) {
$model->setAttribute($model->getKeyName(), Str::uuid()->toString());
}
});
}

// Tells the database not to auto-increment this field
public function getIncrementing()
{
return false;
}

// Helps the application specify the field type in the database
public function getKeyType()
{
return 'string';
}
}
<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait UUID
{
protected static function boot()
{
// Boot other traits on the Model
parent::boot();

/**
* Listen for the creating event on the user model.
* Sets the 'id' to a UUID using Str::uuid() on the instance being created
*/
static::creating(function ($model) {
if ($model->getKey() === null) {
$model->setAttribute($model->getKeyName(), Str::uuid()->toString());
}
});
}

// Tells the database not to auto-increment this field
public function getIncrementing()
{
return false;
}

// Helps the application specify the field type in the database
public function getKeyType()
{
return 'string';
}
}