* * 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 += [ '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; } }