| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669 |
- <?php
- namespace Opentalent\OtAdmin\Controller;
- use Doctrine\DBAL\Driver\Exception;
- use Opentalent\OtAdmin\Domain\Entity\SiteInfos;
- use Opentalent\OtAdmin\Domain\Entity\SiteStatus;
- use Opentalent\OtAdmin\Enum\OtPageTypeEnum;
- use Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException;
- use Opentalent\OtCore\Exception\NoSuchOrganizationException;
- use Opentalent\OtCore\Exception\NoSuchRecordException;
- use Opentalent\OtCore\Exception\NoSuchWebsiteException;
- use Opentalent\OtCore\Cache\OtCacheManager;
- use Opentalent\OtCore\Controller\ActionController;
- use Opentalent\OtCore\Domain\Model\Organization;
- use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
- use Opentalent\OtCore\Exception\ApiRequestException;
- use Opentalent\OtCore\Service\OpentalentEnvService;
- use Opentalent\OtCore\Utility\FileUtility;
- use PDO;
- use RuntimeException;
- use Symfony\Component\Yaml\Yaml;
- use Throwable;
- use TYPO3\CMS\Core\Cache\CacheManager;
- use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
- use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
- use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
- use TYPO3\CMS\Core\Database\ConnectionPool;
- use TYPO3\CMS\Core\Utility\GeneralUtility;
- use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
- /**
- * The SiteController implements some admin-only operations
- * on Typo3 websites, like creation or update.
- */
- class SiteController extends ActionController
- {
- // Templates names
- private const TEMPLATE_HOME = "OpenTalent.OtTemplating->home";
- private const TEMPLATE_1COL = "OpenTalent.OtTemplating->1Col";
- private const TEMPLATE_3COL = "OpenTalent.OtTemplating->3Col";
- private const TEMPLATE_EVENTS = "OpenTalent.OtTemplating->events";
- private const TEMPLATE_STRUCTURESEVENTS = "OpenTalent.OtTemplating->structuresEvents";
- private const TEMPLATE_STRUCTURES = "OpenTalent.OtTemplating->structures";
- private const TEMPLATE_CONTACT = "OpenTalent.OtTemplating->contact";
- private const TEMPLATE_NEWS = "OpenTalent.OtTemplating->news";
- private const TEMPLATE_MEMBERS = "OpenTalent.OtTemplating->members";
- private const TEMPLATE_MEMBERSCA = "OpenTalent.OtTemplating->membersCa";
- private const TEMPLATE_STRUCTUREDETAILS = "OpenTalent.OtTemplating->structureDetails";
- private const TEMPLATE_LEGAL = "OpenTalent.OtTemplating->legal";
- // Pages dokType values
- private const DOK_PAGE = 1;
- private const DOK_SHORTCUT = 4;
- private const DOK_FOLDER = 116;
- // Contents CTypes
- private const CTYPE_TEXT = 'text';
- private const CTYPE_IMAGE = 'image';
- private const CTYPE_TEXTPIC = 'textpic';
- private const CTYPE_TEXTMEDIA = 'textmedia';
- private const CTYPE_HTML = 'html';
- private const CTYPE_HEADER = 'header';
- private const CTYPE_UPLOADS = 'uploads';
- private const CTYPE_LIST = 'list';
- private const CTYPE_SITEMAP = 'menu_sitemap';
- // Default values
- private const DEFAULT_THEME = 'Modern';
- private const DEFAULT_COLOR = 'light-blue';
- // BE rights
- private const BEGROUP_EDITOR_STANDARD = 10;
- private const BEGROUP_EDITOR_PREMIUM = 20;
- private const BEGROUP_ADMIN_STANDARD = 30;
- private const BEGROUP_ADMIN_PREMIUM = 40;
- private const BEGROUP_NAME = [
- self::BEGROUP_EDITOR_STANDARD => "Editor_Standard",
- self::BEGROUP_EDITOR_PREMIUM => "Editor_Premium",
- self::BEGROUP_ADMIN_STANDARD => "Admin_Standard",
- self::BEGROUP_ADMIN_PREMIUM => "Admin_Premium"
- ];
- private const IS_PRODUCT_PREMIUM = [
- "school" => false,
- "artist" => false,
- "school-premium" => true,
- "artist-premium" => true,
- "manager" => true,
- ];
- // access permissions
- private const PERM_NONE = 0;
- private const PERM_SHOW = 1;
- private const PERM_EDIT_CONTENT = 16;
- private const PERM_EDIT_PAGE = 2;
- private const PERM_DELETE = 4;
- private const PERM_NEW = 8;
- // Domain name validation
- private const RX_DOMAIN = "/([a-z0-9A-Z]\.)*[a-z0-9-]+\.([a-z0-9]{2,24})+(\.co\.([a-z0-9]{2,24})|\.([a-z0-9]{2,24}))*\/?/";
- // Redirections creation status
- private const REDIRECTION_UNKNOWN_STATUS = 0;
- public const REDIRECTION_UPDATED = 1;
- public const REDIRECTION_CREATED = 2;
- /**
- * @var ConnectionPool
- */
- private ConnectionPool $connectionPool;
- public function injectConnectionPool(ConnectionPool $connectionPool): void
- {
- $this->connectionPool = $connectionPool;
- }
- /**
- * @var CacheManager
- */
- private CacheManager $cacheManager;
- public function injectCacheManager(CacheManager $cacheManager): void
- {
- $this->cacheManager = $cacheManager;
- }
- /**
- * @var OtCacheManager
- */
- private OtCacheManager $otCacheManager;
- public function injectOtCacheManager(OtCacheManager $otCacheManager): void
- {
- $this->otCacheManager = $otCacheManager;
- }
- /**
- * @var OrganizationRepository
- */
- protected OrganizationRepository $organizationRepository;
- public function injectOrganizationRepository(OrganizationRepository $organizationRepository): void
- {
- $this->organizationRepository = $organizationRepository;
- }
- /**
- * Index of the pages created during the process
- * >> [slug => uid]
- * @var array
- */
- private array $createdPagesIndex;
- /**
- * List of the directories created in the process (for rollback purposes)
- * @var array
- */
- private array $createdDirs;
- /**
- * List of the files created in the process (for rollback purposes)
- * @var array
- */
- private array $createdFiles;
- public function __construct()
- {
- $this->createdPagesIndex = [];
- $this->createdDirs = [];
- $this->createdFiles = [];
- }
- /**
- * Return the SiteInfos object for the organization's website
- *
- * @param int $organizationId
- * @return SiteInfos
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws InvalidWebsiteConfigurationException
- * @throws Exception
- */
- public function getSiteInfosAction(int $organizationId): SiteInfos
- {
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId, false);
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid'], false);
- $organizationExtraData = $this->fetchOrganizationExtraData($organizationId);
- # /!\ because of restrictions in the typo3 repo, this will return an empty array if the page is deleted
- $rootPage = $this->otPageRepository->getPage($rootUid);
- $site = new SiteInfos(
- $rootUid,
- $website['organization_name'],
- $this->otWebsiteRepository->resolveWebsiteDomain($website),
- $website['template'],
- $website['template_preferences'],
- $website['matomo_id'],
- self::IS_PRODUCT_PREMIUM[$organizationExtraData['admin']['product']] ?? false,
- !$rootPage || $rootPage['deleted'],
- ($rootPage['hidden'] || $rootPage['fe_group'] < 0),
- null,
- null,
- $rootPage['perms_userid'],
- $rootPage['perms_groupid']
- );
- // Owners
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- $queryBuilder->getRestrictions()->removeAll();
- $beUsers = $queryBuilder
- ->select('uid', 'username')
- ->from('be_users')
- ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
- ->execute()
- ->fetchAllAssociative();
- foreach ($beUsers as $beUser) {
- $site->addMountedForBeUser($beUser);
- }
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- $queryBuilder->getRestrictions()->removeAll();
- $beGroups = $queryBuilder
- ->select('uid', 'title')
- ->from('be_groups')
- ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
- ->execute()
- ->fetchAllAssociative();
- foreach ($beGroups as $beGroup) {
- $site->addMountedForBeGroups($beGroup);
- }
- return $site;
- }
- /**
- * Creates a new website for the given organization, and
- * returns the root page uid of the newly created site
- *
- * @param int $organizationId
- * @return int Uid of the root page of the newly created website
- * @throws RuntimeException|\Throwable
- */
- public function createSiteAction(int $organizationId): int
- {
- $organization = $this->fetchOrganization($organizationId);
- // This extra-data can not be retrieved from the API for now, but
- // this shall be set up as soon as possible, to avoid requesting
- // the prod-back DB directly.
- $organizationExtraData = $this->fetchOrganizationExtraData($organizationId);
- $isNetwork = $organizationExtraData['category'] === 'NETWORK';
- // Is there already a website with this organization's id?
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder->getRestrictions()->removeAll();
- $statement = $queryBuilder
- ->select('uid')
- ->from('ot_websites')
- ->where($queryBuilder->expr()->eq('organization_id', $queryBuilder->createNamedParameter($organization->getId())))
- ->execute();
- if ($statement->rowCount() > 0) {
- throw new RuntimeException("A website with this organization's id already exists: " . $organization->getName() . "\n(if you can't see it, it might have been soft-deleted)");
- }
- // ** Create the new website
- // start transactions
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- // keep tracks of the created folders and files to be able to remove them during a rollback
- try {
- // Create the website:
- $websiteUid = $this->insertOtWebsite($organization);
- // Create the site pages:
- // > Root page
- $rootUid = $this->insertRootPage(
- $websiteUid,
- $organization->getName()
- );
- // > 'Accueil' shortcut
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Accueil',
- '/accueil',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- '',
- [
- 'dokType' => self::DOK_SHORTCUT,
- 'shortcut' => $rootUid
- ]
- );
- // > 'Présentation' page
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Présentation',
- '/presentation',
- OtPageTypeEnum::MANDATORY_EDITABLE
- );
- // > 'Présentation > Qui sommes-nous ?' page (hidden by default)
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Qui sommes nous?',
- '/qui-sommes-nous',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- '',
- ['hidden' => 1]
- );
- // > 'Présentation > Les adhérents' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Les adhérents',
- '/les-adherents',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_MEMBERS
- );
- // > 'Présentation > Les membres du CA' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Les membres du CA',
- '/les-membres-du-ca',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_MEMBERSCA
- );
- // > 'Présentation > Informations Pratiques' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Informations Pratiques',
- '/informations-pratiques',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_STRUCTUREDETAILS
- );
- if ($isNetwork) {
- // > 'Présentation > Les sociétés adhérentes' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Les sociétés adhérentes',
- '/societes-adherentes',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_STRUCTURES
- );
- }
- // > 'Présentation > Historique' page (hidden by default)
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Historique',
- '/historique',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- '',
- ['hidden' => 1]
- );
- // ~ Contact shortcut will be created after the contact page
- // > 'Actualités' page (hidden by default)
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Actualités',
- '/actualites',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_NEWS,
- ['hidden' => 1]
- );
- // > 'Saison en cours' page
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Saison en cours',
- '/saison-en-cours'
- );
- // > 'Saison en cours > Les évènements' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/saison-en-cours'],
- 'Les évènements',
- '/les-evenements',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_EVENTS
- );
- if ($isNetwork) {
- // > 'Présentation > Evènements des structures' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Évènements des structures',
- '/evenements-des-structures',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- self::TEMPLATE_STRUCTURESEVENTS
- );
- }
- // > 'Vie interne' page (restricted, hidden by default)
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Vie interne',
- '/vie-interne',
- OtPageTypeEnum::NON_MANDATORY,
- '',
- [
- 'hidden' => 1,
- 'fe_group' => -2
- ]
- );
- // > 'Footer' page (not in the menu)
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Footer',
- '/footer',
- OtPageTypeEnum::MANDATORY_NON_EDITABLE,
- '',
- [
- 'dokType' => self::DOK_FOLDER,
- 'nav_hide' => 1
- ]
- );
- // > 'Footer > Contact' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/footer'],
- 'Contact',
- '/contact',
- OtPageTypeEnum::MANDATORY_NON_EDITABLE,
- self::TEMPLATE_CONTACT
- );
- // > 'Footer > Plan du site' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/footer'],
- 'Plan du site',
- '/plan-du-site',
- OtPageTypeEnum::MANDATORY_NON_EDITABLE
- );
- // > 'Footer > Mentions légales' page
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/footer'],
- 'Mentions légales',
- '/mentions-legales',
- OtPageTypeEnum::MANDATORY_NON_EDITABLE,
- self::TEMPLATE_LEGAL
- );
- // > 'Présentation > Contact' shortcut
- $this->insertPage(
- $websiteUid,
- $this->createdPagesIndex['/presentation'],
- 'Contact',
- '/ecrivez-nous',
- OtPageTypeEnum::MANDATORY_EDITABLE,
- '',
- [
- 'dokType' => self::DOK_SHORTCUT,
- 'shortcut' => $this->createdPagesIndex['/contact']
- ]
- );
- // > 'Page introuvable' page (not in the menu, read-only)
- $this->insertPage(
- $websiteUid,
- $rootUid,
- 'Page introuvable',
- '/page-introuvable',
- OtPageTypeEnum::NON_MANDATORY,
- '',
- [
- 'nav_hide' => 1,
- 'no_search' => 1
- ]
- );
- // Add content to these pages
- // >> root page content
- $this->insertContent(
- $rootUid,
- self::CTYPE_TEXTPIC,
- '<h1>Bienvenue sur le site de ' . $organization->getName() . '.</h1>',
- 0
- );
- // >> page 'qui sommes nous?'
- $this->insertContent(
- $this->createdPagesIndex['/qui-sommes-nous'],
- self::CTYPE_TEXT,
- 'Qui sommes nous ...',
- 0
- );
- // >> page 'historique'
- $this->insertContent(
- $this->createdPagesIndex['/historique'],
- self::CTYPE_TEXT,
- "Un peu d'histoire ...",
- 0
- );
- // >> page 'plan du site'
- $this->insertContent(
- $this->createdPagesIndex['/plan-du-site'],
- self::CTYPE_SITEMAP
- );
- // update sys_template
- $constants = $this->getTemplateConstants($organizationId, $organizationExtraData);
- $include = "EXT:fluid_styled_content/Configuration/TypoScript/";
- $include .= ",EXT:fluid_styled_content/Configuration/TypoScript/Styling/";
- $include .= ",EXT:form/Configuration/TypoScript/";
- $include .= ",EXT:news/Configuration/TypoScript";
- $include .= ",EXT:frontend_editing/Configuration/TypoScript";
- $include .= ",EXT:frontend_editing/Configuration/TypoScript/FluidStyledContent9";
- $include .= ",EXT:ot_templating/Configuration/TypoScript";
- $include .= ",EXT:hcaptcha/Configuration/TypoScript";
- $include .= ",EXT:seo/Configuration/TypoScript/XmlSitemap";
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
- $queryBuilder->insert('sys_template')
- ->values([
- 'pid' => $rootUid,
- 'title' => $organization->getName(),
- 'root' => 1,
- 'clear' => 3,
- 'config' => "config.frontend_editing = 1",
- 'include_static_file' => $include,
- 'constants' => $constants
- ])
- ->execute();
- // ## Create the site config.yaml file
- $identifier = $this->writeConfigFile(
- $rootUid,
- true
- );
- // Update the ot_website identifier
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
- $queryBuilder->update('ot_websites')
- ->set('config_identifier', $identifier)
- ->where($queryBuilder->expr()->eq('uid', $websiteUid))
- ->execute();
- // Create the user_upload and form_definitions directories and update the sys_filemounts table
- $uploadRelPath = "user_upload/" . $organizationId;
- $fileadminDir = $_ENV['TYPO3_PATH_APP'] . "/public/fileadmin";
- $uploadDir = rtrim($fileadminDir, '/') . "/" . ltrim($uploadRelPath, '/');
- if (file_exists($uploadDir)) {
- throw new RuntimeException("A directory or file " . $uploadDir . " already exists. Abort.");
- }
- $formsRelPath = '/form_definitions/' . $organizationId;
- $formsDir = $fileadminDir . $formsRelPath;
- if (file_exists($formsDir)) {
- throw new RuntimeException("A directory or file " . $formsDir . " already exists. Abort.");
- }
- $this->mkDir($uploadDir);
- $this->mkDir($formsDir);
- // Insert the filemounts points (sys_filemounts)
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_filemounts');
- $queryBuilder->insert('sys_filemounts')
- ->values([
- 'title' => 'Documents',
- 'identifier' => '1:' . rtrim($uploadRelPath, '/') . '/'
- ])
- ->executeStatement();
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_filemounts');
- $queryBuilder->insert('sys_filemounts')
- ->values([
- 'title' => 'Forms_' . $organizationId,
- 'identifier' => '1:' . rtrim($formsRelPath, '/') . '/'
- ])
- ->executeStatement();
- // Create the BE Editors group
- // -- NB: this user will then be auto-updated by the ot_connect extension --
- $beGroupUid = $this->createOrUpdateBeGroup(
- $organizationId,
- $rootUid,
- $organizationExtraData['admin']
- );
- // Create the BE User
- // -- NB: this user will then be auto-updated by the ot_connect extension --
- $beUserUid = $this->createOrUpdateBeUser(
- $organizationId,
- $beGroupUid,
- $organizationExtraData['admin']
- );
- // Update the user TsConfig
- $tsconfig = "options.uploadFieldsInTopOfEB = 1\n" .
- "options.defaultUploadFolder=1:" . rtrim($uploadRelPath, '/') . "/\n";
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
- $queryBuilder
- ->update('be_users')
- ->where($queryBuilder->expr()->eq('uid', $beUserUid))
- ->set('TSconfig', $tsconfig)
- ->execute();
- // Setup users and group rights
- $this->setBeUserPerms($organizationId, false, $beGroupUid, $beUserUid);
- // Try to commit the result
- $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
- if (!$commitSuccess) {
- throw new RuntimeException('Something went wrong while committing the result');
- }
- // Clear the whole cache, to avoid errors with the configuration files cache
- // >> this could probably be improved by targeting the caches to clean
- // more precisely, but that'll do for the moment
- OtCacheManager::clearCache(false);
- } catch(\Throwable $e) {
- // rollback
- $this->connectionPool->getConnectionByName('Default')->rollback();
- // remove created files and dirs
- foreach (array_reverse($this->createdFiles) as $filename) {
- unlink($filename);
- }
- $this->createdFiles = [];
- foreach (array_reverse($this->createdDirs) as $dirname) {
- rmdir($dirname);
- }
- $this->createdDirs = [];
- throw $e;
- }
- // Extra steps that do not need any rollback:
- $this->enableFeEditing($beUserUid);
- return $rootUid;
- }
- /**
- * Performs an update of the organization's website based on data fetched from the opentalent DB:
- *
- * - Update the pages table (structure id, structure domain)
- * - (hard update only) Update the config.yaml file
- * - Update the `sys_template`.`constants` and the `pages`.`TSConfig` fields
- * - (hard update only) Reset the users permissions
- * - Clear the Typo3 cache for the website
- *
- * @param int $organizationId
- * @return int
- * @throws NoSuchOrganizationException
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws \Doctrine\DBAL\ConnectionException
- * @throws \Doctrine\DBAL\DBALException
- * @throws InvalidWebsiteConfigurationException
- * @throws \Throwable
- */
- public function updateSiteAction(int $organizationId): int
- {
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
- $organization = $this->fetchOrganization($organizationId);
- // This extra-data can not be retrieved from the API for now, but
- // this shall be set up as soon as possible, to avoid requesting
- // the prod-back DB directly.
- $organizationExtraData = $this->fetchOrganizationExtraData($organizationId);
- // start transactions
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- // keep tracks of the created folders and files to be able to remove them during a rollback
- try {
- // ## Update the ot_website table
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
- $queryBuilder->update('ot_websites')
- ->set('subdomain', $organization->getSubDomain())
- ->set('organization_name', $organization->getName())
- ->where($queryBuilder->expr()->eq('uid', $website['uid']))
- ->execute();
- // ## Update the subpages of the rootpage
- $sitePages = $this->otPageRepository->getAllSubpagesForPage($rootUid);
- foreach ($sitePages as $page) {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder->update('pages')
- ->set('ot_website_uid', $website['uid'])
- ->where($queryBuilder->expr()->eq('uid', $page['uid']))
- ->execute();
- }
- // ## Update the `sys_template`.`constants` and the `pages`.`TSConfig` fields
- $constants = $this->getTemplateConstants($organizationId, $organizationExtraData);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
- $queryBuilder
- ->update('sys_template')
- ->set('constants', $constants)
- ->where($queryBuilder->expr()->eq('pid', $rootUid))
- ->execute();
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder
- ->update('pages')
- ->set('TSconfig', 'TCAdefaults.pages.ot_website_uid=' . $website['uid'])
- ->where($queryBuilder->expr()->eq('uid', $rootUid))
- ->execute();
- // ## Update the config.yaml file
- $identifier = $this->otWebsiteRepository->findConfigIdentifierFor($rootUid);
- $this->writeConfigFile($rootUid, true, $identifier);
- // ## Update the ot_website identifier
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
- $queryBuilder->update('ot_websites')
- ->set('config_identifier', $identifier)
- ->where($queryBuilder->expr()->eq('uid', $website['uid']))
- ->execute();
- // Reset the backend users and group permissions
- $this->setBeUserPerms($organizationId, true);
- // Try to commit the result
- $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
- if (!$commitSuccess) {
- throw new RuntimeException('Something went wrong while committing the result');
- }
- } catch(\Throwable $e) {
- // rollback
- $this->connectionPool->getConnectionByName('Default')->rollback();
- throw $e;
- }
- // ## Clear the Typo3 cache for the website
- $this->otCacheManager->clearSiteCache($rootUid, true);
- return $rootUid;
- }
- /**
- * Delete the website with all its pages, contents and related records
- *
- * If the hard parameter is false, the records' `deleted` field will be set to true and
- * the files and directories will be renamed. This kind of 'soft' deletion can be undone.
- *
- * Otherwise, if hard is set to true, the records and files will be permanently removed,
- * with no possibility of undoing anything. In this case, you'll have to confirm your intention
- * by creating a file in the Typo3 root directory, named 'DEL####' (#### is the organization id)
- *
- * If $redirectToParent is set to true, the website's url will be redirected to its parent's website
- *
- * The $force parameter, if true, will both bypass the 'DEL###' file confirmation and recursively delete the
- * user_upload and form_definitions of the website. <!> USE WITH CAUTION
- *
- * @param int $organizationId
- * @param bool $hard
- * @param bool $redirectToParent If set to true, the website's url will be redirected to its parent's website
- * @param bool $force
- * @return int
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws \Doctrine\DBAL\DBALException
- * @throws \Throwable
- */
- public function deleteSiteAction(int $organizationId, bool $hard=false, bool $redirectToParent=true, bool $force = false): int
- {
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
- $websiteUid = $website['uid'];
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $isDeleted = $queryBuilder
- ->select('deleted')
- ->from('ot_websites')
- ->where($queryBuilder->expr()->eq('uid', $website['uid']))
- ->execute()
- ->fetchOne() === 1;
- $confirm_file = $_ENV['TYPO3_PATH_APP'] . "/DEL" . $organizationId;
- if ($hard && !$force && !file_exists($confirm_file)) {
- throw new RuntimeException(
- "You are going to completely delete the website with root uid " . $rootUid .
- ", and all of its pages, files, contents...etc. If you are sure, create a file named '" .
- $confirm_file . "', and launch this command again."
- );
- }
- // Prepare the redirection
- if ($redirectToParent) {
- $originDomain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
- try {
- $organization = $this->fetchOrganization($organizationId);
- $targetOrganizationId = $organization->getParentId();
- $targetOrganizationWebsite = $this->otWebsiteRepository->getWebsiteByOrganizationId($targetOrganizationId);
- $targetDomain = $this->otWebsiteRepository->resolveWebsiteDomain($targetOrganizationWebsite);
- } catch (NoSuchOrganizationException $e) {
- $targetDomain = 'opentalent.fr';
- }
- }
- // start transactions
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- // keep track of renamed file for an eventual rollback
- $renamed = [];
- try {
- $pages = $this->otPageRepository->getAllSubpagesForPage($rootUid);
- foreach($pages as $page) {
- $this->delete('tt_content', 'pid', $page['uid'], $hard);
- $this->delete('pages', 'uid', $page['uid'], $hard);
- }
- $this->delete('tt_content', 'pid', $rootUid, $hard);
- $this->delete('pages', 'uid', $rootUid, $hard);
- $this->delete('sys_template', 'pid', $rootUid, $hard);
- $this->delete('ot_websites', 'uid', $websiteUid, $hard);
- try {
- $adminBeUserUid = $this->findAdminBeUserUid($rootUid);
- if ($adminBeUserUid !== null) {
- $this->delete('be_users', 'uid', $adminBeUserUid, $hard);
- }
- } catch (NoSuchRecordException $e) {}
- try {
- $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid);
- if ($editorsGroupUid !== null) {
- $this->delete('be_groups', 'uid', $editorsGroupUid, $hard);
- }
- } catch (NoSuchRecordException $e) {}
- // Delete the filemounts
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_filemounts');
- $queryBuilder
- ->select('uid')
- ->from('sys_filemounts')
- ->where("identifier LIKE '1:%user_upload/" . $organizationId . "/%'")
- ->orWhere("identifier LIKE '1:%form_definitions/" . $organizationId . "/%'");
- $statement = $queryBuilder->executeQuery();
- $rows = $statement->fetchAllAssociative();
- foreach ($rows as $row) {
- $this->delete('sys_filemounts', 'uid', $row['uid'], $hard);
- }
- // Look up for the config.yaml file of the website
- $configMainDir = $_ENV['TYPO3_PATH_APP'] . '/config/sites';
- $configYamlFile = "";
- foreach (glob($configMainDir . '/*', GLOB_ONLYDIR) as $subdir) {
- if (!$isDeleted) {
- $yamlFile = $subdir . '/config.yaml';
- } else {
- $yamlFile = $subdir . '/config.yaml.deleted';
- }
- if (is_file($yamlFile)) {
- $conf = Yaml::parseFile($yamlFile);
- if ($conf['rootPageId'] === $rootUid) {
- $configYamlFile = $yamlFile;
- break;
- }
- }
- }
- if (!$isDeleted) {
- $uploadDir = $_ENV['TYPO3_PATH_APP'] . '/public/fileadmin/user_upload/' . $organizationId . '/';
- $formsDir = $_ENV['TYPO3_PATH_APP'] . '/public/fileadmin/form_definitions/' . $organizationId . '/';
- } else {
- $uploadDir = $_ENV['TYPO3_PATH_APP'] . '/public/fileadmin/user_upload/deleted_' . $organizationId . '/';
- $formsDir = $_ENV['TYPO3_PATH_APP'] . '/public/fileadmin/form_definitions/deleted_' . $organizationId . '/';
- }
- // If hard deletion, verify that dirs are empty
- if (!$force) {
- foreach ([$uploadDir, $formsDir] as $dir) {
- if ($hard && is_dir($dir)) {
- foreach (scandir($dir) as $subdir) {
- if ($subdir === '.' || $subdir === '..') {
- continue;
- }
- $subdir = $dir . $subdir;
- if (!is_dir($subdir)) {
- throw new RuntimeException(
- 'The directory ' . $dir . ' contains directories' .
- ', please delete them manually. Abort.');
- }
- if (is_readable($subdir)) {
- foreach (scandir($subdir) as $filename) {
- if ($filename !== '.' && $filename !== '..') {
- throw new RuntimeException(
- 'The directory ' . $subdir . ' is not empty, ' .
- 'please delete it manually. Abort.');
- }
- }
- }
- }
- }
- }
- }
- // If soft deletion, check that no deleted file or directory exist
- if (!$hard) {
- $toRename = [];
- if (is_file($configYamlFile)) {
- $toRename[$configYamlFile] = $configYamlFile . '.deleted';
- }
- if (is_dir($uploadDir)) {
- $toRename[$uploadDir] = dirname($uploadDir) . '/deleted_' . basename($uploadDir);
- }
- foreach ($toRename as $initialPath => $newPath) {
- if (is_file($newPath)) {
- throw new RuntimeException(
- 'A file or directory named ' . $newPath . ' already exists, what happened?. Cancel.');
- }
- }
- }
- // Delete or rename files and dirs
- if ($hard) {
- if (is_file($configYamlFile)) {
- unlink($configYamlFile);
- }
- if (is_dir(dirname($configYamlFile))) {
- FileUtility::rmdir(dirname($configYamlFile), $force);
- }
- if (is_dir($uploadDir)) {
- FileUtility::rmdir($uploadDir, $force);
- }
- if (is_dir($formsDir)) {
- FileUtility::rmdir($formsDir, $force);
- }
- } else {
- $renamed = [];
- foreach ($toRename as $initialPath => $newPath) {
- rename($initialPath, $newPath);
- $renamed[$initialPath] = $newPath;
- }
- }
- // Add the redirection
- if ($redirectToParent) {
- try {
- $this->addRedirection($originDomain, $targetDomain);
- } catch (RuntimeException $e) {} // silent the error until this https://assistance.opentalent.fr/browse/V8-3344
- }
- // Try to commit the result
- $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
- if (!$commitSuccess) {
- throw new RuntimeException('Something went wrong while commiting the result');
- }
- return $rootUid;
- } catch(\Throwable $e) {
- // rollback
- $this->connectionPool->getConnectionByName('Default')->rollback();
- if (!$hard) {
- foreach ($renamed as $initialPath => $newPath) {
- rename($newPath, $initialPath);
- }
- }
- throw $e;
- } finally {
- if (file_exists($confirm_file)) {
- unlink($confirm_file);
- }
- }
- }
- /**
- * Delete a record from the typo3 db.
- * If $hard is true, the record is permanently deleted.
- * Else, it's just marked as deleted.
- *
- * @param string $table
- * @param string $whereKey
- * @param $whereValue
- * @param bool $hard
- */
- private function delete(string $table, string $whereKey, $whereValue, bool $hard=false): void
- {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable($table);
- if (!$hard) {
- if ($table === 'pages' || $table === 'tt_content') {
- // Keep track of the manually deleted pages and content, so that these won't be restored if the site
- // is undeleted
- $cnn = $this->connectionPool->getConnectionForTable($table);
- $cnn->executeStatement("
- UPDATE " . $table . " t
- SET t.manually_deleted = t.deleted
- WHERE " . $whereKey . "=?;",
- [$whereValue]
- );
- }
- $queryBuilder
- ->update($table)
- ->set('deleted', 1)
- ->where($queryBuilder->expr()->eq($whereKey, $whereValue))
- ->execute();
- } else {
- $queryBuilder
- ->delete($table)
- ->where($queryBuilder->expr()->eq($whereKey, $whereValue))
- ->execute();
- }
- }
- /**
- * Undo a soft-deletion performed using deleteSiteAction()
- *
- * @param int $organizationId
- * @return int
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws \Doctrine\DBAL\ConnectionException
- * @throws \Doctrine\DBAL\DBALException
- * @throws \Throwable
- */
- public function undeleteSiteAction(int $organizationId): int
- {
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId, false);
- $websiteUid = $website['uid'];
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid'], false);
- // start transactions
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- // keep track of renamed file for an eventual rollback
- $renamed = [];
- try {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content');
- $queryBuilder
- ->update('ot_websites')
- ->set('deleted', 0)
- ->where($queryBuilder->expr()->eq('uid', $websiteUid))
- ->execute();
- $pages = $this->otPageRepository->getAllSubpagesForPage($rootUid);
- foreach($pages as $page) {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content');
- $queryBuilder
- ->update('tt_content')
- ->set('deleted', 0)
- ->where(
- $queryBuilder->expr()->andX(
- $queryBuilder->expr()->eq('pid', $page['uid']),
- $queryBuilder->expr()->eq('manually_deleted', 0)
- )
- )
- ->execute();
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder
- ->update('pages')
- ->set('deleted', 0)
- ->where(
- $queryBuilder->expr()->andX(
- $queryBuilder->expr()->eq('uid', $page['uid']),
- $queryBuilder->expr()->eq('manually_deleted', 0)
- )
- )
- ->execute();
- }
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content');
- $queryBuilder
- ->update('tt_content')
- ->set('deleted', 0)
- ->where(
- $queryBuilder->expr()->andX(
- $queryBuilder->expr()->eq('pid', $rootUid),
- $queryBuilder->expr()->eq('manually_deleted', 0)
- )
- )
- ->execute();
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder
- ->update('pages')
- ->set('deleted', 0)
- ->where(
- $queryBuilder->expr()->andX(
- $queryBuilder->expr()->eq('uid', $rootUid),
- $queryBuilder->expr()->eq('manually_deleted', 0)
- )
- )
- ->execute();
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_template');
- $queryBuilder
- ->update('sys_template')
- ->set('deleted', 0)
- ->where($queryBuilder->expr()->eq('pid', $rootUid))
- ->executeStatement();
- // remove filemounts
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_filemounts');
- $queryBuilder
- ->update('sys_filemounts')
- ->set('deleted', 0)
- ->where($queryBuilder->expr()->eq('identifier', "'1:/user_upload/" . $organizationId . "/'"))
- ->executeStatement();
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_filemounts');
- $queryBuilder
- ->update('sys_filemounts')
- ->set('deleted', 0)
- ->where($queryBuilder->expr()->eq('identifier', "'1:/form_definitions/" . $organizationId . "/'"))
- ->executeStatement();
- try {
- $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid, false);
- if ($editorsGroupUid !== null) {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- $queryBuilder
- ->update('be_groups')
- ->set('deleted', 0)
- ->where($queryBuilder->expr()->eq('uid', $editorsGroupUid))
- ->executeStatement();
- }
- } catch (NoSuchRecordException $e) {}
- try {
- $adminBeUserUid = $this->findAdminBeUserUid($rootUid, false);
- if ($adminBeUserUid !== null) {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
- $queryBuilder
- ->update('be_users')
- ->set('deleted', 0)
- ->where($queryBuilder->expr()->eq('uid', $adminBeUserUid))
- ->executeStatement();
- }
- } catch (NoSuchRecordException $e) {}
- // Look up for the config.yaml file of the website
- $configMainDir = $_ENV['TYPO3_PATH_APP'] . '/config/sites';
- $configYamlFile = "";
- foreach (glob($configMainDir . '/*', GLOB_ONLYDIR) as $subdir) {
- $yamlFile = $subdir . '/config.yaml.deleted';
- if (is_file($yamlFile)) {
- $conf = Yaml::parseFile($yamlFile);
- if ($conf['rootPageId'] === $rootUid) {
- $configYamlFile = $yamlFile;
- break;
- }
- }
- }
- $uploadDir = $_ENV['TYPO3_PATH_APP'] . '/public/fileadmin/user_upload/deleted_' . $organizationId . '/';
- $formsDir = $_ENV['TYPO3_PATH_APP'] . '/public/fileadmin/form_definitions/deleted_' . $organizationId . '/';
- $toRename = [];
- if (is_file($configYamlFile)) {
- $toRename[$configYamlFile] = dirname($configYamlFile) . '/config.yaml';
- }
- if (is_dir($uploadDir)) {
- $toRename[$uploadDir] = dirname($uploadDir) . '/' . $organizationId;
- }
- if (is_dir($formsDir)) {
- $toRename[$formsDir] = dirname($formsDir) . '/' . $organizationId;
- }
- foreach ($toRename as $initialPath => $newPath) {
- if (is_file($newPath)) {
- throw new RuntimeException(
- 'A file or directory named ' . $newPath . ' already exists, what happened?. Cancel.');
- }
- }
- $renamed = [];
- foreach ($toRename as $initialPath => $newPath) {
- rename($initialPath, $newPath);
- $renamed[$initialPath] = $newPath;
- }
- // remove eventual redirection from this site to another
- $originUrl = $this->otWebsiteRepository->resolveWebsiteDomain($website);
- $this->removeRedirectionsFrom($originUrl);
- // Try to commit the result
- $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
- if (!$commitSuccess) {
- throw new RuntimeException('Something went wrong while commiting the result');
- }
- return $rootUid;
- } catch(\Throwable $e) {
- // rollback
- $this->connectionPool->getConnectionByName('Default')->rollback();
- foreach ($renamed as $initialPath => $newPath) {
- rename($newPath, $initialPath);
- }
- throw $e;
- }
- }
- /**
- * Clear the cache of the organization's website
- *
- * @param int $organizationId the organization's id whom site cache should be cleared
- * @param bool $clearAll if true, all caches will be cleared, and not only the frontend one
- * @return int
- * @throws NoSuchWebsiteException
- */
- public function clearSiteCacheAction(int $organizationId, bool $clearAll=false): int
- {
- $rootUid = $this->otWebsiteRepository->findRootUidForOrganization($organizationId);
- $this->otCacheManager->clearSiteCache($rootUid, $clearAll);
- return $rootUid;
- }
- /**
- * Perform a full scan of the website and returns a list of warnings
- *
- * @param int $organizationId
- * @return array
- * @throws NoSuchOrganizationException
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- */
- private function scanSite(int $organizationId): array
- {
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
- $warnings = [];
- // fetch and index pages and root page
- $pages = $this->otPageRepository->getPageWithSubpages($rootUid);
- $rootPage = null;
- $pageIndex = [];
- foreach ($pages as $page) {
- $pageIndex[$page['uid']] = $page;
- if ($page['is_siteroot'] === 1) { $rootPage = $page; }
- }
- // fetch organization and extradata
- $organization = $this->fetchOrganization($organizationId);
- $extraData = $this->fetchOrganizationExtraData($organizationId);
- // load site's settings (uncomment if needed)
- // $config = $this->otWebsiteRepository->findConfigFor($rootUid);
- // Check site's title
- if (trim($website['organization_name']) !== trim($organization->getName())) {
- $warnings[] = "Website's organization name is different from what is registered in the Opentalent DB";
- }
- if (trim($rootPage['title']) !== trim($organization->getName())) {
- $warnings[] = "Root page's title does not match the organization name";
- }
- // Who is the expected owner among the be_users? there should be only one.
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder->getRestrictions()->removeAll();
- $beUsers = $queryBuilder
- ->select('uid', 'username')
- ->from('be_users')
- ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
- ->execute()
- ->fetchAllAssociative();
- $owner = null;
- if (count($beUsers) > 1) {
- $warnings[] = 'Website is mounted on more than one be_user: ' .
- implode(', ', array_map(static function($u) { return $u['username']; } ,$beUsers));
- } elseif (count($beUsers) === 0) {
- $warnings[] = 'Website is not mounted on any be_user';
- } else {
- $owner = $beUsers[0];
- }
- // are template constants up to date?
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $actual_constants = $queryBuilder
- ->select('constants')
- ->from('sys_template')
- ->where($queryBuilder->expr()->eq('pid', $rootUid))
- ->execute()
- ->fetchOne();
- $expected_constants = $this->getTemplateConstants($organizationId, $extraData);
- $norm = static function ($s) {
- return strtolower(preg_replace('/\s/', '', $s));
- };
- if ($norm($expected_constants) !== $norm($actual_constants)) {
- $warnings[] = 'Template constants need an update';
- }
- $expected_templates = [
- "OpenTalent.OtTemplating->home" => 0,
- "OpenTalent.OtTemplating->legal" => 0,
- "OpenTalent.OtTemplating->contact" => 0
- ];
- foreach ($pages as $page) {
- if ($page['deleted']) {
- continue;
- }
- // Is it the correct owner?
- if ($owner !== null && $page['perms_userid'] !== $owner['uid']) {
- $warnings[] = 'Page ' . $page['uid'] . ' has wrong owner';
- }
- if (!$page['is_siteroot']) {
- // is the parent page state (deleted, hidden, restricted) the same as this page?
- $parent = $pageIndex[$page['pid']];
- if ($parent['deleted']) {
- $warnings[] = 'The non-deleted page ' . $page['uid'] . ' has a deleted parent page';
- }
- if ($parent['hidden'] && !$page['hidden']) {
- $warnings[] = 'The non-hidden page ' . $page['uid'] . ' has a hidden parent page';
- }
- if ($parent['fe_group'] < 0 && !($page['fe_group'] >= 0)) {
- $warnings[] = 'The non-restricted page ' . $page['uid'] . ' has a restricted parent page';
- }
- }
- // an expected template was found, remove it from the list of expected
- if (
- !$page['hidden'] &&
- in_array($page['tx_fed_page_controller_action'], $expected_templates, true)
- )
- {
- unset($expected_templates[$page['tx_fed_page_controller_action']]);
- }
- }
- foreach ($expected_templates as $template => $_) {
- $warnings[] = 'No page with template ' . $template;
- }
- return $warnings;
- }
- /**
- * Get the current status of the organization's website
- * If $fullScan is true, a deeper scan will be performed and warnings may be logged
- *
- * The status is among:
- *
- * - STATUS_NO_SUCH_WEBSITE
- * - STATUS_EXISTING
- * - STATUS_EXISTING_DELETED
- * - STATUS_EXISTING_HIDDEN
- * - STATUS_EXISTING_WITH_WARNINGS
- * - STATUS_EXISTING_WITH_CRITICAL_ERRORS
- *
- * @param int $organizationId the organization's id whom site cache should be cleared
- * @param bool $fullScan If true, a 'warnings' entry will be added to the result, and a full scan of
- * the website pages will be performed.
- * @return SiteStatus
- * @throws Exception
- * @throws InvalidWebsiteConfigurationException
- * @throws NoSuchOrganizationException
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- */
- public function getSiteStatusAction(int $organizationId, bool $fullScan = false): SiteStatus
- {
- try {
- $siteInfos = $this->getSiteInfosAction($organizationId);
- } catch (NoSuchWebsiteException $e) {
- return new SiteStatus($organizationId, SiteStatus::STATUS_NO_SUCH_WEBSITE);
- } catch(\Throwable $e) {
- return new SiteStatus($organizationId, SiteStatus::STATUS_EXISTING_WITH_CRITICAL_ERRORS);
- }
- if ($siteInfos->isDeleted()) {
- return new SiteStatus($organizationId, SiteStatus::STATUS_EXISTING_DELETED, $siteInfos);
- }
- if ($siteInfos->isHiddenOrRestricted()) {
- return new SiteStatus($organizationId, SiteStatus::STATUS_EXISTING_HIDDEN, $siteInfos);
- }
- $warnings = null;
- if ($fullScan) {
- // ** Look for potential issues
- $warnings = $this->scanSite($organizationId);
- }
- return new SiteStatus(
- $organizationId,
- $warnings ? SiteStatus::STATUS_EXISTING_WITH_WARNINGS : SiteStatus::STATUS_EXISTING,
- $siteInfos,
- $warnings
- );
- }
- /**
- * Set a new domain for the website.
- * If $redirect is true, also add a redirection from the former domain to the new one
- *
- * @param int $organizationId
- * @param string $newDomain
- * @param bool $redirect
- * @return int
- * @throws InvalidWebsiteConfigurationException
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- */
- public function setSiteCustomDomainAction(int $organizationId, string $newDomain, bool $redirect = true): int
- {
- if (!preg_match(self::RX_DOMAIN,$newDomain) &&
- !preg_match("/[a-z0-9A-Z-]+\//", $newDomain)) {
- throw new \InvalidArgumentException("The given domain does not seems to be a valid domain: " . $newDomain);
- }
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
- $websiteUid = $website['uid'];
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($websiteUid);
- // ## Update the ot_website table
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
- $queryBuilder->update('ot_websites')
- ->set('custom_domain', $newDomain)
- ->where($queryBuilder->expr()->eq('uid', $websiteUid))
- ->execute();
- $originDomain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
- if (preg_replace('/https?:\/\//', '', $originDomain) === preg_replace('/https?:\/\//', '', $newDomain) ) {
- throw new RuntimeException('The new domain should be different of the current one');
- }
- $this->writeConfigFile($rootUid, $newDomain);
- if ($redirect) {
- // Add the redirection
- try {
- $this->addRedirection($originDomain, $newDomain);
- } catch (\RuntimeException $e) {} // silent the error until this https://assistance.opentalent.fr/browse/V8-3344
- }
- // Redirect the www.xyz to xyz
- $this->addRedirection('www.' . $newDomain, $newDomain);
- $this->otCacheManager->clearSiteCache($rootUid, true);
- return $rootUid;
- }
- /**
- * Return all the redirections from the given domain name,
- * even if they have been marked as deleted.
- *
- * @param $domain string Domain name, without the http(s):// part
- * @return array Rows from the sys_redirect table
- * @throws Exception
- */
- protected function getRedirectionsFrom(string $domain): array
- {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_redirect');
- $queryBuilder->getRestrictions()->removeAll();
- return $queryBuilder
- ->select('*')
- ->from('sys_redirect')
- ->where($queryBuilder->expr()->eq('source_host', $queryBuilder->expr()->literal($domain)))
- ->execute()
- ->fetchAllAssociative();
- }
- /**
- * Return all the redirections to the given domain name,
- * even if they have been marked as deleted.
- *
- * @param $domain string Domain name, without the http(s):// part
- * @return array Rows from the sys_redirect table
- */
- protected function getRedirectionsTo(string $domain): array
- {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_redirect');
- $queryBuilder->getRestrictions()->removeAll();
- /** @noinspection HttpUrlsUsage */
- return $queryBuilder
- ->select('*')
- ->from('sys_redirect')
- ->where(
- $queryBuilder->expr()->in(
- 'target',
- [
- 'http://' . $queryBuilder->expr()->literal($domain),
- 'https://' . $queryBuilder->expr()->literal($domain)
- ]
- )
- )
- ->execute()
- ->fetchAllAssociative();
- }
- /**
- * Add a new redirection from $fromDomain to $toDomain.
- * If this redirection already exists but has been deleted and/or disabled, it will be restored and enabled
- * If a redirection already exists but is not deleted and targets another domain, a RuntimeException will be thrown.
- *
- * @param $fromDomain
- * @param $toDomain
- * @return int Status of the operation
- * @throws Exception
- */
- public function addRedirection($fromDomain, $toDomain): int
- {
- $fromDomain = preg_replace('/https?:\/\//', '', $fromDomain);
- $toDomain = preg_replace('/https?:\/\//', '', $toDomain);
- if (!preg_match(self::RX_DOMAIN, $fromDomain)) {
- throw new \InvalidArgumentException("The does not seems to be a valid domain: " . $fromDomain);
- }
- if (!preg_match(self::RX_DOMAIN, $toDomain)) {
- throw new \InvalidArgumentException("The does not seems to be a valid domain: " . $toDomain);
- }
- $existing = $this->getRedirectionsFrom($fromDomain);
- $toUpdate = null;
- foreach ($existing as $redirection) {
- if (!$redirection['deleted'] && !$redirection['disabled']) {
- // a redirection from this domain already exists, and it is not deleted nor disabled
- // check if the target is not the same domain
- if (!preg_match('/https?:\/\/' . $toDomain . '/', $redirection['target'])) {
- throw new RuntimeException(
- 'A redirection is already active for ' . $fromDomain . ' targeting ' . $redirection['target']
- );
- }
- // else, target is already the same domain, it will be updated
- $toUpdate = $redirection;
- break;
- }
- // a redirection from this domain already exists, but it is deleted or disabled
- if (preg_match('/https?:\/\/' . $toDomain . '/', $redirection['target'])) {
- // the target is the same domain, we'll reactivate this record
- $toUpdate = $redirection;
- break;
- }
- }
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_redirect');
- $data = [
- 'deleted' => 0,
- 'disabled' => 0,
- 'source_host' => $fromDomain,
- 'source_path' => '/.*/',
- 'is_regexp' => 1,
- 'force_https' => 0,
- 'respect_query_parameters' => 0,
- 'keep_query_parameters' => 1,
- 'target' => 'https://' . $toDomain,
- 'target_statuscode' => 301
- ];
- if ($toUpdate !== null) {
- $q = $queryBuilder
- ->update('sys_redirect')
- ->where($queryBuilder->expr()->eq('uid', $toUpdate['uid']));
- foreach ($data as $k => $v) {
- $q->set($k, $v);
- }
- $q->execute();
- return self::REDIRECTION_UPDATED;
- }
- $queryBuilder
- ->insert('sys_redirect')
- ->values($data)
- ->execute();
- return self::REDIRECTION_CREATED;
- }
- /**
- * Remove any existing redirection from $fromDomain to any url by marking it as deleted.
- * If $hard is true, delete it completely.
- *
- * @param $fromDomain
- * @param bool $hard
- * @return int Number of affected rows
- * @throws Exception
- */
- public function removeRedirectionsFrom($fromDomain, bool $hard=false): int
- {
- $fromDomain = preg_replace('/https?:\/\//', '', $fromDomain);
- if (!preg_match(self::RX_DOMAIN, $fromDomain)) {
- throw new \InvalidArgumentException("The does not seems to be a valid domain: " . $fromDomain);
- }
- $existing = $this->getRedirectionsFrom($fromDomain);
- $deleted = 0;
- foreach ($existing as $redirection) {
- $this->delete('sys_redirect', 'uid', $redirection['uid'], $hard);
- ++$deleted;
- }
- return $deleted;
- }
- /**
- * Set the rights of admin and editors of the website
- * on all the existing pages, including deleted ones
- *
- * @param int $organizationId
- * @param bool $createIfMissing Create the admin be user and/or the editors group if they are not found in the DB
- * @param int|null $editorsGroupUid Force the editors be-group uid
- * @param int|null $adminUid Force the admin be-user uid
- * @return int The uid of the website root page
- * @throws NoSuchWebsiteException
- * @throws NoSuchRecordException
- */
- protected function setBeUserPerms(
- int $organizationId,
- bool $createIfMissing = false,
- int $editorsGroupUid = null,
- int $adminUid = null
- ): int
- {
- if ($createIfMissing && ($editorsGroupUid || $adminUid)) {
- throw new \InvalidArgumentException("You can not set $createIfMissing to true " .
- "and force the admin or group uid at the same time.");
- }
- $rootUid = $this->otWebsiteRepository->findRootUidForOrganization($organizationId);
- $organizationExtraData = $this->fetchOrganizationExtraData($organizationId);
- $isPremium = self::IS_PRODUCT_PREMIUM[$organizationExtraData['admin']['product']] ?? false;
- if ($editorsGroupUid === null) {
- try {
- $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid);
- } catch (NoSuchRecordException $e) {
- if (!$createIfMissing) {
- throw $e;
- }
- }
- }
- if ($adminUid === null) {
- try {
- $adminUid = $this->findAdminBeUserUid($rootUid);
- } catch (NoSuchRecordException $e) {
- if (!$createIfMissing) {
- throw $e;
- }
- }
- }
- // Creates or update the admin be_group
- $editorsGroupUid = $this->createOrUpdateBeGroup(
- $organizationId,
- $rootUid,
- $organizationExtraData['admin'],
- $editorsGroupUid
- );
- // Creates or update the admin be_user
- $adminUid = $this->createOrUpdateBeUser(
- $organizationId,
- $editorsGroupUid,
- $organizationExtraData['admin'],
- $adminUid
- );
- // Reset the appartenance to groups
- $adminGroupUid = $this->getBaseBeGroupUid($isPremium ? self::BEGROUP_ADMIN_PREMIUM : self::BEGROUP_ADMIN_STANDARD);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
- $queryBuilder
- ->update('be_users')
- ->where($queryBuilder->expr()->eq('uid', $adminUid))
- ->set('usergroup', $adminGroupUid . ',' . $editorsGroupUid)
- ->execute();
- $mainEditorGroupUid = $this->getBaseBeGroupUid($isPremium ? self::BEGROUP_EDITOR_PREMIUM : self::BEGROUP_EDITOR_STANDARD);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- $queryBuilder
- ->update('be_groups')
- ->where($queryBuilder->expr()->eq('uid', $editorsGroupUid))
- ->set('subgroup', $mainEditorGroupUid)
- ->execute();
- // setup default owner for the website
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $tsConfig = $queryBuilder->select('TSconfig')
- ->from('pages')
- ->where($queryBuilder->expr()->eq('uid', $rootUid))
- ->execute()
- ->fetchOne();
- $tsConfig = trim(preg_replace('/TCEMAIN {[^{]*}/', '', $tsConfig));
- $tsConfig .= "\nTCEMAIN {\n" .
- " permissions.userid = " . $adminUid ."\n" .
- " permissions.groupid = " . $editorsGroupUid . "\n" .
- "}";
- $queryBuilder
- ->update('pages')
- ->where($queryBuilder->expr()->eq('uid', $rootUid))
- ->set('TSconfig', $tsConfig)
- ->execute();
- // fetch pages and root page
- $pages = $this->otPageRepository->getPageWithSubpages($rootUid);
- // To understand how the rights levels are computed:
- // @see https://gitlab.2iopenservice.com/opentalent/ot_typo3/-/blob/master/doc/be_users.md?ref_type=heads
- foreach ($pages as $page) {
- $hidden = $page['hidden'];
- if ($page['ot_page_type'] === OtPageTypeEnum::ROOT->value) {
- $adminPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE;
- $editorsPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE;
- if ($isPremium) {
- $adminPerms += self::PERM_NEW;
- $editorsPerms += self::PERM_NEW;
- }
- } else if ($page['ot_page_type'] === OtPageTypeEnum::MANDATORY_NON_EDITABLE->value) {
- $adminPerms = self::PERM_SHOW;
- $editorsPerms = self::PERM_SHOW;
- if ($isPremium) {
- $adminPerms += self::PERM_NEW;
- $editorsPerms += self::PERM_NEW;
- }
- } else if ($page['ot_page_type'] === OtPageTypeEnum::MANDATORY_EDITABLE->value) {
- $adminPerms = self::PERM_SHOW;
- $editorsPerms = self::PERM_SHOW;
- if ($isPremium) {
- $adminPerms += self::PERM_NEW + self::PERM_EDIT_PAGE;
- $editorsPerms += self::PERM_NEW + self::PERM_EDIT_PAGE;
- }
- } else if ($page['ot_page_type'] === OtPageTypeEnum::USER_CREATED->value && !$isPremium) {
- // Un utilisateur standard ne peut pas créer de nouvelles pages; les pages créées précédemment
- // avec une version premium doivent être inaccessibles.
- $adminPerms = self::PERM_NONE;
- $editorsPerms = self::PERM_NONE;
- $hidden = 1;
- } else if ($page['ot_page_type'] === OtPageTypeEnum::LEGACY_USER_CREATED->value && !$isPremium) {
- // Cas particulier des pages créées par les utilisateurs standard avant la remise à plat des droits
- // @see doc/be_users.md
- $adminPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE + self::PERM_DELETE;
- $editorsPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE + self::PERM_DELETE;
- } else {
- $adminPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE;
- if ($isPremium) {
- $adminPerms += self::PERM_DELETE + self::PERM_NEW;
- }
- $editorsPerms = self::PERM_SHOW + self::PERM_EDIT_CONTENT + self::PERM_EDIT_PAGE;
- if ($isPremium) {
- $editorsPerms += self::PERM_DELETE + self::PERM_NEW;
- }
- }
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder
- ->update('pages')
- ->where($queryBuilder->expr()->eq('uid', $page['uid']))
- ->set('perms_userid', $adminUid)
- ->set('perms_groupid', $editorsGroupUid)
- ->set('perms_user', $adminPerms)
- ->set('perms_group', $editorsPerms)
- ->set('perms_everybody', self::PERM_NONE)
- ->set('hidden', $hidden)
- ->execute();
- }
- return $rootUid;
- }
- /**
- * Action for resetting the rights of admin and editors of the website
- * on all the existing pages, including deleted ones
- *
- * @param int $organizationId
- * @param bool $createIfMissing
- * @return int
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws \Throwable
- */
- public function resetBeUserPermsAction(int $organizationId, bool $createIfMissing = false): int
- {
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- try {
- $rootUid = $this->setBeUserPerms($organizationId, $createIfMissing);
- $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
- if (!$commitSuccess) {
- throw new RuntimeException('Something went wrong while commiting the result');
- }
- return $rootUid;
- }
- catch (\Throwable $e) {
- // rollback
- $this->connectionPool->getConnectionByName('Default')->rollback();
- throw $e;
- }
- }
- /**
- * Delete all the pages created by the users, leaving only the starting mandatory pages.
- *
- * @param int $organizationId
- * @return int
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws \Doctrine\DBAL\Exception
- * @throws Throwable
- */
- public function deleteUserCreatedPagesAction(int $organizationId): int
- {
- $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
- $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
- $pages = $this->otPageRepository->getPageWithSubpages($rootUid);
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- try {
- foreach($pages as $page) {
- if ($page['ot_page_type'] === OtPageTypeEnum::USER_CREATED->value) {
- $this->delete('tt_content', 'pid', $page['uid'], true);
- $this->delete('pages', 'uid', $page['uid'], true);
- }
- }
- $this->connectionPool->getConnectionByName('Default')->commit();
- } catch (\Throwable $e) {
- $this->connectionPool->getConnectionByName('Default')->rollBack();
- throw $e;
- }
- return $rootUid;
- }
- /**
- * Delete then regenerate all the typo3 sites yaml config files
- *
- * This is a more efficient alternative to the update --all, designed to be executed on a development environment
- * just after the databases cloning.
- *
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- * @throws \Doctrine\DBAL\ConnectionException
- * @throws \Doctrine\DBAL\DBALException
- * @throws InvalidWebsiteConfigurationException
- * @throws \Throwable
- */
- public function regenConfigFilesAction(): void
- {
- $configRootDir = $_ENV['TYPO3_PATH_APP'] . "/config/sites/";
- $backupConfigDir = $_ENV['TYPO3_PATH_APP'] . "/config/bkp_sites";
- if (file_exists($backupConfigDir)) {
- throw new RuntimeException(
- 'A directory or a file with this name already exist: ' . $backupConfigDir . '. Delete it before continuing.'
- );
- }
- $this->connectionPool->getConnectionByName('Default')->beginTransaction();
- try {
- // archive the existing files, in case a rollback is needed
- rename($configRootDir, $backupConfigDir);
- $this->mkDir($configRootDir);
- $websites = $this->otWebsiteRepository->getAll();
- foreach ($websites as $website) {
- if (!((int)$website['organization_id'] > 0)) {
- continue;
- }
- $identifier = str_replace('/', '', $website['subdomain']) . '_' . $website['organization_id'];
- $configDir = $configRootDir . $identifier;
- $configFilename = $configDir . "/config.yaml";
- $siteConfig = $this->otWebsiteRepository->generateWebsiteConfiguration($website, $identifier);
- $config = $siteConfig->getConfiguration();
- $yamlConfig = Yaml::dump($config, 99, 2);
- try {
- $this->mkDir($configDir);
- } catch (\Throwable $e) {
- throw new RuntimeException('Unable to create the directory ' . $configDir, 0, $e);
- }
- $this->writeFile($configFilename, $yamlConfig);
- // ## Update the ot_website identifier
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
- $queryBuilder->update('ot_websites')
- ->set('config_identifier', $identifier)
- ->where($queryBuilder->expr()->eq('uid', $website['uid']))
- ->execute();
- }
- $commitSuccess = $this->connectionPool->getConnectionByName('Default')->commit();
- if (!$commitSuccess) {
- throw new RuntimeException('Something went wrong while committing the result');
- }
- $this->rrmdir($backupConfigDir);
- }
- catch (\Throwable $e) {
- // rollback
- if (!file_exists($configRootDir)) {
- $this->rrmdir($configRootDir);
- }
- if (!file_exists($backupConfigDir)) {
- rename($backupConfigDir, $configRootDir);
- }
- $this->connectionPool->getConnectionByName('Default')->rollback();
- throw $e;
- }
- }
- /**
- * Retrieve the Organization object from the repository and then,
- * from the Opentalent API
- *
- * @param $organizationId
- * @return Organization
- * @throws NoSuchOrganizationException
- */
- public function fetchOrganization($organizationId): object
- {
- try {
- return $this->organizationRepository->findById($organizationId);
- } catch (ApiRequestException $e) {
- throw new NoSuchOrganizationException('Unable to fetch the organization with id: ' . $organizationId);
- }
- }
- /**
- * Insert a new row in the 'pages' table of the Typo3 DB
- * and return its uid
- *
- * @param Organization $organization
- * @return int
- */
- private function insertOtWebsite(Organization $organization): int
- {
- $values = [
- 'organization_id' => $organization->getId(),
- 'subdomain' => $organization->getSubDomain(),
- 'locale' => 'fr_FR',
- 'organization_name' => $organization->getName(),
- 'template' => self::DEFAULT_THEME
- ];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
- $queryBuilder->insert('ot_websites')
- ->values($values)
- ->execute();
- return (int)$queryBuilder->getConnection()->lastInsertId();
- }
- /**
- * Determine which folder-type Typo3 page should contain the new website
- * CREATES IT if needed, and return its uid
- *
- * @return int
- */
- private function getParentFolderUid(): int
- {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $siteCount = $queryBuilder
- ->count('uid')
- ->from('pages')
- ->where('is_siteroot=1')
- ->execute()
- ->fetchOne();
- $thousand = (int)(($siteCount + 1) / 1000);
- $folderName = "Web Sites " . (1000 * $thousand) . " - " . ((1000 * $thousand) + 999);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $uid = $queryBuilder
- ->select('uid')
- ->from('pages')
- ->where($queryBuilder->expr()->eq('title', $queryBuilder->createNamedParameter($folderName)))
- ->andWhere('dokType=254')
- ->execute()
- ->fetchOne();
- if ($uid === null) {
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder->insert('pages')
- ->values([
- 'pid' => 0,
- 'title' => $folderName,
- 'dokType' => 254,
- 'sorting' => 11264,
- 'perms_userid' => 1,
- 'perms_groupid' => 31,
- 'perms_group' => 27,
- ])
- ->execute();
- $uid = $queryBuilder->getConnection()->lastInsertId();
- }
- return $uid;
- }
- /**
- * Insert a new row in the 'pages' table of the Typo3 DB
- * and return its uid
- *
- * @param int $website_uid
- * @param int $pid
- * @param string $title
- * @param string $slug
- * @param OtPageTypeEnum $otPageType
- * @param string $template
- * @param array $moreValues
- * @return int
- */
- private function insertPage(int $website_uid,
- int $pid,
- string $title,
- string $slug,
- OtPageTypeEnum $otPageType = OtPageTypeEnum::MANDATORY_EDITABLE,
- string $template = '',
- array $moreValues = []
- ): int
- {
- $defaultValues = [
- 'pid' => $pid,
- 'perms_groupid' => 3,
- 'perms_user' => 27,
- 'dokType' => self::DOK_PAGE,
- 'title' => $title,
- 'slug' => $slug,
- 'backend_layout' => 'flux__grid',
- 'backend_layout_next_level' => 'flux__grid',
- 'ot_website_uid' => $website_uid,
- 'ot_page_type' => $otPageType->value
- ];
- if ($template) {
- $defaultValues['tx_fed_page_controller_action'] = $template;
- $defaultValues['tx_fed_page_controller_action_sub'] = self::TEMPLATE_1COL;
- }
- $values = array_merge($defaultValues, $moreValues);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages');
- $queryBuilder->insert('pages')
- ->values($values)
- ->execute();
- $uid = (int)$queryBuilder->getConnection()->lastInsertId();
- $this->createdPagesIndex[$slug] = $uid;
- return $uid;
- }
- /**
- * Insert the root page of a new organization's website
- * and return its uid
- *
- * @param int $website_uid
- * @param string $title
- * @return int
- */
- private function insertRootPage(int $website_uid, string $title): int
- {
- return $this->insertPage(
- $website_uid,
- $this->getParentFolderUid(),
- $title,
- '/',
- OtPageTypeEnum::ROOT,
- self::TEMPLATE_HOME,
- [
- 'is_siteroot' => 1,
- 'TSconfig' => 'TCAdefaults.pages.ot_website_uid=' . $website_uid
- ]
- );
- }
- /**
- * Insert a new row in the 'tt_content' table of the Typo3 DB
- *
- * @param int $pid
- * @param string $cType
- * @param string $bodyText
- * @param int $colPos
- * @param array $moreValues
- */
- private function insertContent(int $pid,
- string $cType=self::CTYPE_TEXT,
- string $bodyText = '',
- int $colPos=0,
- array $moreValues = []): void
- {
- $defaultValues = [
- 'pid' => $pid,
- 'CType' => $cType,
- 'colPos' => $colPos,
- 'bodyText' => $bodyText
- ];
- $values = array_merge($defaultValues, $moreValues);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('tt_content');
- $queryBuilder->insert('tt_content')
- ->values($values)
- ->execute();
- }
- private function fetchOrganizationExtraData(int $organizationId) {
- $cnn = new PDO(
- "mysql:host=" . OpentalentEnvService::get('DB_HOST') . ";dbname=opentalent",
- OpentalentEnvService::get('DB_USER'),
- OpentalentEnvService::get('DB_PASSWORD'),
- array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
- );
- $cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $stmt = $cnn->query(
- "SELECT o.id, o.name, o.facebook, o.twitter, o.instagram, o.youtube,
- o.category, o.logo_id, p.logoDonorsMove
- FROM opentalent.Organization o INNER JOIN opentalent.Parameters p
- ON o.parameters_id = p.id
- WHERE o.id=" . $organizationId . ";"
- );
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- $data = $stmt->fetch();
- $stmt = $cnn->query(
- "SELECT c.email
- FROM opentalent.ContactPoint c
- INNER JOIN opentalent.organization_contactpoint o
- ON o.contactPoint_id = c.id
- WHERE o.organization_id = " . $organizationId . "
- ORDER BY FIELD(c.contactType, 'CONTACT', 'PRINCIPAL', 'BILL', 'OTHER');"
- );
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- $data['email'] = $stmt->fetch()['email'];
- $stmt = $cnn->query(
- "SELECT n.name, n.logo, n.url
- FROM opentalent.Network n
- INNER JOIN
- (opentalent.NetworkOrganization l
- INNER JOIN opentalent.Organization o
- ON l.organization_id = o.id)
- ON l.network_id = n.id
- WHERE l.endDate is NULL
- AND o.id=" . $organizationId . ";"
- );
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- $data['network'] = $stmt->fetch();
- $stmt = $cnn->query(
- "SELECT p.username, p.id as person_id, s.product
- FROM opentalent.Person p
- INNER JOIN opentalent.Access a ON p.id = a.person_id
- INNER JOIN opentalent.Settings s on a.organization_id = s.organization_id
- where a.organization_id=" . $organizationId . " AND a.adminAccess=1;"
- );
- $stmt->setFetchMode(PDO::FETCH_ASSOC);
- $data['admin'] = $stmt->fetch();
- return $data;
- }
- /**
- * Return the content of `sys_template`.`constants` of
- * the website of the given organization
- *
- * @param int $organizationId
- * @param array $organizationExtraData
- * @return string
- */
- private function getTemplateConstants(int $organizationId, array $organizationExtraData): string
- {
- return "plugin.tx_ottemplating {\n" .
- " settings {\n" .
- " organization {\n" .
- " id = " . $organizationId . "\n" .
- " name = " . $organizationExtraData['name'] . "\n" .
- " is_network = " . ($organizationExtraData['category'] === 'NETWORK' ? '1' : '0') . "\n" .
- " email = " . $organizationExtraData['email'] . "\n" .
- " logoid = " . $organizationExtraData['logo_id'] . "\n" .
- " twitter = " . $organizationExtraData['twitter'] . "\n" .
- " facebook = " . $organizationExtraData['facebook'] . "\n" .
- " instagram = " . $organizationExtraData['instagram'] . "\n" .
- " youtube = " . $organizationExtraData['youtube'] . "\n" .
- " }\n" .
- " network {\n" .
- " logo = " . $organizationExtraData['network']['logo'] . "\n" .
- " name = " . $organizationExtraData['network']['name'] . "\n" .
- " url = " . $organizationExtraData['network']['url'] . "\n" .
- " }\n" .
- " }\n" .
- "}";
- }
- /**
- * Create the given directory, give its property to the www-data group and
- * record it as a newly created dir (for an eventual rollback)
- *
- * @param string $dirPath
- */
- private function mkDir(string $dirPath): void
- {
- if (!mkdir($dirPath) && !is_dir($dirPath)) {
- throw new RuntimeException(sprintf('Directory "%s" was not created', $dirPath));
- }
- $this->createdDirs[] = $dirPath;
- chgrp($dirPath, 'www-data');
- }
- /**
- * Write the given file with content, give its property to the www-data group and
- * record it as a newly created file (for an eventual rollback)
- *
- * @param string $path
- * @param string $content
- */
- private function writeFile(string $path, string $content): void
- {
- $f = fopen($path, 'wb');
- try
- {
- fwrite($f, $content);
- $this->createdFiles[] = $path;
- try {
- chgrp($path, 'www-data');
- } catch (\Throwable $e) {
- }
- } finally {
- fclose($f);
- }
- }
- /**
- * Create or update the .../sites/.../config.yaml file of the given site
- * Return the identifier of the created website
- *
- * @param int $rootUid
- * @param bool $forceRecreate
- * @param null $identifier
- * @return string Identifier of the newly created configuration file
- * @throws InvalidWebsiteConfigurationException
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- */
- private function writeConfigFile(int $rootUid, bool $forceRecreate = false, $identifier = null): string
- {
- $website = $this->otWebsiteRepository->getWebsiteByPageUid($rootUid);
- $domain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
- try {
- $existing = $this->otWebsiteRepository->findConfigFileAndContentFor($rootUid, $identifier);
- } catch (RuntimeException $e) {
- // identifier might be obsolete
- $existing = $this->otWebsiteRepository->findConfigFileAndContentFor($rootUid);
- }
- [$configFilename, $config] = $existing;
- if (!$configFilename) {
- $identifier = $website['subdomain'] . '_' . $website['organization_id'];
- $configDir = $_ENV['TYPO3_PATH_APP'] . "/config/sites/" . $identifier;
- $configFilename = $configDir . "/config.yaml";
- $isNew = true;
- if (file_exists($configFilename)) {
- throw new RuntimeException("A file named " . $configFilename . " already exists. Abort.");
- }
- } else {
- $configDir = dirname($configFilename);
- if ($identifier === null) {
- $identifier = basename($configDir);
- }
- $config['base'] = 'https://' . $domain;
- $isNew = false;
- }
- if ($isNew || $forceRecreate) {
- $siteConfig = $this->otWebsiteRepository->generateWebsiteConfiguration($website, $identifier);
- $config = $siteConfig->getConfiguration();
- }
- $yamlConfig = Yaml::dump($config, 99, 2);
- if (!file_exists($configDir)) {
- $this->mkDir($configDir);
- }
- $this->writeFile($configFilename, $yamlConfig);
- // Set the owner and mods, in case www-data is not the one who run this command
- // @see https://www.php.net/manual/fr/function.stat.php
- try {
- $stats = stat($_ENV['TYPO3_PATH_APP'] . '/public/index.php');
- chown($configFilename, $stats['4']);
- chgrp($configFilename, $stats['5']);
- chmod($configFilename, $stats['2']);
- } catch (\Throwable $e) {
- }
- // Flush cache:
- try {
- $cacheSystem = $this->cacheManager->getCache('core');
- $cacheSystem->remove('site-configuration');
- $cacheSystem->remove('pseudo-sites');
- } catch (NoSuchCacheException $e) {
- }
- return $identifier;
- }
- /**
- * Recursively remove the target directory (! no rollback available)
- */
- private function rrmdir(string $dir): bool
- {
- if (!is_dir($dir) || is_link($dir)) {
- return unlink($dir);
- }
- foreach (scandir($dir) as $file) {
- if ($file === '.' || $file === '..') {
- continue;
- }
- if (!$this->rrmdir($dir . DIRECTORY_SEPARATOR . $file)) {
- chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
- if (!$this->rrmdir($dir . DIRECTORY_SEPARATOR . $file)) {
- return false;
- }
- }
- }
- return rmdir($dir);
- }
- /**
- * Create the BE user for the website, then return its uid
- * The user shall be already created in the Opentalent DB
- *
- * @param int $organizationId
- * @param int $siteGroupUid
- * @param array $userData
- * @param int|null $updateUid If passed, this method will update this be user instead of creating a new one
- * @return int The uid of the created be_user
- * @throws InvalidPasswordHashException
- */
- private function createOrUpdateBeUser(int $organizationId,
- int $siteGroupUid,
- array $userData,
- int $updateUid = null): int
- {
- if (!isset($userData['username'])) {
- throw new RuntimeException('Can not find any user with admin access in the Opentalent DB. Abort.');
- }
- $hashInstance = GeneralUtility::makeInstance(PasswordHashFactory::class)->getDefaultHashInstance('BE');
- $hashedRandomPassword = $hashInstance->getHashedPassword(uniqid("", true));
- $adminGroup = (self::IS_PRODUCT_PREMIUM[$userData['product']] ?? false) ? self::BEGROUP_ADMIN_PREMIUM : self::BEGROUP_ADMIN_STANDARD;
- $adminGroupUid = $this->getBaseBeGroupUid($adminGroup);
- $values = [
- 'username' => $userData['username'],
- 'password' => $hashedRandomPassword,
- 'description' => '[Auto-generated] BE Admin for organization ' . $organizationId,
- 'deleted' => 0,
- 'lang' => 'fr',
- 'usergroup' => $siteGroupUid . ',' . $adminGroupUid,
- 'userMods' => null, // inherited from the base AdminGroup
- 'db_mountpoints' => null, // inherited from the editors group
- 'file_mountpoints' => null, // inherited from the editors group
- 'options' => 3, // allow to inherit both db and file mountpoints from groups
- 'tx_opentalent_opentalentId' => $userData['id'] ?? null,
- 'tx_opentalent_organizationId' => $organizationId,
- 'tx_opentalent_generationDate' => date('Y/m/d H:i:s')
- ];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
- if ($updateUid !== null) {
- $q = $queryBuilder->update('be_users')->where($queryBuilder->expr()->eq('uid', $updateUid));
- foreach ($values as $k => $v) {
- $q->set($k, $v);
- }
- $q->execute();
- return $updateUid;
- }
- $queryBuilder->insert('be_users')
- ->values($values)
- ->execute();
- return (int)$queryBuilder->getConnection()->lastInsertId();
- }
- /**
- * Create the BE editors group for the website, then return its uid
- *
- * @param int $organizationId
- * @param int $rootUid
- * @param array $userData
- * @param int|null $updateUid If passed, this method will update this be_group instead of creating a new one
- * @return int The uid of the created be_group
- */
- private function createOrUpdateBeGroup(int $organizationId,
- int $rootUid,
- array $userData,
- int $updateUid = null): int
- {
- $groupName = 'editors_' . $organizationId;
- // get the existing filemounts
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_filemounts');
- $queryBuilder
- ->select('uid')
- ->from('sys_filemounts')
- ->where("identifier LIKE '1:%user_upload/" . $organizationId . "/'")
- ->orWhere("identifier LIKE '1:%form_definitions/" . $organizationId . "/'");
- $statement = $queryBuilder->executeQuery();
- $rows = $statement->fetchAllAssociative() ?: [];
- $fileMounts = [];
- foreach ($rows as $row) {
- $fileMounts[] = $row['uid'];
- }
- $mainGroup = (self::IS_PRODUCT_PREMIUM[$userData['product']] ?? false) ? self::BEGROUP_EDITOR_PREMIUM : self::BEGROUP_EDITOR_STANDARD;
- $mainGroupUid = $this->getBaseBeGroupUid($mainGroup);
- $values = [
- 'title' => $groupName,
- 'deleted' => 0,
- 'subgroup' => $mainGroupUid,
- 'db_mountpoints' => $rootUid,
- 'file_mountPoints' => implode(',', $fileMounts),
- 'file_permissions' => 'readFolder,writeFolder,addFolder,renameFolder,moveFolder,deleteFolder,readFile,writeFile,addFile,renameFile,replaceFile,moveFile,copyFile,deleteFile',
- 'groupMods' => '', // inherited from the base EditorsGroup
- 'pagetypes_select' => '', // inherited from the base EditorsGroup
- 'tables_select' => '', // inherited from the base EditorsGroup
- 'tables_modify' => '', // inherited from the base EditorsGroup
- 'non_exclude_fields' => '', // inherited from the base EditorsGroup
- ];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- if ($updateUid !== null) {
- $q = $queryBuilder->update('be_groups')->where($queryBuilder->expr()->eq('uid', $updateUid));
- foreach ($values as $k => $v) {
- $q->set($k, $v);
- }
- $q->executeStatement();
- return $updateUid;
- }
- $queryBuilder->insert('be_groups')
- ->values($values)
- ->executeStatement();
- return $queryBuilder->getConnection()->lastInsertId();
- }
- /**
- * Try to find and return the uid of the editors be_group
- * for this website
- *
- * @param int $rootUid
- * @param bool $withRestrictions If false, the default restrictions won't apply, meaning this could return a deleted record
- * @return int
- * @throws Exception
- * @throws NoSuchRecordException
- */
- protected function findEditorsBeGroupUid(int $rootUid, bool $withRestrictions=true): int {
- $editorsGroups = [
- $this->getBaseBeGroupUid(self::BEGROUP_EDITOR_STANDARD),
- $this->getBaseBeGroupUid(self::BEGROUP_EDITOR_PREMIUM)
- ];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- if (!$withRestrictions) {
- $queryBuilder->getRestrictions()->removeAll();
- }
- $editorsGroupUid = $queryBuilder
- ->select('uid')
- ->from('be_groups')
- ->where('FIND_IN_SET(' . $rootUid . ', db_mountpoints) > 0')
- ->andWhere('(FIND_IN_SET(' . $editorsGroups[0] . ', subgroup) > 0 OR FIND_IN_SET(' . $editorsGroups[1] . ', subgroup) > 0)')
- ->execute()
- ->fetchOne();
- if ($editorsGroupUid === null) {
- throw new NoSuchRecordException("No editors be_group found " .
- "among the groups that have this website mounted (root uid: " . $rootUid . ")");
- }
- return $editorsGroupUid;
- }
- /**
- * Try to find and return the uid of the admin be_user
- * for this website
- *
- * @param int $rootUid
- * @param bool $withRestrictions If false, the default restrictions won't apply, meaning this could return a deleted record
- * @return int
- * @throws NoSuchRecordException
- * @throws NoSuchWebsiteException
- */
- protected function findAdminBeUserUid(int $rootUid, bool $withRestrictions=true): int {
- $adminGroups = [
- $this->getBaseBeGroupUid(self::BEGROUP_ADMIN_STANDARD),
- $this->getBaseBeGroupUid(self::BEGROUP_ADMIN_PREMIUM)
- ];
- $adminUid = null;
- try {
- $editorsGroupUid = $this->findEditorsBeGroupUid($rootUid);
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
- if (!$withRestrictions) {
- $queryBuilder->getRestrictions()->removeAll();
- }
- return $queryBuilder
- ->select('uid')
- ->from('be_users')
- ->where('FIND_IN_SET(' . $editorsGroupUid . ', usergroup) > 0')
- ->andWhere('(FIND_IN_SET(' . $adminGroups[0] . ', usergroup) > 0 OR FIND_IN_SET(' . $adminGroups[1] . ', usergroup) > 0)')
- ->execute()
- ->fetchOne();
- } catch (NoSuchRecordException $e) {
- // the editors group does not exist
- }
- // [For retro-compatibility] Try to find if there is a be_user still in the v8.7 data format
- if ($adminUid === null) {
- $website = $this->otWebsiteRepository->getWebsiteByPageUid($rootUid);
- $extraData = $this->fetchOrganizationExtraData($website['organization_id']);
- $expectedUsername = $extraData['admin']['username'];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_users');
- if (!$withRestrictions) {
- $queryBuilder->getRestrictions()->removeAll();
- }
- $row = $queryBuilder
- ->select('uid', 'db_mountpoints')
- ->from('be_users')
- ->where($queryBuilder->expr()->eq('username', $queryBuilder->expr()->literal($expectedUsername)))
- ->execute()
- ->fetchAssociative();
- if ($row['uid']) {
- if ((string)$rootUid !== (string)$row['db_mountpoints']) {
- throw new RuntimeException(
- "The be_user named '" . $expectedUsername .
- "' has unexpected mounted website(s) (expected: " . $rootUid .
- ", found: " . $row['db_mountpoints'] . "). Abort."
- );
- }
- $adminUid = $row['uid'];
- }
- }
- if ($adminUid === null) {
- throw new NoSuchRecordException("No admin be_user found " .
- "among the users that have this website mounted (root uid: " . $rootUid . ")");
- }
- return $adminUid;
- }
- /**
- * Return the uid of one of the base groups (BEGROUP_EDITOR_STANDARD, BEGROUP_EDITOR_PREMIUM, ...)
- *
- * @param int $groupType
- * @return int
- */
- protected function getBaseBeGroupUid(int $groupType): int
- {
- $expectedName = self::BEGROUP_NAME[$groupType];
- $queryBuilder = $this->connectionPool->getQueryBuilderForTable('be_groups');
- $uid = $queryBuilder
- ->select('uid')
- ->from('be_groups')
- ->where($queryBuilder->expr()->eq('title', $queryBuilder->expr()->literal($expectedName)))
- ->execute()
- ->fetchOne();
- if (!$uid) {
- throw new RuntimeException("Expects a BE group named '" . $expectedName . "', but none was found.");
- }
- return $uid;
- }
- /**
- * Enable frontend editing for user
- *
- * @param int $adminUid
- * @noinspection PhpInternalEntityUsedInspection
- */
- private function enableFeEditing(int $adminUid): void
- {
- $BE_USER = GeneralUtility::makeInstance(BackendUserAuthentication::class);
- $user = $BE_USER->getRawUserByUid($adminUid);
- $BE_USER->user = $user;
- $BE_USER->backendSetUC();
- $BE_USER->uc['frontend_editing'] = 1;
- $BE_USER->uc['frontend_editing_overlay'] = 1;
- $BE_USER->writeUC($BE_USER->uc);
- }
- }
|