Java Code Examples for org.alfresco.repo.security.authentication.AuthenticationUtil#setRunAsUser()

The following examples show how to use org.alfresco.repo.security.authentication.AuthenticationUtil#setRunAsUser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InviteServiceTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
private JSONObject getInviteInfo(String inviteId, String inviteTicket, String inviteeUid) throws Exception
{
    String url = "/api/invite/" + inviteId + "/" + inviteTicket + "?inviteeUserName=" + inviteeUid;

    String runAsUser = AuthenticationUtil.getRunAsUser();

    Response response = sendRequest(new GetRequest(url), Status.STATUS_OK);

    if (!runAsUser.equals(AuthenticationUtil.getRunAsUser()))
    {
        AuthenticationUtil.setRunAsUser(runAsUser);
    }

    JSONObject result = new JSONObject(response.getContentAsString());

    return result;
}
 
Example 2
Source File: ProcessesImplTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void setUp() throws Exception
{
    applicationContext = ApplicationContextHelper.getApplicationContext(CONFIG_LOCATIONS);

    processes = (Processes) applicationContext.getBean(PROCESSES_BEAN_NAME);

    ServiceRegistry registry = (ServiceRegistry) applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
    workflowService = registry.getWorkflowService();
    personService = registry.getPersonService();

    transaction = registry.getTransactionService().getUserTransaction();
    transaction.begin();

    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getAdminUserName());

    NodeRef adminUserNodeRef = personService.getPerson(AuthenticationUtil.getAdminUserName());

    WorkflowDefinition workflowDefinition = findAppropriateWorkflowDefinitionId();

    for (int i = 0; i < ACTIVE_WORKFLOWS_INITIAL_AMOUNT; i++)
    {
        startWorkflow(workflowDefinition, adminUserNodeRef);
    }
}
 
Example 3
Source File: LocalTestRunAsAuthenticatorFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean authenticate(RequiredAuthentication required, boolean isGuest)
{
    if (! emptyCredentials())
    {
        AuthenticationUtil.setRunAsUser(userName);
        return true;
    }
    return false;
}
 
Example 4
Source File: RuleTriggerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void before() throws Exception
{
    ServiceRegistry serviceRegistry = (ServiceRegistry) applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
    this.nodeService = serviceRegistry.getNodeService();
    this.contentService = serviceRegistry.getContentService();
    
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getSystemUserName());
    
    this.testStoreRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
    this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
}
 
Example 5
Source File: CopyServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * https://issues.alfresco.com/jira/browse/ALF-17549
 */
public void testALF17549() throws Exception
{
    permissionService.setPermission(rootNodeRef, USER_1, PermissionService.COORDINATOR, true);

    AuthenticationUtil.setRunAsUser(USER_1);

    String sourceName = "sourceNode.txt";
    Map<QName, Serializable> props = new HashMap<QName, Serializable>();

    props.put(ContentModel.PROP_NAME, sourceName);

    NodeRef sourceNodeRef = nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("{test}" + sourceName), ContentModel.TYPE_CONTENT, props)
            .getChildRef();

    ContentWriter writer = contentService.getWriter(sourceNodeRef, ContentModel.PROP_CONTENT, true);
    writer.setEncoding("UTF-8");
    writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
    writer.putContent("This is sample text content for unit test.");

    NodeRef targetNodeRef = nodeService.createNode(this.rootNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("{test}targetNode"), ContentModel.TYPE_FOLDER)
            .getChildRef();

    List<ChildAssociationRef> childAssoc = nodeService.getChildAssocs(targetNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("{test}sourceNode.html"));

    assertEquals(0, childAssoc.size());

    Action action = this.actionService.createAction(TransformActionExecuter.NAME);

    action.setParameterValue(TransformActionExecuter.PARAM_MIME_TYPE, MimetypeMap.MIMETYPE_HTML);
    action.setParameterValue(TransformActionExecuter.PARAM_DESTINATION_FOLDER, targetNodeRef);
    action.setParameterValue(TransformActionExecuter.PARAM_ASSOC_QNAME, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "copy"));
    action.setParameterValue(TransformActionExecuter.PARAM_ASSOC_TYPE_QNAME, ContentModel.ASSOC_CONTAINS);
    actionService.executeAction(action, sourceNodeRef);

    childAssoc = nodeService.getChildAssocs(targetNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName("{test}sourceNode.html"));

    assertEquals(1, childAssoc.size());
}
 
