Migration complete
This commit is contained in:
86
Helper/Migration.php
Normal file
86
Helper/Migration.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace RomhackPlaza\Master\Helper;
|
||||
|
||||
use XF\Container;
|
||||
use XF\Entity\User;
|
||||
use XF\Entity\UserAuth;
|
||||
use XF\Service\User\AvatarService;
|
||||
use XF\Service\User\ProfileBannerService;
|
||||
|
||||
class Migration
|
||||
{
|
||||
public static function oldXfDb(): ?\XF\Db\Mysqli\Adapter {
|
||||
|
||||
$container = \XF::app()->container();
|
||||
if( !isset( $container['oldXfDb'] ) ){
|
||||
$container['oldXfDb'] = function(Container $c){
|
||||
$config = \XF::config('oldxfdb');
|
||||
if( !$config )
|
||||
return null;
|
||||
|
||||
return new \XF\Db\Mysqli\Adapter($config, true);
|
||||
};
|
||||
}
|
||||
|
||||
return $container['oldXfDb'];
|
||||
}
|
||||
|
||||
public static function uniqueUsername(string $username): string
|
||||
{
|
||||
$u = $username;
|
||||
$i = 1;
|
||||
while(\XF::em()->findOne('XF:User', ['username' => $u])){
|
||||
$u = $username . '_' . $i++;
|
||||
}
|
||||
|
||||
return $u;
|
||||
}
|
||||
|
||||
public static function copyUserPassword(int $xf_user_id, User $user): bool
|
||||
{
|
||||
$oldDb = self::oldXfDb();
|
||||
if( !$oldDb ){
|
||||
return false;
|
||||
}
|
||||
|
||||
$row = $oldDb->fetchRow(
|
||||
'SELECT scheme_class, data FROM xf_user_authenticate WHERE user_id = ?',
|
||||
[$xf_user_id]
|
||||
);
|
||||
if( !$row ){
|
||||
return false;
|
||||
}
|
||||
|
||||
$user->Auth->scheme_class = $row['scheme_class'];
|
||||
$user->Auth->data = $row['data'];
|
||||
return $user->Auth->save();
|
||||
}
|
||||
|
||||
public static function setAvatarFromPath(string $avatar_path, User $user): bool
|
||||
{
|
||||
if(!is_readable($avatar_path))
|
||||
return false;
|
||||
|
||||
$avatarService = \XF::service(AvatarService::class, $user);
|
||||
|
||||
if (!$avatarService->setImage($avatar_path))
|
||||
return false;
|
||||
|
||||
return $avatarService->updateAvatar();
|
||||
}
|
||||
|
||||
public static function setBannerFromPath(mixed $banner_path, User $user)
|
||||
{
|
||||
if( !is_readable($banner_path) )
|
||||
return false;
|
||||
|
||||
$bannerService = \XF::service(ProfileBannerService::class, $user);
|
||||
if( !$bannerService->setImage($banner_path) )
|
||||
return false;
|
||||
|
||||
return $bannerService->updateBanner();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user