View Javadoc
1   package usecase.user;
2   
3   import common.http.ParameterConverter;
4   import common.http.error.JSONError;
5   import common.http.interceptor.InterceptableServlet;
6   import usecase.auth.AuthorizationConstraints;
7   
8   import javax.inject.Inject;
9   import javax.servlet.ServletException;
10  import javax.servlet.annotation.WebServlet;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import java.io.IOException;
14  
15  import static usecase.auth.AuthorizationConstraints.Types.ADMINS_ONLY;
16  
17  /**
18   * Classe che permette di invertire il ruolo di admin di un utente.
19   */
20  @WebServlet("/admin/toggleAdmin")
21  @AuthorizationConstraints(ADMINS_ONLY)
22  class ToggleAdminServlet extends InterceptableServlet {
23  
24      @Inject private UserService service;
25  
26      @Override
27      @JSONError
28      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
29          ParameterConverter converter = new ParameterConverter(req);
30          int userId = converter.getIntParameter("userId").orElse(0);
31          service.toggleAdmin(userId);
32      }
33  }