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) {
}
}
}
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();
}
}
Aggregations