public void unassignContent(UnassignContentParams params) {
    final UserEntity unassigner = securityService.getImpersonatedPortalUser();

    UnassignContentCommand command = new UnassignContentCommand();
    command.setContentKey(new ContentKey(params.contentKey));
    command.setUnassigner(unassigner.getKey());
    contentService.unassignContent(command);
  }
  @Test
  public void testHasWriteAccess() {
    final UserEntity user = Mockito.mock(UserEntity.class);

    Mockito.when(user.isAnonymous()).thenReturn(true);
    assertFalse(this.resolver.hasWriteAccess(user));

    Mockito.when(user.isAnonymous()).thenReturn(false);
    assertTrue(this.resolver.hasWriteAccess(user));
  }
  @Test
  public void testCreateContentWithBinary() {
    fixture.createAndStoreNormalUserWithUserGroup("testuser", "Test user", "testuserstore");

    fixture.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));
    fixture.save(
        factory.createContentType(
            "MyContentType", ContentHandlerName.CUSTOM.getHandlerClassShortName(), standardConfig));
    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(
        factory.createCategory("MyCategory", "MyContentType", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("MyCategory", "testuser", "read,create"));

    fixture.flushAndClearHibernateSesssion();

    UserEntity runningUser = fixture.findUserByName("testuser");
    PortalSecurityHolder.setImpersonatedUser(runningUser.getKey());

    ContentDataInput contentData = new ContentDataInput("MyContentType");
    contentData.add(new TextInput("myTitle", "testtitle"));
    contentData.add(new BinaryInput("myBinaryfile", dummyBinary, "dummyBinary"));

    CreateContentParams params = new CreateContentParams();
    params.categoryKey = fixture.findCategoryByName("MyCategory").getKey().toInt();
    params.contentData = contentData;
    params.publishFrom = new Date();
    params.publishTo = null;
    params.status = ContentStatus.STATUS_DRAFT;
    int contentKey = internalClient.createContent(params);

    fixture.flushAndClearHibernateSesssion();

    ContentEntity persistedContent = fixture.findContentByKey(new ContentKey(contentKey));
    assertNotNull(persistedContent);
    assertEquals("MyCategory", persistedContent.getCategory().getName());
    ContentVersionEntity persistedVersion = persistedContent.getMainVersion();
    assertNotNull(persistedVersion);
    assertEquals("testtitle", persistedVersion.getTitle());
    assertEquals(
        com.enonic.cms.core.content.ContentStatus.DRAFT.getKey(),
        persistedVersion.getStatus().getKey());

    // verify binary was saved
    Set<ContentBinaryDataEntity> contentBinaryDatas = persistedVersion.getContentBinaryData();
    assertEquals(1, contentBinaryDatas.size());
    ContentBinaryDataEntity contentBinaryData = contentBinaryDatas.iterator().next();
    BinaryDataEntity binaryData = contentBinaryData.getBinaryData();
    assertEquals("dummyBinary", binaryData.getName());

    CustomContentData customContentData = (CustomContentData) persistedVersion.getContentData();
    assertNotNull(customContentData);
  }
  public void snapshotContent(SnapshotContentParams params) {
    final UserEntity modifier = securityService.getImpersonatedPortalUser();

    SnapshotContentCommand snapshotCommand = new SnapshotContentCommand();
    snapshotCommand.setSnapshotComment(params.snapshotComment);
    snapshotCommand.setSnapshotterKey(modifier.getKey());
    snapshotCommand.setContentKey(new ContentKey(params.contentKey));
    snapshotCommand.setClearCommentInDraft(params.clearCommentInDraft);

    contentService.snapshotContent(snapshotCommand);
  }
  public void assignContent(AssignContentParams params) {
    final UserEntity assigner = securityService.getImpersonatedPortalUser();
    final UserParser userParser =
        new UserParser(
            securityService, userStoreService, userDao, new UserStoreParser(userStoreDao));
    final UserEntity assignee = userParser.parseUser(params.assignee);
    final ContentKey contentToAssignOn = new ContentKey(params.contentKey);

    AssignContentCommand command = new AssignContentCommand();
    command.setContentKey(contentToAssignOn);
    command.setAssignerKey(assigner.getKey());
    command.setAssigneeKey(assignee.getKey());
    command.setAssignmentDueDate(params.assignmentDueDate);
    command.setAssignmentDescription(params.assignmentDescription);
    contentService.assignContent(command);
  }
  @Test
  public void request_user_image() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    UserEntity testUser = fixture.findUserByName("testuser");
    testUser.setPhoto(bytes);

    fixture.flushAndClearHibernateSesssion();

    String imageRequestPath = "_image/user/" + testUser.getKey() + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");
    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  @Before
  public final void startUp() throws Exception {
    this.handler = this.type.newInstance();
    this.request = new DataSourceRequest();
    this.request.setName(this.handler.getName());
    final UserEntity user = new UserEntity();
    user.setKey(new UserKey(User.ANONYMOUS_UID));
    user.setName(User.ANONYMOUS_UID);
    user.setDisplayName(User.ANONYMOUS_UID);
    this.request.setUser(user);
    this.request.setPreviewContext(PreviewContext.NO_PREVIEW);

    this.dummyDoc = XMLDocumentFactory.create("<dummy/>");
    this.dataSourceService = Mockito.mock(DataSourceService.class);

    initTest();
  }
