mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-22 13:07:59 +01:00
#25: Implemented a command to ensure all users have valid refresh tokens and email addresses.
This commit is contained in:
parent
f10da0019f
commit
1e670b673f
6 changed files with 122 additions and 9 deletions
100
app/Console/Commands/SyncPoniverseAccounts.php
Normal file
100
app/Console/Commands/SyncPoniverseAccounts.php
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pony.fm - A community for pony fan music.
|
||||||
|
* Copyright (C) 2016 Peter Deltchev
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Poniverse\Ponyfm\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use League\OAuth2\Client\Token\AccessToken;
|
||||||
|
use Poniverse\Lib\Client;
|
||||||
|
use Poniverse\Ponyfm\Models\User;
|
||||||
|
|
||||||
|
class SyncPoniverseAccounts extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'accounts:sync-with-poniverse';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Client
|
||||||
|
*/
|
||||||
|
protected $poniverse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Ensures each Pony.fm account has a valid refresh token and email address from Poniverse on file.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->poniverse = new Client(
|
||||||
|
config('poniverse.client_id'), config('poniverse.secret'), new \GuzzleHttp\Client());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$usersToUpdate = User::whereLinkedToPoniverse();
|
||||||
|
|
||||||
|
$progress = $this->output->createProgressBar($usersToUpdate->count());
|
||||||
|
$progress->setFormat(
|
||||||
|
'<info>%message%</info>
|
||||||
|
%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%');
|
||||||
|
|
||||||
|
$usersToUpdate
|
||||||
|
->orderBy('id', 'ASC')
|
||||||
|
->chunk(100, function($users) use ($progress) {
|
||||||
|
/** @var User $user */
|
||||||
|
foreach ($users as $user) {
|
||||||
|
$progress->setMessage("Updating user ID {$user->id}...");
|
||||||
|
$progress->advance();
|
||||||
|
|
||||||
|
$this->poniverse->poniverse()->meta()
|
||||||
|
->syncAccount(
|
||||||
|
$user->getAccessToken()->getResourceOwnerId(),
|
||||||
|
function(AccessToken $accessTokenInfo) use ($user) {
|
||||||
|
$user->setAccessToken($accessTokenInfo);
|
||||||
|
},
|
||||||
|
function(string $newEmailAddress) use ($user) {
|
||||||
|
$user->email = $newEmailAddress;
|
||||||
|
$user->save();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$progress->finish();
|
||||||
|
$this->line('');
|
||||||
|
$this->info('All done!');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -44,6 +44,7 @@ class Kernel extends ConsoleKernel
|
||||||
\Poniverse\Ponyfm\Console\Commands\RebuildFilesizes::class,
|
\Poniverse\Ponyfm\Console\Commands\RebuildFilesizes::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\RebuildSearchIndex::class,
|
\Poniverse\Ponyfm\Console\Commands\RebuildSearchIndex::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\MergeAccounts::class,
|
\Poniverse\Ponyfm\Console\Commands\MergeAccounts::class,
|
||||||
|
\Poniverse\Ponyfm\Console\Commands\SyncPoniverseAccounts::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\FixMLPMAImages::class,
|
\Poniverse\Ponyfm\Console\Commands\FixMLPMAImages::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\VersionFiles::class
|
\Poniverse\Ponyfm\Console\Commands\VersionFiles::class
|
||||||
];
|
];
|
||||||
|
|
|
@ -69,7 +69,7 @@ class AuthenticateOAuth
|
||||||
$accessToken = $this->determineAccessToken($request, false);
|
$accessToken = $this->determineAccessToken($request, false);
|
||||||
|
|
||||||
// check that access token is valid at Poniverse.net
|
// check that access token is valid at Poniverse.net
|
||||||
$accessTokenInfo = $this->poniverse->poniverse()->accessTokenInfo()->introspect($accessToken);
|
$accessTokenInfo = $this->poniverse->poniverse()->meta()->introspect($accessToken);
|
||||||
|
|
||||||
if (!$accessTokenInfo->getIsActive()) {
|
if (!$accessTokenInfo->getIsActive()) {
|
||||||
throw new AccessDeniedHttpException('This access token is expired or invalid!');
|
throw new AccessDeniedHttpException('This access token is expired or invalid!');
|
||||||
|
|
|
@ -98,6 +98,7 @@ use Venturecraft\Revisionable\RevisionableTrait;
|
||||||
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\User withEmailSubscriptionFor($activityType)
|
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\User withEmailSubscriptionFor($activityType)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\User wherePoniverseId($poniverseId)
|
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\User wherePoniverseId($poniverseId)
|
||||||
|
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\User whereLinkedToPoniverse()
|
||||||
*/
|
*/
|
||||||
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, \Illuminate\Contracts\Auth\Access\Authorizable, Searchable, Commentable
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, \Illuminate\Contracts\Auth\Access\Authorizable, Searchable, Commentable
|
||||||
{
|
{
|
||||||
|
@ -154,11 +155,22 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
|
||||||
*/
|
*/
|
||||||
public function scopeWherePoniverseId($query, int $poniverseId) {
|
public function scopeWherePoniverseId($query, int $poniverseId) {
|
||||||
return $query
|
return $query
|
||||||
->join('oauth2_tokens', 'users.id', '=', 'oauth2_tokens.user_id')
|
->whereLinkedToPoniverse($query)
|
||||||
->select('users.*', 'oauth2_tokens.external_user_id')
|
|
||||||
->where('oauth2_tokens.external_user_id', '=', $poniverseId);
|
->where('oauth2_tokens.external_user_id', '=', $poniverseId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the list of users to those who have a linked Poniverse account.
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function scopeWhereLinkedToPoniverse($query) {
|
||||||
|
return $query
|
||||||
|
->join('oauth2_tokens', 'users.id', '=', 'oauth2_tokens.user_id')
|
||||||
|
->select('users.*', 'oauth2_tokens.external_user_id');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets this user's OAuth access token record.
|
* Gets this user's OAuth access token record.
|
||||||
*
|
*
|
||||||
|
|
8
composer.lock
generated
8
composer.lock
generated
|
@ -2597,12 +2597,12 @@
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/Poniverse/poniverse-php.git",
|
"url": "https://github.com/Poniverse/poniverse-php.git",
|
||||||
"reference": "851f9ea1495142dcbf0307d48131cedc70d4e44e"
|
"reference": "241c32e32076eaa4c0cf4d2a33cddcda006f2ba8"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/Poniverse/poniverse-php/zipball/851f9ea1495142dcbf0307d48131cedc70d4e44e",
|
"url": "https://api.github.com/repos/Poniverse/poniverse-php/zipball/241c32e32076eaa4c0cf4d2a33cddcda006f2ba8",
|
||||||
"reference": "851f9ea1495142dcbf0307d48131cedc70d4e44e",
|
"reference": "241c32e32076eaa4c0cf4d2a33cddcda006f2ba8",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -2638,7 +2638,7 @@
|
||||||
"laravel",
|
"laravel",
|
||||||
"laravel4"
|
"laravel4"
|
||||||
],
|
],
|
||||||
"time": "2016-12-29 16:04:47"
|
"time": "2016-12-30 16:09:52"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "predis/predis",
|
"name": "predis/predis",
|
||||||
|
|
|
@ -50,7 +50,7 @@ class ApiAuthTest extends TestCase
|
||||||
]));
|
]));
|
||||||
$poniverse->shouldReceive('setAccessToken');
|
$poniverse->shouldReceive('setAccessToken');
|
||||||
|
|
||||||
$accessTokenService = Mockery::mock('overload:Poniverse\Lib\Service\Poniverse\AccessTokenInfo');
|
$accessTokenService = Mockery::mock('overload:Poniverse\Lib\Service\Poniverse\Meta');
|
||||||
$accessTokenService->shouldReceive('introspect')
|
$accessTokenService->shouldReceive('introspect')
|
||||||
->andReturn($accessTokenInfo);
|
->andReturn($accessTokenInfo);
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ class ApiAuthTest extends TestCase
|
||||||
]));
|
]));
|
||||||
$poniverse->shouldReceive('setAccessToken');
|
$poniverse->shouldReceive('setAccessToken');
|
||||||
|
|
||||||
$accessTokenService = Mockery::mock('overload:Poniverse\Lib\Service\Poniverse\AccessTokenInfo');
|
$accessTokenService = Mockery::mock('overload:Poniverse\Lib\Service\Poniverse\Meta');
|
||||||
$accessTokenService
|
$accessTokenService
|
||||||
->shouldReceive('introspect')
|
->shouldReceive('introspect')
|
||||||
->andReturn($accessTokenInfo);
|
->andReturn($accessTokenInfo);
|
||||||
|
|
Loading…
Reference in a new issue