PHP code example of michele-angioni / phalcon-confer

1. Go to this page and download the library: Download michele-angioni/phalcon-confer 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/ */

    

michele-angioni / phalcon-confer example snippets




namespace MyApp;

use MicheleAngioni\PhalconConfer\ConferTrait;
use MicheleAngioni\PhalconConfer\Models\AbstractConferModel;

class Users extends AbstractConferModel
{
    use ConferTrait;

    protected $id;

    protected $email;

    protected $password;

    public function getId(): int
    {
        return $this->id;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email)
    {
        $this->email = $email;
    }

    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password)
    {
        $this->password = $password;
    }
}



namespace MyApp;

use MicheleAngioni\PhalconConfer\ConferTeamTrait;
use MicheleAngioni\PhalconConfer\Models\AbstractConferTeamModel;

class Teams extends AbstractConferTeamModel
{
    use ConferTeamTrait;

    protected $id;

    protected $name;

    public function getId(): int
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name)
    {
        $this->name = $name;
    }
}

$confer = new Confer(
    new RoleService(new Roles()),
    new PermissionService(new Permissions())
);

$di->setShared('confer', function () use ($di) {
    return new Confer(
     new RoleService(new Roles()),
     new PermissionService(new Permissions())
   );
});

$roles = $confer->getRoles();

$permissions = $confer->getPermissions();

$role = $confer->getRole('Admin'); // Also the Role id can be used as parameter

$permission = $confer->getPermission('manage_roles'); // Also the Permission id can be used as parameter

$role = $confer->createRole([
    'name' => 'Admin'
]);

$permission = $confer->createPermission([
    'name' => 'manage_roles'
]);

$role = $confer->getRole('Admin'); // Also the Role id can be used as parameter

$permission = $confer->getPermission('manage_roles'); // Also the Permission id can be used as parameter

$role->attachPermission($permission);

$role->detachPermission($permission);

$permissions = $role->getPermissions();

$user->attachRole($role);

$user->detachRole($role);

$roles = $user->getRoles();

$user->hasRole($roleName);

$user->can($permissionName);

$team->attachRole($idUser, $role);

$team->detachRole($idUser);

$user->hasRoleInTeam($idTeam, $roleName);
$team->userHasRole($idUser, $roleName);

$team->userCan($idUser, $permissionName);
$team->canInTeam($idTeam, $permissionName);



namespace MyApp;

use Phalcon\Http\Response;
use Phalcon\Mvc\User\Plugin;

class RolesMiddleware extends Plugin
{
    /**
     * The Role name.
     *
     * @var string
     */
    protected $roleName;

    /**
     * @param string $roleName
     */
    function __construct(string $roleName)
    {
        $this->roleName = $roleName;
    }

    /**
     * Check if there is an Authenticated User and if he/she has the is not authenticated, return false
            return false;
        }

        // 3) We have the authenticated User. Check if he/she has the 

$router->addGet('/super-private-route', [
    'module' => 'web',
    'controller' => 'secret',
    'action' => 'index'
])->beforeMatch([new \MyApp\RolesMiddleware('DEV', '/homepage'), 'check']);

$router->addGet('/super-private-route', [
    'module' => 'web',
    'controller' => 'secret',
    'action' => 'index'
])->beforeMatch([new \MicheleAngioni\PhalconConfer\Middlewares('DEV', '/homepage'), 'check']);