Removed the usage of get current organization id. Requires addition of organization id to a few api calls.

This commit is contained in:
Trisha Anand 2020-06-08 06:45:04 +00:00
parent 1923f0ddea
commit a31796a216
9 changed files with 81 additions and 102 deletions

View File

@ -9,5 +9,7 @@ public class PluginOrgDTO {
String pluginId;
String organizationId;
OrganizationPluginStatus status;
}

View File

@ -521,35 +521,24 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
actionExample.setPageId(params.getFirst(FieldName.PAGE_ID));
}
Mono<String> orgIdMono = sessionUserService
.getCurrentUser()
.map(user -> user.getCurrentOrganizationId());
if (params.getFirst(FieldName.APPLICATION_ID) != null) {
return orgIdMono
.flatMapMany(orgId -> pageService
.findNamesByApplicationId(params.getFirst(FieldName.APPLICATION_ID))
.switchIfEmpty(Mono.error(new AppsmithException(
AppsmithError.NO_RESOURCE_FOUND, "pages for application", params.getFirst(FieldName.APPLICATION_ID)))
)
.map(applicationPagesDTO -> applicationPagesDTO.getPages())
.flatMapMany(Flux::fromIterable)
.map(pageNameIdDTO -> {
Action example = new Action();
example.setPageId(pageNameIdDTO.getId());
example.setOrganizationId(orgId);
return example;
})
.flatMap(example -> repository.findAll(Example.of(example), sort))
)
return pageService
.findNamesByApplicationId(params.getFirst(FieldName.APPLICATION_ID))
.switchIfEmpty(Mono.error(new AppsmithException(
AppsmithError.NO_RESOURCE_FOUND, "pages for application", params.getFirst(FieldName.APPLICATION_ID)))
)
.map(applicationPagesDTO -> applicationPagesDTO.getPages())
.flatMapMany(Flux::fromIterable)
.map(pageNameIdDTO -> {
Action example = new Action();
example.setPageId(pageNameIdDTO.getId());
return example;
})
.flatMap(example -> repository.findAll(Example.of(example), sort))
.flatMap(this::setTransientFieldsInAction);
}
return orgIdMono
.flatMapMany(orgId -> {
actionExample.setOrganizationId(orgId);
return repository.findAll(Example.of(actionExample), sort);
})
.flatMap(this::setTransientFieldsInAction);
return repository.findAll(Example.of(actionExample), sort)
.flatMap(this::setTransientFieldsInAction);
}
private ActionConfiguration updateActionConfigurationForPagination(ActionConfiguration actionConfiguration,

View File

@ -9,8 +9,6 @@ public interface ApplicationPageService {
Mono<Application> addPageToApplication(Mono<Application> applicationMono, Page page, Boolean isDefault);
Mono<Page> doesPageBelongToCurrentUserOrganization(Page page);
Mono<Page> getPage(String pageId, Boolean viewMode);
Mono<Application> createApplication(Application application, String orgId);

View File

@ -117,21 +117,6 @@ public class ApplicationPageServiceImpl implements ApplicationPageService {
.flatMap(applicationService::save);
}
public Mono<Page> doesPageBelongToCurrentUserOrganization(Page page) {
Mono<User> userMono = sessionUserService.getCurrentUser();
final String[] username = {null};
return userMono
.map(user -> {
username[0] = user.getEmail();
return user;
})
.flatMap(user -> applicationService.findByIdAndOrganizationId(page.getApplicationId(), user.getCurrentOrganizationId()))
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.PAGE_DOESNT_BELONG_TO_USER_ORGANIZATION, page.getId(), username[0])))
//If mono transmits, then application id belongs to the current user's organization. Return page.
.then(Mono.just(page));
}
public Mono<Page> getPage(String pageId, Boolean viewMode) {
return pageService.findById(pageId, READ_PAGES)
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.PAGE_ID)))

View File

@ -243,12 +243,17 @@ public class DatasourceServiceImpl extends BaseService<DatasourceRepository, Dat
return new HashSet<>();
}
@Override
public Flux<Datasource> get(MultiValueMap<String, String> params) {
/**
* Note : Currently this API is ONLY used to fetch datasources for an organization.
*/
if (params.getFirst(FieldName.ORGANIZATION_ID) != null) {
return repository.findAllByOrganizationId(params.getFirst(FieldName.ORGANIZATION_ID), AclPermission.READ_DATASOURCES);
}
return sessionUserService
.getCurrentUser()
.flatMapMany(user -> repository.findAllByOrganizationId(user.getCurrentOrganizationId(), AclPermission.READ_DATASOURCES));
return Flux.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ORGANIZATION_ID));
}
@Override

View File

