/** * Gets topic with specified type by id * * @param id id of interested topic * @param type type of interested topic * * @return topic with specified id and type * @throws NotFoundException if topic with specified id is not found or has different type */ public Topic get(Long id, String type) throws NotFoundException { Topic topic = topicFetchService.get(id); if (!topic.getType().equals(type)) { throw new NotFoundException(); } return topic; }
/** * {@inheritDoc} */ @Override public void filter(Collection<JCUser> users, SubscriptionAwareEntity entity) { if (entity instanceof Post && ((Post)entity).getTopic().getType().equals(QuestionsAndAnswersPlugin.TOPIC_TYPE)) { JCUser postCreator = ((Post)entity).getUserCreated(); boolean containsCreator = users.contains(postCreator); users.clear(); if (containsCreator) { users.add(((Post) entity).getUserCreated()); } } } }
/** * Creates topic dto depends of topic type and state of plugin which provides this topic * * @param topic {@link Topic} entity * @param plugins list of enabled topic plugins * * @return topic dto */ private TopicDto createTopicDto(Topic topic, List<Plugin> plugins) { TopicDto dto = new TopicDto(topic); if (topic.isPlugable()) { for (Plugin plugin : plugins) { TopicPlugin topicPlugin = (TopicPlugin) plugin; if (topicPlugin.getTopicType().equals(topic.getType())) { return populatePlugableTopicDto(dto, topicPlugin); } } } return populateCoreTopicDto(dto); }
/** * Checks for plugable topic if current user is granted to create topics with type. * * @param topic topic to be checked * @throws AccessDeniedException if user not granted to create current topic type * or if type of current topic is unknown */ private void assertCreationAllowedForPlugableTopic(Topic topic) { if (topic.isPlugable()) { Authentication auth = securityContextFacade.getContext().getAuthentication(); List<Plugin> topicPlugins = pluginLoader.getPlugins(new TypeFilter(TopicPlugin.class), new StateFilter(Plugin.State.ENABLED)); boolean pluginFound = false; for (Plugin plugin : topicPlugins) { TopicPlugin topicPlugin = (TopicPlugin)plugin; if (topicPlugin.getTopicType().equals(topic.getType())) { pluginFound = true; if (!permissionEvaluator.hasPermission(auth, topic.getBranch().getId(), "BRANCH", topicPlugin.getCreateTopicPermission())) { throw new AccessDeniedException("Creating of topic with type " + topic.getType() + " is forbidden"); } } } if (!pluginFound) { throw new AccessDeniedException("Creating of unknown topic type is forbidden"); } } }
/** * Checks if current user can create comments in specified plugable topic * * @param topic plugable topic to check permission * @throws AccessDeniedException if user not granted to create comments in specified topic type or if type of * current topic is unknown */ private void assertCommentsAllowedForPlugableTopic(Topic topic) { List<Plugin> topicPlugins = pluginLoader.getPlugins(new TypeFilter(TopicPlugin.class), new StateFilter(Plugin.State.ENABLED)); boolean pluginFound = false; for (Plugin plugin : topicPlugins) { TopicPlugin topicPlugin = (TopicPlugin)plugin; if (topicPlugin.getTopicType().equals(topic.getType())) { pluginFound = true; permissionService.checkPermission(topic.getBranch().getId(), AclClassName.BRANCH, topicPlugin.getCommentPermission()); break; } } if (!pluginFound) { throw new AccessDeniedException("Creation of comments not allowed for unknown topic type"); } } }
topic.setType(topicDto.getType()); topic.addPost(first); topic.setBranch(branch);