Create a built-in role

Built-in roles are bundled with your app, theme or plugin. They are not managed through the administration interface, therefore not deleteable like custom roles created by the administrator.

The only thing to do is to create new UserRole fixtures.

Create a class in src/DataFixtures/UserRoleFixtures.php file.

namespace App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Persistence\ObjectManager;
use NumberNine\Entity\UserRole;
use NumberNine\Security\Capabilities;
use NumberNine\Security\CapabilityGenerator;

final class UserRoleFixtures extends Fixture implements FixtureGroupInterface
{
    public function __construct(private CapabilityGenerator $capabilityGenerator)
    {
    }

    public function load(ObjectManager $manager): void
    {
        $teacher = (new UserRole())
            ->setName('Teacher')
            ->setLocked(true) // Locked roles can't be removed in the admin interface
            ->setCapabilities(
                [
                    // Choose the capabilities you want to be enabled by default.
                    // Add your custom capabilities, or remove some you don't want.
                    //
                    // Below are the complete Administrator capabilities
                    Capabilities::READ,
                    Capabilities::ACCESS_ADMIN,
                    Capabilities::UPLOAD_FILES,
                    Capabilities::MANAGE_CATEGORIES,
                    Capabilities::MODERATE_COMMENTS,
                    Capabilities::MANAGE_OPTIONS,
                    Capabilities::LIST_USERS,
                    Capabilities::PROMOTE_USERS,
                    Capabilities::REMOVE_USERS,
                    Capabilities::EDIT_USERS,
                    Capabilities::ADD_USERS,
                    Capabilities::CREATE_USERS,
                    Capabilities::DELETE_USERS,
                    Capabilities::MANAGE_ROLES,
                    Capabilities::CUSTOMIZE,
                    ...$this->capabilityGenerator->generateMappedEditorCapabilities('post'),
                    ...$this->capabilityGenerator->generateMappedEditorCapabilities('page'),
                    ...$this->capabilityGenerator->generateMappedEditorCapabilities('block'),
                    ...$this->capabilityGenerator->generateMappedEditorCapabilities('media_file'),
                ]
            )
        ;

        // add as many roles as you need here

        // and persist them all in database
        $manager->persist($teacher);
        $manager->flush();
    }

    public static function getGroups(): array
    {
        return ['app'];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

The new roles will be automatically created at project installation.

To manually load them on an existing project, run the command below.

docker compose exec php bin/console doctrine:fixtures:load --append --group=app
1

If you're not using Docker, strip the docker compose exec php part.