Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit acd5cd3

Browse files
committed
New action MigrateLegacyShares
1 parent 3ac26a6 commit acd5cd3

File tree

3 files changed

+180
-1
lines changed

3 files changed

+180
-1
lines changed

core/src/plugins/action.share/class.LegacyPubliclet.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,172 @@ public static function publicletToJson($shareId, $shareMeta, $shareStore, $publi
300300
return $jsonData;
301301
}
302302

303+
/**
304+
* @param ShareCenter $shareCenter
305+
* @param ShareStore $shareStore
306+
* @param ShareRightsManager $shareRightManager
307+
*/
308+
public static function migrateLegacyMeta($shareCenter, $shareStore, $shareRightManager, $dryRun = true){
309+
$metaStoreDir = AJXP_DATA_PATH."/plugins/metastore.serial";
310+
$publicFolder = ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER");
311+
$metastores = glob($metaStoreDir."/ajxp_meta_0");
312+
if($dryRun){
313+
print("RUNNING A DRY RUN FOR META MIGRATION");
314+
}
315+
foreach($metastores as $store){
316+
if(strpos($store, ".bak") !== false) continue;
317+
// Backup store
318+
if(!$dryRun){
319+
copy($store, $store.".bak");
320+
}
321+
322+
$data = unserialize(file_get_contents($store));
323+
foreach($data as $filePath => &$metadata){
324+
325+
326+
327+
foreach($metadata as $userName => &$meta){
328+
if(!AuthService::userExists($userName)){
329+
continue;
330+
}
331+
$userObject = ConfService::getConfStorageImpl()->createUserObject($userName);
332+
333+
if(isSet($meta["ajxp_shared"]) && isSet($meta["ajxp_shared"]["element"])){
334+
print("\n\nItem $filePath requires upgrade :");
335+
$share = $meta["ajxp_shared"];
336+
$element = $meta["ajxp_shared"]["element"];
337+
if(is_array($element)) $element = array_shift(array_keys($element));// Take the first one only
338+
$legacyLinkFile = $publicFolder."/".$element.".php";
339+
if(file_exists($legacyLinkFile)){
340+
// Load file, move it to DB and move the meta
341+
$publiclet = $shareStore->loadShare($element);
342+
rename($legacyLinkFile, $legacyLinkFile.".migrated");
343+
if(isSet($share["minisite"])){
344+
print("\n--Migrate legacy minisite to new minisite?");
345+
$sharedRepoId = $publiclet["REPOSITORY"];
346+
$sharedRepo = ConfService::getRepositoryById($sharedRepoId);
347+
if($sharedRepo == null){
348+
print("\n--ERROR: Cannot find repository with id ".$sharedRepoId);
349+
continue;
350+
}
351+
$shareLink = new ShareLink($shareStore, $publiclet);
352+
$user = $shareLink->getUniqueUser();
353+
if(AuthService::userExists($user)){
354+
$userObject = ConfService::getConfStorageImpl()->createUserObject($user);
355+
$userObject->setHidden(true);
356+
print("\n--Should set existing user $user as hidden");
357+
if(!$dryRun){
358+
$userObject->save();
359+
}
360+
}
361+
$shareLink->parseHttpVars(["custom_handle" => $element]);
362+
$shareLink->setParentRepositoryId($sharedRepo->getParentId());
363+
print("\n--Creating the following share object");
364+
print_r($shareLink->getJsonData($shareCenter->getPublicAccessManager(), ConfService::getMessages()));
365+
if(!$dryRun){
366+
$shareLink->save();
367+
}
368+
$meta["ajxp_shared"] = ["shares" => [$element => ["type" => "minisite"], $sharedRepoId => ["type" => "repository"]]];
369+
370+
371+
}else{
372+
print("\n--Should migrate legacy link to new minisite with ContentFilter");
373+
374+
try{
375+
$link = new ShareLink($shareStore);
376+
$link->setOwnerId($userName);
377+
$parameters = array("custom_handle" => $element, "simple_right_download" => true);
378+
if(isSet($publiclet["EXPIRE_TIME"])) $parameters["expiration"] = $publiclet["EXPIRE_TIME"];
379+
if(isSet($publiclet["DOWNLOAD_LIMIT"])) $parameters["downloadlimit"] = $publiclet["DOWNLOAD_LIMIT"];
380+
$link->parseHttpVars($parameters);
381+
$parentRepositoryObject = $publiclet["REPOSITORY"];
382+
383+
$driverInstance = AJXP_PluginsService::findPlugin("access", $parentRepositoryObject->getAccessType());
384+
if(empty($driverInstance)){
385+
print("\n-- ERROR: Cannot find driver instance!");
386+
continue;
387+
}
388+
$options = $driverInstance->makeSharedRepositoryOptions(["file" => "/"], $parentRepositoryObject);
389+
$options["SHARE_ACCESS"] = "private";
390+
$newRepo = $parentRepositoryObject->createSharedChild(
391+
basename($filePath),
392+
$options,
393+
$parentRepositoryObject->getId(),
394+
$userObject->getId(),
395+
null
396+
);
397+
$gPath = $userObject->getGroupPath();
398+
if (!empty($gPath) && !ConfService::getCoreConf("CROSSUSERS_ALLGROUPS", "conf")) {
399+
$newRepo->setGroupPath($gPath);
400+
}
401+
$newRepo->setDescription("");
402+
// Smells like dirty hack!
403+
$newRepo->options["PATH"] = SystemTextEncoding::fromStorageEncoding($newRepo->options["PATH"]);
404+
405+
$newRepo->setContentFilter(new ContentFilter([new AJXP_Node("pydio://".$parentRepositoryObject->getId().$filePath)]));
406+
if(!$dryRun){
407+
ConfService::addRepository($newRepo);
408+
}
409+
410+
$hiddenUserEntry = $shareRightManager->prepareSharedUserEntry(
411+
["simple_right_read" => true, "simple_right_download" => true],
412+
$link, false, null);
413+
414+
$selection = new UserSelection($parentRepositoryObject, []);
415+
$selection->addFile($filePath);
416+
if(!$dryRun){
417+
$shareRightManager->assignSharedRepositoryPermissions(
418+
$parentRepositoryObject,
419+
$newRepo,
420+
false,
421+
[$hiddenUserEntry["ID"] => $hiddenUserEntry], [],
422+
$selection
423+
);
424+
}
425+
$link->setParentRepositoryId($parentRepositoryObject->getId());
426+
$link->attachToRepository($newRepo->getId());
427+
print("\n-- Should save following LINK: ");
428+
print_r($link->getJsonData($shareCenter->getPublicAccessManager(), ConfService::getMessages()));
429+
if(!$dryRun){
430+
$hash = $link->save();
431+
}
432+
433+
// UPDATE METADATA
434+
$meta["ajxp_shared"] = ["shares" => [$element => array("type" => "minisite")]];
435+
436+
}catch(Exception $e){
437+
print("\n-- ERROR: ".$e->getMessage());
438+
}
439+
440+
441+
}
442+
if($dryRun){
443+
rename($legacyLinkFile.".migrated", $legacyLinkFile);
444+
}
445+
continue;
446+
}else{
447+
//
448+
// File does not exists, remove meta
449+
//
450+
unset($meta["ajxp_shared"]);
451+
}
452+
453+
454+
$repo = ConfService::getRepositoryById($element);
455+
if($repo !== null){
456+
print("\n--Shared repository: just metadata");
457+
// Shared repo, migrating the meta should be enough
458+
$meta["ajxp_shared"] = array("shares" => [$element => array("type" => "repository")]);
459+
}
460+
}
461+
}
462+
}
463+
print("\n\n SHOULD NOW UPDATE METADATA WITH FOLLOWING :");
464+
print_r($data);
465+
if(!$dryRun){
466+
file_put_contents($store, serialize($data));
467+
}
468+
}
469+
}
470+
303471
}

