Commit a4fd95a8 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Handled permissions on UI

parent 93f1d1f0
Loading
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
package it.inaf.ia2.vospace.ui;

import it.inaf.ia2.aa.data.User;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

/**
 * Extracts user from the session and set it as request Principal.
 */
public class UserFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
        HttpServletRequestWrapper requestWithPrincipal = new RequestWithPrincipal((HttpServletRequest) req);
        fc.doFilter(requestWithPrincipal, res);
    }

    private static class RequestWithPrincipal extends HttpServletRequestWrapper {

        private final User user;

        public RequestWithPrincipal(HttpServletRequest request) {
            super(request);
            HttpSession session = request.getSession(false);
            if (session == null || session.getAttribute("user_data") == null) {
                this.user = new User()
                        .setUserId("anonymous").setUserLabel("Anonymous")
                        .setGroups(new ArrayList<>());
            } else {
                this.user = (User) session.getAttribute("user_data");
            }
        }

        @Override
        public Principal getUserPrincipal() {
            return user;
        }
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -35,6 +35,14 @@ public class VOSpaceUiApplication {
        return registration;
    }

    @Bean
    public FilterRegistrationBean userFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new UserFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }

    @Bean
    public UserManager userManager() {
        return ServiceLocator.getInstance().getUserManager();
+5 −11
Original line number Diff line number Diff line
package it.inaf.ia2.vospace.ui.controller;

import it.inaf.ia2.aa.data.User;
import it.inaf.ia2.vospace.ui.client.VOSpaceClient;
import it.inaf.ia2.vospace.ui.data.ListNodeData;
import it.inaf.ia2.vospace.ui.service.NodesService;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -35,20 +37,12 @@ public class NodesController extends BaseController {
    @Autowired
    private HttpServletRequest servletRequest;

    /**
     * This is the only API endpoint that returns HTML code instead of JSON. The
     * reason is that JavaScript frameworks are not very efficient in handling
     * very long lists and tables, so this part of the code is generated
     * server-side. The content type is set to text/plain even if it is an HTML
     * fragment to avoid browser parsing issues since it is not a complete HTML
     * document.
     */
    @GetMapping(value = {"/nodes", "/nodes/**"}, produces = MediaType.TEXT_PLAIN_VALUE)
    public String listNodes() throws Exception {
    @GetMapping(value = {"/nodes", "/nodes/**"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ListNodeData> listNodes(User principal) throws Exception {

        String path = getPath("/nodes/");

        return nodesService.generateNodesHtml(path);
        return ResponseEntity.ok(nodesService.generateNodesHtml(path, principal));
    }

    @DeleteMapping(value = {"/nodes", "/nodes/**"})
+3 −0
Original line number Diff line number Diff line
@@ -29,6 +29,9 @@ public class Job {
    }

    private String formatCreationTime(XMLGregorianCalendar calendar) {
        if (calendar == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(calendar.toGregorianCalendar().getTime());
    }
+23 −0
Original line number Diff line number Diff line
package it.inaf.ia2.vospace.ui.data;

public class ListNodeData {

    private String htmlTable;
    private boolean writable;

    public String getHtmlTable() {
        return htmlTable;
    }

    public void setHtmlTable(String htmlTable) {
        this.htmlTable = htmlTable;
    }

    public boolean isWritable() {
        return writable;
    }

    public void setWritable(boolean writable) {
        this.writable = writable;
    }
}
Loading