From 855a6b1dfea17ffa3d692a992453db91f2bb8c03 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 19 Sep 2017 19:22:58 +0930 Subject: [PATCH 1/2] Introduce user display names It is not uncommon for forums to be intergrated with sites where users don't have a unique "handle" - they might just have their first name, or a full name, which is not guaranteed to be unique. This commit introduces the concept of "display names" for users. By default display names are the same as usernames, but extensions may override this and set them to something different. The important thing is that all code should use `display_name` whenever intending to output a human-readable name - `username` is reserved for cases where you want to output a unique identifier (which may or may not be human-friendly). The new "GetDisplayName" API is probably sub-optimal, but I didn't worry too much because we can come up with something better in `next-back`. ref #557 --- js/forum/dist/app.js | 7 +++-- js/forum/src/components/UsersSearchSource.js | 2 +- js/lib/helpers/username.js | 2 +- js/lib/models/User.js | 1 + src/Api/Serializer/UserBasicSerializer.php | 5 ++-- .../Command/RequestPasswordResetHandler.php | 2 +- src/Core/Listener/EmailConfirmationMailer.php | 2 +- src/Core/User.php | 11 +++++++ src/Event/GetDisplayName.php | 30 +++++++++++++++++++ 9 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/Event/GetDisplayName.php diff --git a/js/forum/dist/app.js b/js/forum/dist/app.js index 16374dc0bd..fffd65333b 100644 --- a/js/forum/dist/app.js +++ b/js/forum/dist/app.js @@ -28942,7 +28942,9 @@ System.register('flarum/components/UsersSearchSource', ['flarum/helpers/highligh query = query.toLowerCase(); var results = app.store.all('users').filter(function (user) { - return user.username().toLowerCase().substr(0, query.length) === query; + return [user.username(), user.displayName()].some(function (value) { + return value.toLowerCase().substr(0, query.length) === query; + }); }); if (!results.length) return ''; @@ -29534,7 +29536,7 @@ System.register("flarum/helpers/username", [], function (_export, _context) { "use strict"; function username(user) { - var name = user && user.username() || app.translator.trans('core.lib.username.deleted_text'); + var name = user && user.displayName() || app.translator.trans('core.lib.username.deleted_text'); return m( "span", @@ -30533,6 +30535,7 @@ System.register('flarum/models/User', ['flarum/Model', 'flarum/utils/stringToCol babelHelpers.extends(User.prototype, { username: Model.attribute('username'), + displayName: Model.attribute('displayName'), email: Model.attribute('email'), isActivated: Model.attribute('isActivated'), password: Model.attribute('password'), diff --git a/js/forum/src/components/UsersSearchSource.js b/js/forum/src/components/UsersSearchSource.js index f05fbb30d3..7f687b811d 100644 --- a/js/forum/src/components/UsersSearchSource.js +++ b/js/forum/src/components/UsersSearchSource.js @@ -20,7 +20,7 @@ export default class UsersSearchResults { query = query.toLowerCase(); const results = app.store.all('users') - .filter(user => user.username().toLowerCase().substr(0, query.length) === query); + .filter(user => [user.username(), user.displayName()].some(value => value.toLowerCase().substr(0, query.length) === query)); if (!results.length) return ''; diff --git a/js/lib/helpers/username.js b/js/lib/helpers/username.js index 16e12654fc..7e05ca4d6d 100644 --- a/js/lib/helpers/username.js +++ b/js/lib/helpers/username.js @@ -6,7 +6,7 @@ * @return {Object} */ export default function username(user) { - const name = (user && user.username()) || app.translator.trans('core.lib.username.deleted_text'); + const name = (user && user.displayName()) || app.translator.trans('core.lib.username.deleted_text'); return {name}; } diff --git a/js/lib/models/User.js b/js/lib/models/User.js index a28173c3b4..07d5aaf1b2 100644 --- a/js/lib/models/User.js +++ b/js/lib/models/User.js @@ -10,6 +10,7 @@ export default class User extends Model {} Object.assign(User.prototype, { username: Model.attribute('username'), + displayName: Model.attribute('displayName'), email: Model.attribute('email'), isActivated: Model.attribute('isActivated'), password: Model.attribute('password'), diff --git a/src/Api/Serializer/UserBasicSerializer.php b/src/Api/Serializer/UserBasicSerializer.php index 2699c87147..f5919ebf5b 100644 --- a/src/Api/Serializer/UserBasicSerializer.php +++ b/src/Api/Serializer/UserBasicSerializer.php @@ -36,8 +36,9 @@ protected function getDefaultAttributes($user) } return [ - 'username' => $user->username, - 'avatarUrl' => $user->avatar_url + 'username' => $user->username, + 'displayName' => $user->display_name, + 'avatarUrl' => $user->avatar_url ]; } diff --git a/src/Core/Command/RequestPasswordResetHandler.php b/src/Core/Command/RequestPasswordResetHandler.php index fb484fbda2..3318c98824 100644 --- a/src/Core/Command/RequestPasswordResetHandler.php +++ b/src/Core/Command/RequestPasswordResetHandler.php @@ -107,7 +107,7 @@ public function handle(RequestPasswordReset $command) $token->save(); $data = [ - '{username}' => $user->username, + '{username}' => $user->display_name, '{url}' => $this->url->toRoute('resetPassword', ['token' => $token->id]), '{forum}' => $this->settings->get('forum_title'), ]; diff --git a/src/Core/Listener/EmailConfirmationMailer.php b/src/Core/Listener/EmailConfirmationMailer.php index 295d45d7ce..6c7583cad5 100755 --- a/src/Core/Listener/EmailConfirmationMailer.php +++ b/src/Core/Listener/EmailConfirmationMailer.php @@ -130,7 +130,7 @@ protected function getEmailData(User $user, $email) $token = $this->generateToken($user, $email); return [ - '{username}' => $user->username, + '{username}' => $user->display_name, '{url}' => $this->url->toRoute('confirmEmail', ['token' => $token->id]), '{forum}' => $this->settings->get('forum_title') ]; diff --git a/src/Core/User.php b/src/Core/User.php index fa4568df72..08dc459813 100755 --- a/src/Core/User.php +++ b/src/Core/User.php @@ -18,6 +18,7 @@ use Flarum\Database\AbstractModel; use Flarum\Event\CheckUserPassword; use Flarum\Event\ConfigureUserPreferences; +use Flarum\Event\GetDisplayName; use Flarum\Event\PostWasDeleted; use Flarum\Event\PrepareUserGroups; use Flarum\Event\UserAvatarWasChanged; @@ -332,6 +333,16 @@ public function getAvatarUrlAttribute() } } + /** + * Get the user's display name. + * + * @return string + */ + public function getDisplayNameAttribute() + { + return static::$dispatcher->until(new GetDisplayName($this)) ?: $this->username; + } + /** * Get the user's locale, falling back to the forum's default if they * haven't set one. diff --git a/src/Event/GetDisplayName.php b/src/Event/GetDisplayName.php new file mode 100644 index 0000000000..71fbe6ba4c --- /dev/null +++ b/src/Event/GetDisplayName.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Event; + +use Flarum\Core\User; + +class GetDisplayName +{ + /** + * @var User + */ + public $user; + + /** + * @param User $user + */ + public function __construct(User $user) + { + $this->user = $user; + } +} From 03916f4c574f581a0601831eafcd99fd7ecf20be Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 20 Sep 2017 07:11:37 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI [ci skip] [skip ci] --- src/Api/Serializer/PostBasicSerializer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Api/Serializer/PostBasicSerializer.php b/src/Api/Serializer/PostBasicSerializer.php index c5ef111686..3a79eed848 100644 --- a/src/Api/Serializer/PostBasicSerializer.php +++ b/src/Api/Serializer/PostBasicSerializer.php @@ -12,7 +12,6 @@ namespace Flarum\Api\Serializer; use Flarum\Core\Post; -use Flarum\Core\Post\CommentPost; use InvalidArgumentException; class PostBasicSerializer extends AbstractSerializer