mirror of
https://github.com/flarum/core.git
synced 2025-02-25 19:53:48 +01:00
Allow provision of an avatar URL to upload during sign up
This can be used by authentication extensions (i.e. mirror Facebook/Twitter profile picture). Rough implementation, needs refactoring.
This commit is contained in:
parent
7eab206f91
commit
2c4fae60bc
@ -73,7 +73,7 @@ export default class SignUpModal extends Modal {
|
|||||||
|
|
||||||
<div className="Form Form--centered">
|
<div className="Form Form--centered">
|
||||||
<div className="Form-group">
|
<div className="Form-group">
|
||||||
<input className="FormControl" name="username" placeholder={extractText(app.translator.trans('core.forum.sign_up.username_placeholder'))}
|
<input className="FormControl" name="username" type="text" placeholder={extractText(app.translator.trans('core.forum.sign_up.username_placeholder'))}
|
||||||
value={this.username()}
|
value={this.username()}
|
||||||
onchange={m.withAttr('value', this.username)}
|
onchange={m.withAttr('value', this.username)}
|
||||||
disabled={this.loading} />
|
disabled={this.loading} />
|
||||||
@ -216,6 +216,10 @@ export default class SignUpModal extends Modal {
|
|||||||
data.password = this.password();
|
data.password = this.password();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.props.avatarUrl) {
|
||||||
|
data.avatarUrl = this.props.avatarUrl;
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,16 @@ use Flarum\Core\AuthToken;
|
|||||||
use Flarum\Core\Validator\UserValidator;
|
use Flarum\Core\Validator\UserValidator;
|
||||||
use Flarum\Event\UserWillBeSaved;
|
use Flarum\Event\UserWillBeSaved;
|
||||||
use Flarum\Core\Support\DispatchEventsTrait;
|
use Flarum\Core\Support\DispatchEventsTrait;
|
||||||
|
use Flarum\Foundation\Application;
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Flarum\Core\Exception\PermissionDeniedException;
|
use Flarum\Core\Exception\PermissionDeniedException;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Intervention\Image\ImageManager;
|
||||||
|
use League\Flysystem\Adapter\Local;
|
||||||
|
use League\Flysystem\Filesystem;
|
||||||
|
use League\Flysystem\FilesystemInterface;
|
||||||
|
use League\Flysystem\MountManager;
|
||||||
|
|
||||||
class RegisterUserHandler
|
class RegisterUserHandler
|
||||||
{
|
{
|
||||||
@ -35,16 +42,30 @@ class RegisterUserHandler
|
|||||||
*/
|
*/
|
||||||
protected $validator;
|
protected $validator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Application
|
||||||
|
*/
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var FilesystemInterface
|
||||||
|
*/
|
||||||
|
protected $uploadDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Dispatcher $events
|
* @param Dispatcher $events
|
||||||
* @param SettingsRepositoryInterface $settings
|
* @param SettingsRepositoryInterface $settings
|
||||||
* @param UserValidator $validator
|
* @param UserValidator $validator
|
||||||
|
* @param Application $app
|
||||||
|
* @param FilesystemInterface $uploadDir
|
||||||
*/
|
*/
|
||||||
public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings, UserValidator $validator)
|
public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings, UserValidator $validator, Application $app, FilesystemInterface $uploadDir)
|
||||||
{
|
{
|
||||||
$this->events = $events;
|
$this->events = $events;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->validator = $validator;
|
$this->validator = $validator;
|
||||||
|
$this->app = $app;
|
||||||
|
$this->uploadDir = $uploadDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -98,6 +119,8 @@ class RegisterUserHandler
|
|||||||
|
|
||||||
$this->validator->assertValid(array_merge($user->getAttributes(), compact('password')));
|
$this->validator->assertValid(array_merge($user->getAttributes(), compact('password')));
|
||||||
|
|
||||||
|
$this->saveAvatarFromUrl($user, array_get($data, 'attributes.avatarUrl'));
|
||||||
|
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
if (isset($token)) {
|
if (isset($token)) {
|
||||||
@ -108,4 +131,23 @@ class RegisterUserHandler
|
|||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function saveAvatarFromUrl(User $user, $avatarUrl)
|
||||||
|
{
|
||||||
|
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar');
|
||||||
|
|
||||||
|
$manager = new ImageManager;
|
||||||
|
$manager->make($avatarUrl)->fit(100, 100)->save($tmpFile);
|
||||||
|
|
||||||
|
$mount = new MountManager([
|
||||||
|
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
|
||||||
|
'target' => $this->uploadDir,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$uploadName = Str::lower(Str::quickRandom()) . '.jpg';
|
||||||
|
|
||||||
|
$user->changeAvatarPath($uploadName);
|
||||||
|
|
||||||
|
$mount->move("source://".pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,10 @@ class CoreServiceProvider extends AbstractServiceProvider
|
|||||||
$this->app->when('Flarum\Core\Command\DeleteAvatarHandler')
|
$this->app->when('Flarum\Core\Command\DeleteAvatarHandler')
|
||||||
->needs('League\Flysystem\FilesystemInterface')
|
->needs('League\Flysystem\FilesystemInterface')
|
||||||
->give($avatarsFilesystem);
|
->give($avatarsFilesystem);
|
||||||
|
|
||||||
|
$this->app->when('Flarum\Core\Command\RegisterUserHandler')
|
||||||
|
->needs('League\Flysystem\FilesystemInterface')
|
||||||
|
->give($avatarsFilesystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user