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

Commit

Permalink
New action MigrateLegacyShares
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed May 4, 2016
1 parent 3ac26a6 commit acd5cd3
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 1 deletion.
168 changes: 168 additions & 0 deletions core/src/plugins/action.share/class.LegacyPubliclet.php
Expand Up @@ -300,4 +300,172 @@ public static function publicletToJson($shareId, $shareMeta, $shareStore, $publi
return $jsonData;
}

/**
* @param ShareCenter $shareCenter
* @param ShareStore $shareStore
* @param ShareRightsManager $shareRightManager
*/
public static function migrateLegacyMeta($shareCenter, $shareStore, $shareRightManager, $dryRun = true){
$metaStoreDir = AJXP_DATA_PATH."/plugins/metastore.serial";
$publicFolder = ConfService::getCoreConf("PUBLIC_DOWNLOAD_FOLDER");
$metastores = glob($metaStoreDir."/ajxp_meta_0");
if($dryRun){
print("RUNNING A DRY RUN FOR META MIGRATION");
}
foreach($metastores as $store){
if(strpos($store, ".bak") !== false) continue;
// Backup store
if(!$dryRun){
copy($store, $store.".bak");
}

$data = unserialize(file_get_contents($store));
foreach($data as $filePath => &$metadata){



foreach($metadata as $userName => &$meta){
if(!AuthService::userExists($userName)){
continue;
}
$userObject = ConfService::getConfStorageImpl()->createUserObject($userName);

if(isSet($meta["ajxp_shared"]) && isSet($meta["ajxp_shared"]["element"])){
print("\n\nItem $filePath requires upgrade :");
$share = $meta["ajxp_shared"];
$element = $meta["ajxp_shared"]["element"];
if(is_array($element)) $element = array_shift(array_keys($element));// Take the first one only
$legacyLinkFile = $publicFolder."/".$element.".php";
if(file_exists($legacyLinkFile)){
// Load file, move it to DB and move the meta
$publiclet = $shareStore->loadShare($element);
rename($legacyLinkFile, $legacyLinkFile.".migrated");
if(isSet($share["minisite"])){
print("\n--Migrate legacy minisite to new minisite?");
$sharedRepoId = $publiclet["REPOSITORY"];
$sharedRepo = ConfService::getRepositoryById($sharedRepoId);
if($sharedRepo == null){
print("\n--ERROR: Cannot find repository with id ".$sharedRepoId);
continue;
}
$shareLink = new ShareLink($shareStore, $publiclet);
$user = $shareLink->getUniqueUser();
if(AuthService::userExists($user)){
$userObject = ConfService::getConfStorageImpl()->createUserObject($user);
$userObject->setHidden(true);
print("\n--Should set existing user $user as hidden");
if(!$dryRun){
$userObject->save();
}
}
$shareLink->parseHttpVars(["custom_handle" => $element]);
$shareLink->setParentRepositoryId($sharedRepo->getParentId());
print("\n--Creating the following share object");
print_r($shareLink->getJsonData($shareCenter->getPublicAccessManager(), ConfService::getMessages()));
if(!$dryRun){
$shareLink->save();
}
$meta["ajxp_shared"] = ["shares" => [$element => ["type" => "minisite"], $sharedRepoId => ["type" => "repository"]]];


}else{
print("\n--Should migrate legacy link to new minisite with ContentFilter");

try{
$link = new ShareLink($shareStore);
$link->setOwnerId($userName);
$parameters = array("custom_handle" => $element, "simple_right_download" => true);
if(isSet($publiclet["EXPIRE_TIME"])) $parameters["expiration"] = $publiclet["EXPIRE_TIME"];
if(isSet($publiclet["DOWNLOAD_LIMIT"])) $parameters["downloadlimit"] = $publiclet["DOWNLOAD_LIMIT"];
$link->parseHttpVars($parameters);
$parentRepositoryObject = $publiclet["REPOSITORY"];

$driverInstance = AJXP_PluginsService::findPlugin("access", $parentRepositoryObject->getAccessType());
if(empty($driverInstance)){
print("\n-- ERROR: Cannot find driver instance!");
continue;
}
$options = $driverInstance->makeSharedRepositoryOptions(["file" => "/"], $parentRepositoryObject);
$options["SHARE_ACCESS"] = "private";
$newRepo = $parentRepositoryObject->createSharedChild(
basename($filePath),
$options,
$parentRepositoryObject->getId(),
$userObject->getId(),
null
);
$gPath = $userObject->getGroupPath();
if (!empty($gPath) && !ConfService::getCoreConf("CROSSUSERS_ALLGROUPS", "conf")) {
$newRepo->setGroupPath($gPath);
}
$newRepo->setDescription("");
// Smells like dirty hack!
$newRepo->options["PATH"] = SystemTextEncoding::fromStorageEncoding($newRepo->options["PATH"]);

$newRepo->setContentFilter(new ContentFilter([new AJXP_Node("pydio://".$parentRepositoryObject->getId().$filePath)]));
if(!$dryRun){
ConfService::addRepository($newRepo);
}

$hiddenUserEntry = $shareRightManager->prepareSharedUserEntry(
["simple_right_read" => true, "simple_right_download" => true],
$link, false, null);

$selection = new UserSelection($parentRepositoryObject, []);
$selection->addFile($filePath);
if(!$dryRun){
$shareRightManager->assignSharedRepositoryPermissions(
$parentRepositoryObject,
$newRepo,
false,
[$hiddenUserEntry["ID"] => $hiddenUserEntry], [],
$selection
);
}
$link->setParentRepositoryId($parentRepositoryObject->getId());
$link->attachToRepository($newRepo->getId());
print("\n-- Should save following LINK: ");
print_r($link->getJsonData($shareCenter->getPublicAccessManager(), ConfService::getMessages()));
if(!$dryRun){
$hash = $link->save();
}

// UPDATE METADATA
$meta["ajxp_shared"] = ["shares" => [$element => array("type" => "minisite")]];

}catch(Exception $e){
print("\n-- ERROR: ".$e->getMessage());
}


}
if($dryRun){
rename($legacyLinkFile.".migrated", $legacyLinkFile);
}
continue;
}else{
//
// File does not exists, remove meta
//
unset($meta["ajxp_shared"]);
}


$repo = ConfService::getRepositoryById($element);
if($repo !== null){
print("\n--Shared repository: just metadata");
// Shared repo, migrating the meta should be enough
$meta["ajxp_shared"] = array("shares" => [$element => array("type" => "repository")]);
}
}
}
}
print("\n\n SHOULD NOW UPDATE METADATA WITH FOLLOWING :");
print_r($data);
if(!$dryRun){
file_put_contents($store, serialize($data));
}
}
}

}
7 changes: 6 additions & 1 deletion core/src/plugins/action.share/class.ShareCenter.php
Expand Up @@ -378,6 +378,11 @@ public function preProcessDownload($action, &$httpVars, &$fileVars){
}
}

