Pass the values of locale,repo and galleryId

“repo” is used by the template code to indicate a user supplied template
repository (so you can build your own webpage and link to App Inventor
with something like
http://ai2.appinventor.mit.edu/?repo=<myrepo>. Similarly the galleryId
value is used to point to an App in the Gallery which should be opened
upon login. The locale value determines which language App Inventor
should start in.

This commit fixes how we handle these values as we move from the client
application to the LoginServlet and back to the client application so
that they are preserved.

Note: A person’s very first login does not preserve these values because
the code that displays the terms of service fails to preserve them. This
is an old bug that we do not fix here (but can at a later date).

Change-Id: I53768cfaadd12175e3c1fe821e58024d984afcfa
parent fbe026a4
......@@ -755,11 +755,22 @@ public class Ode implements EntryPoint {
return;
case Response.SC_PRECONDITION_FAILED:
String locale = Window.Location.getParameter("locale");
if (locale == null || locale.equals("")) {
Window.Location.replace("/login/");
} else {
Window.Location.replace("/login/?locale=" + locale);
String repo = Window.Location.getParameter("repo");
galleryId = Window.Location.getParameter("galleryId");
String separator = "?";
String uri = "/login/";
if (locale != null && !locale.equals("")) {
uri += separator + "locale=" + locale;
separator = "&";
}
if (repo != null & !repo.equals("")) {
uri += separator + "repo=" + repo;
separator = "&";
}
if (galleryId != null && !galleryId.equals("")) {
uri += separator + "galleryId=" + galleryId;
}
Window.Location.replace(uri);
return; // likely not reached
}
}
......
......@@ -88,7 +88,7 @@ public class AdminInfoServiceImpl extends OdeRemoteServiceServlet implements Adm
// session.setAttribute("readonly", true);
OdeAuthFilter.UserInfo nuser = new OdeAuthFilter.UserInfo(user.getId(),
false, "en");
false);
nuser.setReadOnly(true);
String newCookie = nuser.buildCookie(false);
Cookie cook = new Cookie("AppInventor", newCookie);
......
......@@ -77,36 +77,22 @@ public class LoginServlet extends HttpServlet {
resp.setContentType("text/html; charset=utf-8");
PrintWriter out;
String locale = "en";
String [] components = req.getRequestURI().split("/");
LOG.info("requestURI = " + req.getRequestURI());
String page = getPage(req);
// String locale = (String) req.getSession().getAttribute("locale");
// if (locale == null) { // Default to English
// locale = "en";
// }
OdeAuthFilter.UserInfo userInfo = OdeAuthFilter.getUserInfo(req);
if (userInfo != null && userInfo.getLocale() != null) {
locale = userInfo.getLocale();
}
String queryString = req.getQueryString();
HashMap<String, String> params = getQueryMap(queryString);
String pLocale = params.get("locale");
if (pLocale != null) {
if (!pLocale.equals(locale)) { // Hmmm, changed the locale did we...
locale = pLocale;
// Can only set the locale in userInfo if we have a userInfo
// If we do not and we are using Google authentication we
// will set it later after we have logged ourselves in and
// have allocated a userInfo
if (userInfo != null) {
userInfo.setLocale(locale);
}
}
// These params are passed around so they can take effect even if we
// were not logged in.
String locale = params.get("locale");
if (locale == null) {
locale = "en";
}
String repo = params.get("repo");
String galleryId = params.get("galleryId");
LOG.info("locale = " + locale + " bundle: " + new Locale(locale));
ResourceBundle bundle = ResourceBundle.getBundle("com/google/appinventor/server/loginmessages", new Locale(locale));
......@@ -122,14 +108,9 @@ public class LoginServlet extends HttpServlet {
String email = apiUser.getEmail();
String userId = apiUser.getUserId();
User user = storageIo.getUser(userId, email);
// OLD CODE
// req.getSession().setAttribute("userid", user.getUserId()); // This effectively logs us in!
// if (userService.isUserAdmin()) { // If Google says you are an admin
// req.getSession().setAttribute("isadmin", true); // Tell the session we are admin
if (userInfo == null) { // Need to create it
userInfo = new OdeAuthFilter.UserInfo();
userInfo.setLocale(locale);
}
userInfo = new OdeAuthFilter.UserInfo(); // Create a new userInfo object
userInfo.setUserId(user.getUserId()); // This effectively logs us in!
userInfo.setIsAdmin(user.getIsAdmin());
if (userService.isUserAdmin()) { // If we are a developer, we are always an admin
......@@ -148,11 +129,8 @@ public class LoginServlet extends HttpServlet {
cook.setPath("/");
cook.setMaxAge(0);
resp.addCookie(cook);
if (locale.equals("en")) {
resp.sendRedirect("/");
} else {
resp.sendRedirect("/?locale=" + locale);
}
String uri = buildUri("/", locale, repo, galleryId);
resp.sendRedirect(uri);
return;
} else {
if (useLocal.get() == false) {
......@@ -165,11 +143,8 @@ public class LoginServlet extends HttpServlet {
out.println("</html>\n");
return;
}
if (locale.equals("en")) {
resp.sendRedirect("/login/google");
} else {
resp.sendRedirect("/login/google?locale=" + locale);
}
String uri = buildUri("/login/google", locale, repo, galleryId);
resp.sendRedirect(uri);
return;
}
}
......@@ -189,10 +164,7 @@ public class LoginServlet extends HttpServlet {
}
LOG.info("setpw email = " + data.email);
User user = storageIo.getUserFromEmail(data.email);
if (userInfo == null) { // Didn't get it from an incoming cookie, so need to create it
userInfo = new OdeAuthFilter.UserInfo();
userInfo.setLocale(locale);
}
userInfo = new OdeAuthFilter.UserInfo(); // Create new userInfo object
userInfo.setUserId(user.getUserId()); // This effectively logs us in!
out = setCookieOutput(userInfo, resp);
// req.getSession().setAttribute("userid", user.getUserId()); // This effectively logs us in!
......@@ -245,6 +217,9 @@ public class LoginServlet extends HttpServlet {
req.setAttribute("localeLabel", locale);
req.setAttribute("pleaselogin", bundle.getString("pleaselogin"));
req.setAttribute("login", bundle.getString("login"));
req.setAttribute("repo", repo);
req.setAttribute("locale", locale);
req.setAttribute("galleryId", galleryId);
try {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
} catch (ServletException e) {
......@@ -255,29 +230,15 @@ public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(req.getInputStream()));
String queryString = input.readLine();
// PrintWriter out = resp.getWriter();
// String locale = (String) req.getSession().getAttribute("locale");
// if (locale == null) {
// locale = "en";
// }
PrintWriter out;
String locale = "en";
OdeAuthFilter.UserInfo userInfo = OdeAuthFilter.getUserInfo(req);
if (userInfo != null && userInfo.getLocale() != null) {
locale = userInfo.getLocale();
}
if (userInfo == null) {
userInfo = new OdeAuthFilter.UserInfo();
userInfo.setLocale(locale);
}
LOG.info("locale = " + locale + " bundle: " + new Locale(locale));
ResourceBundle bundle = ResourceBundle.getBundle("com/google/appinventor/server/loginmessages", new Locale(locale));
if (queryString == null) {
out = setCookieOutput(userInfo, resp);
out.println("queryString is null");
......@@ -286,6 +247,16 @@ public class LoginServlet extends HttpServlet {
HashMap<String, String> params = getQueryMap(queryString);
String page = getPage(req);
String locale = params.get("locale");
if (locale == null) {
locale = "en";
}
ResourceBundle bundle = ResourceBundle.getBundle("com/google/appinventor/server/loginmessages", new Locale(locale));
String repo = params.get("repo");
String galleryId = params.get("galleryId");
LOG.info("locale = " + locale + " bundle: " + new Locale(locale));
if (page.equals("sendlink")) {
String email = params.get("email");
if (email == null) {
......@@ -301,13 +272,9 @@ public class LoginServlet extends HttpServlet {
String link = trimPage(req) + pwData.id + "/setpw";
sendmail(email, link, locale);
resp.sendRedirect("/login/linksent/");
// req.getSession().setAttribute("error", link);
// resp.sendRedirect("/");
storageIo.cleanuppwdata();
return;
} else if (page.equals("setpw")) {
// String userid = (String) req.getSession().getAttribute("userid");
// if (userid == null) {
if (userInfo == null || userInfo.getUserId().equals("")) {
fail(req, resp, "Session Timed Out");
return;
......@@ -330,10 +297,7 @@ public class LoginServlet extends HttpServlet {
}
storageIo.setUserPassword(user.getUserId(), hashedPassword);
String uri = "/";
if (!locale.equals("en")) {
uri += "?locale=" + locale;
}
String uri = buildUri("/", locale, repo, galleryId);
resp.sendRedirect(uri); // Logged in, go to service
return;
}
......@@ -360,7 +324,6 @@ public class LoginServlet extends HttpServlet {
return;
}
// req.getSession().setAttribute("userid", user.getUserId());
LOG.info("userInfo = " + userInfo + " user = " + user);
userInfo.setUserId(user.getUserId());
userInfo.setIsAdmin(user.getIsAdmin());
......@@ -372,10 +335,7 @@ public class LoginServlet extends HttpServlet {
resp.addCookie(cook);
}
String uri = "/";
if (!locale.equals("en")) {
uri += "?locale=" + locale;
}
String uri = buildUri("/", locale, repo, galleryId);
resp.sendRedirect(uri);
}
......@@ -468,4 +428,20 @@ public class LoginServlet extends HttpServlet {
return out;
}
private String buildUri(String uri, String locale, String repo, String galleryId) {
String separator = "?";
if (locale != null && !locale.equals("")) {
uri += separator + "locale=" + locale;
separator = "&";
}
if (repo != null && !repo.equals("")) {
uri += separator + "repo=" + repo;
separator = "&";
}
if (galleryId != null && !galleryId.equals("")) {
uri += separator + "galleryId=" + galleryId;
}
return (uri);
}
}
......@@ -227,7 +227,6 @@ public class OdeAuthFilter implements Filter {
String userId = "";
boolean isAdmin = false;
boolean isReadOnly = false;
String locale = "en";
long ts;
transient boolean modified = false;
......@@ -240,18 +239,12 @@ public class OdeAuthFilter implements Filter {
return this.isReadOnly;
}
public UserInfo(String userId, boolean isAdmin, String locale) {
public UserInfo(String userId, boolean isAdmin) {
this.userId = userId;
this.isAdmin = isAdmin;
this.locale = locale;
this.ts = System.currentTimeMillis();
}
public void setLocale(String locale) {
this.locale = locale;
modified = true;
}
public void setUserId(String userId) {
this.userId = userId;
modified = true;
......@@ -266,10 +259,6 @@ public class OdeAuthFilter implements Filter {
return userId;
}
public String getLocale() {
return locale;
}
public boolean getIsAdmin() {
return isAdmin;
}
......@@ -293,7 +282,6 @@ public class OdeAuthFilter implements Filter {
.setUuid(this.userId)
.setTs(this.ts)
.setIsAdmin(this.isAdmin)
.setLocale(this.locale)
.setIsReadOnly(this.isReadOnly).build();
return Base64Coder.encode(crypter.encrypt(cookie.toByteArray()));
} else {
......@@ -334,7 +322,6 @@ public class OdeAuthFilter implements Filter {
uInfo.userId = cookieToken.getUuid();
uInfo.ts = cookieToken.getTs();
uInfo.isAdmin = cookieToken.getIsAdmin();
uInfo.locale = cookieToken.getLocale();
uInfo.isReadOnly = cookieToken.getIsReadOnly();
if (uInfo.isValid()) {
return uInfo;
......
......@@ -8,7 +8,9 @@ message cookie {
required uint64 ts = 2;
optional bool isAdmin = 3;
optional bool isReadOnly = 4;
optional string locale = 5;
// locale is deprecated. Kept here so we
// do not recycle the id number too soon
// optional string locale = 5;
optional uint64 oneProjectId = 6;
}
<%@page import="javax.servlet.http.HttpServletRequest"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%!
public String buildUri(String uri, String locale, String repo, String galleryId) {
String separator = "?";
if (locale != null && !locale.equals("")) {
uri += separator + "locale=" + locale;
separator = "&";
}
if (repo != null && !repo.equals("")) {
uri += separator + "repo=" + repo;
separator = "&";
}
if (galleryId != null && !galleryId.equals("")) {
uri += separator + "galleryId=" + galleryId;
}
return (uri);
}
%>
<!doctype html>
<%
String error = request.getParameter("error");
String useGoogleLabel = (String) request.getAttribute("useGoogleLabel");
String locale = request.getParameter("locale");
String repo = (String) request.getAttribute("repo");
String galleryId = (String) request.getAttribute("galleryId");
if (locale == null) {
locale = "en";
}
%>
<html>
<head>
......@@ -30,17 +50,29 @@ out.println("<center><font color=red><b>" + error + "</b></font></center><br/>")
<tr><td></td></td>
<tr><td>${passwordLabel}</td><td><input type=password name=password value="" size="35"></td></tr>
</table></center>
<% if (locale != null && !locale.equals("")) {
%>
<input type=hidden name=locale value="<%= locale %>">
<% }
if (repo != null && !repo.equals("")) {
%>
<input type=hidden name=repo value="<%= repo %>">
<% }
if (galleryId != null && !galleryId.equals("")) {
%>
<input type=hidden name=galleryId value="<%= galleryId %>">
<% } %>
<p></p>
<center><input type=Submit value="${login}" style="font-size: 300%;"></center>
</form>
<p></p>
<center><p><a href="/login/sendlink" style="text-decoration:none;">${passwordclickhereLabel}</a></p></center>
<% if (useGoogleLabel != null && useGoogleLabel.equals("true")) { %>
<center><p><a href="/login/google" style="text-decoration:none;">Click Here to use your Google Account to login</a></p></center>
<center><p><a href="<%= buildUri("/login/google", locale, repo, galleryId) %>" style="text-decoration:none;">Click Here to use your Google Account to login</a></p></center>
<% } %>
<footer>
<center><a href="/login?locale=zh_CN" style="text-decoration:none;" >中文</a>&nbsp;
<a href="/login?locale=en" style="text-decoration:none;" >English</a></center>
<center><a href="<%= buildUri("/login", "zh_CN", repo, galleryId) %>" style="text-decoration:none;" >中文</a>&nbsp;
<a href="<%= buildUri("/login", "en", repo, galleryId) %>" style="text-decoration:none;" >English</a></center>
<p></p>
<center>
<% if (locale != null && locale.equals("zh_CN")) { %>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment