Skip to content

Commit

Permalink
Merge pull request #44 from corowne/feature/auth-rework
Browse files Browse the repository at this point in the history
Rework alias storage system to accommodate multiple aliases and sites
Requires migrate, migrate-aliases
  • Loading branch information
itinerare committed Nov 29, 2020
2 parents 74ddb5a + dd326d1 commit 0517c23
Show file tree
Hide file tree
Showing 42 changed files with 1,097 additions and 477 deletions.
60 changes: 0 additions & 60 deletions app/Console/Commands/AssignArtCreditsToIds.php

This file was deleted.

239 changes: 239 additions & 0 deletions app/Console/Commands/MigrateAliases.php
@@ -0,0 +1,239 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

use App\Models\User\User;
use App\Models\User\UserAlias;
use App\Models\Character\Character;
use App\Models\Character\CharacterImageCreator;
use App\Models\Character\CharacterLog;
use App\Models\User\UserCharacterLog;
use App\Models\Item\Item;

class MigrateAliases extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate-aliases {--drop-columns : Whether the alias columns should be dropped after moving data from them}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrates alias information associated with users, characters, and character image creators to the new storage system.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('*****************************');
$this->info('* MIGRATE ALIAS INFORMATION *');
$this->info('*****************************'."\n");

$this->line("Migrating aliases...\n");

/** MOVE USER ALIASES */
if(Schema::hasColumn('users', 'alias')) {
// Get users with a set alias
$aliasUsers = User::whereNotNull('alias')->get();

if($aliasUsers->count()) {
foreach($aliasUsers as $user) {
if(!DB::table('user_aliases')->where('user_id', $user->id)->where('site', 'dA')->where('alias', $user->alias)->exists()) {
// Create a new row for the user's current dA alias
DB::table('user_aliases')->insert([
[
'user_id' => $user->id,
'site' => 'dA',
'alias' => $user->alias,
'is_visible' => 1,
'is_primary_alias' => 1,
]
]);

// Clear the user's alias in the users table and set the has_alias bool in its place
$user->update([
'alias' => null,
'has_alias' => 1
]);
}
}
$this->info("Migrated: User aliases");
}
else $this->line("Skipped: User aliases (nothing to migrate)");
}
else $this->line("Skipped: User aliases (column no longer exists)");

/** MOVE CHARACTER OWNER ALIASES */
if(Schema::hasColumn('characters', 'owner_alias')) {
// This and the following section operate on the assumption that all aliases to this point have been dA accounts

// Get characters with an owner identified by alias
$aliasCharacters = Character::whereNotNull('owner_alias')->get();

if($aliasCharacters->count()) {
foreach($aliasCharacters as $character) {
// Just in case, check to update character ownership
$userAlias = UserAlias::where('site', 'dA')->where('alias', $character->owner_alias)->first();
if($userAlias) {
$character->update(['owner_alias' => null, 'user_id' => $userAlias->user_id]);
}
elseif(!$userAlias) {
$alias = $character->owner_alias;
$character->update(['owner_alias' => null, 'owner_url' => 'https://deviantart.com/'.$alias]);
}
}

$this->info("Migrated: Character owner aliases");
}
else $this->line("Skipped: Character owner aliases (nothing to migrate)");
}
else $this->line("Skipped: Character owner aliases (column no longer exists)");

if(Schema::hasColumn('character_image_creators', 'alias')) {
/** MOVE CHARACTER IMAGE CREATOR ALIASES */

// Get character image creators with a set alias
$aliasImageCreators = CharacterImageCreator::whereNotNull('alias')->get();

if($aliasImageCreators->count()){
foreach($aliasImageCreators as $creator) {
$userAlias = UserAlias::where('site', 'dA')->where('alias', $creator->alias)->first();
if($userAlias) {
$creator->update(['alias' => null, 'user_id' => $userAlias->user_id]);
}
elseif(!$userAlias) {
$alias = $creator->alias;
$creator->update(['alias' => null, 'url' => 'https://deviantart.com/'.$alias]);
}
}

$this->info("Migrated: Character image creator aliases");
}
else $this->line("Skipped: Character image creator aliases (nothing to migrate)");
}
else $this->line("Skipped: Character image creator aliases (column no longer exists)");

/** MOVE CHARACTER LOG ALIASES */

if(Schema::hasColumn('character_log', 'recipient_alias')) {
// Get character logs with a set recipient alias
$aliasCharacterLogs = CharacterLog::whereNotNull('recipient_alias')->get();

if($aliasCharacterLogs->count()) {
foreach($aliasCharacterLogs as $characterLog) {
$userAlias = UserAlias::where('site', 'dA')->where('alias', $characterLog->recipient_alias)->first();
if($userAlias) {
$characterLog->update(['recipient_alias' => null, 'recipient_id' => $userAlias->user_id]);
}
elseif(!$userAlias) {
$alias = $characterLog->recipient_alias;
$characterLog->update(['recipient_alias' => null, 'recipient_url' => 'https://deviantart.com/'.$alias]);
}
}

$this->info("Migrated: Character log aliases");
}
else $this->line("Skipped: Character log aliases (nothing to migrate)");
}
else $this->line("Skipped: Character log aliases (column no longer exists)");

