From 82a1aa348f2ac1622859cbd5665bd5862a1eff6b Mon Sep 17 00:00:00 2001 From: geisi Date: Fri, 7 Jan 2022 19:25:14 +0100 Subject: [PATCH 1/4] anonymize defaultPhotoUrl() calls --- src/HasProfilePhoto.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/HasProfilePhoto.php b/src/HasProfilePhoto.php index d5bb4ced8..8953e93c7 100644 --- a/src/HasProfilePhoto.php +++ b/src/HasProfilePhoto.php @@ -4,6 +4,7 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; use Laravel\Jetstream\Features; trait HasProfilePhoto @@ -66,7 +67,15 @@ public function getProfilePhotoUrlAttribute() */ protected function defaultProfilePhotoUrl() { - return 'https://ui-avatars.com/api/?name='.urlencode($this->name).'&color=7F9CF5&background=EBF4FF'; + $anonymizedName = ""; + + foreach (explode(" ", $this->name) as $word) { + $anonymizedName .= $word[0].' '; + } + + $anonymizedName = Str::replaceLast(' ', '', $anonymizedName); + + return 'https://ui-avatars.com/api/?name='.urlencode($anonymizedName).'&color=7F9CF5&background=EBF4FF'; } /** From 8e4d3b1b0e43a5d0cfe8912bd0bd72f2678d3c8a Mon Sep 17 00:00:00 2001 From: geisi Date: Fri, 7 Jan 2022 19:41:55 +0100 Subject: [PATCH 2/4] refactor to Str macro --- src/HasProfilePhoto.php | 10 +--------- src/JetstreamServiceProvider.php | 11 +++++++++++ tests/StrMacroTest.php | 12 ++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 tests/StrMacroTest.php diff --git a/src/HasProfilePhoto.php b/src/HasProfilePhoto.php index 8953e93c7..74b44b928 100644 --- a/src/HasProfilePhoto.php +++ b/src/HasProfilePhoto.php @@ -67,15 +67,7 @@ public function getProfilePhotoUrlAttribute() */ protected function defaultProfilePhotoUrl() { - $anonymizedName = ""; - - foreach (explode(" ", $this->name) as $word) { - $anonymizedName .= $word[0].' '; - } - - $anonymizedName = Str::replaceLast(' ', '', $anonymizedName); - - return 'https://ui-avatars.com/api/?name='.urlencode($anonymizedName).'&color=7F9CF5&background=EBF4FF'; + return 'https://ui-avatars.com/api/?name='.urlencode(Str::anonymizeName($this->name)).'&color=7F9CF5&background=EBF4FF'; } /** diff --git a/src/JetstreamServiceProvider.php b/src/JetstreamServiceProvider.php index 141ac8617..8fb36b577 100644 --- a/src/JetstreamServiceProvider.php +++ b/src/JetstreamServiceProvider.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Str; use Illuminate\View\Compilers\BladeCompiler; use Inertia\Inertia; use Laravel\Fortify\Fortify; @@ -90,6 +91,16 @@ public function boot() ]); }); + Str::macro('anonymizeName',function ($name){ + $anonymizedName = ""; + + foreach (explode(" ", $name) as $word) { + $anonymizedName .= $word[0].' '; + } + + return Str::replaceLast(' ', '', $anonymizedName); + }); + if (config('jetstream.stack') === 'inertia') { $this->bootInertia(); } diff --git a/tests/StrMacroTest.php b/tests/StrMacroTest.php new file mode 100644 index 000000000..cc4bc8fa9 --- /dev/null +++ b/tests/StrMacroTest.php @@ -0,0 +1,12 @@ +assertTrue(Str::anonymizeName('Taylor Otwell') === 'T O'); + } +} \ No newline at end of file From fabfc5188eed9824db83155c44b9a5f475cff7a1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 7 Jan 2022 13:35:13 -0600 Subject: [PATCH 3/4] simplify --- src/HasProfilePhoto.php | 6 +++++- src/JetstreamServiceProvider.php | 10 ---------- tests/StrMacroTest.php | 12 ------------ 3 files changed, 5 insertions(+), 23 deletions(-) delete mode 100644 tests/StrMacroTest.php diff --git a/src/HasProfilePhoto.php b/src/HasProfilePhoto.php index 74b44b928..9295e4aa1 100644 --- a/src/HasProfilePhoto.php +++ b/src/HasProfilePhoto.php @@ -67,7 +67,11 @@ public function getProfilePhotoUrlAttribute() */ protected function defaultProfilePhotoUrl() { - return 'https://ui-avatars.com/api/?name='.urlencode(Str::anonymizeName($this->name)).'&color=7F9CF5&background=EBF4FF'; + $name = trim(collect(explode(' ', $this->name))->map(function ($segment) { + return $segment[0] ?? ''; + })->join(' ')); + + return 'https://ui-avatars.com/api/?name='.urlencode($name).'&color=7F9CF5&background=EBF4FF'; } /** diff --git a/src/JetstreamServiceProvider.php b/src/JetstreamServiceProvider.php index 8fb36b577..9760c0f3a 100644 --- a/src/JetstreamServiceProvider.php +++ b/src/JetstreamServiceProvider.php @@ -91,16 +91,6 @@ public function boot() ]); }); - Str::macro('anonymizeName',function ($name){ - $anonymizedName = ""; - - foreach (explode(" ", $name) as $word) { - $anonymizedName .= $word[0].' '; - } - - return Str::replaceLast(' ', '', $anonymizedName); - }); - if (config('jetstream.stack') === 'inertia') { $this->bootInertia(); } diff --git a/tests/StrMacroTest.php b/tests/StrMacroTest.php deleted file mode 100644 index cc4bc8fa9..000000000 --- a/tests/StrMacroTest.php +++ /dev/null @@ -1,12 +0,0 @@ -assertTrue(Str::anonymizeName('Taylor Otwell') === 'T O'); - } -} \ No newline at end of file From e4b9acbaa106b0bbb15cc30f2e8c8827a6a491cc Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 7 Jan 2022 13:36:15 -0600 Subject: [PATCH 4/4] remove import --- src/JetstreamServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/JetstreamServiceProvider.php b/src/JetstreamServiceProvider.php index 9760c0f3a..141ac8617 100644 --- a/src/JetstreamServiceProvider.php +++ b/src/JetstreamServiceProvider.php @@ -9,7 +9,6 @@ use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; -use Illuminate\Support\Str; use Illuminate\View\Compilers\BladeCompiler; use Inertia\Inertia; use Laravel\Fortify\Fortify;