Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
roledealcategoryservice.php
1<?php
3
18
19Loc::loadMessages(__FILE__);
20
22{
23 public const ALL_CATEGORIES = -1;
27 private $rolePermissionService;
32 public function __construct()
33 {
34 $this->rolePermissionService = new RolePermissionService();
35 }
36
44 public function getAbleDealCategories(int $userId): array
45 {
46 return PermissionTable::query()
47 ->registerRuntimeField('role',
48 (new Reference('role',RoleTable::class, Join::on('this.ROLE_ID', 'ref.ID')))
49 ->configureJoinType(Join::TYPE_INNER)
50 )
51 ->setDistinct()
52 ->setSelect(['CATEGORY_ID' => 'role.DEAL_CATEGORY_ID'])
53 ->where('PERMISSION_ID', PermissionDictionary::SEGMENT_CLIENT_OWN_CATEGORY)
54 ->where('VALUE', PermissionDictionary::VALUE_YES)
55 ->whereIn('ROLE_ID', $this->rolePermissionService->getRoleListByUser($userId))
56 ->fetchAll();
57 }
58
66 public function getFilteredDealCategories(int $userId, array $categories): array
67 {
68 $ableDealCategories = $this->getAbleDealCategories($userId);
69 $dealCategories = [];
70
71 $this->accessController = new AccessController($userId);
72 $allowAll = $this->accessController->isAdmin();
73
74 $dealCategories[''] = $categories['']??Loc::getMessage('SENDER_DEAL_CATEGORY_WITHOUT_DEAL_PREP_MSG_1');
75 foreach ($ableDealCategories as $ableDealCategory)
76 {
77 if((int)$ableDealCategory['CATEGORY_ID'] === self::ALL_CATEGORIES)
78 {
79 $allowAll = true;
80 break;
81 }
82 }
83
84 if(!$allowAll)
85 {
86 foreach ($ableDealCategories as $ableDealCategory)
87 {
88 $dealCategories[$ableDealCategory['CATEGORY_ID']] = $categories[$ableDealCategory['CATEGORY_ID']];
89 }
90 return $dealCategories;
91 }
92
93 return $dealCategories + $categories;
94 }
95
96
106 public function fillDefaultDealCategoryPermission(int $dealCategoryId): array
107 {
108 $query = [];
109 $map = RoleUtil::preparedRoleMap();
110
111 $managerRoleId = null;
112 $adminRoleId = null;
113 foreach ($map as $roleKey => $permissions)
114 {
115 $roleName = RoleUtil::getLocalizedName($roleKey);
116
117 $roleId = $this->rolePermissionService->saveRole($roleName, $dealCategoryId);
118 $query = array_merge($query, RoleUtil::buildInsertPermissionQuery($permissions, $roleId));
119
120 if ($roleKey === 'MANAGER')
121 {
122 $managerRoleId = $roleId;
123 }
124
125 if ($roleKey === 'ADMIN')
126 {
127 $adminRoleId = $roleId;
128 }
129 }
130
131 RoleUtil::insertPermissions($query);
132 (new RoleRelationService())->saveRoleRelation([
133 [
134 'id' => $managerRoleId,
135 'accessCodes' => [
136 'AE0' => 'usergroups'
137 ],
138 ],
139 [
140 'id' => $adminRoleId,
141 'accessCodes' => [
142 'G1' => ''
143 ],
144 ],
145 ]);
146
147 return $this->rolePermissionService->getRoleList(
148 [
149
150 "filter" => ["=DEAL_CATEGORY_ID" => $dealCategoryId]
151 ]
152 );
153 }
154}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29