001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020package pt.ua.dicoogle.server.web.servlets.accounts;
021
022import java.io.IOException;
023
024import javax.servlet.ServletException;
025import javax.servlet.http.HttpServlet;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import javax.servlet.http.HttpSession;
029
030import net.sf.json.JSONArray;
031import net.sf.json.JSONObject;
032import pt.ua.dicoogle.server.users.Role;
033import pt.ua.dicoogle.server.users.User;
034import pt.ua.dicoogle.server.users.UsersStruct;
035import pt.ua.dicoogle.server.web.auth.LoggedIn;
036import pt.ua.dicoogle.server.web.auth.LoggedInStatus;
037import pt.ua.dicoogle.server.web.auth.Session;
038
039/**
040 *
041 * @author Frederico Silva <fredericosilva@ua.pt>
042 */
043public class LoginServlet extends HttpServlet {
044
045    @Override
046    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
047        //Try login
048        // Does not require admin rights.
049        LoggedInStatus loginStatus = Session.webappLogin(req, resp, false);
050        LoggedIn mLoggedIn = loginStatus.getLogin();
051
052        if (mLoggedIn == null) {
053            resp.sendError(401, "Login failed");
054            return;
055        }
056
057        JSONObject json_resp = new JSONObject();
058        json_resp.put("user", mLoggedIn.getUserName());
059        json_resp.put("admin", mLoggedIn.isAdmin());
060        User u = UsersStruct.getInstance().getUser(mLoggedIn.getUserName());
061        JSONArray rolesObj = new JSONArray();
062        if (u!=null&&u.getRoles()!=null) {
063            for (Role r : u.getRoles()) {
064                if (r!=null)
065                    rolesObj.add(r.getName());
066            }
067
068            json_resp.put("roles", rolesObj);
069        }
070        json_resp.put("token", mLoggedIn.getToken());
071
072        //Set response content type
073        resp.setContentType("application/json");
074
075        //Write response
076        json_resp.write(resp.getWriter());
077    }
078
079        @Override
080        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
081                        throws ServletException, IOException {
082        //resp.addHeader("Access-Control-Allow-Origin", "*");
083                HttpSession session = req.getSession(false);
084                
085                LoggedIn mLoggedIn = Session.getUserLoggedIn(session);
086                if(mLoggedIn == null){
087                        resp.sendError(401);
088            return;
089                }
090                        
091                JSONObject json_resp = new JSONObject();
092        json_resp.put("user", mLoggedIn.getUserName());
093        json_resp.put("admin", mLoggedIn.isAdmin());
094        User u = UsersStruct.getInstance().getUser(mLoggedIn.getUserName());
095        JSONArray rolesObj = new JSONArray();
096        for (Role r : u.getRoles())
097        {
098            rolesObj.add(r.getName());
099        }
100
101        json_resp.put("roles", rolesObj);
102
103        //Set response content type
104        resp.setContentType("application/json");
105
106        //Write response
107        json_resp.write(resp.getWriter());
108    }
109
110}