if(Schema::hasColumn('user_character_log', 'recipient_alias')) {
// Get character logs with a set recipient alias
$aliasUserCharacterLogs = UserCharacterLog::whereNotNull('recipient_alias')->get();

if($aliasUserCharacterLogs->count()) {
foreach($aliasUserCharacterLogs as $characterLog) {
$userAlias = UserAlias::where('site', 'dA')->where('alias', $characterLog->recipient_alias)->first();
if($userAlias) {
$characterLog->update(['recipient_alias' => null, 'recipient_id' => $userAlias->user_id]);
}
elseif(!$userAlias) {
$alias = $characterLog->recipient_alias;
$characterLog->update(['recipient_alias' => null, 'recipient_url' => 'https://deviantart.com/'.$alias]);
}
}

$this->info("Migrated: User character log aliases");
}
else $this->line("Skipped: User character log aliases (nothing to migrate)");
}
else $this->line("Skipped: User character log aliases (column no longer exists)");

if(Schema::hasColumn('items', 'artist_alias')) {
// Get character logs with a set recipient alias
$aliasItemArtists = Item::whereNotNull('artist_alias')->get();

if($aliasItemArtists->count()) {
foreach($aliasItemArtists as $itemArtist) {
$userAlias = UserAlias::where('site', 'dA')->where('alias', $itemArtist->artist_alias)->first();
if($userAlias) {
$itemArtist->update(['artist_alias' => null, 'artist_id' => $userAlias->user_id]);
}
elseif(!$userAlias) {
$alias = $itemArtist->artist_alias;
$itemArtist->update(['artist_alias' => null, 'artist_url' => 'https://deviantart.com/'.$alias]);
}
}

$this->info("Migrated: Item artist aliases");
}
else $this->line("Skipped: Item artist aliases (nothing to migrate)");
}
else $this->line("Skipped: Item artist aliases (column no longer exists)");

if($this->option('drop-columns')) {
// Drop alias columns from the impacted tables.
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('alias');
});
Schema::table('characters', function (Blueprint $table) {
$table->dropColumn('owner_alias');
});
Schema::table('character_image_creators', function (Blueprint $table) {
$table->dropColumn('alias');
});
Schema::table('character_log', function (Blueprint $table) {
//
$table->dropColumn('sender_alias');
$table->dropColumn('recipient_alias');
});
Schema::table('user_character_log', function (Blueprint $table) {
//
$table->dropColumn('sender_alias');
$table->dropColumn('recipient_alias');
});
Schema::table('items', function (Blueprint $table) {
//
$table->dropColumn('artist_alias');
});
}
else $this->line("Skipped: Dropping alias columns");

$this->line("\nAlias information migrated!");
$this->line("After checking that all data has been moved from them,\nrun again with --drop-columns to drop alias columns if desired.");
}
}
48 changes: 48 additions & 0 deletions app/Helpers/Helpers.php
Expand Up @@ -188,6 +188,35 @@ function randomString($characters)
return $code;
}

/**
* Check that a url is from a site used for authentication,
* and if it belongs to a user.
*
* @param string $url
* @param bool $failOnError
* @return \App\Models\User\User|string
*/
function checkAlias($url, $failOnError = true)
{
$recipient = null;
$matches = [];
// Check to see if url is 1. from a site used for auth
foreach(Config::get('lorekeeper.sites') as $key=>$site) if(isset($site['auth']) && $site['auth']) {
preg_match_all($site['regex'], $url, $matches);
if($matches != []) {$urlSite = $key; break;}
}
if($matches[0] == [] && $failOnError) throw new \Exception('This URL is from an invalid site. Please provide a URL for a user profile from a site used for authentication.');

// and 2. if it contains an alias associated with a user on-site.
if($matches[1] != [] && isset($matches[1][0])) {
$alias = App\Models\User\UserAlias::where('site', $urlSite)->where('alias', $matches[1][0])->first();
if($alias) $recipient = $alias->user;
else $recipient = $url;
}

return $recipient;
}

/**
* Prettifies links to user profiles on various sites in a "user@site" format.
*
Expand All @@ -206,3 +235,22 @@ function prettyProfileLink($url)
if(isset($name) && isset($site) && isset($link)) return '<a href="https://'.$link.'">'.$name.'@'.$site.'</a>';
else return '<a href="'.$url.'">'.$url.'</a>';
}

/**
* Prettifies user profile names for use in various functions.
*
* @param string $url
* @return string
*/
function prettyProfileName($url)
{
$matches = [];
// Check different sites and return site if a match is made, plus username (retreived from the URL)
foreach(Config::get('lorekeeper.sites') as $siteName=>$siteInfo) {
if(preg_match_all($siteInfo['regex'], $url, $matches)) {$site = $siteName; $name = $matches[1][0]; break;}
}

// Return formatted name if possible; failing that, an unformatted url
if(isset($name) && isset($site)) return $name.'@'.$site;
else return $url;
}

0 comments on commit 0517c23

Please sign in to comment.