Search in sources :

Example 6 with AuthenticationService

use of org.alfresco.service.cmr.security.AuthenticationService in project acs-community-packaging by Alfresco.

the class Application method logOut.

/**
 * Invalidate Alfresco ticket and Web/Portlet session and clear the Security context for this thread.
 * @param context
 */
public static void logOut(FacesContext context) {
    String ticket = null;
    if (Application.inPortalServer()) {
        ticket = AlfrescoFacesPortlet.onLogOut(context.getExternalContext().getRequest());
    } else {
        SessionUser user = getCurrentUser(context);
        if (user != null) {
            ticket = user.getTicket();
        }
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
    }
    // Explicitly invalidate the Alfresco ticket. This no longer happens on session expiry to allow for ticket
    // 'sharing'
    WebApplicationContext wc = FacesContextUtils.getRequiredWebApplicationContext(context);
    AuthenticationService unprotAuthService = (AuthenticationService) wc.getBean(BEAN_UNPROTECTED_AUTH_SERVICE);
    if (ticket != null) {
        unprotAuthService.invalidateTicket(ticket);
    }
    unprotAuthService.clearCurrentSecurityContext();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionUser(org.alfresco.repo.SessionUser) HttpSession(javax.servlet.http.HttpSession) AuthenticationService(org.alfresco.service.cmr.security.AuthenticationService) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 7 with AuthenticationService

use of org.alfresco.service.cmr.security.AuthenticationService in project acs-community-packaging by Alfresco.

the class AlfrescoFacesPortlet method processAction.

/**
 * Called by the portlet container to allow the portlet to process an action request.
 */
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
    Application.setInPortalServer(true);
    try {
        // Set the current locale
        I18NUtil.setLocale(getLanguage(request.getPortletSession()));
        boolean isMultipart = PortletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            if (logger.isDebugEnabled())
                logger.debug("Handling multipart request...");
            PortletSession session = request.getPortletSession();
            // get the file from the request and put it in the session
            DiskFileItemFactory factory = new DiskFileItemFactory();
            PortletFileUpload upload = new PortletFileUpload(factory);
            List<FileItem> fileItems = upload.parseRequest(request);
            Iterator<FileItem> iter = fileItems.iterator();
            FileUploadBean bean = new FileUploadBean();
            while (iter.hasNext()) {
                FileItem item = iter.next();
                String filename = item.getName();
                if (item.isFormField() == false) {
                    if (logger.isDebugEnabled())
                        logger.debug("Processing uploaded file: " + filename);
                    // workaround a bug in IE where the full path is returned
                    // IE is only available for Windows so only check for the Windows path separator
                    int idx = filename.lastIndexOf('\\');
                    if (idx == -1) {
                        // if there is no windows path separator check for *nix
                        idx = filename.lastIndexOf('/');
                    }
                    if (idx != -1) {
                        filename = filename.substring(idx + File.separator.length());
                    }
                    File tempFile = TempFileProvider.createTempFile("alfresco", ".upload");
                    item.write(tempFile);
                    bean.setFile(tempFile);
                    bean.setFileName(filename);
                    bean.setFilePath(tempFile.getAbsolutePath());
                    session.setAttribute(FileUploadBean.FILE_UPLOAD_BEAN_NAME, bean, PortletSession.PORTLET_SCOPE);
                }
            }
            // Set the VIEW_ID parameter to tell the faces portlet bridge to treat the request
            // as a JSF request, this will send us back to the previous page we came from.
            String lastViewId = (String) request.getPortletSession().getAttribute(SESSION_LAST_VIEW_ID);
            if (lastViewId != null) {
                response.setRenderParameter(VIEW_ID, lastViewId);
            }
        } else {
            SessionUser sessionUser = (SessionUser) request.getPortletSession().getAttribute(AuthenticationHelper.AUTHENTICATION_USER, PortletSession.APPLICATION_SCOPE);
            User user = sessionUser instanceof User ? (User) sessionUser : null;
            if (user != null) {
                // setup the authentication context
                try {
                    WebApplicationContext ctx = (WebApplicationContext) getPortletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
                    AuthenticationService auth = (AuthenticationService) ctx.getBean("AuthenticationService");
                    auth.validate(user.getTicket());
                    // save last username into portlet preferences, get from LoginBean state
                    LoginBean loginBean = (LoginBean) request.getPortletSession().getAttribute(AuthenticationHelper.LOGIN_BEAN);
                    if (loginBean != null) {
                        // TODO: Need to login to the Portal to get a user here to store prefs against
                        // so not really a suitable solution as they get thrown away at present!
                        // Also would need to store prefs PER user - so auto login for each...?
                        String oldValue = request.getPreferences().getValue(PREF_ALF_USERNAME, null);
                        if (oldValue == null || oldValue.equals(loginBean.getUsernameInternal()) == false) {
                            if (request.getPreferences().isReadOnly(PREF_ALF_USERNAME) == false) {
                                request.getPreferences().setValue(PREF_ALF_USERNAME, loginBean.getUsernameInternal());
                                request.getPreferences().store();
                            }
                        }
                    }
                    // do the normal JSF processing
                    super.processAction(request, response);
                } catch (AuthenticationException authErr) {
                    // remove User object as it's now useless
                    request.getPortletSession().removeAttribute(AuthenticationHelper.AUTHENTICATION_USER, PortletSession.APPLICATION_SCOPE);
                }
            } else {
                // do the normal JSF processing as we may be on the login page
                super.processAction(request, response);
            }
        }
    } catch (Throwable e) {
        if (getErrorPage() != null) {
            handleError(request, response, e);
        } else {
            logger.warn("No error page configured, re-throwing exception");
            if (e instanceof PortletException) {
                throw (PortletException) e;
            } else if (e instanceof IOException) {
                throw (IOException) e;
            } else {
                throw new PortletException(e);
            }
        }
    } finally {
        Application.setInPortalServer(false);
    }
}
Also used : User(org.alfresco.web.bean.repository.User) SessionUser(org.alfresco.repo.SessionUser) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) PortletException(javax.portlet.PortletException) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) WebApplicationContext(org.springframework.web.context.WebApplicationContext) FileItem(org.apache.commons.fileupload.FileItem) SessionUser(org.alfresco.repo.SessionUser) PortletSession(javax.portlet.PortletSession) FileUploadBean(org.alfresco.web.bean.FileUploadBean) LoginBean(org.alfresco.web.bean.LoginBean) PortletFileUpload(org.apache.commons.fileupload.portlet.PortletFileUpload) File(java.io.File) AuthenticationService(org.alfresco.service.cmr.security.AuthenticationService)

