protected function processActionChangeSharingAndRights() { $this->checkRequiredPostParams(array('objectId')); if ($this->errorCollection->hasErrors()) { $this->sendJsonErrorResponse(); } /** @var \Bitrix\Disk\File|\Bitrix\Disk\Folder $object */ $object = BaseObject::loadById((int) $this->request->getPost('objectId'), array('STORAGE')); if (!$object) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_FOLDER_LIST_ERROR_COULD_NOT_FIND_OBJECT'), self::ERROR_COULD_NOT_FIND_OBJECT))); $this->sendJsonErrorResponse(); } $securityContext = $object->getStorage()->getCurrentUserSecurityContext(); if (!$object->canChangeRights($securityContext)) { $this->sendJsonAccessDeniedResponse(); } $entityToNewShared = $this->request->getPost('entityToNewShared'); if (!empty($entityToNewShared) && is_array($entityToNewShared)) { $extendedRights = $entityToNewShared; $newExtendedRightsReformat = array(); foreach ($extendedRights as $entityId => $right) { switch ($right['right']) { case 'disk_access_read': $newExtendedRightsReformat[$entityId] = \Bitrix\Disk\RightsManager::TASK_READ; break; case 'disk_access_edit': $newExtendedRightsReformat[$entityId] = \Bitrix\Disk\RightsManager::TASK_EDIT; break; case 'disk_access_full': $newExtendedRightsReformat[$entityId] = \Bitrix\Disk\RightsManager::TASK_FULL; break; } } //todo move this code to Object or Sharing model (reset sharing) $query = Sharing::getList(array('filter' => array('REAL_OBJECT_ID' => $object->getRealObjectId(), 'REAL_STORAGE_ID' => $object->getRealObject()->getStorageId(), '!=STATUS' => SharingTable::STATUS_IS_DECLINED, 'PARENT_ID' => null))); $needToOverwrite = $needToDelete = $needToAdd = array(); while ($sharingRow = $query->fetch()) { if (isset($newExtendedRightsReformat[$sharingRow['TO_ENTITY']])) { if ($newExtendedRightsReformat[$sharingRow['TO_ENTITY']] != $sharingRow['TASK_NAME']) { $needToOverwrite[$sharingRow['TO_ENTITY']] = $sharingRow; } elseif ($newExtendedRightsReformat[$sharingRow['TO_ENTITY']] == $sharingRow['TASK_NAME']) { unset($newExtendedRightsReformat[$sharingRow['TO_ENTITY']]); } } else { $needToDelete[$sharingRow['TO_ENTITY']] = $sharingRow; } } unset($sharingRow); $needToAdd = array_diff_key($newExtendedRightsReformat, $needToOverwrite); if ($needToAdd) { Sharing::addToManyEntities(array('FROM_ENTITY' => Sharing::CODE_USER . $this->getUser()->getId(), 'REAL_OBJECT' => $object, 'CREATED_BY' => $this->getUser()->getId(), 'CAN_FORWARD' => false), $needToAdd, $this->errorCollection); } if ($needToOverwrite) { $rightsManager = Driver::getInstance()->getRightsManager(); foreach ($needToOverwrite as $sharingRow) { $rightsManager->deleteByDomain($object->getRealObject(), $rightsManager->getSharingDomain($sharingRow['ID'])); } unset($sharingRow); $newRights = array(); foreach ($needToOverwrite as $sharingRow) { $sharingDomain = $rightsManager->getSharingDomain($sharingRow['ID']); $newRights[] = array('ACCESS_CODE' => $sharingRow['TO_ENTITY'], 'TASK_ID' => $rightsManager->getTaskIdByName($newExtendedRightsReformat[$sharingRow['TO_ENTITY']]), 'DOMAIN' => $sharingDomain); //todo refactor. Move most logic to Sharing and SharingTable! if ($sharingRow['TYPE'] == SharingTable::TYPE_TO_DEPARTMENT) { /** @var \Bitrix\Disk\Sharing $sharingModel */ $sharingModel = Sharing::buildFromArray($sharingRow); $sharingModel->changeTaskName($newExtendedRightsReformat[$sharingRow['TO_ENTITY']]); } else { SharingTable::update($sharingRow['ID'], array('TASK_NAME' => $newExtendedRightsReformat[$sharingRow['TO_ENTITY']])); } } unset($sharingRow); if ($newRights) { $rightsManager->append($object->getRealObject(), $newRights); } } if ($needToDelete) { $ids = array(); foreach ($needToDelete as $sharingRow) { $ids[] = $sharingRow['ID']; } unset($sharingRow); foreach (Sharing::getModelList(array('filter' => array('ID' => $ids))) as $sharing) { $sharing->delete($this->getUser()->getId()); } unset($sharing); } unset($right); $this->sendJsonSuccessResponse(); } else { //user delete all sharing $userId = $this->getUser()->getId(); foreach ($object->getRealObject()->getSharingsAsReal() as $sharing) { $sharing->delete($userId); } unset($sharing); $this->sendJsonSuccessResponse(); } }