Skip to content

Commit

Permalink
User avatar: Use Baraja CDN for get all avatars.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Barášek committed May 13, 2021
1 parent d686cc5 commit ee4b087
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/Api/UserEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function actionDefault(
}
}

$users = $selection->select('PARTIAL user.{id, firstName, lastName, password, emails, phone, roles, active, avatarUrl, registerDate, otpCode}')
$users = $selection->select('PARTIAL user.{id, firstName, lastName, password, email, phone, roles, active, registerDate, otpCode}')
->setMaxResults($limit)
->setFirstResult(($page - 1) * $limit)
->addOrderBy('user.active', 'DESC')
Expand All @@ -131,10 +131,10 @@ public function actionDefault(

return Strings::firstUpper((string) $return) ?: null;
})(),
'email' => $user['emails'][0] ?? null,
'email' => $user['email'],
'roles' => $user['roles'],
'phone' => $user['phone'],
'avatarUrl' => $user['avatarUrl'],
'avatarUrl' => 'https://cdn.baraja.cz/avatar/' . md5($user['email']) . '.png',
'registerDate' => $user['registerDate'],
'options' => [
'active' => $user['active'],
Expand Down Expand Up @@ -306,7 +306,7 @@ public function actionOverview(int $id): void
})($user->getPhone()),
'avatarUrl' => $user->getAvatarUrl(),
],
'iconUrl' => $user->getAvatarUrl() ?? 'https://www.gravatar.com/avatar/' . md5($user->getEmail()) . '?s=256',
'avatarUrl' => $user->getAvatarUrl(),
'created' => $user->getCreateDate(),
'meta' => (static function (array $data): array {
$return = [];
Expand Down Expand Up @@ -741,6 +741,7 @@ public function actionSaveMeta(int $id, string $key, ?string $value = null): voi

public function actionSavePhoto(int $id, string $url): void
{
// TODO: Refactor this to file upload to Baraja CDN
if (Validators::isUrl($url) === false) {
$this->sendError('URL must be absolute valid URL.');
}
Expand All @@ -764,7 +765,6 @@ public function actionSavePhoto(int $id, string $url): void
return;
}

$user->setAvatarUrl($url);
$this->entityManager->flush();
$this->flashMessage('User photo has been changed.', 'success');
$this->sendOk();
Expand Down
4 changes: 1 addition & 3 deletions src/User/Entity/CmsUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ public function isActive(): bool;

public function setActive(bool $active): void;

public function getAvatarUrl(): ?string;

public function setAvatarUrl(?string $avatarUrl): void;
public function getAvatarUrl(): string;

public function getName(bool $reverse = false): string;

Expand Down
17 changes: 2 additions & 15 deletions src/User/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ class User implements CmsUser
*/
private $metas;

/** @ORM\Column(type="string", nullable=true) */
private ?string $avatarUrl;

/**
* @var UserLogin[]|Collection
* @ORM\OneToMany(targetEntity="UserLogin", mappedBy="user")
Expand Down Expand Up @@ -503,19 +500,9 @@ public function setActive(bool $active): void
}


public function getAvatarUrl(): ?string
public function getAvatarUrl(): string
{
return $this->avatarUrl;
}


public function setAvatarUrl(?string $avatarUrl): void
{
if ($avatarUrl !== null && Validators::isUrl($avatarUrl) === false) {
throw new \InvalidArgumentException('Avatar URL "' . $avatarUrl . '" must be valid absolute URL.');
}

$this->avatarUrl = $avatarUrl;
return 'https://cdn.baraja.cz/avatar/' . md5($this->getEmail()) . '.png';
}


Expand Down
8 changes: 3 additions & 5 deletions template/user/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ Vue.component('user-default', {
</tr>
<tr v-for="(item, offset) in user.items">
<td>
<template v-if="item.avatarUrl">
<a :href="link('User:detail', {id: item.id})">
<img v-if="item.avatarUrl" :src="item.avatarUrl" :alt="item.name" style="max-height:32px">
</a>
</template>
<a :href="link('User:detail', {id: item.id})">
<img v-if="item.avatarUrl" :src="item.avatarUrl" :alt="item.name" style="max-height:32px">
</a>
<a :href="link('User:detail', {id: item.id})">{{ item.name }}</a>
<div v-if="item.options['2fa']" class="badge badge-pill badge-primary" v-b-tooltip title="This user is using 2-step login authentication (better security).">2FA</div>
<div v-if="item.id === currentUserId" class="badge badge-pill badge-primary" v-b-tooltip title="This is your account.">You</div>
Expand Down
8 changes: 4 additions & 4 deletions template/user/overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Vue.component('user-overview', {
<b-row>
<b-col cols="1">
<div>
<img :src="iconUrl" :alt="'User ' + form.username" class="w-100">
<img :src="avatarUrl" :alt="'User ' + form.username" class="w-100">
</div>
<div class="mt-3">
<b-button variant="secondary" class="btn btn-sm py-0" v-b-modal.modal-change-photo>Change photo</b-button>
Expand Down Expand Up @@ -114,7 +114,7 @@ Vue.component('user-overview', {
<b-row>
<b-col cols="3">
<p class="text-secondary">Preview:</p>
<img :src="editPhoto.url ? editPhoto.url : iconUrl" alt="Photo" class="w-100">
<img :src="editPhoto.url ? editPhoto.url : avatarUrl" alt="Photo" class="w-100">
</b-col>
<b-col>
<p class="text-secondary">A&nbsp;photo helps personalize your account.</p>
Expand Down Expand Up @@ -176,7 +176,7 @@ Vue.component('user-overview', {
phone: ''
},
created: '',
iconUrl: null,
avatarUrl: null,
form: {
fullName: null,
email: null,
Expand Down Expand Up @@ -210,7 +210,7 @@ Vue.component('user-overview', {
axiosApi.get(`user/overview?id=${this.id}`).then(req => {
this.form = req.data.form;
this.created = req.data.created;
this.iconUrl = req.data.iconUrl;
this.avatarUrl = req.data.avatarUrl;
this.meta = req.data.meta;
this.loading.init = false;
this.loading.meta = false;
Expand Down

0 comments on commit ee4b087

Please sign in to comment.