Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/auth rework #44

Merged
merged 30 commits into from Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e465f71
- Change art/design credit linking function to use regex in config
itinerare Nov 13, 2020
edeaa19
Basic construction of altered auth storage
itinerare Nov 13, 2020
ea0716e
Fix middleware check
itinerare Nov 13, 2020
7c8b7c4
Merge branch 'develop' into feature/auth-rework
itinerare Nov 15, 2020
c3fa52b
Update deviantArt service for new alias framework
itinerare Nov 15, 2020
2d2a9a8
Fix check for existing alias in deviantArt service
itinerare Nov 16, 2020
a38be59
- Add owner url column to owner table
itinerare Nov 17, 2020
e9fd863
- Adjust update characters/art/design credit functions to work with n…
itinerare Nov 17, 2020
bbd6ece
Add drop-columns option to alias migration command
itinerare Nov 17, 2020
8569062
- Update character ownership to use url instead of alias
itinerare Nov 17, 2020
3a1c4d8
Merge branch 'develop' into feature/auth-rework
itinerare Nov 17, 2020
edc8088
- Add checking for if a designer/artist credit url is a user at uploa…
itinerare Nov 19, 2020
ea10005
Fix alias query
itinerare Nov 19, 2020
d62d650
Merge branch 'develop' into feature/auth-rework
itinerare Nov 19, 2020
d8874a4
Tidy up various text around aliases and URLs
itinerare Nov 24, 2020
49efc8b
Remove search by alias from masterlist, MYO, sublist search
itinerare Nov 24, 2020
7ff8ea3
- Change item artist credits to user ID/url
itinerare Nov 25, 2020
11a8024
Merge branch 'develop' into feature/auth-rework
itinerare Nov 25, 2020
a13f996
- Add tooltip to admin character transfer re only authenticatable sit…
itinerare Nov 25, 2020
acbbe7a
Merge remote-tracking branch 'local/feature/auth-rework' into feature…
itinerare Nov 25, 2020
1655488
- Update user alias display in edit user panel to support multiple al…
itinerare Nov 25, 2020
4320f4e
Update user alias clear form display
itinerare Nov 25, 2020
5bcb78a
Fix MYO slots not being admin transferrable to offsite users
itinerare Nov 25, 2020
b6e8348
Adjust submission tooltip to account for URL field nullability
itinerare Nov 26, 2020
bdfa2ad
Finish making submission URL nullable
itinerare Nov 26, 2020
42faf1f
Merge remote-tracking branch 'local/feature/auth-rework' into feature…
itinerare Nov 26, 2020
3f9f8d9
Merge branch 'develop' into feature/auth-rework
itinerare Nov 26, 2020
20e5880
Merge branch 'develop' into feature/auth-rework
itinerare Nov 28, 2020
024d71a
Merge branch 'develop' into feature/auth-rework
itinerare Nov 28, 2020
dd326d1
- Label non-visible user aliases in user edit admin panel and update …
itinerare Nov 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}