X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/62342433f41f2eaef19c5e85f5ce960297ee8206..refs/pull/684/head:/app/Repos/UserRepo.php diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php index d329c307c..3cfd61d27 100644 --- a/app/Repos/UserRepo.php +++ b/app/Repos/UserRepo.php @@ -1,6 +1,7 @@ attachDefaultRole($user); // Get avatar from gravatar and save - if (!config('services.disable_services')) { - try { - $avatar = Images::saveUserGravatar($user); - $user->avatar()->associate($avatar); - $user->save(); - } catch (Exception $e) { - $user->save(); - \Log::error('Failed to save user gravatar image'); - } - } + $this->downloadGravatarToUserAvatar($user); return $user; } @@ -113,6 +105,21 @@ class UserRepo $user->attachRoleId($roleId); } + /** + * Assign a user to a system-level role. + * @param User $user + * @param $systemRoleName + * @throws NotFoundException + */ + public function attachSystemRole(User $user, $systemRoleName) + { + $role = $this->role->newQuery()->where('system_name', '=', $systemRoleName)->first(); + if ($role === null) { + throw new NotFoundException("Role '{$systemRoleName}' not found"); + } + $user->attachRole($role); + } + /** * Checks if the give user is the only admin. * @param User $user @@ -228,4 +235,28 @@ class UserRepo { return $this->role->where('system_name', '!=', 'admin')->get(); } + + /** + * Get a gravatar image for a user and set it as their avatar. + * Does not run if gravatar disabled in config. + * @param User $user + * @return bool + */ + public function downloadGravatarToUserAvatar(User $user) + { + // Get avatar from gravatar and save + if (!config('services.gravatar')) { + return false; + } + + try { + $avatar = Images::saveUserGravatar($user); + $user->avatar()->associate($avatar); + $user->save(); + return true; + } catch (Exception $e) { + \Log::error('Failed to save user gravatar image'); + return false; + } + } }