PHP code example of kenepa / resource-lock

1. Go to this page and download the library: Download kenepa/resource-lock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kenepa / resource-lock example snippets


>    'resource' => [
>        'class' => \Kenepa\ResourceLock\Resources\LockResource::class,
>    ],
> 

use Kenepa\ResourceLock\ResourceLockPlugin;
use Filament\Panel;
 
public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugin(ResourceLockPlugin::make());
}

// Post.php

use Kenepa\ResourceLock\Models\Concerns\HasLocks;

class Post extends Model
{
    use HasFactory;
    use HasLocks;

    protected $table = 'posts';

    protected $guarded = [];
}

// EditPost.php

use Kenepa\ResourceLock\Resources\Pages\Concerns\UsesResourceLock;

class EditPost extends EditRecord
{
    use UsesResourceLock;

    protected static string $resource = PostResource::class;
}

// ManagePosts.php

use Kenepa\ResourceLock\Resources\Pages\Concerns\UsesSimpleResourceLock;

class ManagePosts extends ManageRecords
{
    use UsesSimpleResourceLock;

    protected static string $resource = PostResource::class;

}

// resource-lock.php

   /*
    |--------------------------------------------------------------------------
    | Resource Unlocker
    |--------------------------------------------------------------------------
    |
    | The unlocker configuration specifies whether limited access is enabled for
    | the resource lock feature. If limited access is enabled, only specific
    | users or roles will be able to unlock locked resources.
    |
    */

    'unlocker' => [
        'limited_access' => true,
        'gate' => 'unlock'
    ],


// Example using gates
// More info about gates: https://laravel.com/docs/authorization#writing-gates
Gate::define('unlock', function (User $user, Post $post) {
  return $user->email === '[email protected]';
});

// Example using spatie permission package
Permission::create(['name' => 'unlock']);

// resource-lock.php

 /*
    |--------------------------------------------------------------------------
    | Models
    |--------------------------------------------------------------------------
    |
    | The models configuration specifies the classes that represent your application's
    | data objects. This configuration is used by the framework to interact with
    | the application's data models. You can even implement your own ResourceLock model.
    |
    */

    'models' => [
        'User' => \App\Models\CustomUser::class,
         'ResourceLock' => \App\Models\CustomResourceLock::class,
    ],

// CustomGetResourceLockOwnerAction.php

namespace App\Actions;

use Kenepa\ResourceLock\Actions\GetResourceLockOwnerAction;

class CustomResourceLockOwnerAction extends GetResourceLockOwnerAction
{
    public function execute($userModel): string|null
    {
        return $userModel->email;
    }
}

// resource-lock.php

    'actions' => [
-       'get_resource_lock_owner_action' => \Kenepa\ResourceLock\Actions\GetResourceLockOwnerAction::class
+       'get_resource_lock_owner_action' => \Kenepa\ResourceLock\Actions\CustomGetResourceLockOwnerAction::class   
    ],


     public function resourceLockReturnUrl(): string 
    {
        return 'https://laracasts.com';
    }
bash
php artisan resource-lock:install
bash
php artisan vendor:publish --tag=resource-lock-config
resource-lock.php
CustomGetResourceLockOwnerAction.php
bash
php artisan vendor:publish --tag="resource-lock-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="resource-lock-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="resource-lock-config"
bash
php artisan vendor:publish --tag="resource-lock-views"