mirror of
https://github.com/flarum/core.git
synced 2025-07-25 18:51:40 +02:00
Implement user "bio" field
Perhaps this should be an extension, but it is pretty essential and I can’t think of many instances where it wouldn’t be wanted. Would be very easy to extract later on if need be.
This commit is contained in:
@@ -12,6 +12,8 @@ class EditUserCommand
|
||||
|
||||
public $password;
|
||||
|
||||
public $bio;
|
||||
|
||||
public $readTime;
|
||||
|
||||
public function __construct($userId, $user)
|
||||
|
@@ -118,6 +118,7 @@ class CoreServiceProvider extends ServiceProvider
|
||||
Model::setValidator($this->app['validator']);
|
||||
|
||||
User::setHasher($this->app['hash']);
|
||||
User::setFormatter($this->app['flarum.formatter']);
|
||||
}
|
||||
|
||||
public function registerPermissions()
|
||||
|
13
src/Core/Events/UserBioWasChanged.php
Normal file
13
src/Core/Events/UserBioWasChanged.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php namespace Flarum\Core\Events;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class UserBioWasChanged
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
@@ -1,6 +1,8 @@
|
||||
<?php namespace Flarum\Core\Handlers\Commands;
|
||||
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface as UserRepository;
|
||||
use Flarum\Core\Events\UserWillBeSaved;
|
||||
use Flarum\Core\Support\DispatchesEvents;
|
||||
|
||||
class EditUserCommandHandler
|
||||
{
|
||||
@@ -29,6 +31,9 @@ class EditUserCommandHandler
|
||||
if (isset($command->password)) {
|
||||
$userToEdit->changePassword($command->password);
|
||||
}
|
||||
if (isset($command->bio)) {
|
||||
$userToEdit->changeBio($command->bio);
|
||||
}
|
||||
if (! empty($command->readTime)) {
|
||||
$userToEdit->markAllAsRead();
|
||||
}
|
||||
|
@@ -2,12 +2,14 @@
|
||||
|
||||
use Illuminate\Contracts\Hashing\Hasher;
|
||||
use Tobscure\Permissible\Permissible;
|
||||
use Flarum\Core\Formatter\FormatterManager;
|
||||
use Flarum\Core\Exceptions\InvalidConfirmationTokenException;
|
||||
use Flarum\Core\Events\UserWasDeleted;
|
||||
use Flarum\Core\Events\UserWasRegistered;
|
||||
use Flarum\Core\Events\UserWasRenamed;
|
||||
use Flarum\Core\Events\UserEmailWasChanged;
|
||||
use Flarum\Core\Events\UserPasswordWasChanged;
|
||||
use Flarum\Core\Events\UserBioWasChanged;
|
||||
use Flarum\Core\Events\UserWasActivated;
|
||||
use Flarum\Core\Events\UserEmailWasConfirmed;
|
||||
|
||||
@@ -15,6 +17,13 @@ class User extends Model
|
||||
{
|
||||
use Permissible;
|
||||
|
||||
/**
|
||||
* The text formatter instance.
|
||||
*
|
||||
* @var \Flarum\Core\Formatter\Formatter
|
||||
*/
|
||||
protected static $formatter;
|
||||
|
||||
/**
|
||||
* The validation rules for this model.
|
||||
*
|
||||
@@ -146,6 +155,37 @@ class User extends Model
|
||||
$this->attributes['password'] = $value ? static::$hasher->make($value) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the user's bio.
|
||||
*
|
||||
* @param string $bio
|
||||
* @return $this
|
||||
*/
|
||||
public function changeBio($bio)
|
||||
{
|
||||
$this->bio = $bio;
|
||||
|
||||
$this->raise(new UserBioWasChanged($this));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content formatter as HTML.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getBioHtmlAttribute($value)
|
||||
{
|
||||
if (! $value) {
|
||||
$this->bio_html = $value = static::formatBio($this->bio);
|
||||
$this->save();
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark all discussions as read by setting the user's read_time.
|
||||
*
|
||||
@@ -332,4 +372,35 @@ class User extends Model
|
||||
{
|
||||
static::$hasher = $hasher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text formatter instance.
|
||||
*
|
||||
* @return \Flarum\Core\Formatter\FormatterManager
|
||||
*/
|
||||
public static function getFormatter()
|
||||
{
|
||||
return static::$formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set text formatter instance.
|
||||
*
|
||||
* @param \Flarum\Core\Formatter\FormatterManager $formatter
|
||||
*/
|
||||
public static function setFormatter(FormatterManager $formatter)
|
||||
{
|
||||
static::$formatter = $formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a string of post content using the set formatter.
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
protected static function formatBio($content)
|
||||
{
|
||||
return static::$formatter->format($content);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user