Search in sources :

Example 1 with ScriptInvitationService

use of org.alfresco.repo.invitation.script.ScriptInvitationService in project alfresco-remote-api by Alfresco.

the class InviteServiceTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    /**
     * We don't want to be authenticated as 'system' but run as 'InviterUser', because then
     * 'system' will be the creator for the sites and 'inviterUser' will be a nobody.
     */
    AuthenticationUtil.clearCurrentSecurityContext();
    // get references to services
    this.authenticationService = (MutableAuthenticationService) getServer().getApplicationContext().getBean("AuthenticationService");
    this.authenticationComponent = (AuthenticationComponent) getServer().getApplicationContext().getBean("AuthenticationComponent");
    this.personService = (PersonService) getServer().getApplicationContext().getBean("PersonService");
    this.siteService = (SiteService) getServer().getApplicationContext().getBean("SiteService");
    this.nodeService = (NodeService) getServer().getApplicationContext().getBean("NodeService");
    this.workflowService = (WorkflowService) getServer().getApplicationContext().getBean("WorkflowService");
    this.mutableAuthenticationDao = (MutableAuthenticationDao) getServer().getApplicationContext().getBean("authenticationDao");
    this.transactionService = (TransactionService) getServer().getApplicationContext().getBean("TransactionService");
    this.nodeArchiveService = (NodeArchiveService) getServer().getApplicationContext().getBean("nodeArchiveService");
    this.invitationServiceImpl = (InvitationServiceImpl) getServer().getApplicationContext().getBean("invitationService");
    ScriptInvitationService scriptInvitationService = (ScriptInvitationService) getServer().getApplicationContext().getBean("invitationServiceScript");
    scriptInvitationService.setSiteService(this.siteService);
    configureMailExecutorForTestMode(this.getServer());
    // We're using a MailActionExecuter defined in outboundSMTP-test-context.xml which
    // sets the testMode property to true via spring injection. This will prevent emails
    // from being sent from within this test case.
    // This MailExecutorAction bean is named "test-mail" but is in all other respects equivalent to the
    // 'real' executer bean. It is automatically included during OutboundSMTP subsystem startup.
    this.transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // 
            // various setup operations which need to be run as system user
            // 
            AuthenticationUtil.runAs(new RunAsWork<Object>() {

                public Object doWork() throws Exception {
                    // Create new invitee email address list
                    inviteeEmailAddrs = new ArrayList<String>();
                    // Create inviter person
                    createPerson(PERSON_FIRSTNAME, PERSON_LASTNAME, USER_INVITER, INVITER_EMAIL);
                    // Create inviter2 person
                    createPerson(PERSON_FIRSTNAME, PERSON_LASTNAME, USER_INVITER_2, INVITER_EMAIL_2);
                    return null;
                }
            }, AuthenticationUtil.getSystemUserName());
            // The creation of sites is heavily dependent on the authenticated user.  We must ensure that,
            // when doing the runAs below, the user both 'runAs' and 'fullyAuthenticated'.  In order for
            // this to be the case, the security context MUST BE EMPTY now.  We could do the old
            // "defensive clear", but really there should not be any lurking authentications on this thread
            // after the context starts up.  If there are, that is a bug, and we fail explicitly here.
            String residuallyAuthenticatedUser = AuthenticationUtil.getFullyAuthenticatedUser();
            assertNull("Residual authentication on context-initiating thread (this thread):" + residuallyAuthenticatedUser, residuallyAuthenticatedUser);
            // 
            // various setup operations which need to be run as inviter user
            // 
            AuthenticationUtil.runAs(new RunAsWork<Object>() {

                public Object doWork() throws Exception {
                    // Create first site for Inviter to invite Invitee to
                    SiteInfo siteInfo = siteService.getSite(SITE_SHORT_NAME_INVITE_1);
                    if (siteInfo == null) {
                        siteService.createSite("InviteSitePreset", SITE_SHORT_NAME_INVITE_1, "InviteSiteTitle", "InviteSiteDescription", SiteVisibility.PUBLIC);
                    }
                    // Create second site for inviter to invite invitee to
                    siteInfo = siteService.getSite(SITE_SHORT_NAME_INVITE_2);
                    if (siteInfo == null) {
                        siteService.createSite("InviteSitePreset", SITE_SHORT_NAME_INVITE_2, "InviteSiteTitle", "InviteSiteDescription", SiteVisibility.PUBLIC);
                    }
                    // Create third site for inviter to invite invitee to
                    siteInfo = InviteServiceTest.this.siteService.getSite(SITE_SHORT_NAME_INVITE_3);
                    if (siteInfo == null) {
                        siteService.createSite("InviteSitePreset", SITE_SHORT_NAME_INVITE_3, "InviteSiteTitle", "InviteSiteDescription", SiteVisibility.PUBLIC);
                    }
                    // set inviter2's role on third site to collaborator
                    String inviterSiteRole = siteService.getMembersRole(SITE_SHORT_NAME_INVITE_3, USER_INVITER_2);
                    if ((inviterSiteRole == null) || (inviterSiteRole.equals(SiteModel.SITE_COLLABORATOR) == false)) {
                        siteService.setMembership(SITE_SHORT_NAME_INVITE_3, USER_INVITER_2, SiteModel.SITE_COLLABORATOR);
                    }
                    return null;
                }
            }, USER_INVITER);
            // Do tests as inviter user
            InviteServiceTest.this.authenticationComponent.setCurrentUser(USER_INVITER);
            return null;
        }
    });
}
Also used : ScriptInvitationService(org.alfresco.repo.invitation.script.ScriptInvitationService) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)

Aggregations

ScriptInvitationService (org.alfresco.repo.invitation.script.ScriptInvitationService)1 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)1 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)1