Organisations

A lot of applications have an organisations structure. Teams, Organisations, Offices, … To add this functionality to your Application, you will have to create an entity that implements LaravelDoctrine\ACL\Contracts\Organisation. Next change acl.organisations.entity to your entity.

<?php

namespace App;

use Doctrine\ORM\Mapping as ORM;
use LaravelDoctrine\ACL\Contracts\Organisation;
use LaravelDoctrine\ACL\Attribute as ACL;

#[ORM\Entity]
class Team implements Organisation
{
    #[ORM\Column(type: "integer")]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: "AUTO")]
    protected $id;

    #[ORM\Column(type: "string")]
    protected $name;

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

You can use the Organisation stub as a starting point for your own entity.

php artisan vendor:publish --tag="acl-entity-organisation"

This command will publish the [Organisation](../stubs/Organisation.php) stub for the Organisation entity to the app/Entities directory.

> Note: Pay attention that we published a stub for Organisation so you should update acl.organisation.entity in the config file.

User can belong to one organisation

The User class should implement LaravelDoctrine\ACL\Contracts\BelongsToOrganisation. You can use the #[ACL\BelongsToOrganisation] attribute to define the relation.

<?php

use Doctrine\ORM\Mapping as ORM;
use LaravelDoctrine\ACL\Attribute as ACL;
use LaravelDoctrine\ACL\Contracts\BelongsToOrganisation;

#[ORM\Entity]
class User implements BelongsToOrganisation
{
    #[ACL\BelongsToOrganisation]
    protected $organisation;

    /**
     * @return Organisation
     */
    public function getOrganisation()
    {
        return $this->organisation;
    }
}

User can belong to multiple organisations

The User class should implement LaravelDoctrine\ACL\Contracts\BelongsToOrganisations. You can use the #[ACL\BelongsToOrganisations] attribute to define the relation.

<?php

use Doctrine\ORM\Mapping as ORM;
use LaravelDoctrine\ACL\Attribute as ACL;
use LaravelDoctrine\ACL\Contracts\BelongsToOrganisations;

#[ORM\Entity]
class User implements BelongsToOrganisations
{
    #[ACL\BelongsToOrganisations]
    protected $organisations;

    /**
     * @return Organisation[]
     */
    public function getOrganisations()
    {
        return $this->organisations;
    }
}

Checking if a User has a certain Organisation

The LaravelDoctrine\ACL\Organisations\BelongsToOrganisation trait provides methods to check if the User has a certain Organisation.

$user->belongsToOrganisation($org);

An array of Organisations or Organisation names can also be checked for.

$user->belongsToOrganisation([$org1,$org2,$org3]);
$user->belongsToOrganisation(['Company 1','Company 2','Company 3']);

Specifying true for the second argument will check that all roles are present.

$user->belongsToOrganisation([$org1,$org2,$org3], true); //User must belong to all three organisations to return true
$user->belongsToOrganisation(['Company 1','Company 2','Company 3'], true);

This is documentation for laravel-doctrine/acl. Please add your ★ star to the project.