Skip to content

Commit

Permalink
-Add the newly added user to existing comment threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayan committed Jun 21, 2021
1 parent 655918f commit 0383c7b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.appsmith.server.events;

import com.appsmith.server.domains.UserRole;
import lombok.Data;

@Data
public class UserAddedToOrganization {
private final String organizationId;
private final UserRole userRole;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import com.appsmith.server.domains.NewAction;
import com.appsmith.server.domains.NewPage;
import com.appsmith.server.domains.User;
import com.appsmith.server.domains.UserRole;
import com.appsmith.server.repositories.ApplicationRepository;
import com.appsmith.server.repositories.DatasourceRepository;
import com.appsmith.server.repositories.NewActionRepository;
import com.appsmith.server.repositories.NewPageRepository;
import com.appsmith.server.solutions.UserChangedHandler;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
Expand All @@ -32,25 +35,15 @@
import static com.appsmith.server.acl.AclPermission.MANAGE_DATASOURCES;

@Component
@AllArgsConstructor
public class PolicyUtils {

private final PolicyGenerator policyGenerator;
private final ApplicationRepository applicationRepository;
private final DatasourceRepository datasourceRepository;
private final NewPageRepository newPageRepository;
private final NewActionRepository newActionRepository;

public PolicyUtils(PolicyGenerator policyGenerator,
ApplicationRepository applicationRepository,
DatasourceRepository datasourceRepository,
NewPageRepository newPageRepository,
NewActionRepository newActionRepository) {
this.policyGenerator = policyGenerator;
this.applicationRepository = applicationRepository;
this.datasourceRepository = datasourceRepository;
this.newPageRepository = newPageRepository;
this.newActionRepository = newActionRepository;
}
private final UserChangedHandler userChangedHandler;

public <T extends BaseDomain> T addPoliciesToExistingObject(Map<String, Policy> policyMap, T obj) {
// Making a deep copy here so we don't modify the `policyMap` object.
Expand Down Expand Up @@ -202,6 +195,13 @@ public Flux<Application> updateWithNewPoliciesToApplicationsByOrgId(String orgId
.flatMapMany(updatedApplications -> applicationRepository.saveAll(updatedApplications));
}

public Flux<UserRole> updateCommentThreadPoliciesByOrgId(String orgId, List<UserRole> userRoles) {
for(UserRole userRole : userRoles) {
UserRole publish = userChangedHandler.publish(orgId, userRole);
}
return Flux.fromIterable(userRoles);
}

public Flux<NewPage> updateWithApplicationPermissionsToAllItsPages(String applicationId, Map<String, Policy> newPagePoliciesMap, boolean addPolicyToObject) {

// Instead of fetching pages from the application object, we fetch pages from the page repository. This ensures that all the published
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.domains.CommentThread;
import com.mongodb.client.result.UpdateResult;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface CustomCommentThreadRepository extends AppsmithRepository<CommentThread> {

Flux<CommentThread> findByApplicationId(String applicationId, AclPermission permission);

Mono<UpdateResult> updatePolicyUsers(String applicationId, AclPermission permission, String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.domains.CommentThread;
import com.appsmith.server.domains.QCommentThread;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

Expand All @@ -28,4 +32,13 @@ public Flux<CommentThread> findByApplicationId(String applicationId, AclPermissi
return queryAll(List.of(criteria), permission);
}

@Override
public Mono<UpdateResult> updatePolicyUsers(String applicationId, AclPermission permission,
String username) {
Criteria criteria = where(fieldName(QCommentThread.commentThread.applicationId)).is(applicationId);
Update update = new Update()
.addToSet("policies.$[element].users", username)
.filterArray(where("element.permission").is(permission.getValue()));
return this.mongoOperations.updateMulti(Query.query(criteria), update, CommentThread.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,13 @@ public Mono<Organization> bulkAddUsersToOrganization(Organization organization,
.flatMap(application -> policyUtils.updateWithApplicationPermissionsToAllItsPages(application.getId(), pagePolicyMap, true));
Flux<NewAction> updatedActionsFlux = updatedApplicationsFlux
.flatMap(application -> policyUtils.updateWithPagePermissionsToAllItsActions(application.getId(), actionPolicyMap, true));
Flux<UserRole> commentThreadPoliciesFlux = policyUtils.updateCommentThreadPoliciesByOrgId(organization.getId(), userRoles);

return Mono.when(updatedDatasourcesFlux.collectList(), updatedPagesFlux.collectList(), updatedActionsFlux.collectList())
return Mono.when(
updatedDatasourcesFlux.collectList(),
updatedPagesFlux.collectList(),
updatedActionsFlux.collectList(),
commentThreadPoliciesFlux)
//By now all the datasources/applications/pages/actions have been updated. Just save the organization now
.then(organizationRepository.save(updatedOrganization));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,9 @@ public Mono<List<User>> inviteUsers(InviteUsersDTO inviteUsersDTO, String origin
.flatMap(tuple -> {
List<User> invitedUsers = tuple.getT1();
Organization organization = tuple.getT2();

return userOrganizationService.bulkAddUsersToOrganization(organization, invitedUsers, inviteUsersDTO.getRoleName());
return userOrganizationService.bulkAddUsersToOrganization(
organization, invitedUsers, inviteUsersDTO.getRoleName()
);
});

// Add organization id to each invited user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.appsmith.server.solutions;

import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.domains.User;
import com.appsmith.server.domains.UserRole;
import com.appsmith.server.events.UserAddedToOrganization;
import com.appsmith.server.events.UserChangedEvent;
import com.appsmith.server.repositories.ApplicationRepository;
import com.appsmith.server.repositories.CommentRepository;
import com.appsmith.server.repositories.CommentThreadRepository;
import com.appsmith.server.repositories.OrganizationRepository;
import com.mongodb.client.result.UpdateResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -19,15 +25,21 @@
public class UserChangedHandler {

private final ApplicationEventPublisher applicationEventPublisher;

private final CommentRepository commentRepository;
private final CommentThreadRepository commentThreadRepository;
private final OrganizationRepository organizationRepository;
private final ApplicationRepository applicationRepository;

public User publish(User user) {
applicationEventPublisher.publishEvent(new UserChangedEvent(user));
return user;
}

public UserRole publish(String organizationId, UserRole userRole) {
applicationEventPublisher.publishEvent(new UserAddedToOrganization(organizationId, userRole));
return userRole;
}

@Async
@EventListener
public void handle(UserChangedEvent event) {
Expand All @@ -43,6 +55,21 @@ public void handle(UserChangedEvent event) {
.subscribe();
}

@Async
@EventListener
public void handle(UserAddedToOrganization event) {
log.debug("Handling user added to organization {} changes {}", event.getOrganizationId(), event.getUserRole());
applicationRepository.findByOrganizationId(event.getOrganizationId()).flatMap(application -> {
Mono<UpdateResult> updateResultMono1 = commentThreadRepository.updatePolicyUsers(
application.getId(), AclPermission.COMMENT_ON_THREAD, event.getUserRole().getUsername()
);
Mono<UpdateResult> updateResultMono2 = commentThreadRepository.updatePolicyUsers(
application.getId(), AclPermission.READ_THREAD, event.getUserRole().getUsername()
);
return Mono.zip(updateResultMono1, updateResultMono2);
}).subscribeOn(Schedulers.elastic()).subscribe();
}

private Mono<Void> updateNameInComments(User user) {
if (user.getId() == null) {
log.warn("Attempt to update name in comments for user with null ID.");
Expand Down

0 comments on commit 0383c7b

Please sign in to comment.