framework/src/Api/Serializer/UserSerializer.php
2017-10-03 18:47:23 +02:00

67 lines
1.6 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Serializer;
use Flarum\User\Gate;
class UserSerializer extends BasicUserSerializer
{
/**
* @var \Flarum\User\Gate
*/
protected $gate;
/**
* @param Gate $gate
*/
public function __construct(Gate $gate)
{
$this->gate = $gate;
}
/**
* {@inheritdoc}
*/
protected function getDefaultAttributes($user)
{
$attributes = parent::getDefaultAttributes($user);
$gate = $this->gate->forUser($this->actor);
$canEdit = $gate->allows('edit', $user);
$attributes += [
'bio' => $user->bio,
'joinTime' => $this->formatDate($user->join_time),
'discussionsCount' => (int) $user->discussions_count,
'commentsCount' => (int) $user->comments_count,
'canEdit' => $canEdit,
'canDelete' => $gate->allows('delete', $user),
];
if ($user->getPreference('discloseOnline')) {
$attributes += [
'lastSeenTime' => $this->formatDate($user->last_seen_time)
];
}
if ($canEdit || $this->actor->id === $user->id) {
$attributes += [
'isActivated' => (bool) $user->is_activated,
'email' => $user->email
];
}
return $attributes;
}
}