Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Oidc preferred username #3629

Merged
merged 42 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
55e471e
table Users add column PreferredUsername
vdiskg Apr 3, 2021
e97c949
Users add PreferredUsername sql
vdiskg Apr 3, 2021
48ef240
Users add PreferredUsername page
vdiskg Apr 3, 2021
a9d4510
Users add PreferredUsername app page
vdiskg Apr 3, 2021
1561c5e
update user's info after login
vdiskg Apr 4, 2021
6487608
sql
vdiskg Apr 4, 2021
0f7f5e9
fix unit test
vdiskg Apr 4, 2021
20f8f39
change the page
vdiskg Apr 5, 2021
723e068
fix the unit test timeout
vdiskg Apr 5, 2021
db6afc9
commit page add preferred username
vdiskg Apr 5, 2021
1d4f575
namespace page add preferred username
vdiskg Apr 5, 2021
7850814
release history page add preferred username
vdiskg Apr 5, 2021
4f477e7
preferred username sql
vdiskg Apr 5, 2021
106d1f8
move logic to service
vdiskg Apr 10, 2021
abe329b
fix unit test time out
vdiskg Apr 10, 2021
4d074e8
namespaceService unit test
vdiskg Apr 10, 2021
21d928a
Merge branch 'master' into oidc-preferred-username
vdiskg Apr 10, 2021
5fd63e2
enricher
vdiskg Apr 17, 2021
148289e
name space enrich change
vdiskg Apr 17, 2021
a61e796
CommitService enrich change
vdiskg Apr 17, 2021
78da8f1
revert NamespaceBOUtils
vdiskg Apr 17, 2021
5358d34
namespace branch page
vdiskg Apr 17, 2021
e4de699
enricher adjust
vdiskg Apr 22, 2021
98a3e4f
unit test
vdiskg Apr 22, 2021
c96dca4
user manage page
vdiskg Apr 22, 2021
f88773b
nav page user info fix
vdiskg Apr 23, 2021
1e63a3b
temp
vdiskg May 8, 2021
21580b3
preferred username enrich adjust
vdiskg May 9, 2021
f7ae63a
UserDisplayName
vdiskg May 9, 2021
8e7b163
fix unit test
vdiskg May 9, 2021
030ac20
Merge branch 'fix-unit-test' into oidc-pre-username-temp
vdiskg May 9, 2021
1b39773
Merge remote-tracking branch 'apollo/master' into oidc-preferred-user…
vdiskg May 9, 2021
9e67af2
Merge branch 'master' into oidc-preferred-username
nobodyiam May 15, 2021
223ef17
Update apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/…
vdiskg May 22, 2021
87b0833
cleanup code
vdiskg May 22, 2021
44b6a75
Merge branch 'master' into oidc-preferred-username
nobodyiam May 22, 2021
e04ee8e
License
vdiskg May 22, 2021
cf72a14
add License
vdiskg May 25, 2021
2c58545
License
vdiskg May 22, 2021
021da89
Merge branch 'oidc-pre-temp-2' into oidc-preferred-username
vdiskg May 25, 2021
31c4f79
Merge remote-tracking branch 'apollo/master' into oidc-preferred-user…
vdiskg May 25, 2021
191cdd4
add License
vdiskg May 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
when(someResponse.getBody()).thenReturn(newApolloConfig);

longPollFinished.get(5000, TimeUnit.MILLISECONDS);
longPollFinished.get(10000, TimeUnit.MILLISECONDS);
vdiskg marked this conversation as resolved.
Show resolved Hide resolved

