Commits

Samuel Le Berrigaud committed 52adb50

Updating stage 5:
- The Todo list show the username of the current user
- One must be logged in to access the Todo list

Comments (0)

Files changed (2)

ao-tutorial-stage5/pom.xml

                         </pluginArtifact>
                     </pluginArtifacts>
                     <productVersion>${refapp.version}</productVersion>
-                    <log4jProperties>src/aps/log4j.properties</log4jProperties>
+                    <log4jProperties>src/test/resources/log4j.properties</log4jProperties>
                 </configuration>
             </plugin>
         </plugins>

ao-tutorial-stage5/src/main/java/com/atlassian/tutorial/ao/todo/TodoServlet.java

 package com.atlassian.tutorial.ao.todo;
 
+import com.atlassian.sal.api.user.UserManager;
+
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 public final class TodoServlet extends HttpServlet
 {
     private final TodoService todoService;
+    private final UserManager userManager;
 
-    public TodoServlet(TodoService todoService)
+    public TodoServlet(TodoService todoService, UserManager userManager)
     {
         this.todoService = checkNotNull(todoService);
+        this.userManager = checkNotNull(userManager);
     }
 
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
     {
+        // is the user is not connected, redirect to the login page
+        if (userManager.getRemoteUsername() == null)
+        {
+            res.sendRedirect(req.getContextPath() + "/plugins/servlet/login");
+        }
+
         final PrintWriter w = res.getWriter();
-        w.write("<h1>Todos</h1>");
+        w.printf("<h1>Todos (%s)</h1>", userManager.getRemoteUsername());
 
         // the form to post more TODOs
         w.write("<form method=\"post\">");