]> BookStack Code Mirror - bookstack/commitdiff
Merge branch 'Vinrobot-custom-avatar-provider'
authorDan Brown <redacted>
Sat, 22 Dec 2018 19:29:35 +0000 (19:29 +0000)
committerDan Brown <redacted>
Sat, 22 Dec 2018 19:29:35 +0000 (19:29 +0000)
.env.example
app/Auth/UserRepo.php
app/Console/Commands/CreateAdmin.php
app/Http/Controllers/UserController.php
app/Uploads/ImageService.php
config/services.php

index 11dafa2ab26b75870f660324ed3ac45d2338036e..1005ad208d459e1812d01e4061898c70621f76ea 100644 (file)
@@ -60,8 +60,13 @@ GITLAB_BASE_URI=false
 DISCORD_APP_ID=false
 DISCORD_APP_SECRET=false
 
-# External services such as Gravatar and Draw.IO
+
+# Disable default services such as Gravatar and Draw.IO
 DISABLE_EXTERNAL_SERVICES=false
+# Use custom avatar service, Sets fetch URL
+# Possible placeholders: ${hash} ${size} ${email}
+# If set, Avatars will be fetched regardless of DISABLE_EXTERNAL_SERVICES option.
+# AVATAR_URL=https://seccdn.libravatar.org/avatar/${hash}?s=${size}&d=identicon
 
 # LDAP Settings
 LDAP_SERVER=false
index 7c88badb843297a37901a70883179c9a20757447..d436ab8eb0e72f2eeee6f5f04198ae6a57f2a4a0 100644 (file)
@@ -85,9 +85,7 @@ class UserRepo
     {
         $user = $this->create($data, $verifyEmail);
         $this->attachDefaultRole($user);
-
-        // Get avatar from gravatar and save
-        $this->downloadGravatarToUserAvatar($user);
+        $this->downloadAndAssignUserAvatar($user);
 
         return $user;
     }
@@ -238,25 +236,24 @@ class UserRepo
     }
 
     /**
-     * Get a gravatar image for a user and set it as their avatar.
-     * Does not run if gravatar disabled in config.
+     * Get aavatar image for a user and set it as their avatar.
+     * Returns early if avatars disabled or not set in config.
      * @param User $user
      * @return bool
      */
-    public function downloadGravatarToUserAvatar(User $user)
+    public function downloadAndAssignUserAvatar(User $user)
     {
-        // Get avatar from gravatar and save
-        if (!config('services.gravatar')) {
+        if (!Images::avatarFetchEnabled()) {
             return false;
         }
 
         try {
-            $avatar = Images::saveUserGravatar($user);
+            $avatar = Images::saveUserAvatar($user);
             $user->avatar()->associate($avatar);
             $user->save();
             return true;
         } catch (Exception $e) {
-            \Log::error('Failed to save user gravatar image');
+            \Log::error('Failed to save user avatar image');
             return false;
         }
     }
index 6bfc544698fdc2dba39ddae88f18ac4a154fc0c1..90c1ddb1c325e4e67761b5a81d9d602ad254c103 100644 (file)
@@ -76,7 +76,7 @@ class CreateAdmin extends Command
 
         $user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
         $this->userRepo->attachSystemRole($user, 'admin');
-        $this->userRepo->downloadGravatarToUserAvatar($user);
+        $this->userRepo->downloadAndAssignUserAvatar($user);
         $user->email_confirmed = true;
         $user->save();
 
index 5f5c8365eecc34de70ef0dae569f2c20406a79e2..24f8b67cbc4663a83e6777d585326b7759bc9759 100644 (file)
@@ -92,7 +92,7 @@ class UserController extends Controller
             $user->roles()->sync($roles);
         }
 
-        $this->userRepo->downloadGravatarToUserAvatar($user);
+        $this->userRepo->downloadAndAssignUserAvatar($user);
 
         return redirect('/settings/users');
     }
index f109db60068f5b919509a450df371bc8bd50eeee..d5f4068ef26e64a5bf19b508f8ea1ec8d8b1a0d7 100644 (file)
@@ -279,24 +279,51 @@ class ImageService extends UploadService
     }
 
     /**
-     * Save a gravatar image and set a the profile image for a user.
+     * Save an avatar image from an external service.
      * @param \BookStack\Auth\User $user
      * @param int $size
-     * @return mixed
+     * @return Image
      * @throws Exception
      */
-    public function saveUserGravatar(User $user, $size = 500)
+    public function saveUserAvatar(User $user, $size = 500)
     {
-        $emailHash = md5(strtolower(trim($user->email)));
-        $url = 'https://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
-        $imageName = str_replace(' ', '-', $user->name . '-gravatar.png');
-        $image = $this->saveNewFromUrl($url, 'user', $imageName);
+        $avatarUrl = $this->getAvatarUrl();
+        $email = strtolower(trim($user->email));
+
+        $replacements = [
+            '${hash}' => md5($email),
+            '${size}' => $size,
+            '${email}' => urlencode($email),
+        ];
+
+        $userAvatarUrl = strtr($avatarUrl, $replacements);
+        $imageName = str_replace(' ', '-', $user->name . '-avatar.png');
+        $image = $this->saveNewFromUrl($userAvatarUrl, 'user', $imageName);
         $image->created_by = $user->id;
         $image->updated_by = $user->id;
         $image->save();
+
         return $image;
     }
 
+    /**
+     * Check if fetching external avatars is enabled.
+     * @return bool
+     */
+    public function avatarFetchEnabled()
+    {
+        $fetchUrl = $this->getAvatarUrl();
+        return is_string($fetchUrl) && strpos($fetchUrl, 'http') === 0;
+    }
+
+    /**
+     * Get the URL to fetch avatars from.
+     * @return string|mixed
+     */
+    protected function getAvatarUrl()
+    {
+        return trim(config('services.avatar_url'));
+    }
 
     /**
      * Delete gallery and drawings that are not within HTML content of pages or page revisions.
index ba16488918ab085cdc35e00fd5b29e9c0a958e59..7b9cf4e74b4da822882afb68058badf081b371cc 100644 (file)
@@ -16,10 +16,16 @@ return [
 
     // Single option to disable non-auth external services such as Gravatar and Draw.io
     'disable_services' => env('DISABLE_EXTERNAL_SERVICES', false),
-    'gravatar' => env('GRAVATAR', !env('DISABLE_EXTERNAL_SERVICES', false)),
+
+    // Draw.io integration active
     'drawio' => env('DRAWIO', !env('DISABLE_EXTERNAL_SERVICES', false)),
 
+    // URL for fetching avatars
+    'avatar_url' => env('AVATAR_URL',
+        env('DISABLE_EXTERNAL_SERVICES', false) ? false : 'https://www.gravatar.com/avatar/${hash}?s=${size}&d=identicon'
+    ),
 
+    // Callback URL for social authentication methods
     'callback_url' => env('APP_URL', false),
 
     'mailgun'  => [