@@ -92,3 +92,176 @@ menu.xml - `urn:magento:module:Magento_Backend:/etc/menu.xsd` - flat structure
92
92
return $allowedResources;
93
93
}
94
94
```
95
+
96
+ However the actual code to set privilage permission may look like this in the core code
97
+ vendor/magento/magento2-base/setup/src/Magento/Setup/Fixtures/AdminUsersFixture.php
98
+
99
+ In particular this section:
100
+
101
+ ``` php
102
+ $adminUser = $this->userFactory->create();
103
+ $adminUser->setRoleId($role->getId())
104
+ ->setEmail('admin' . $i . '@example.com')
105
+ ->setFirstName('Firstname')
106
+ ->setLastName('Lastname')
107
+ ->setUserName('admin' . $i)
108
+ ->setPassword('123123q')
109
+ ->setIsActive(1);
110
+ $adminUser->save();
111
+ ```
112
+
113
+ ``` php
114
+ <?php
115
+ /**
116
+ * Copyright © Magento, Inc. All rights reserved.
117
+ * See COPYING.txt for license details.
118
+ */
119
+
120
+ namespace Magento\Setup\Fixtures;
121
+
122
+ use Magento\Authorization\Model\Acl\Role\Group;
123
+ use Magento\Authorization\Model\RoleFactory;
124
+ use Magento\Authorization\Model\RulesFactory;
125
+ use Magento\Authorization\Model\UserContextInterface;
126
+ use Magento\Framework\Acl\RootResource;
127
+ use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;
128
+ use Magento\User\Model\UserFactory;
129
+
130
+ /**
131
+ * Generate admin users
132
+ *
133
+ * Support the following format:
134
+ * <!-- Number of admin users -->
135
+ * <admin _users >{int}</admin _users >
136
+ */
137
+ class AdminUsersFixture extends Fixture
138
+ {
139
+ /**
140
+ * @var int
141
+ */
142
+ protected $priority = 5;
143
+
144
+ /**
145
+ * @var UserFactory
146
+ */
147
+ private $userFactory;
148
+
149
+ /**
150
+ * @var RoleFactory
151
+ */
152
+ private $roleFactory;
153
+
154
+ /**
155
+ * @var UserCollectionFactory
156
+ */
157
+ private $userCollectionFactory;
158
+
159
+ /**
160
+ * @var RulesFactory
161
+ */
162
+ private $rulesFactory;
163
+
164
+ /**
165
+ * @var RootResource
166
+ */
167
+ private $rootResource;
168
+
169
+ /**
170
+ * @param FixtureModel $fixtureModel
171
+ * @param UserFactory $userFactory
172
+ * @param UserCollectionFactory $userCollectionFactory
173
+ * @param RoleFactory $roleFactory
174
+ * @param RulesFactory $rulesFactory
175
+ * @param RootResource $rootResource
176
+ */
177
+ public function __construct(
178
+ FixtureModel $fixtureModel,
179
+ UserFactory $userFactory,
180
+ UserCollectionFactory $userCollectionFactory,
181
+ RoleFactory $roleFactory,
182
+ RulesFactory $rulesFactory,
183
+ RootResource $rootResource
184
+ ) {
185
+ parent::__construct($fixtureModel);
186
+ $this->userFactory = $userFactory;
187
+ $this->roleFactory = $roleFactory;
188
+ $this->userCollectionFactory = $userCollectionFactory;
189
+ $this->rulesFactory = $rulesFactory;
190
+ $this->rootResource = $rootResource;
191
+ }
192
+
193
+ /**
194
+ * {@inheritdoc}
195
+ */
196
+ public function execute()
197
+ {
198
+ $adminUsersNumber = $this->fixtureModel->getValue('admin_users', 0);
199
+ $adminUsersStartIndex = $this->userCollectionFactory->create()->getSize();
200
+
201
+ if ($adminUsersStartIndex >= $adminUsersNumber) {
202
+ return;
203
+ }
204
+
205
+ $role = $this->createAdministratorRole();
206
+
207
+ for ($i = $adminUsersStartIndex; $i <= $adminUsersNumber; $i++) {
208
+ $adminUser = $this->userFactory->create();
209
+ $adminUser->setRoleId($role->getId())
210
+ ->setEmail('admin' . $i . '@example.com')
211
+ ->setFirstName('Firstname')
212
+ ->setLastName('Lastname')
213
+ ->setUserName('admin' . $i)
214
+ ->setPassword('123123q')
215
+ ->setIsActive(1);
216
+ $adminUser->save();
217
+ }
218
+ }
219
+
220
+ /**
221
+ * {@inheritdoc}
222
+ */
223
+ public function getActionTitle()
224
+ {
225
+ return 'Generating admin users';
226
+ }
227
+
228
+ /**
229
+ * {@inheritdoc}
230
+ */
231
+ public function introduceParamLabels()
232
+ {
233
+ return [
234
+ 'admin_users' => 'Admin Users'
235
+ ];
236
+ }
237
+
238
+ /**
239
+ * Create administrator role with all privileges.
240
+ *
241
+ * @return \Magento\Authorization\Model\Role
242
+ */
243
+ private function createAdministratorRole()
244
+ {
245
+ $role = $this->roleFactory->create();
246
+ $role->setParentId(0)
247
+ ->setTreeLevel(1)
248
+ ->setSortOrder(1)
249
+ ->setRoleType(Group::ROLE_TYPE)
250
+ ->setUserId(0)
251
+ ->setUserType(UserContextInterface::USER_TYPE_ADMIN)
252
+ ->setRoleName('Example Administrator');
253
+ $role->save();
254
+
255
+ /** @var \Magento\Authorization\Model\Rules $rule */
256
+ $rule = $this->rulesFactory->create();
257
+ $rule->setRoleId($role->getId())
258
+ ->setResourceId($this->rootResource->getId())
259
+ ->setPrivilegies(null)
260
+ ->setPermission('allow');
261
+ $rule->save();
262
+
263
+ return $role;
264
+ }
265
+ }
266
+
267
+ ```
0 commit comments