Skip to content

Commit

Permalink
LPD-25508 Clear threadlocal before company creation
Browse files Browse the repository at this point in the history
Since we are now running the company creation during initial request,
the PrincipalThreadLocal and PermissionThreadLocal may be populated
with a user from an already existing company.
  • Loading branch information
ericyanLr authored and brianchandotcom committed Oct 3, 2024
1 parent d0732f9 commit 99884e1
Showing 1 changed file with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.module.framework.ModuleServiceLifecycle;
import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.kernel.servlet.InitialRequestSyncUtil;
import com.liferay.portal.kernel.util.GetterUtil;
Expand Down Expand Up @@ -49,45 +52,61 @@ protected void activate(Map<String, Object> properties) {
int maxUsers = portalInstancesConfiguration.maxUsers();
boolean active = portalInstancesConfiguration.active();

Company company = null;
String name = PrincipalThreadLocal.getName();
PermissionChecker permissionChecker =
PermissionThreadLocal.getPermissionChecker();

try {
company = _companyLocalService.getCompanyByWebId(webId);
}
catch (NoSuchCompanyException noSuchCompanyException) {
if (_log.isDebugEnabled()) {
_log.debug(noSuchCompanyException);
PermissionThreadLocal.setPermissionChecker(null);
PrincipalThreadLocal.setName(null);

Company company = null;

try {
company = _companyLocalService.getCompanyByWebId(webId);
}
catch (NoSuchCompanyException noSuchCompanyException) {
if (_log.isDebugEnabled()) {
_log.debug(noSuchCompanyException);
}
}
}

if (company == null) {
PortalInstances.addCompany(
portalInstancesConfiguration.siteInitializerKey(),
() -> _companyLocalService.addCompany(
null, webId, virtualHostname, mx, maxUsers,
portalInstancesConfiguration.active(),
portalInstancesConfiguration.addDefaultAdminUser(),
portalInstancesConfiguration.adminPassword(),
portalInstancesConfiguration.adminScreenName(),
portalInstancesConfiguration.adminEmailAddress(),
portalInstancesConfiguration.adminFirstName(),
portalInstancesConfiguration.adminMiddleName(),
portalInstancesConfiguration.adminLastName()));
}
else {
if (company.getCompanyId() ==
_portalInstancesLocalService.
getDefaultCompanyId()) {
if (company == null) {
PortalInstances.addCompany(
portalInstancesConfiguration.siteInitializerKey(),
() -> _companyLocalService.addCompany(
null, webId, virtualHostname, mx, maxUsers,
portalInstancesConfiguration.active(),
portalInstancesConfiguration.
addDefaultAdminUser(),
portalInstancesConfiguration.adminPassword(),
portalInstancesConfiguration.adminScreenName(),
portalInstancesConfiguration.
adminEmailAddress(),
portalInstancesConfiguration.adminFirstName(),
portalInstancesConfiguration.adminMiddleName(),
portalInstancesConfiguration.adminLastName()));
}
else {
if (company.getCompanyId() ==
_portalInstancesLocalService.
getDefaultCompanyId()) {

active = true;
}

active = true;
_companyLocalService.updateCompany(
company.getCompanyId(), virtualHostname, mx,
maxUsers, active);
}

_companyLocalService.updateCompany(
company.getCompanyId(), virtualHostname, mx, maxUsers,
active);
_portalInstancesLocalService.synchronizePortalInstances();
}
finally {
PermissionThreadLocal.setPermissionChecker(
permissionChecker);
PrincipalThreadLocal.setName(name);
}

_portalInstancesLocalService.synchronizePortalInstances();

return null;
});
Expand Down

0 comments on commit 99884e1

Please sign in to comment.