Exemple #8
0
  public UserEntity createUserWithAllValues(
      String uid, String displayName, UserType type, String userStoreName, UserInfo userInfo) {
    UserEntity user = new UserEntity();
    user.setName(uid);
    user.setDisplayName(displayName);
    user.setSyncValue(uid);
    user.setTimestamp(new DateTime());
    user.setType(type);
    user.setDeleted(0);
    if (userStoreName != null) {
      user.setUserStore(fixture.findUserStoreByName(userStoreName));
    }
    user.setUserGroup(null);

    user.setUserFields(new UserInfoTransformer().toUserFields(userInfo));

    return user;
  }
  public int createCategory(CreateCategoryParams params) {
    if (params.contentTypeKey != null && params.contentTypeName != null) {
      throw new ClientException("Specify content type by either key or name, not both");
    }

    if (params.parentCategoryKey == null) {
      throw new ClientException("The parent category must be set");
    }
    CategoryEntity parentCategory =
        categoryDao.findByKey(new CategoryKey(params.parentCategoryKey));
    if (parentCategory == null) {
      throw new ClientException("The parent category does not exist");
    }

    UserEntity runningUser = securityService.getImpersonatedPortalUser();

    CategoryAccessResolver categoryAccessResolver = new CategoryAccessResolver(groupDao);
    if (!categoryAccessResolver.hasAccess(
        runningUser, parentCategory, CategoryAccessType.ADMINISTRATE)) {
      throw new ClientException(
          "The currently logged in user does not have create access on the category");
    }

    ContentTypeKey contentTypeKey = null;
    if (params.contentTypeKey != null) {
      contentTypeKey = new ContentTypeKey(params.contentTypeKey);
    } else if (params.contentTypeName != null) {
      ContentTypeEntity contentType = contentTypeDao.findByName(params.contentTypeName);
      if (contentType == null) {
        throw new ClientException(
            "Specified content type by name '" + params.contentTypeName + "' does not exist");
      }
      contentTypeKey = contentType.getContentTypeKey();
    }

    final StoreNewCategoryCommand command = new StoreNewCategoryCommand();
    command.setCreator(runningUser.getKey());
    command.setName(params.name);
    command.setParentCategory(new CategoryKey(params.parentCategoryKey));
    command.setContentType(contentTypeKey);
    command.setAutoApprove(params.autoApprove);
    final CategoryKey newCategoryKey = categoryService.storeNewCategory(command);
    return newCategoryKey.toInt();
  }
  @Before
  public void before() throws IOException, JDOMException {
    factory = fixture.getFactory();
    fixture.initSystemData();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("127.0.0.1");
    ServletRequestAccessor.setRequest(request);

    createContentTypeXml();

    saveNeededEntities();

    UserEntity runningUser = fixture.findUserByName("testuser");
    PortalSecurityHolder.setImpersonatedUser(runningUser.getKey());

    createUpdateContent();
    createUpdateContentWithBinary();
  }
