mirror of
https://github.com/flarum/framework.git
synced 2025-02-01 00:58:46 +08: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:
parent
6ffba13205
commit
4804d95b37
|
@ -21,6 +21,8 @@ class CreateUsersTable extends Migration {
|
|||
$table->string('confirmation_token')->nullable();
|
||||
$table->boolean('is_activated')->default(0);
|
||||
$table->string('password');
|
||||
$table->text('bio')->nullable();
|
||||
$table->text('bio_html')->nullable();
|
||||
$table->dateTime('join_time')->nullable();
|
||||
$table->dateTime('last_seen_time')->nullable();
|
||||
$table->dateTime('read_time')->nullable();
|
||||
|
|
|
@ -22,7 +22,7 @@ class UpdateAction extends BaseAction
|
|||
// second one failed, the first one would still have succeeded.)
|
||||
$command = new EditUserCommand($userId, $this->actor->getUser());
|
||||
$this->hydrate($command, $params->get('users'));
|
||||
$this->dispatch($command);
|
||||
$user = $this->dispatch($command, $params);
|
||||
|
||||
// Presumably, the user was updated successfully. (The command handler
|
||||
// would have thrown an exception if not.) We set this user as our
|
||||
|
|
|
@ -30,6 +30,7 @@ class UserSerializer extends UserBasicSerializer
|
|||
$canEdit = $user->can($actorUser, 'edit');
|
||||
|
||||
$attributes += [
|
||||
'bioHtml' => $user->bioHtml,
|
||||
'joinTime' => $user->join_time ? $user->join_time->toRFC3339String() : null,
|
||||
'lastSeenTime' => $user->last_seen_time ? $user->last_seen_time->toRFC3339String() : null,
|
||||
'discussionsCount' => (int) $user->discussions_count,
|
||||
|
@ -40,6 +41,7 @@ class UserSerializer extends UserBasicSerializer
|
|||
|
||||
if ($canEdit) {
|
||||
$attributes += [
|
||||
'bio' => $user->bio,
|
||||
'isActivated' => $user->is_activated,
|
||||
'email' => $user->email,
|
||||
'isConfirmed' => $user->is_confirmed
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user