Example 8 with AuthenticationService

use of org.alfresco.service.cmr.security.AuthenticationService in project alfresco-remote-api by Alfresco.

the class WorkflowModelBuilderTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    namespaceService = new NamespaceServiceMemoryImpl();
    namespaceService.registerNamespace("test", URI);
    namespaceService.registerNamespace(NamespaceService.CONTENT_MODEL_PREFIX, NamespaceService.CONTENT_MODEL_1_0_URI);
    namespaceService.registerNamespace(NamespaceService.BPM_MODEL_PREFIX, NamespaceService.BPM_MODEL_1_0_URI);
    personService = mock(PersonService.class);
    when(personService.getPerson(userName)).thenReturn(person);
    when(personService.personExists(userName)).thenReturn(true);
    nodeService = mock(NodeService.class);
    Map<QName, Serializable> personProps = new HashMap<QName, Serializable>();
    personProps.put(ContentModel.PROP_USERNAME, userName);
    personProps.put(ContentModel.PROP_FIRSTNAME, firstName);
    personProps.put(ContentModel.PROP_LASTNAME, lastName);
    when(nodeService.getProperties(person)).thenReturn(personProps);
    when(nodeService.getProperty(person, ContentModel.PROP_USERNAME)).thenReturn(userName);
    when(nodeService.getProperty(person, ContentModel.PROP_FIRSTNAME)).thenReturn(firstName);
    when(nodeService.getProperty(person, ContentModel.PROP_LASTNAME)).thenReturn(lastName);
    workflowService = mock(WorkflowService.class);
    dictionaryService = mock(DictionaryService.class);
    authenticationService = mock(AuthenticationService.class);
    builder = new WorkflowModelBuilder(namespaceService, nodeService, authenticationService, personService, workflowService, dictionaryService);
}
Also used : Serializable(java.io.Serializable) DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) HashMap(java.util.HashMap) WorkflowService(org.alfresco.service.cmr.workflow.WorkflowService) QName(org.alfresco.service.namespace.QName) NamespaceServiceMemoryImpl(org.alfresco.service.namespace.NamespaceServiceMemoryImpl) PersonService(org.alfresco.service.cmr.security.PersonService) NodeService(org.alfresco.service.cmr.repository.NodeService) AuthenticationService(org.alfresco.service.cmr.security.AuthenticationService)

Aggregations

AuthenticationService (org.alfresco.service.cmr.security.AuthenticationService)8 SessionUser (org.alfresco.repo.SessionUser)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)5 User (org.alfresco.web.bean.repository.User)5 HttpSession (javax.servlet.http.HttpSession)4 IOException (java.io.IOException)2 PortletException (javax.portlet.PortletException)2 PortletSession (javax.portlet.PortletSession)2 NodeService (org.alfresco.service.cmr.repository.NodeService)2 PersonService (org.alfresco.service.cmr.security.PersonService)2 LoginBean (org.alfresco.web.bean.LoginBean)2 File (java.io.File)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 PortletRequestDispatcher (javax.portlet.PortletRequestDispatcher)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 AuthenticationComponent (org.alfresco.repo.security.authentication.AuthenticationComponent)1