Example 6
Source File: LocalSynchronousTransformClientIntegrationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception
{
    super.setUp();
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getAdminUserName());
    synchronousTransformClient = localSynchronousTransformClient;
}
 
Example 7
Source File: LocalTransformClientIntegrationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception
{
    super.setUp();
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getAdminUserName());
    transformClient = localTransformClient;
}
 
Example 8
Source File: AbstractRenditionTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception
{
    super.setUp();
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getAdminUserName());
}
 
Example 9
Source File: ActionServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * http://issues.alfresco.com/jira/browse/ALF-5027
 */
@Test
public void testALF5027() throws Exception
{
    String userName = "bob" + GUID.generate();
    createUser(userName);
    PermissionService permissionService = (PermissionService)applicationContext.getBean("PermissionService");
    permissionService.setPermission(rootNodeRef, userName, PermissionService.COORDINATOR, true);
    
    AuthenticationUtil.setRunAsUser(userName);
    
    NodeRef myNodeRef = nodeService.createNode(
            this.rootNodeRef,
            ContentModel.ASSOC_CHILDREN,
            QName.createQName("{test}myTestNode" + GUID.generate()),
            ContentModel.TYPE_CONTENT).getChildRef();
    
    CheckOutCheckInService coci = (CheckOutCheckInService)applicationContext.getBean("CheckoutCheckinService");
    NodeRef workingcopy = coci.checkout(myNodeRef);
    assertNotNull(workingcopy);
    
    assertFalse(nodeService.hasAspect(myNodeRef, ContentModel.ASPECT_DUBLINCORE));
    
    Action action1 = this.actionService.createAction(AddFeaturesActionExecuter.NAME);
    action1.setParameterValue(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_DUBLINCORE);        
    actionService.executeAction(action1, myNodeRef);
    
    // The action should have been ignored since the node is locked
    assertFalse(nodeService.hasAspect(myNodeRef, ContentModel.ASPECT_DUBLINCORE));
    
    coci.checkin(workingcopy, null);
    actionService.executeAction(action1, myNodeRef);
    
    assertTrue(nodeService.hasAspect(myNodeRef, ContentModel.ASPECT_DUBLINCORE));
}
 
Example 10
Source File: InviteByTicket.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status)
{
    String tenantDomain = TenantService.DEFAULT_DOMAIN;
    
    if (tenantService.isEnabled())
    {
        String inviteeUserName = req.getParameter(PARAM_INVITEE_USER_NAME);
        if (inviteeUserName != null)
        {
            tenantDomain = tenantService.getUserDomain(inviteeUserName);
        }
    }
    
    // run as system user
    String mtAwareSystemUser = tenantService.getDomainUser(AuthenticationUtil.getSystemUserName(), tenantDomain);
    
    Map<String, Object> ret = TenantUtil.runAsSystemTenant(new TenantRunAsWork<Map<String, Object>>()
    {
        public Map<String, Object> doWork() throws Exception
        {
            return execute(req, status);
        }
    }, tenantDomain);
    
    // authenticate as system for the rest of the webscript
    AuthenticationUtil.setRunAsUser(mtAwareSystemUser);
    
    return ret;
}
 
Example 11
Source File: NodeLocatorWebScriptTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testUserHomeNodeLocator() throws Exception
{
    String url = baseURL + UserHomeNodeLocator.NAME;
    // Run as System User, no User Home.
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getSystemUserName());
    checkNodeLocator(url, null);
    
    //Run as Admin User
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    
    NodeRef admin = repositoryHelper.getPerson();
    NodeRef userHome = repositoryHelper.getUserHome(admin);
    
    checkNodeLocator(url, userHome);
}
 
