Search in sources :

Example 1 with AccessDeniedException

use of org.xwiki.security.authorization.AccessDeniedException in project xwiki-platform by xwiki.

the class NotificationPreferenceScriptServiceTest method saveNotificationPreferencesWithoutRight.

@Test
public void saveNotificationPreferencesWithoutRight() throws Exception {
    DocumentReference userDoc = new DocumentReference("wikiA", "SpaceA", "UserA");
    AccessDeniedException e = mock(AccessDeniedException.class);
    doThrow(e).when(authorizationManager).checkAccess(Right.EDIT, userDoc);
    String json = "";
    Exception caughtException = null;
    try {
        mocker.getComponentUnderTest().saveNotificationPreferences(json, userDoc);
    } catch (Exception ex) {
        caughtException = ex;
    }
    assertNotNull(caughtException);
    assertEquals(e, caughtException);
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) DocumentReference(org.xwiki.model.reference.DocumentReference) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) Test(org.junit.Test)

Example 2 with AccessDeniedException

use of org.xwiki.security.authorization.AccessDeniedException in project xwiki-platform by xwiki.

the class WikiUserManagerScriptServiceTest method currentScriptHasNotAdminRight.

/**
 * Mocks the components to simulate that a non admin user have saved the current script.
 *
 * @return the exception expected when the current script has the not the admin right
 */
private Exception currentScriptHasNotAdminRight() throws AccessDeniedException {
    DocumentReference authorDocRef = new DocumentReference("mainWiki", "XWiki", "NonAdmin");
    when(currentDoc.getAuthorReference()).thenReturn(authorDocRef);
    DocumentReference currentDocRef = new DocumentReference("subwiki", "Space", "PageToTest");
    when(currentDoc.getDocumentReference()).thenReturn(currentDocRef);
    Exception exception = new AccessDeniedException(Right.ADMIN, authorDocRef, currentDocRef);
    doThrow(exception).when(authorizationManager).checkAccess(Right.ADMIN, authorDocRef, currentDocRef);
    return exception;
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) DocumentReference(org.xwiki.model.reference.DocumentReference) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException)

Example 3 with AccessDeniedException

use of org.xwiki.security.authorization.AccessDeniedException in project xwiki-platform by xwiki.

the class WikiUserManagerScriptServiceTest method currentUserHasNotAdminRight.

/**
 * Mocks the components to simulate that the current user is not an admin.
 *
 * @return the exception expected when the current user has the not the admin right
 */
private Exception currentUserHasNotAdminRight() throws AccessDeniedException {
    WikiReference wiki = new WikiReference("subwiki");
    Exception exception = new AccessDeniedException(Right.ADMIN, userDocRef, wiki);
    doThrow(exception).when(authorizationManager).checkAccess(eq(Right.ADMIN), eq(userDocRef), eq(wiki));
    return exception;
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiReference(org.xwiki.model.reference.WikiReference) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException)

Example 4 with AccessDeniedException

use of org.xwiki.security.authorization.AccessDeniedException in project xwiki-platform by xwiki.

the class WikiManagerScriptServiceTest method currentUserHasNotCreateWikiRight.

/**
 * @return the exception expected when the current user has the not the 'create wiki' right
 */
private Exception currentUserHasNotCreateWikiRight() throws AccessDeniedException {
    WikiReference wiki = new WikiReference("mainWiki");
    Exception exception = new AccessDeniedException(Right.CREATE_WIKI, currentUserRef, wiki);
    doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUserRef), eq(wiki));
    return exception;
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiReference(org.xwiki.model.reference.WikiReference) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException)

Example 5 with AccessDeniedException

use of org.xwiki.security.authorization.AccessDeniedException in project xwiki-platform by xwiki.

the class WikiCreationJobScriptServices method createWiki.

/**
 * Asynchronously create a wiki.
 *
 * @param request creation wiki request containing all information about the wiki to create
 * @return the creationjob that creates the wiki
 */
public Job createWiki(WikiCreationRequest request) {
    try {
        // Verify that the user has the CREATE_WIKI right
        XWikiContext xcontext = xcontextProvider.get();
        WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId());
        authorizationManager.checkAccess(Right.CREATE_WIKI, xcontext.getUserReference(), mainWikiReference);
        // Verify that if an extension id is provided, this extension is authorized.
        if (request.getExtensionId() != null) {
            if (!isAuthorizedExtension(request.getExtensionId())) {
                throw new WikiCreationException(String.format("The extension [%s] is not authorized.", request.getExtensionId()));
            }
        }
        return wikiCreator.createWiki(request);
    } catch (WikiCreationException e) {
        setLastError(e);
        logger.warn("Failed to create a new wiki.", e);
    } catch (AccessDeniedException e) {
        setLastError(e);
    }
    return null;
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiCreationException(org.xwiki.platform.wiki.creationjob.WikiCreationException) XWikiContext(com.xpn.xwiki.XWikiContext) WikiReference(org.xwiki.model.reference.WikiReference)

Aggregations

AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)18 WikiReference (org.xwiki.model.reference.WikiReference)13 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)10 DocumentReference (org.xwiki.model.reference.DocumentReference)8 Test (org.junit.Test)5 WikiTemplateManagerException (org.xwiki.wiki.template.WikiTemplateManagerException)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)4 WikiUserManagerException (org.xwiki.wiki.user.WikiUserManagerException)2 NotificationException (org.xwiki.notifications.NotificationException)1 WikiCreationException (org.xwiki.platform.wiki.creationjob.WikiCreationException)1 WikiCreationRequest (org.xwiki.platform.wiki.creationjob.WikiCreationRequest)1 AuthorizationException (org.xwiki.security.authorization.AuthorizationException)1