core/src/plugins/action.share/class.ShareCenter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ public function preProcessDownload($action, &$httpVars, &$fileVars){
378378
}
379379
}
380380

381+
public function migrateLegacyShares($action, $httpVars, $fileVars){
382+
require_once("class.LegacyPubliclet.php");
383+
LegacyPubliclet::migrateLegacyMeta($this, $this->getShareStore(), $this->getRightsManager(), $httpVars["dry_run"] !== "run");
384+
}
385+
381386
/**
382387
* Main callback for all share- actions.
383388
* @param string $action
@@ -879,7 +884,7 @@ public function nodeSharedMetadata(&$ajxpNode)
879884
if(!empty($shares)){
880885
$compositeShare = $this->getShareStore()->getMetaManager()->getCompositeShareForNode($ajxpNode);
881886
if(empty($compositeShare) || $compositeShare->isInvalid()){
882-
$this->getShareStore()->getMetaManager()->clearNodeMeta($ajxpNode);
887+
//$this->getShareStore()->getMetaManager()->clearNodeMeta($ajxpNode);
883888
return;
884889
}
885890
}

core/src/plugins/action.share/manifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
</serverCallback>
101101
</processing>
102102
</action>
103+
<action name="migrate_legacy_shares">
104+
<rightsContext adminOnly="true" noUser="false" read="true" userLogged="true" write="true"/>
105+
<processing>
106+
<serverCallback methodName="migrateLegacyShares" restParams="/dry_run"/>
107+
</processing>
108+
</action>
103109
<!--
104110
<action name="share-edit-shared">
105111
<gui src="share.png" iconClass="icon-share" text="share_center.125" title="share_center.126">

0 commit comments

Comments
 (0)