Example 12
Source File: ChainingUserRegistrySynchronizer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public final void beforeProcess() throws Throwable
{
    // Authentication
    AuthenticationUtil.setRunAsUser(AuthenticationUtil.getSystemUserName());
}
 
Example 13
Source File: GroupsTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates authority context.
 *
 * @param userName
 *            The user to run as.
 */
private void createAuthorityContext(String userName) throws PublicApiException
{
    String groupName = "Group_ROOT" + GUID.generate();

    AuthenticationUtil.setRunAsUser(userName);
    if (rootGroupName == null)
    {
        rootGroupName = authorityService.getName(AuthorityType.GROUP, groupName);
    }

    if (!authorityService.authorityExists(rootGroupName))
    {
        AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();

        rootGroupName = authorityService.createAuthority(AuthorityType.GROUP, groupName);
        authorityService.addAuthorityToZones(rootGroupName, zoneSet("APITEST.MYZONE"));
        authorityService.setAuthorityDisplayName(rootGroupName, "Root Group");

        String groupBAuthorityName = authorityService.createAuthority(AuthorityType.GROUP, "Test_GroupB" + GUID.generate());
        authorityService.setAuthorityDisplayName(groupBAuthorityName, "B Group");
        authorityService.addAuthority(rootGroupName, groupBAuthorityName);
        authorityService.addAuthorityToZones(groupBAuthorityName, zoneSet("APITEST.MYZONE"));
        when(groupBResultSetRow.getNodeRef()).thenReturn(authorityService.getAuthorityNodeRef(groupBAuthorityName));

        String groupAAuthorityName = authorityService.createAuthority(AuthorityType.GROUP, "Test_GroupA" + GUID.generate());
        authorityService.setAuthorityDisplayName(groupAAuthorityName, "A Group");
        authorityService.addAuthority(rootGroupName, groupAAuthorityName);
        authorityService.addAuthorityToZones(groupAAuthorityName, zoneSet("APITEST.MYZONE", "APITEST.ANOTHER"));
        when(groupAResultSetRow.getNodeRef()).thenReturn(authorityService.getAuthorityNodeRef(groupAAuthorityName));

        authorityService.addAuthority(groupAAuthorityName, user1);
        authorityService.addAuthority(groupBAuthorityName, user2);

        rootGroup = new Group();
        rootGroup.setId(rootGroupName);
        rootGroup.setDisplayName("Root Group");

        groupA = new Group();
        groupA.setId(groupAAuthorityName);
        groupA.setDisplayName("A Group");

        groupB = new Group();
        groupB.setId(groupBAuthorityName);
        groupB.setDisplayName("B Group");

        groupMemberA = new GroupMember();
        groupMemberA.setId(groupAAuthorityName);
        groupMemberA.setMemberType(AuthorityType.GROUP.toString());

        groupMemberB = new GroupMember();
        groupMemberB.setId(groupBAuthorityName);
        groupMemberB.setMemberType(AuthorityType.GROUP.toString());
    }

    {
        publicApiClient.setRequestContext(new RequestContext(networkOne.getId(), networkAdmin, "admin"));
        Person personAlice = new Person();
        String aliceId = "alice-" + UUID.randomUUID() + "@" + networkOne.getId();
        personAlice.setUserName(aliceId);
        personAlice.setId(aliceId);
        personAlice.setFirstName("Alice");
        personAlice.setEmail("[email protected]");
        personAlice.setPassword("password");
        personAlice.setEnabled(true);
        PublicApiClient.People people = publicApiClient.people();
        people.create(personAlice);
        personMember = new GroupMember();
        personMember.setId(personAlice.getId());
        personMember.setMemberType(MEMBER_TYPE_PERSON);
    }
}