String namespace = httpRequest.getParameter("ns");
String requestUri = httpRequest.getRequestURI().toLowerCase();
if (requestUri.contains("admin") && !requestUri.startsWith("/_ah/")) {
UserService userService = UserServiceFactory.getUserService();
User currentUser = userService.getCurrentUser();
if (namespace == null) {
List<String> authorizedNamespaces = util.getAuthorizedNamespacesForUser(currentUser);
switch (authorizedNamespaces.size()) {
case 0:
throw new ServletException("The 'ns' parameter is missing, and the user isn't" +
" permissioned for any namespaces.");
case 1:
httpResponse.sendRedirect(util.addNamespaceParam("/admin",
authorizedNamespaces.get(0)));
return;
default:
httpResponse.sendRedirect("/choose_instance.html?instances=" +
util.sortedJoin(authorizedNamespaces, ","));
return;
}
}
if (currentUser == null) {
throw new ServletException("You must be logged in to a Google Account.");
}
if (!util.isUserPermissionedForNamespace(currentUser, namespace)) {
throw new ServletException(String.format("Error while requesting '%s'. The user '%s' " +
"is not allowed to access namespace '%s'.", httpRequest.getRequestURI(),
currentUser.getEmail(), namespace));
}
}
if (namespace != null) {
NamespaceManager.set(namespace);