AdminUsersFixture.php 3.86 KB
Newer Older
Ketan's avatar
Ketan committed
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Setup\Fixtures;

use Magento\Authorization\Model\Acl\Role\Group;
use Magento\Authorization\Model\RoleFactory;
use Magento\Authorization\Model\RulesFactory;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Acl\RootResource;
use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;
use Magento\User\Model\UserFactory;

/**
 * Generate admin users
 *
 * Support the following format:
 * <!-- Number of admin users -->
 * <admin_users>{int}</admin_users>
 */
class AdminUsersFixture extends Fixture
{
    /**
     * @var int
     */
    protected $priority = 5;

    /**
     * @var UserFactory
     */
    private $userFactory;

    /**
     * @var RoleFactory
     */
    private $roleFactory;

    /**
     * @var UserCollectionFactory
     */
    private $userCollectionFactory;

    /**
     * @var RulesFactory
     */
    private $rulesFactory;

    /**
     * @var RootResource
     */
    private $rootResource;

    /**
     * @param FixtureModel $fixtureModel
     * @param UserFactory $userFactory
     * @param UserCollectionFactory $userCollectionFactory
     * @param RoleFactory $roleFactory
     * @param RulesFactory $rulesFactory
     * @param RootResource $rootResource
     */
    public function __construct(
        FixtureModel $fixtureModel,
        UserFactory $userFactory,
        UserCollectionFactory $userCollectionFactory,
        RoleFactory $roleFactory,
        RulesFactory $rulesFactory,
        RootResource $rootResource
    ) {
        parent::__construct($fixtureModel);
        $this->userFactory = $userFactory;
        $this->roleFactory = $roleFactory;
        $this->userCollectionFactory = $userCollectionFactory;
        $this->rulesFactory = $rulesFactory;
        $this->rootResource = $rootResource;
    }

    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        $adminUsersNumber = $this->fixtureModel->getValue('admin_users', 0);
        $adminUsersStartIndex = $this->userCollectionFactory->create()->getSize();

        if ($adminUsersStartIndex >= $adminUsersNumber) {
            return;
        }

        $role = $this->createAdministratorRole();

        for ($i = $adminUsersStartIndex; $i <= $adminUsersNumber; $i++) {
            $adminUser = $this->userFactory->create();
            $adminUser->setRoleId($role->getId())
                ->setEmail('admin' . $i . '@example.com')
                ->setFirstName('Firstname')
                ->setLastName('Lastname')
                ->setUserName('admin' . $i)
                ->setPassword('123123q')
                ->setIsActive(1);
            $adminUser->save();
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getActionTitle()
    {
        return 'Generating admin users';
    }

    /**
     * {@inheritdoc}
     */
    public function introduceParamLabels()
    {
        return [
            'admin_users' => 'Admin Users'
        ];
    }

    /**
     * Create administrator role with all privileges.
     *
     * @return \Magento\Authorization\Model\Role
     */
    private function createAdministratorRole()
    {
        $role = $this->roleFactory->create();
        $role->setParentId(0)
            ->setTreeLevel(1)
            ->setSortOrder(1)
            ->setRoleType(Group::ROLE_TYPE)
            ->setUserId(0)
            ->setUserType(UserContextInterface::USER_TYPE_ADMIN)
            ->setRoleName('Example Administrator');
        $role->save();

        /** @var \Magento\Authorization\Model\Rules $rule */
        $rule = $this->rulesFactory->create();
        $rule->setRoleId($role->getId())
            ->setResourceId($this->rootResource->getId())
            ->setPrivilegies(null)
            ->setPermission('allow');
        $rule->save();

        return $role;
    }
}