@ -75,8 +75,6 @@ public class LayoutServiceImpl implements LayoutService {
public Mono<Layout> getLayout(String pageId, String layoutId, Boolean viewMode) {
return pageService.findByIdAndLayoutsId(pageId, layoutId)
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.PAGE_ID + " or " + FieldName.LAYOUT_ID)))
.flatMap(applicationPageService::doesPageBelongToCurrentUserOrganization)
//The pageId given is correct and belongs to the current user's organization.
.map(page -> {
List<Layout> layoutList = page.getLayouts();
//Because the findByIdAndLayoutsId call returned non-empty result, we are guaranteed to find the layoutId here.

View File

@ -5,7 +5,6 @@ import com.appsmith.server.domains.Organization;
import com.appsmith.server.domains.OrganizationPlugin;
import com.appsmith.server.domains.Plugin;
import com.appsmith.server.domains.PluginType;
import com.appsmith.server.domains.User;
import com.appsmith.server.dtos.InstallPluginRedisDTO;
import com.appsmith.server.dtos.OrganizationPluginStatus;
import com.appsmith.server.dtos.PluginOrgDTO;
@ -84,36 +83,40 @@ public class PluginServiceImpl extends BaseService<PluginRepository, Plugin, Str
@Override
public Flux<Plugin> get(MultiValueMap<String, String> params) {
return sessionUserService.getCurrentUser()
.flatMapMany(user -> {
log.debug("Going to filter plugin params for user: {}", user.getEmail());
return organizationService.findById(user.getCurrentOrganizationId())
.flatMapMany(org -> {
log.debug("Fetching plugins by params: {} for org: {}", params, org.getName());
if (org.getPlugins() == null) {
log.debug("Null installed plugins found for org: {}. Return empty plugins", org.getName());
return Flux.fromIterable(new ArrayList<>());
}
String organizationId = params.getFirst(FieldName.ORGANIZATION_ID);
if (organizationId == null) {
return Flux.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ORGANIZATION_ID));
}
List<String> pluginIds = org.getPlugins()
.stream()
.map(obj -> obj.getPluginId())
.collect(Collectors.toList());
Query query = new Query();
query.addCriteria(Criteria.where(FieldName.ID).in(pluginIds));
// TODO : Think about the various scenarios where this plugin api is called and then decide on permissions.
Mono<Organization> organizationMono = organizationService.findById(organizationId);
if (params.getFirst(FieldName.TYPE) != null) {
try {
PluginType pluginType = PluginType.valueOf(params.getFirst(FieldName.TYPE));
query.addCriteria(Criteria.where(FieldName.TYPE).is(pluginType));
} catch (IllegalArgumentException e) {
log.error("No plugins for type : {}", params.getFirst(FieldName.TYPE));
List<Plugin> emptyPlugins = new ArrayList<>();
return Flux.fromIterable(emptyPlugins);
}
}
return mongoTemplate.find(query, Plugin.class);
});
return organizationMono
.flatMapMany(org -> {
log.debug("Fetching plugins by params: {} for org: {}", params, org.getName());
if (org.getPlugins() == null) {
log.debug("Null installed plugins found for org: {}. Return empty plugins", org.getName());
return Flux.fromIterable(new ArrayList<>());
}
List<String> pluginIds = org.getPlugins()
.stream()
.map(obj -> obj.getPluginId())
.collect(Collectors.toList());
Query query = new Query();
query.addCriteria(Criteria.where(FieldName.ID).in(pluginIds));
if (params.getFirst(FieldName.TYPE) != null) {
try {
PluginType pluginType = PluginType.valueOf(params.getFirst(FieldName.TYPE));
query.addCriteria(Criteria.where(FieldName.TYPE).is(pluginType));
} catch (IllegalArgumentException e) {
log.error("No plugins for type : {}", params.getFirst(FieldName.TYPE));
List<Plugin> emptyPlugins = new ArrayList<>();
return Flux.fromIterable(emptyPlugins);
}
}
return mongoTemplate.find(query, Plugin.class);
});
}
@ -137,9 +140,11 @@ public class PluginServiceImpl extends BaseService<PluginRepository, Plugin, Str
if (pluginOrgDTO.getPluginId() == null) {
return Mono.error(new AppsmithException(AppsmithError.PLUGIN_ID_NOT_GIVEN));
}
if (pluginOrgDTO.getOrganizationId() == null) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ORGANIZATION_ID));
}
return Mono.just(pluginOrgDTO)
.flatMap(plugin -> storeOrganizationPlugin(plugin, pluginOrgDTO.getStatus()))
return storeOrganizationPlugin(pluginOrgDTO, pluginOrgDTO.getStatus())
.switchIfEmpty(Mono.empty());
}
@ -148,47 +153,46 @@ public class PluginServiceImpl extends BaseService<PluginRepository, Plugin, Str
if (pluginDTO.getPluginId() == null) {
return Mono.error(new AppsmithException(AppsmithError.PLUGIN_ID_NOT_GIVEN));
}
if (pluginDTO.getOrganizationId() == null) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ORGANIZATION_ID));
}
//Find the organization using id and plugin id -> This is to find if the organization has the plugin installed
Mono<User> userMono = sessionUserService.getCurrentUser();
Mono<Organization> organizationMono = userMono.flatMap(user ->
organizationService.findByIdAndPluginsPluginId(user.getCurrentOrganizationId(), pluginDTO.getPluginId()));
Mono<Organization> organizationMono = organizationService.findByIdAndPluginsPluginId(pluginDTO.getOrganizationId(),
pluginDTO.getPluginId());
return organizationMono
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.PLUGIN_NOT_INSTALLED, pluginDTO.getPluginId())))
//In case the plugin is not found for the organization, the organizationMono would not emit and the rest of the flow would stop
//i.e. the rest of the code flow would only happen when there is a plugin found for the organization that can
//be uninstalled.
.map(organization -> {
.flatMap(organization -> {
List<OrganizationPlugin> organizationPluginList = organization.getPlugins();
organizationPluginList.removeIf(listPlugin -> listPlugin.getPluginId().equals(pluginDTO.getPluginId()));
organization.setPlugins(organizationPluginList);
return organization;
})
.flatMap(organizationService::save);
return organizationService.save(organization);
});
}
private Mono<Organization> storeOrganizationPlugin(PluginOrgDTO pluginDTO, OrganizationPluginStatus status) {
//Find the organization using id and plugin id -> This is to find if the organization already has the plugin installed
Mono<User> userMono = sessionUserService.getCurrentUser();
Mono<Organization> pluginInOrganizationMono = userMono.flatMap(user ->
organizationService.findByIdAndPluginsPluginId(user.getCurrentOrganizationId(), pluginDTO.getPluginId()));
Mono<Organization> pluginInOrganizationMono = organizationService
.findByIdAndPluginsPluginId(pluginDTO.getOrganizationId(), pluginDTO.getPluginId());
//If plugin is already present for the organization, just return the organization, else install and return organization
return pluginInOrganizationMono
.switchIfEmpty(Mono.defer(() -> {
log.debug("Plugin {} not already installed. Running the switch if empty code block", pluginDTO.getPluginId());
log.debug("Plugin {} not already installed. Installing now", pluginDTO.getPluginId());
//If the plugin is not found in the organization, its not installed already. Install now.
return repository
.findById(pluginDTO.getPluginId())
.zipWith(userMono, (plugin, user) -> {
.map(plugin -> {
log.debug("Before publishing to the redis queue");
//Publish the event to the pub/sub queue
InstallPluginRedisDTO installPluginRedisDTO = new InstallPluginRedisDTO();
installPluginRedisDTO.setOrganizationId(user.getCurrentOrganizationId());
installPluginRedisDTO.setOrganizationId(pluginDTO.getOrganizationId());
installPluginRedisDTO.setPluginOrgDTO(pluginDTO);
String jsonString;
try {
@ -202,13 +206,12 @@ public class PluginServiceImpl extends BaseService<PluginRepository, Plugin, Str
.subscribe();
})
//Now that the plugin jar has been successfully downloaded, go on and add the plugin to the organization
.then(userMono)
.flatMap(user -> organizationService.findById(user.getCurrentOrganizationId()))
.map(organization -> {
.then(organizationService.findById(pluginDTO.getOrganizationId()))
.flatMap(organization -> {
List<OrganizationPlugin> organizationPluginList = organization.getPlugins();
if (organizationPluginList == null) {
organizationPluginList = new ArrayList<OrganizationPlugin>();
organizationPluginList = new ArrayList<>();
}
OrganizationPlugin organizationPlugin = new OrganizationPlugin();
@ -219,9 +222,8 @@ public class PluginServiceImpl extends BaseService<PluginRepository, Plugin, Str
log.debug("Going to save the organization with install plugin. This means that installation has been successful");
return organization;
})
.flatMap(organizationService::save);
return organizationService.save(organization);
});
}));
}

View File

@ -461,7 +461,7 @@ public class UserServiceImpl extends BaseService<UserRepository, User, String> i
* platform. This flow also ensures that a personal workspace name is created for the user. The new user is then
* given admin permissions to the personal workspace.
* <p>
* For new user invite flow, please {@link UserOrganizationService#inviteUserNew(InviteUserDTO, String)}
* For new user invite flow, please {@link UserService#inviteUser(InviteUserDTO, String)}
*
* @param user
* @return

View File

@ -38,5 +38,5 @@ spring.mail.host=email-smtp.us-east-1.amazonaws.com
spring.mail.port=587
spring.mail.username=AKIAVWHAAGIQOHPT4BZ7
spring.mail.password=BEE5W6i7YznAJ/YDOLbppovmOlRzxXElJ+uJtGhdCfjY
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=true