package net.sourceforge.fenixedu.presentationTier.Action.person; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.applicationTier.IUserView; import net.sourceforge.fenixedu.applicationTier.Filtro.exception.FenixFilterException; import net.sourceforge.fenixedu.applicationTier.Servico.exceptions.FenixServiceException; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.User; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.domain.integration.gapps.GoogleAccount; import net.sourceforge.fenixedu.presentationTier.Action.BaseAuthenticationAction; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import net.sourceforge.fenixedu.presentationTier.Action.resourceAllocationManager.utils.ServiceUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import pt.iscte.ci.authentication.GoogleAppsUtils; import pt.iscte.ci.metadata.ISCTE; import pt.ist.fenixWebFramework.services.Service; @ISCTE(author = "António Casqueiro") public class GoogleAppsDA extends FenixDispatchAction { public ActionForward prepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { final IUserView userView = getUserView(request); if (userView == null) { throw new DomainException("no.user.view"); } final Person person = userView.getPerson(); final GoogleAccount googleAccount = person.getUser().getGoogleAccount(); if (googleAccount != null && googleAccount.getActivated()) { return mapping.findForward("activated"); } else { return mapping.findForward("activate"); } } public ActionForward activate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { final String oldPassword = request.getParameter("oldPassword"); if (oldPassword == null) { addActionMessage(request, "errors.invalidPassword"); return mapping.getInputForward(); } final IUserView userView = getUserView(request); final String alias = userView.getUsername(); final String requestURL = request.getRequestURL().toString(); final String remoteHost = BaseAuthenticationAction.getRemoteHostName(request); boolean successOnAuthentication = false; try { authenticate(userView, requestURL, remoteHost, oldPassword, alias); successOnAuthentication = true; } catch (Exception e) { e.printStackTrace(); } if (!successOnAuthentication) { addActionMessage(request, "errors.invalidPassword"); return mapping.getInputForward(); } // Activate account try { activateAccountServiceCall(alias, oldPassword); } catch (DomainException e) { addActionMessage(request, e.getKey(), e.getArgs()); return mapping.getInputForward(); } return prepare(mapping, form, request, response); } @Service private void activateAccountServiceCall(String username, String password) { final User user = User.readUserByUserUId(username); user.activateGoogleApps(); final GoogleAccount googleAccount = user.getGoogleAccount(); final Person person = user.getPerson(); String[] names = person.getName().split(" "); String firstName = names[0]; String familyName = names[names.length - 1]; String alias = person.getInstitutionalEmail(); Boolean gmailBlocked = googleAccount.getGmailBlocked(); GoogleAppsUtils.synchronizeAccount(username, firstName, familyName, password, gmailBlocked, alias); GoogleAppsUtils.changePassword(username, password); if (!googleAccount.getCreated()) { googleAccount.setCreated(Boolean.TRUE); } } private void authenticate(IUserView userView, final String requestURL, final String remoteHost, String oldPassword, final String alias) throws FenixServiceException, FenixFilterException { System.out.println("Checking old password for username " + alias); ServiceUtils.executeService("Autenticacao", new Object[] { alias, oldPassword, requestURL, remoteHost }); } public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward("cancel"); } }