PHP code example of dnj / laravel-aaa

1. Go to this page and download the library: Download dnj/laravel-aaa 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/ */

    

dnj / laravel-aaa example snippets


[
    'guestType' => 2, // The id of guest type
];



namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Models\Post;
use App\Policies\PostPolicy;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        Post::class => PostPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        ///
    }
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function getPost(Request $request)
    {
        $post = $request->get('post');
        $response = Gate::inspect('view_post_permission', $post);
        if ($response) {
            // allow
        } else {
            // deny
        }
        return view('view_post', ['title' => $post->getTitle()]);
    }
}


use dnj\AAA\Contracts\ITypeManager;
use dnj\AAA\Contracts\IType;


class TypeController extends Controller
{
    private $typeManager;

    public function __construct(ITypeManager $typeManager)
    {
        $this->typeManager = $typeManager;
    }

    public function createType()
    {
        // the name of this type in diffrent languages
        $localizedDetails = [
            'en' => ['title' => 'Admin'],
            'fr' => ['title' => 'Administratrice'],
            'nl' => ['title' => 'beheerder'],
        ];

        // abilities that we want to define for this type (user role)
        $abilities = [
            'add_post',
            'edit_post',
            'remove_post',
            'publish_post',
        ]

        // the subtype IDs, consider this type as parent of other types, that means this type will have permission to another types
        $childIds = [
            // id
        ];

        // if you want to define any other data for this type, use this field
        $meta = [
            'notes' => 'Some notes about this type',
        ];

        // save log for this action
        $userActivityLog = true;

        $type = $this->typeManager->create($localizedDetails, $abilities, $childIds, $meta, $userActivityLog);
    }

    public function updateType(IType $type, array $update)
    {
        $changes = [];

        if (isset($update['localizedDetails'])) {
            $changes['localizedDetails'] = $update['localizedDetails'];
        }
        if (isset($update['abilities'])) {
            $changes['abilities'] = $update['abilities'];
        }
        if (isset($update['childIds'])) {
            $changes['childIds'] = $update['childIds'];
        }
        if (isset($update['meta'])) {
            $changes['meta'] = $update['meta'];
        }
        // save log for this action
        $userActivityLog = true;

        $type = $this->typeManager->create($type, $changes, $userActivityLog);
    }

    public function deleteType(IType $type)
    {
        // save log for this action
        $userActivityLog = true;

        $this->typeManager->delete($type, $userActivityLog);
    }
}

use dnj\AAA\Contracts\IUserManager;
use dnj\AAA\Contracts\IUser;
use dnj\AAA\Contracts\ITypeManager;
use dnj\AAA\Contracts\IType;

class UserController extends Controller
{
    private $userManager;

    public function __construct(IUserManager $userManager)
    {
        $this->userManager = $userManager;
    }

    public function createUser()
    {
        // name of the user
        $name = 'Hossein Hosni'

        // username of the user
        $username = 'hosni'; // or cellphone number, or any unique thing

        // password of the user
        $password = 'SomeStrongPassword@123';

        // the id of the user, or the Type object
        $type = 1;
        // or create user as guest
        $type = app(ITypeManager::class)->getGuestTypeID();

        // if you want to define any other data for this user, use this field
        $meta = [
            'notes' => 'Some notes about this user',
        ];

        // save log for this action
        $userActivityLog = true;

        $user = $this->userManager->create($name, $username, $type, $meta, $userActivityLog);
    }

    public function updateUser(IUser $user, array $update)
    {
        $changes = [];

        if (isset($update['type'])) {
            $changes['type'] = $update['type'];
        }
        if (isset($update['usernames'])) {
            $changes['usernames'] = $update['usernames'];
        }
        // save log for this action
        $userActivityLog = true;

        $user = $this->userManager->create($user, $changes, $userActivityLog);
    }

    public function deleteUser(IUser $user)
    {
        // save log for this action
        $userActivityLog = true;

        $this->userManager->delete($user, $userActivityLog);
    }
}
bash
php artisan vendor:publish --tag=config

INFO  Publishing [config] assets.  

Copying file [vendor/dnj/laravel-aaa/config/aaa.php] to [config/aaa.php] .................................................................... DONE
Copying file [vendor/dnj/laravel-user-logger/config/user-logger.php] to [config/user-logger.php] ............................................ DONE
bash
php artisan make:policy:aaa