Simplify some logic in project controllers
Summary: Ref T10010. Several controlers currently have similar logic for handling tags and slugs, loading projects, and canonicalizing URIs. Clean it up a bit. Test Plan: - Visited profile, boards, feed. - Visited by ID and by tag. - Visited by non-normal tag (redircted). - Visited by alternate tag (redirected). - Visited non-policy project by non-normal tag (redirected into policy error). Reviewers: chad Reviewed By: chad Maniphest Tasks: T10010 Differential Revision: https://secure.phabricator.com/D14890
This commit is contained in:
@@ -11,31 +11,11 @@ final class PhabricatorProjectViewController
|
||||
$request = $this->getRequest();
|
||||
$viewer = $request->getViewer();
|
||||
|
||||
$query = id(new PhabricatorProjectQuery())
|
||||
->setViewer($viewer)
|
||||
->needMembers(true)
|
||||
->needWatchers(true)
|
||||
->needImages(true)
|
||||
->needSlugs(true);
|
||||
$id = $request->getURIData('id');
|
||||
$slug = $request->getURIData('slug');
|
||||
if ($slug) {
|
||||
$query->withSlugs(array($slug));
|
||||
} else {
|
||||
$query->withIDs(array($id));
|
||||
}
|
||||
$project = $query->executeOne();
|
||||
if (!$project) {
|
||||
|
||||
// If this request corresponds to a project but just doesn't have the
|
||||
// slug quite right, redirect to the proper URI.
|
||||
$uri = $this->getNormalizedURI($slug);
|
||||
if ($uri !== null) {
|
||||
return id(new AphrontRedirectResponse())->setURI($uri);
|
||||
}
|
||||
|
||||
return new Aphront404Response();
|
||||
$response = $this->loadProject();
|
||||
if ($response) {
|
||||
return $response;
|
||||
}
|
||||
$project = $this->getProject();
|
||||
|
||||
$columns = id(new PhabricatorProjectColumnQuery())
|
||||
->setViewer($viewer)
|
||||
@@ -60,31 +40,4 @@ final class PhabricatorProjectViewController
|
||||
return $this->delegateToController($controller_object);
|
||||
}
|
||||
|
||||
private function getNormalizedURI($slug) {
|
||||
if (!strlen($slug)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$normal = PhabricatorSlug::normalizeProjectSlug($slug);
|
||||
if ($normal === $slug) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$viewer = $this->getViewer();
|
||||
|
||||
// Do execute() instead of executeOne() here so we canonicalize before
|
||||
// raising a policy exception. This is a little more polished than letting
|
||||
// the user hit the error on any variant of the slug.
|
||||
|
||||
$projects = id(new PhabricatorProjectQuery())
|
||||
->setViewer($viewer)
|
||||
->withSlugs(array($normal))
|
||||
->execute();
|
||||
if (!$projects) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return "/tag/{$normal}/";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user