Exemple #11
0
  @SuppressWarnings("unchecked")
  private void handlerCreateContentWizardStep2(
      HttpServletRequest request,
      HttpServletResponse response,
      ExtendedMap formItems,
      ExtendedMap parameters,
      User user)
      throws VerticalAdminException, VerticalEngineException {

    final UserEntity runningUser = securityService.getUser(user);

    ContentTypeKey contentTypeKey = new ContentTypeKey(formItems.getInt("contenttypekey"));
    CategoryKey topCategoryKey = CategoryKey.parse(formItems.getInt("topcategorykey", -1));

    CategoryXmlCreator xmlCreator = new CategoryXmlCreator();
    xmlCreator.setCategoryAccessResolver(new CategoryAccessResolver(groupDao));
    xmlCreator.setUser(runningUser);
    xmlCreator.setAnonymousUser(securityService.getUser(securityService.getAnonymousUserKey()));
    xmlCreator.setAllowedContentType(contentTypeKey);
    xmlCreator.setIncludeOwnerAndModiferInfo(false);
    xmlCreator.setIncludeCreatedAndTimestampInfo(false);
    xmlCreator.setIncludeAutoApproveInfo(false);
    xmlCreator.setIncludeDescriptionInfo(false);
    xmlCreator.setIncludeSuperCategoryKeyInfo(false);
    xmlCreator.setRootAccess(false);

    List<CategoryEntity> rootCategories;
    if (runningUser.isEnterpriseAdmin()) {
      rootCategories = categoryDao.findRootCategories();
    } else {
      rootCategories = categoryDao.findRootCategories(runningUser.getAllMembershipsGroupKeys());
    }
    final XMLDocument doc = xmlCreator.createCategoryBranch(rootCategories, topCategoryKey);

    parameters.put("step", "2");
    parameters.put("contenttypekey", contentTypeKey);
    parameters.put("source", formItems.getString("source"));
    parameters.put("subop", formItems.getString("subop", ""));
    parameters.put("fieldrow", formItems.getString("fieldrow", ""));
    parameters.put("fieldname", formItems.getString("fieldname", ""));
    transformXML(request, response, doc.getAsJDOMDocument(), "createcontentwizard.xsl", parameters);
  }
  @Test
  public void testUpdateContentDoNotChangeAssignment() {
    // exercise: updateContent

    AssignContentCommand assignContentCommand = new AssignContentCommand();
    assignContentCommand.setAssignerKey(fixture.findUserByName("testuser").getKey());
    assignContentCommand.setAssigneeKey(fixture.findUserByName("testuser").getKey());
    assignContentCommand.setAssignmentDescription("test assignment");
    assignContentCommand.setAssignmentDueDate(new DateTime(2010, 6, 6, 10, 0, 0, 0).toDate());
    assignContentCommand.setContentKey(contentWithBinaryKey);
    contentService.assignContent(assignContentCommand);

    ContentDataInput newContentData = new ContentDataInput("MyContentType");
    newContentData.add(new TextInput("myTitle", "changedtitle"));
    newContentData.add(new BinaryInput("myBinaryfile", dummyBinary, "dummyBinary"));

    UserEntity runningUser = fixture.findUserByName("testuser");
    PortalSecurityHolder.setImpersonatedUser(runningUser.getKey());

    UpdateContentParams params = new UpdateContentParams();
    params.contentKey = contentWithBinaryKey.toInt();
    params.contentData = newContentData;
    params.publishFrom = new Date();
    params.publishTo = null;
    params.createNewVersion = false;
    params.status = ContentStatus.STATUS_DRAFT;
    int contentVersionKey = internalClient.updateContent(params);

    fixture.flushAndClearHibernateSession();

    ContentVersionEntity actualVersion =
        contentVersionDao.findByKey(new ContentVersionKey(contentVersionKey));
    ContentEntity persistedContent = contentDao.findByKey(actualVersion.getContent().getKey());

    assertEquals(runningUser, persistedContent.getAssignee());
    assertEquals(runningUser, persistedContent.getAssigner());
    assertEquals("test assignment", persistedContent.getAssignmentDescription());
    assertEquals(
        new DateTime(2010, 6, 6, 10, 0, 0, 0).toDate(), persistedContent.getAssignmentDueDate());
  }
