Search in sources :

Example 1 with AuthenticationContext

use of org.alfresco.repo.security.authentication.AuthenticationContext in project acs-community-packaging by Alfresco.

the class ContextListener method contextInitialized.

/**
 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
 */
public void contextInitialized(ServletContextEvent event) {
    // make sure that the spaces store in the repository exists
    this.servletContext = event.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    // If no context has been initialised, exit silently so config changes can be made
    if (ctx == null) {
        return;
    }
    ServiceRegistry registry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    TransactionService transactionService = registry.getTransactionService();
    NodeService nodeService = registry.getNodeService();
    SearchService searchService = registry.getSearchService();
    NamespaceService namespaceService = registry.getNamespaceService();
    AuthenticationContext authenticationContext = (AuthenticationContext) ctx.getBean("authenticationContext");
    // repo bootstrap code for our client
    UserTransaction tx = null;
    NodeRef companySpaceNodeRef = null;
    try {
        tx = transactionService.getUserTransaction();
        tx.begin();
        authenticationContext.setSystemUserAsCurrentUser();
        // get and setup the initial store ref and root path from config
        StoreRef storeRef = Repository.getStoreRef(servletContext);
        // get root path
        String rootPath = Application.getRootPath(servletContext);
        // Extract company space id and store it in the Application object
        companySpaceNodeRef = Repository.getCompanyRoot(nodeService, searchService, namespaceService, storeRef, rootPath);
        Application.setCompanyRootId(companySpaceNodeRef.getId());
        // commit the transaction
        tx.commit();
    } catch (Throwable e) {
        // rollback the transaction
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
        }
        logger.error("Failed to initialise ", e);
        throw new AlfrescoRuntimeException("Failed to initialise ", e);
    } finally {
        try {
            authenticationContext.clearCurrentSecurityContext();
        } catch (Exception ex) {
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) StoreRef(org.alfresco.service.cmr.repository.StoreRef) AuthenticationContext(org.alfresco.repo.security.authentication.AuthenticationContext) TransactionService(org.alfresco.service.transaction.TransactionService) NodeService(org.alfresco.service.cmr.repository.NodeService) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebApplicationContext(org.springframework.web.context.WebApplicationContext) NodeRef(org.alfresco.service.cmr.repository.NodeRef) NamespaceService(org.alfresco.service.namespace.NamespaceService) SearchService(org.alfresco.service.cmr.search.SearchService) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ServiceRegistry(org.alfresco.service.ServiceRegistry)

Example 2 with AuthenticationContext

use of org.alfresco.repo.security.authentication.AuthenticationContext in project alfresco-remote-api by Alfresco.

the class WebDAVServlet method initializeRootNode.

/**
 * @param storeValue String
 * @param rootPath String
 * @param context WebApplicationContext
 * @param nodeService NodeService
 * @param searchService SearchService
 * @param namespaceService NamespaceService
 * @param tenantService TenantService
 * @param m_transactionService TransactionService
 */
private void initializeRootNode(String storeValue, String rootPath, WebApplicationContext context, NodeService nodeService, SearchService searchService, NamespaceService namespaceService, TenantService tenantService, TransactionService m_transactionService) {
    // Use the system user as the authenticated context for the filesystem initialization
    AuthenticationContext authComponent = (AuthenticationContext) context.getBean("authenticationContext");
    authComponent.setSystemUserAsCurrentUser();
    // Wrap the initialization in a transaction
    UserTransaction tx = m_transactionService.getUserTransaction(true);
    try {
        if (tx != null)
            tx.begin();
        StoreRef storeRef = new StoreRef(storeValue);
        if (nodeService.exists(storeRef) == false) {
            throw new RuntimeException("No store for path: " + storeRef);
        }
        NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
        List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, rootPath, null, namespaceService, false);
        if (nodeRefs.size() > 1) {
            throw new RuntimeException("Multiple possible children for : \n" + "   path: " + rootPath + "\n" + "   results: " + nodeRefs);
        } else if (nodeRefs.size() == 0) {
            throw new RuntimeException("Node is not found for : \n" + "   root path: " + rootPath);
        }
        defaultRootNode = nodeRefs.get(0);
        // Commit the transaction
        if (tx != null)
            tx.commit();
    } catch (Exception ex) {
        logger.error(ex);
    } finally {
        // Clear the current system user
        authComponent.clearCurrentSecurityContext();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) StoreRef(org.alfresco.service.cmr.repository.StoreRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuthenticationContext(org.alfresco.repo.security.authentication.AuthenticationContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

Aggregations

UserTransaction (javax.transaction.UserTransaction)2 AuthenticationContext (org.alfresco.repo.security.authentication.AuthenticationContext)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 StoreRef (org.alfresco.service.cmr.repository.StoreRef)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 ServiceRegistry (org.alfresco.service.ServiceRegistry)1 NodeService (org.alfresco.service.cmr.repository.NodeService)1 SearchService (org.alfresco.service.cmr.search.SearchService)1 NamespaceService (org.alfresco.service.namespace.NamespaceService)1 TransactionService (org.alfresco.service.transaction.TransactionService)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1