remoteConfigLongPollService.stopLongPollingRefresh();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class AppDTO extends BaseDTO{

private String ownerName;

private String ownerPreferredUsername;

private String ownerEmail;

public long getId() {
Expand All @@ -31,52 +33,59 @@ public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAppId() {
return appId;
}

public String getName() {
return name;
public void setAppId(String appId) {
this.appId = appId;
}

public String getOrgId() {
return orgId;
}

public void setOrgId(String orgId) {
this.orgId = orgId;
}

public String getOrgName() {
return orgName;
}

public String getOwnerEmail() {
return ownerEmail;
public void setOrgName(String orgName) {
this.orgName = orgName;
}

public String getOwnerName() {
return ownerName;
}

public void setAppId(String appId) {
this.appId = appId;
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}

public void setName(String name) {
this.name = name;
public String getOwnerPreferredUsername() {
return ownerPreferredUsername;
}

public void setOrgId(String orgId) {
this.orgId = orgId;
public void setOwnerPreferredUsername(String ownerPreferredUsername) {
this.ownerPreferredUsername = ownerPreferredUsername;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
public String getOwnerEmail() {
return ownerEmail;
}

public void setOwnerEmail(String ownerEmail) {
this.ownerEmail = ownerEmail;
}

public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class BaseDTO {

protected String dataChangeLastModifiedBy;

protected String dataChangeCreatedByPreferredUsername;

protected String dataChangeLastModifiedByPreferredUsername;

protected Date dataChangeCreatedTime;

protected Date dataChangeLastModifiedTime;
Expand All @@ -29,6 +33,24 @@ public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) {
this.dataChangeLastModifiedBy = dataChangeLastModifiedBy;
}

public String getDataChangeCreatedByPreferredUsername() {
return dataChangeCreatedByPreferredUsername;
}

public void setDataChangeCreatedByPreferredUsername(
String dataChangeCreatedByPreferredUsername) {
this.dataChangeCreatedByPreferredUsername = dataChangeCreatedByPreferredUsername;
}

public String getDataChangeLastModifiedByPreferredUsername() {
return dataChangeLastModifiedByPreferredUsername;
}

public void setDataChangeLastModifiedByPreferredUsername(
String dataChangeLastModifiedByPreferredUsername) {
this.dataChangeLastModifiedByPreferredUsername = dataChangeLastModifiedByPreferredUsername;
}

public Date getDataChangeCreatedTime() {
return dataChangeCreatedTime;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.ctrip.framework.apollo.common.utils;

import com.ctrip.framework.apollo.common.dto.BaseDTO;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
* @author vdisk <vdisk@foxmail.com>
*/
public class PreferredUsernameUtil {

/**
* extract operator id from the dto list
*
* @param dtoList dto list with operator id
* @return operator id set
*/
public static Set<String> extractOperatorId(List<? extends BaseDTO> dtoList) {
vdiskg marked this conversation as resolved.
Show resolved Hide resolved
if (CollectionUtils.isEmpty(dtoList)) {
return Collections.emptySet();
}
Set<String> operatorIdSet = new HashSet<>();
for (BaseDTO dto : dtoList) {
operatorIdSet.add(dto.getDataChangeCreatedBy());
operatorIdSet.add(dto.getDataChangeLastModifiedBy());
}
return operatorIdSet;
}

/**
* extract operator id from the dto
*
* @param dto dto with operator id
* @return operator id set
*/
public static Set<String> extractOperatorId(BaseDTO dto) {
vdiskg marked this conversation as resolved.
Show resolved Hide resolved
Set<String> operatorIdSet = new HashSet<>();
operatorIdSet.add(dto.getDataChangeCreatedBy());
operatorIdSet.add(dto.getDataChangeLastModifiedBy());
return operatorIdSet;
}

/**
* set the preferred username
*
* @param dto dto with operator id
* @param preferredUsernameMap (userId - preferredUsername) prepared preferred username map
*/
public static void setPreferredUsername(BaseDTO dto,
Map<String, String> preferredUsernameMap) {
if (dto == null) {
return;
}
if (CollectionUtils.isEmpty(preferredUsernameMap)) {
return;
}
if (StringUtils.hasText(dto.getDataChangeCreatedBy())) {
String preferredUsername = preferredUsernameMap.get(dto.getDataChangeCreatedBy());
if (StringUtils.hasText(preferredUsername)) {
dto.setDataChangeCreatedByPreferredUsername(preferredUsername);
}
}
if (StringUtils.hasText(dto.getDataChangeLastModifiedBy())) {
String preferredUsername = preferredUsernameMap.get(dto.getDataChangeLastModifiedBy());
if (StringUtils.hasText(preferredUsername)) {
dto.setDataChangeLastModifiedByPreferredUsername(preferredUsername);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand All @@ -46,7 +48,8 @@ public abstract class AbstractBaseIntegrationTest {

private static final Gson GSON = new Gson();

protected RestTemplate restTemplate = (new TestRestTemplate()).getRestTemplate();
protected RestTemplate restTemplate = (new TestRestTemplate(new RestTemplateBuilder()
.setConnectTimeout(Duration.ofSeconds(5)))).getRestTemplate();
vdiskg marked this conversation as resolved.
Show resolved Hide resolved

@PostConstruct
private void postConstruct() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.ctrip.framework.apollo.portal.controller;


import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.http.MultiResponseEntity;
import com.ctrip.framework.apollo.common.http.RichResponseEntity;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.entity.model.AppModel;
import com.ctrip.framework.apollo.portal.entity.po.Role;
import com.ctrip.framework.apollo.portal.entity.vo.EnvClusterInfo;
Expand All @@ -19,6 +22,7 @@
import com.ctrip.framework.apollo.portal.service.RoleInitializationService;
import com.ctrip.framework.apollo.portal.service.RolePermissionService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.spi.UserService;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import com.google.common.collect.Sets;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -55,20 +59,23 @@ public class AppController {
private final ApplicationEventPublisher publisher;
private final RolePermissionService rolePermissionService;
private final RoleInitializationService roleInitializationService;
private final UserService userService;

public AppController(
final UserInfoHolder userInfoHolder,
final AppService appService,
final PortalSettings portalSettings,
final ApplicationEventPublisher publisher,
final RolePermissionService rolePermissionService,
final RoleInitializationService roleInitializationService) {
final RoleInitializationService roleInitializationService,
final UserService userService) {
this.userInfoHolder = userInfoHolder;
this.appService = appService;
this.portalSettings = portalSettings;
this.publisher = publisher;
this.rolePermissionService = rolePermissionService;
this.roleInitializationService = roleInitializationService;
this.userService = userService;
}

@GetMapping
Expand Down Expand Up @@ -166,9 +173,14 @@ public ResponseEntity<Void> create(@PathVariable String env, @Valid @RequestBody
}

@GetMapping("/{appId:.+}")
public App load(@PathVariable String appId) {

return appService.load(appId);
public AppDTO load(@PathVariable String appId) {
App app = appService.load(appId);
AppDTO appDto = BeanUtils.transform(AppDTO.class, app);
UserInfo userInfo = userService.findByUserId(app.getOwnerName());
if (userInfo != null) {
appDto.setOwnerPreferredUsername(userInfo.getName());
}
return appDto;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
package com.ctrip.framework.apollo.portal.controller;

import com.ctrip.framework.apollo.common.dto.CommitDTO;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.common.utils.PreferredUsernameUtil;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.CommitService;
import com.ctrip.framework.apollo.portal.spi.UserService;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Validated
@RestController
public class CommitController {

private final CommitService commitService;
private final PermissionValidator permissionValidator;
private final UserService userService;

public CommitController(final CommitService commitService, final PermissionValidator permissionValidator) {
public CommitController(final CommitService commitService,
final PermissionValidator permissionValidator,
final UserService userService) {
this.commitService = commitService;
this.permissionValidator = permissionValidator;
this.userService = userService;
}

@GetMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/commits")
Expand All @@ -36,7 +46,18 @@ public List<CommitDTO> find(@PathVariable String appId, @PathVariable String env
if (permissionValidator.shouldHideConfigToCurrentUser(appId, env, namespaceName)) {
return Collections.emptyList();
}

return commitService.find(appId, Env.valueOf(env), clusterName, namespaceName, page, size);
List<CommitDTO> dtoList = commitService
.find(appId, Env.valueOf(env), clusterName, namespaceName, page, size);
if (CollectionUtils.isEmpty(dtoList)) {
return Collections.emptyList();
}
Set<String> operatorIdSet = PreferredUsernameUtil.extractOperatorId(dtoList);
vdiskg marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> preferredUsernameMap = userService
.findPreferredUsernameMapByUserIds(new ArrayList<>(operatorIdSet));
if (CollectionUtils.isEmpty(preferredUsernameMap)) {
return dtoList;
}
dtoList.forEach(dto -> PreferredUsernameUtil.setPreferredUsername(dto, preferredUsernameMap));
return dtoList;
}
}
Loading