public function migrateLegacyShares($action, $httpVars, $fileVars){
require_once("class.LegacyPubliclet.php");
LegacyPubliclet::migrateLegacyMeta($this, $this->getShareStore(), $this->getRightsManager(), $httpVars["dry_run"] !== "run");
}

/**
* Main callback for all share- actions.
* @param string $action
Expand Down Expand Up @@ -879,7 +884,7 @@ public function nodeSharedMetadata(&$ajxpNode)
if(!empty($shares)){
$compositeShare = $this->getShareStore()->getMetaManager()->getCompositeShareForNode($ajxpNode);
if(empty($compositeShare) || $compositeShare->isInvalid()){
$this->getShareStore()->getMetaManager()->clearNodeMeta($ajxpNode);
//$this->getShareStore()->getMetaManager()->clearNodeMeta($ajxpNode);
return;
}
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/plugins/action.share/manifest.xml
Expand Up @@ -100,6 +100,12 @@
</serverCallback>
</processing>
</action>
<action name="migrate_legacy_shares">
<rightsContext adminOnly="true" noUser="false" read="true" userLogged="true" write="true"/>
<processing>
<serverCallback methodName="migrateLegacyShares" restParams="/dry_run"/>
</processing>
</action>
<!--
<action name="share-edit-shared">
<gui src="share.png" iconClass="icon-share" text="share_center.125" title="share_center.126">
Expand Down

0 comments on commit acd5cd3

Please sign in to comment.