mirror of
https://github.com/nextcloud/server.git
synced 2026-02-20 00:12:30 -05:00
Create new user, create new data dir, copy files, import app data
This commit is contained in:
parent
247b25e7a9
commit
bc085c3ff4
5 changed files with 59 additions and 46 deletions
|
|
@ -144,7 +144,6 @@ function unlinkRecursive($dir, $deleteRootToo)
|
|||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
function copy_r( $path, $dest )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -32,18 +32,17 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{
|
|||
}
|
||||
|
||||
// Import function for bookmarks
|
||||
function import( $info ){
|
||||
|
||||
switch( $info->appversion ){
|
||||
function import( $app, $info ){
|
||||
switch( $app->version ){
|
||||
default:
|
||||
// All versions of the app have had the same db structure, so all can use the same import function
|
||||
$query = OC_Migrate::prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" );
|
||||
$results = $query->execute( array( $info->olduid ) );
|
||||
$results = $query->execute( array( $info['olduid'] ) );
|
||||
$idmap = array();
|
||||
while( $row = $data->fetchRow() ){
|
||||
// Import each bookmark, saving its id into the map
|
||||
$query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" );
|
||||
$query->execute( array( $row['url'], $row['title'], $info->newuid, $row['public'], $row['added'], $row['lastmodified'] ) );
|
||||
$query->execute( array( $row['url'], $row['title'], $info['newuid'], $row['public'], $row['added'], $row['lastmodified'] ) );
|
||||
// Map the id
|
||||
$idmap[$row['id']] = OC_DB::insertid();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,14 +62,15 @@ if (isset($_POST['user_import'])) {
|
|||
// Get the user
|
||||
if( count($files) != 1 ){
|
||||
OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR);
|
||||
die('invalid import');
|
||||
die('invalid import, no user included');
|
||||
}
|
||||
|
||||
$user = reset($files);
|
||||
$olduser = reset($files);
|
||||
|
||||
// Check for dbexport.xml and export info and data dir
|
||||
$files = scandir( $importdir . '/' . $user );
|
||||
$required = array( 'migration.db', 'exportinfo.json', 'files');
|
||||
$files = scandir( $importdir . '/' . $olduser );
|
||||
|
||||
$required = array( 'migration.db', 'export_info.json', 'files');
|
||||
foreach($required as $require){
|
||||
if( !in_array( $require, $files) ){
|
||||
OC_Log::write('migration', 'Invlaid import file', OC_Log::ERROR);
|
||||
|
|
@ -77,31 +78,37 @@ if (isset($_POST['user_import'])) {
|
|||
}
|
||||
}
|
||||
|
||||
$migrateinfo = $importdir . '/' . $user . '/exportinfo.json';
|
||||
$migrateinfo = $importdir . '/' . $olduser . '/export_info.json';
|
||||
$migrateinfo = json_decode( file_get_contents( $migrateinfo ) );
|
||||
$olduid = $migrateinfo->migrateinfo->uid;
|
||||
|
||||
// Check if uid is available
|
||||
if( OC_User::UserExists( $olduid ) ){
|
||||
if( OC_User::UserExists( $olduser ) ){
|
||||
OC_Log::write('migration','Username exists', OC_Log::ERROR);
|
||||
die('user exists');
|
||||
}
|
||||
|
||||
// Create the user
|
||||
if( !OC_Migrate::createUser( $olduid, $migrateinfo->migrateinfo->hash ) ){
|
||||
if( !OC_Migrate::createUser( $olduser, $migrateinfo->hash ) ){
|
||||
OC_Log::write('migration', 'Failed to create the new user', OC_Log::ERROR);
|
||||
die('coundlt create new user');
|
||||
}
|
||||
|
||||
$datadir = OC_Config::getValue( 'datadirectory' );
|
||||
// Make the new users data dir
|
||||
$path = $datadir . '/' . $olduser . '/files/';
|
||||
if( !mkdir( $path, 0755, true ) ){
|
||||
OC_Log::write('migration','Failed to create users data dir: '.$path, OC_Log::ERROR);
|
||||
die('failed to create users data dir');
|
||||
}
|
||||
|
||||
// Copy data
|
||||
if( !copy_r( $importdir . '/files', $datadir . '/' ) ){
|
||||
if( !copy_r( $importdir . '/' . $olduser . '/files', $datadir . '/' . $olduser . '/files' ) ){
|
||||
OC_Log::write('migration','Failed to copy user files to destination', OC_Log::ERROR);
|
||||
die('failed to copy user files');
|
||||
}
|
||||
|
||||
// Import user data
|
||||
if( !OC_Migrate::importUser( $importdir . '/migration.db', $migrateinfo ) ){
|
||||
if( !OC_Migrate::importAppData( $importdir . '/' . $olduser . '/migration.db', $migrateinfo ) ){
|
||||
OC_Log::write('migration','Failed to import user data', OC_Log::ERROR);
|
||||
die('failed to import user data');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,21 @@ class OC_Migrate{
|
|||
self::$providers[]=$provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* @breif finds and loads the providers
|
||||
*/
|
||||
static private function findProviders(){
|
||||
// Find the providers
|
||||
$apps = OC_App::getAllApps();
|
||||
|
||||
foreach($apps as $app){
|
||||
$path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php';
|
||||
if( file_exists( $path ) ){
|
||||
include( $path );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @breif creates a migration.db in the users data dir with their app data in
|
||||
* @return bool whether operation was successfull
|
||||
|
|
@ -64,14 +79,7 @@ class OC_Migrate{
|
|||
$return = array();
|
||||
|
||||
// Find the providers
|
||||
$apps = OC_App::getAllApps();
|
||||
|
||||
foreach($apps as $app){
|
||||
$path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php';
|
||||
if( file_exists( $path ) ){
|
||||
include( $path );
|
||||
}
|
||||
}
|
||||
self::findProviders();
|
||||
|
||||
// Foreach provider
|
||||
foreach( self::$providers as $provider ){
|
||||
|
|
@ -217,7 +225,8 @@ class OC_Migrate{
|
|||
OC_Log::write( 'migration', 'Failed to get the users password hash', OC_log::ERROR);
|
||||
return false;
|
||||
}
|
||||
$info['hash'] = $hash;
|
||||
$info['hash'] = $hash;
|
||||
$info['exporteduser'] = self::$uid;
|
||||
}
|
||||
// Merge in other data
|
||||
$info = array_merge( $info, $array );
|
||||
|
|
@ -393,17 +402,15 @@ class OC_Migrate{
|
|||
* @param $uid optional uid to use
|
||||
* @return bool if the import succedded
|
||||
*/
|
||||
public static function importAppData( $db, $info, $uid=false ){
|
||||
public static function importAppData( $db, $info, $uid=null ){
|
||||
|
||||
if(!self::$uid){
|
||||
OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL);
|
||||
return false;
|
||||
}
|
||||
self::$uid = !is_null( $uid ) ? $uid : $info->exporteduser;
|
||||
|
||||
// Check if the db exists
|
||||
if( file_exists( $db ) ){
|
||||
// Connect to the db
|
||||
if(!self::connectDB( $db )){
|
||||
OC_Log::write('migration','Failed to connect to migration.db',OC_Log::ERROR);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -411,25 +418,25 @@ class OC_Migrate{
|
|||
return false;
|
||||
}
|
||||
|
||||
if( !is_array( $info ) ){
|
||||
OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the user id
|
||||
self::$uid = $info->migrateinfo->uid;
|
||||
|
||||
$apps = $info->app;
|
||||
|
||||
// Find providers
|
||||
self::findProviders();
|
||||
|
||||
// Generate importinfo array
|
||||
$importinfo = array(
|
||||
'olduid' => $info->exporteduser,
|
||||
'newuid' => self::$uid
|
||||
);
|
||||
|
||||
foreach( self::$providers as $provider){
|
||||
// Is the app in the export?
|
||||
if( array_key_exists( $provider->id, $apps ) ){
|
||||
$id = $provider->id;
|
||||
if( isset( $info->apps->$id ) ){
|
||||
// Did it succeed?
|
||||
if( $app[$provider->id] ){
|
||||
if( $info->apps->$id->success ){
|
||||
// Then do the import
|
||||
$provider->import( $info );
|
||||
$provider->import( $info->apps->$id, $importinfo );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -691,7 +698,7 @@ class OC_Migrate{
|
|||
|
||||
// Create the user
|
||||
$query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" );
|
||||
$result = $query->execute( array( $uid, $data['hash']));
|
||||
$result = $query->execute( array( $uid, $hash));
|
||||
if( !$result ){
|
||||
OC_Log::write('migration', 'Failed to create the new user "'.$uid."");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,8 +20,9 @@ abstract class OC_Migrate_Provider{
|
|||
|
||||
/**
|
||||
* @breif imports data for the app
|
||||
* @param $appinfo object with the data that the app exported
|
||||
* @param $info array of info including exportinfo.json
|
||||
* @return void
|
||||
*/
|
||||
abstract function import( $info );
|
||||
abstract function import( $appinfo, $info );
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue