diff --git a/app/Factories/LinkFactory.php b/app/Factories/LinkFactory.php index 2320613..14e5463 100644 --- a/app/Factories/LinkFactory.php +++ b/app/Factories/LinkFactory.php @@ -55,10 +55,10 @@ class LinkFactory { looks like a shortened URL.'); } - if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url) !== false)) { + if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url, $creator) !== false)) { // if link is not specified as secret, is non-custom, and // already exists in Polr, lookup the value and return - $existing_link = LinkHelper::longLinkExists($long_url); + $existing_link = LinkHelper::longLinkExists($long_url, $creator); return self::formatLink($existing_link); } diff --git a/app/Helpers/LinkHelper.php b/app/Helpers/LinkHelper.php index b876d77..5abd675 100644 --- a/app/Helpers/LinkHelper.php +++ b/app/Helpers/LinkHelper.php @@ -51,16 +51,30 @@ class LinkHelper { } } - static public function longLinkExists($long_url) { + static public function longLinkExists($long_url, $username=false) { /** * Provided a long link (string), * check whether the link is in the DB. + * If a username is provided, only search for links created by the + * user. * @return boolean */ - $link = Link::longUrl($long_url) + $link_base = Link::longUrl($long_url) ->where('is_custom', 0) - ->where('secret_key', '') - ->first(); + ->where('secret_key', ''); + + if (is_null($username)) { + // Search for links without a creator only + $link = $link_base->where('creator', '')->first(); + } + else if (($username !== false)) { + // Search for links created by $username only + $link = $link_base->where('creator', $username)->first(); + } + else { + // Search for links created by any user + $link = $link_base->first(); + } if ($link == null) { return false;