芝麻web文件管理V1.00
编辑当前文件:/home/pulsehostuk9/www/cloud.pulsehost.co.uk/modules/TeamContacts/Module.php
Constants::ADDRESSBOOK_TEAM_NAME ]; public function init() { $this->subscribeEvent('Contacts::GetAddressBooks::after', array($this, 'onAfterGetAddressBooks')); $this->subscribeEvent('Core::CreateUser::after', array($this, 'onAfterCreateUser')); $this->subscribeEvent('Contacts::PrepareFiltersFromStorage', array($this, 'onPrepareFiltersFromStorage')); $this->subscribeEvent('Contacts::GetContacts::after', array($this, 'onAfterGetContacts')); $this->subscribeEvent('Contacts::GetContact::after', array($this, 'onAfterGetContact')); $this->subscribeEvent('Core::DoServerInitializations::after', array($this, 'onAfterDoServerInitializations')); $this->subscribeEvent('Contacts::CheckAccessToObject::after', array($this, 'onAfterCheckAccessToObject')); $this->subscribeEvent('Contacts::GetContactSuggestions', array($this, 'onGetContactSuggestions')); $this->subscribeEvent('Contacts::CheckAccessToAddressBook::after', array($this, 'onAfterCheckAccessToAddressBook')); $this->subscribeEvent('Contacts::UpdateAddressBook::before', array($this, 'onBeforeUpdateAddressBook')); $this->subscribeEvent('Contacts::PopulateContactArguments', array($this, 'populateContactArguments')); $this->subscribeEvent('Contacts::CreateContact::before', array($this, 'populateContactArguments')); $this->subscribeEvent('Contacts::ContactQueryBuilder', array($this, 'onContactQueryBuilder')); $this->subscribeEvent('Core::DeleteUser::before', array($this, 'onBeforeDeleteUser')); $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser')); $this->subscribeEvent('Contacts::UpdateContactObject::before', array($this, 'onBeforeUpdateContactObject')); $this->subscribeEvent('Contacts::GetStoragesMapToAddressbooks::after', array($this, 'onAfterGetStoragesMapToAddressbooks')); $this->subscribeEvent('Contacts::CheckAccessToObject::after', array($this, 'onAfterCheckAccessToObject')); $this->subscribeEvent('Core::GetGroupContactsEmails', array($this, 'onGetGroupContactsEmails')); } /** * @return Module */ public static function getInstance() { return parent::getInstance(); } /** * @return Module */ public static function Decorator() { return parent::Decorator(); } /** * @return Settings */ public function getModuleSettings() { return $this->oModuleSettings; } public function onAfterGetAddressBooks(&$aArgs, &$mResult) { if (!is_array($mResult)) { $mResult = []; } $addressbook = $this->GetTeamAddressbook($aArgs['UserId']); if ($addressbook) { /** * @var array $addressbook */ $mResult[] = [ 'Id' => 'team', 'EntityId' => (int) $addressbook['id'], 'CTag' => (int) $addressbook['{http://sabredav.org/ns}sync-token'], 'Display' => true, 'Order' => 1, 'DisplayName' => $addressbook['{DAV:}displayname'], 'Uri' => $addressbook['uri'], 'Url' => $addressbook['uri'], ]; } } public function GetTeamAddressbook($UserId) { Api::CheckAccess($UserId); $addressbook = false; $oUser = Api::getUserById($UserId); if ($oUser) { $sPrincipalUri = \Afterlogic\DAV\Constants::PRINCIPALS_PREFIX . $oUser->IdTenant . '_' . \Afterlogic\DAV\Constants::DAV_TENANT_PRINCIPAL; $addressbook = Backend::Carddav()->getAddressBookForUser($sPrincipalUri, 'gab'); if (!$addressbook) { if (Backend::Carddav()->createAddressBook($sPrincipalUri, 'gab', ['{DAV:}displayname' => \Afterlogic\DAV\Constants::ADDRESSBOOK_TEAM_DISPLAY_NAME])) { $addressbook = Backend::Carddav()->getAddressBookForUser($sPrincipalUri, 'gab'); } } } return $addressbook; } private function createContactForUser($iUserId, $sEmail) { $mResult = false; if (0 < $iUserId) { $addressbook = $this->GetTeamAddressbook($iUserId); if ($addressbook) { $uid = UUIDUtil::getUUID(); $vcard = new \Sabre\VObject\Component\VCard(['UID' => $uid]); $vcard->add( 'EMAIL', $sEmail, [ 'type' => ['work'], 'pref' => 1, ] ); $mResult = !!Backend::Carddav()->createCard($addressbook['id'], $uid . '.vcf', $vcard->serialize()); } } return $mResult; } public function onAfterCreateUser($aArgs, &$mResult) { $iUserId = isset($mResult) && (int) $mResult > 0 ? $mResult : 0; if ((int) $iUserId > 0) { $this->createContactForUser($iUserId, $aArgs['PublicId']); } } public function onPrepareFiltersFromStorage(&$aArgs, &$mResult) { if (isset($aArgs['Storage']) && ($aArgs['Storage'] === StorageType::Team || $aArgs['Storage'] === StorageType::All)) { $aArgs['IsValid'] = true; $oUser = \Aurora\System\Api::getAuthenticatedUser(); $addressbook = $this->GetTeamAddressbook($oUser->Id); if ($addressbook) { if (isset($aArgs['Query'])) { $aArgs['Query']->addSelect(Capsule::connection()->raw( 'CASE WHEN ' . Capsule::connection()->getTablePrefix() . 'adav_cards.addressbookid = ' . $addressbook['id'] . ' THEN true ELSE false END as IsTeam' )); } $mResult = $mResult->orWhere('adav_cards.addressbookid', $addressbook['id']); } } } public function onAfterGetContacts($aArgs, &$mResult) { if (\is_array($mResult) && \is_array($mResult['List'])) { $userPublicId = Api::getUserPublicIdById($aArgs['UserId']); $teamAddressbook = $this->GetTeamAddressbook($aArgs['UserId']); if ($teamAddressbook) { foreach ($mResult['List'] as $iIndex => $aContact) { if (isset($aContact['AddressBookId']) && $aContact['AddressBookId'] == $teamAddressbook['id']) { if ($aContact['ViewEmail'] === $userPublicId) { $aContact['ItsMe'] = true; } else { $aContact['ReadOnly'] = true; } $mResult['List'][$iIndex] = $aContact; } } } } } public function onAfterGetContact($aArgs, &$mResult) { $authenticatedUser = \Aurora\System\Api::getAuthenticatedUser(); $teamAddressbook = $this->GetTeamAddressbook($authenticatedUser->Id); if ($teamAddressbook) { if ($mResult && $authenticatedUser && $mResult->AddressBookId == $teamAddressbook['id']) { $allowEditTeamContactsByTenantAdmins = ContactsModule::getInstance()->oModuleSettings->AllowEditTeamContactsByTenantAdmins; $isUserTenantAdmin = $authenticatedUser->Role === UserRole::TenantAdmin; $isContactInTenant = $mResult->IdTenant === $authenticatedUser->IdTenant; if ($mResult->BusinessEmail === $authenticatedUser->PublicId) { $mResult->ExtendedInformation['ItsMe'] = true; } elseif (!($allowEditTeamContactsByTenantAdmins && $isUserTenantAdmin && $isContactInTenant)) { $mResult->ExtendedInformation['ReadOnly'] = true; } } } } /** * Creates team contacts if they are missing within current tenant. */ public function onAfterDoServerInitializations($aArgs, &$mResult) { $oUser = \Aurora\System\Api::getAuthenticatedUser(); if ($oUser && $oUser->Role === UserRole::NormalUser) { $teamAddressBook = $this->GetTeamAddressbook($oUser->Id); if ($teamAddressBook) { $contact = Capsule::connection()->table('contacts_cards') ->where('AddressBookId', $teamAddressBook['id']) ->where('ViewEmail', $oUser->PublicId) ->first(); if (!$contact) { $this->createContactForUser($oUser->Id, $oUser->PublicId); } } } } public function onAfterCheckAccessToObject(&$aArgs, &$mResult) { $oUser = $aArgs['User']; $oContact = isset($aArgs['Contact']) ? $aArgs['Contact'] : null; $teamAddressBook = $this->GetTeamAddressbook($oUser->Id); if ($oContact instanceof \Aurora\Modules\Contacts\Classes\Contact && $oContact->AddressBookId === $teamAddressBook['id']) { if ($oUser->Role !== UserRole::SuperAdmin && $oUser->IdTenant !== $oContact->IdTenant) { $mResult = false; } else { $mResult = true; } } } public function onGetContactSuggestions(&$aArgs, &$mResult) { if ($aArgs['Storage'] === 'all' || $aArgs['Storage'] === StorageType::Team) { $mResult[StorageType::Team] = \Aurora\Modules\Contacts\Module::Decorator()->GetContacts( $aArgs['UserId'], StorageType::Team, 0, $aArgs['Limit'], $aArgs['SortField'], $aArgs['SortOrder'], $aArgs['Search'] ); } } /** * */ public function populateContactArguments(&$aArgs, &$mResult) { if (isset($aArgs['Storage'], $aArgs['UserId'])) { $aStorageParts = \explode('-', $aArgs['Storage']); if (isset($aStorageParts[0]) && $aStorageParts[0] === StorageType::Team) { $addressbook = $this->GetTeamAddressbook($aArgs['UserId']); if ($addressbook) { $aArgs['Storage'] = StorageType::Team; $aArgs['AddressBookId'] = $addressbook['id']; $mResult = true; } } } } public function onContactQueryBuilder(&$aArgs, &$query) { $addressbook = $this->GetTeamAddressbook($aArgs['UserId']); $query->orWhere(function ($q) use ($addressbook, $aArgs) { $q->where('adav_addressbooks.id', $addressbook['id']); if (is_array($aArgs['UUID'])) { $ids = $aArgs['UUID']; if (count($aArgs['UUID']) === 0) { $ids = [null]; } $q->whereIn('adav_cards.id', $ids); } else { $q->where('adav_cards.id', $aArgs['UUID']); } }); } public function onBeforeDeleteUser(&$aArgs, &$mResult) { if (isset($aArgs['UserId'])) { $this->userPublicIdToDelete = Api::getUserPublicIdById($aArgs['UserId']); $this->teamAddressBook = $this->GetTeamAddressbook($aArgs['UserId']); } } public function onAfterDeleteUser($aArgs, &$mResult) { if ($mResult && $this->userPublicIdToDelete && $this->teamAddressBook) { $card = Capsule::connection()->table('contacts_cards') ->join('adav_cards', 'contacts_cards.CardId', '=', 'adav_cards.id') ->join('adav_addressbooks', 'adav_cards.addressbookid', '=', 'adav_addressbooks.id') ->where('adav_addressbooks.id', $this->teamAddressBook['id']) ->where('ViewEmail', $this->userPublicIdToDelete) ->select('adav_cards.uri as card_uri', 'adav_addressbooks.id as addressbook_id') ->first(); if ($card) { Backend::Carddav()->deleteCard($card->addressbook_id, $card->card_uri); } } } public function onAfterCheckAccessToAddressBook(&$aArgs, &$mResult) { if (isset($aArgs['User'], $aArgs['AddressBookId'])) { $addressbook = $this->GetTeamAddressbook($aArgs['User']->Id); if ($addressbook && $addressbook['id'] == $aArgs['AddressBookId']) { $mResult = true; return $mResult; } } } public function onBeforeUpdateContactObject(&$aArgs, &$mResult) { $user = Api::getAuthenticatedUser(); if ($user && isset($aArgs['Contact'])) { $addressbook = Backend::Carddav()->getAddressBookById($aArgs['Contact']->AddressBookId); if ($addressbook['uri'] === 'gab') { $teamAddressbook = $this->GetTeamAddressbook($user->Id); $isSuperAdmin = $user->Role === UserRole::SuperAdmin; $isTenant = $user->Role === UserRole::TenantAdmin; $isCorrectTeamAddressbook = $teamAddressbook['id'] == $aArgs['Contact']->AddressBookId; $isItsMe = isset($aArgs['Contact']->ExtendedInformation['ItsMe']) && $aArgs['Contact']->ExtendedInformation['ItsMe']; $isReadOnly = isset($aArgs['Contact']->ExtendedInformation['ReadOnly']) && $aArgs['Contact']->ExtendedInformation['ReadOnly']; if (!($isSuperAdmin || ($isTenant && !$isReadOnly && $isCorrectTeamAddressbook) || $isItsMe)) { throw new ApiException(\Aurora\System\Notifications::AccessDenied, null, 'AccessDenied'); } } } } public function onBeforeUpdateAddressBook(&$aArgs, &$mResult) { $addressbook = Backend::Carddav()->getAddressBookById($aArgs['EntityId']); if ($addressbook && $addressbook['uri'] === 'gab') { throw new ApiException(\Aurora\System\Notifications::AccessDenied, null, 'AccessDenied'); } } public function onAfterGetStoragesMapToAddressbooks(&$aArgs, &$mResult) { $mResult = array_merge($mResult, $this->storagesMapToAddressbooks); } public function onGetGroupContactsEmails(&$aArgs, &$mResult) { $oUser = $aArgs['User']; $oGroup = $aArgs['Group']; if ($oUser && $oGroup) { $abook = $this->GetTeamAddressbook($oUser->Id); if ($abook) { if ($oGroup->IsAll) { $mResult = ContactCard::where('AddressBookId', $abook['id'])->get()->map( function (ContactCard $oContact) { if (!empty($oContact->FullName)) { return '"' . $oContact->FullName . '"' . '<' . $oContact->ViewEmail . '>'; } else { return $oContact->ViewEmail; } } )->toArray(); } else { $mResult = $oGroup->Users->map(function ($oUser) use ($abook) { $oContact = ContactCard::where('IdUser', $oUser->Id)->where('AddressBookId', $abook['id'])->first(); if ($oContact && !empty($oContact->FullName)) { return '"' . $oContact->FullName . '"' . '<' . $oUser->PublicId . '>'; } else { return $oUser->PublicId; } })->toArray(); } } } } }