Exemple #13
0
  public UserEntity createUser(
      String uid, String displayName, UserType type, String userStoreName, GroupEntity group) {
    UserEntity user = new UserEntity();
    user.setName(uid);
    user.setDisplayName(displayName);
    user.setSyncValue(uid);
    user.setTimestamp(new DateTime());
    user.setType(type);
    user.setDeleted(0);
    if (userStoreName != null) {
      user.setUserStore(fixture.findUserStoreByName(userStoreName));
    }
    if (group != null) {
      user.setUserGroup(group);
    }

    return user;
  }
  @Before
  public void setUp() throws Exception {
    final GroupEntity adminGroupEntity = new GroupEntity();
    final GroupKey adminGroupKey = new GroupKey("admin");
    adminGroupEntity.setKey(adminGroupKey);

    final GroupEntity anonGroupEntity = new GroupEntity();
    final GroupKey anonGroupKey = new GroupKey("anonymous");
    anonGroupEntity.setKey(anonGroupKey);

    servlet = new MyPageServlet();
    servlet.groupDao = Mockito.mock(GroupDao.class);
    Mockito.when(servlet.groupDao.findBuiltInAnonymous()).thenReturn(anonGroupEntity);
    Mockito.when(servlet.groupDao.findBuiltInEnterpriseAdministrator())
        .thenReturn(adminGroupEntity);

    final GroupEntity groupEntity = new GroupEntity();
    final GroupKey groupKey = new GroupKey("user_group");
    groupEntity.setKey(groupKey);

    userEntity = new UserEntity();
    userEntity.setName("user");
    userEntity.setType(UserType.NORMAL);
    userEntity.setUserGroup(groupEntity);

    final Map<GroupKey, CategoryAccessEntity> accessRights =
        new HashMap<GroupKey, CategoryAccessEntity>();

    accessEntity = new CategoryAccessEntity();
    accessRights.put(groupKey, accessEntity);

    contentType = Mockito.mock(ContentTypeEntity.class);

    final CategoryEntity categoryEntity = new CategoryEntity();
    categoryEntity.setAccessRights(accessRights);
    Mockito.when(contentType.getCategories(false))
        .thenReturn(Arrays.<CategoryEntity>asList(categoryEntity));
  }
  private void doExecuteSetContentHomeCommand(final SetContentHomeCommand command) {
    Preconditions.checkNotNull(command.getSetter(), "setter cannot be null");
    Preconditions.checkNotNull(command.getContent(), "content to set home for cannot be null");
    Preconditions.checkNotNull(command.getSection(), "section to set home to cannot be null");

    final UserEntity setter = doResolveUser(command.getSetter(), "setter");
    final ContentEntity content = contentDao.findByKey(command.getContent());
    final MenuItemEntity section = doResolveSection(command.getSection());

    final CategoryAccessResolver categoryAccessResolver = new CategoryAccessResolver(groupDao);
    if (!categoryAccessResolver.hasApproveContentAccess(setter, content.getCategory())) {
      throw new CategoryAccessException(
          "Cannot set content home",
          setter.getQualifiedName(),
          CategoryAccessType.APPROVE,
          content.getCategory().getKey());
    }

    PageTemplateEntity pageTemplate = null;
    if (command.getPageTemplate() != null) {
      pageTemplate = pageTemplateDao.findByKey(command.getPageTemplate().toInt());
    }
    doSetContentHome(content, section, pageTemplate);
  }
  private void doArchiveContent(
      UserEntity importer, ContentKey contentKey, ImportResult importResult) {
    final ContentEntity content = contentDao.findByKey(contentKey);

    if (content == null) {
      return;
    }

    boolean contentArchived = contentStorer.archiveMainVersion(importer, content);
    if (contentArchived) {
      importResult.addArchived(content);

      UnassignContentCommand unassignContentCommand = new UnassignContentCommand();
      unassignContentCommand.setContentKey(content.getKey());
      unassignContentCommand.setUnassigner(importer.getKey());
      contentStorer.unassignContent(unassignContentCommand);
    } else {
      importResult.addAlreadyArchived(content);
    }
  }
  @Test
  public void testCreateContentWithBlockGroup() {
    fixture.createAndStoreUserAndUserGroup(
        "testuser", "testuser fullname", UserType.NORMAL, "testuserstore");

    fixture.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));

    // setup content type
    ContentTypeConfigBuilder ctyconf = new ContentTypeConfigBuilder("Skole", "tittel");
    ctyconf.startBlock("Skole");
    ctyconf.addInput("tittel", "text", "contentdata/tittel", "Tittel", true);
    ctyconf.endBlock();
    ctyconf.startBlock("Elever", "contentdata/elever");
    ctyconf.addInput("elev-navn", "text", "navn", "Navn");
    ctyconf.addInput("elev-karakter", "text", "karakter", "Karakter");
    ctyconf.endBlock();
    ctyconf.startBlock("Laerere", "contentdata/laerere");
    ctyconf.addInput("laerer-navn", "text", "navn", "Navn");
    ctyconf.addInput("laerer-karakter", "text", "karakter", "Karakter");
    ctyconf.endBlock();
    Document configAsXmlBytes = XMLDocumentFactory.create(ctyconf.toString()).getAsJDOMDocument();
    fixture.save(
        factory.createContentType(
            "Skole", ContentHandlerName.CUSTOM.getHandlerClassShortName(), configAsXmlBytes));

    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(factory.createCategory("Skole", "Skole", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("Skole", "testuser", "read,create,approve"));

    UserEntity runningUser = fixture.findUserByName("testuser");
    PortalSecurityHolder.setImpersonatedUser(runningUser.getKey());

    CreateContentParams content = new CreateContentParams();
    content.categoryKey = fixture.findCategoryByName("Skole").getKey().toInt();
    content.publishFrom = new Date();
    content.status = ContentStatus.STATUS_APPROVED;

    ContentDataInput contentData = new ContentDataInput("Skole");
    contentData.add(new TextInput("tittel", "St. Olav Videregaende skole"));

    GroupInput groupInputElev1 = contentData.addGroup("Elever");
    groupInputElev1.add(new TextInput("elev-navn", "Vegar Jansen"));
    groupInputElev1.add(new TextInput("elev-karakter", "S"));

    GroupInput groupInputElev2 = contentData.addGroup("Elever");
    groupInputElev2.add(new TextInput("elev-navn", "Thomas Sigdestad"));
    groupInputElev2.add(new TextInput("elev-karakter", "M"));

    GroupInput groupInputLaerer1 = contentData.addGroup("Laerere");
    groupInputLaerer1.add(new TextInput("laerer-navn", "Mutt Hansen"));
    groupInputLaerer1.add(new TextInput("laerer-karakter", "LG"));

    GroupInput groupInputLaerer2 = contentData.addGroup("Laerere");
    groupInputLaerer2.add(new TextInput("laerer-navn", "Striks Jansen"));
    groupInputLaerer2.add(new TextInput("laerer-karakter", "M"));

    content.contentData = contentData;
    ContentKey contentKey = new ContentKey(internalClient.createContent(content));

    ContentEntity createdContent = fixture.findContentByKey(contentKey);
    ContentVersionEntity createdVersion = createdContent.getMainVersion();
    CustomContentData createdContentData = (CustomContentData) createdVersion.getContentData();
    BlockGroupDataEntries elever = createdContentData.getBlockGroupDataEntries("Elever");

    GroupDataEntry elev1 = elever.getGroupDataEntry(1);
    assertEquals("Vegar Jansen", ((TextDataEntry) elev1.getEntry("elev-navn")).getValue());
    assertEquals("S", ((TextDataEntry) elev1.getEntry("elev-karakter")).getValue());

    GroupDataEntry elev2 = elever.getGroupDataEntry(2);
    assertEquals("Thomas Sigdestad", ((TextDataEntry) elev2.getEntry("elev-navn")).getValue());
    assertEquals("M", ((TextDataEntry) elev2.getEntry("elev-karakter")).getValue());

    BlockGroupDataEntries laerere = createdContentData.getBlockGroupDataEntries("Laerere");

    GroupDataEntry laerer1 = laerere.getGroupDataEntry(1);
    assertEquals("Mutt Hansen", ((TextDataEntry) laerer1.getEntry("laerer-navn")).getValue());
    assertEquals("LG", ((TextDataEntry) laerer1.getEntry("laerer-karakter")).getValue());

    GroupDataEntry laerer2 = laerere.getGroupDataEntry(2);
    assertEquals("Striks Jansen", ((TextDataEntry) laerer2.getEntry("laerer-navn")).getValue());
    assertEquals("M", ((TextDataEntry) laerer2.getEntry("laerer-karakter")).getValue());
  }
Exemple #18
0
 public ContentAccessEntity createContentAccess(
     ContentKey contentKey, UserEntity user, String accesses) {
   return createContentAccess(contentKey, user.getUserGroup(), accesses);
 }
Exemple #19
0
 public CategoryAccessEntity createCategoryAccessForUser(
     String categoryName, String userName, String accesses) {
   UserEntity user = fixture.findUserByName(userName);
   return createCategoryAccess(categoryName, user.getUserGroup(), accesses);
 }
Exemple #20
0
 public CategoryAccessEntity createCategoryAccess(
     String categoryName, UserEntity user, String accesses) {
   return createCategoryAccess(categoryName, user.getUserGroup(), accesses);
 }
Exemple #21
0
 public MenuItemAccessEntity createMenuItemAccess(
     MenuItemEntity menuItem, UserEntity user, String accesses) {
   return createMenuItemAccess(menuItem, user.getUserGroup(), accesses);
 }