Skip to content

Commit

Permalink
Add user service and user validation when assigning roles
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Jun 23, 2016
1 parent d4ed153 commit 566b6fb
Show file tree
Hide file tree
Showing 20 changed files with 613 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apollo-adminservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-biz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apollo-buildtools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ private Properties transformApolloConfigToProperties(ApolloConfig apolloConfig)
}

private ApolloConfig loadApolloConfig() {
m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS);//wait at most 5 seconds
if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) {
//wait at most 5 seconds
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
}
}
String appId = m_configUtil.getAppId();
String cluster = m_configUtil.getCluster();
String dataCenter = m_configUtil.getDataCenter();
Expand Down Expand Up @@ -285,7 +291,13 @@ private void doLongPollingRefresh(String appId, String cluster, String dataCente
final Random random = new Random();
ServiceDTO lastServiceDto = null;
while (!m_longPollingStopped.get() && !Thread.currentThread().isInterrupted()) {
m_longPollRateLimiter.tryAcquire(5, TimeUnit.SECONDS);//wait at most 5 seconds
if (!m_longPollRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) {
//wait at most 5 seconds
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
}
}
Transaction transaction = Cat.newTransaction("Apollo.ConfigService", "pollNotification");
try {
if (lastServiceDto == null) {
Expand Down
2 changes: 1 addition & 1 deletion apollo-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-configservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apollo-portal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package com.ctrip.framework.apollo.portal.auth;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
import com.ctrip.framework.apollo.portal.service.ServerConfigService;
import com.ctrip.framework.apollo.portal.service.UserService;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Jason Song([email protected])
*/
public class CtripUserService implements UserService {
private ServerConfigService serverConfigService;
private RestTemplate restTemplate;
private List<String> searchUserMatchFields;
private ParameterizedTypeReference<Map<String, List<UserServiceResponse>>> responseType;

public CtripUserService(ServerConfigService serverConfigService) {
this.serverConfigService = serverConfigService;
this.restTemplate = new RestTemplate();
this.searchUserMatchFields =
Lists.newArrayList("empcode", "empaccount", "displayname", "c_name", "pinyin");
this.responseType = new ParameterizedTypeReference<Map<String, List<UserServiceResponse>>>() {
};
}

@Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit);

HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request);
ResponseEntity<Map<String, List<UserServiceResponse>>> response =
restTemplate.exchange(getUserServiceUrl(), HttpMethod.POST, entity, responseType);

if (!response.getBody().containsKey("result")) {
return Collections.emptyList();
}

List<UserInfo> result = Lists.newArrayList();
result.addAll(
response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo)
.collect(Collectors.toList()));

return result;
}

@Override
public UserInfo findByUserId(String userId) {
List<UserInfo> userInfoList = this.findByUserIds(Lists.newArrayList(userId));
if (CollectionUtils.isEmpty(userInfoList)) {
return null;
}
return userInfoList.get(0);
}

public List<UserInfo> findByUserIds(List<String> userIds) {
UserServiceRequest request = assembleFindUserRequest(Lists.newArrayList(userIds));

HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request);
ResponseEntity<Map<String, List<UserServiceResponse>>> response =
restTemplate.exchange(getUserServiceUrl(), HttpMethod.POST, entity, responseType);

if (!response.getBody().containsKey("result")) {
return Collections.emptyList();
}

List<UserInfo> result = Lists.newArrayList();
result.addAll(
response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo)
.collect(Collectors.toList()));

return result;
}

private UserInfo transformUserServiceResponseToUserInfo(UserServiceResponse userServiceResponse) {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(userServiceResponse.getEmpaccount());
userInfo.setName(userServiceResponse.getDisplayname());
userInfo.setEmail(userServiceResponse.getEmail());
return userInfo;
}

UserServiceRequest assembleSearchUserRequest(String keyword, int offset, int limit) {
Map<String, Object> query = Maps.newHashMap();
Map<String, Object> multiMatchMap = Maps.newHashMap();
multiMatchMap.put("fields", searchUserMatchFields);
multiMatchMap.put("operator", "and");
multiMatchMap.put("query", keyword);
multiMatchMap.put("type", "best_fields");
query.put("multi_match", multiMatchMap);

return assembleUserServiceRequest(query, offset, limit);
}

UserServiceRequest assembleFindUserRequest(List<String> userIds) {
Map<String, Object>
query =
ImmutableMap.of("filtered", ImmutableMap
.of("filter", ImmutableMap.of("terms", ImmutableMap.of("empaccount", userIds))));

return assembleUserServiceRequest(query, 0, userIds.size());
}

private UserServiceRequest assembleUserServiceRequest(Map<String, Object> query, int offset,
int limit) {
UserServiceRequest request = new UserServiceRequest();
request.setAccess_token(getUserServiceAccessToken());
request.setType("emloyee");

UserServiceRequestBody requestBody = new UserServiceRequestBody();
requestBody.setIndexAlias("itdb_emloyee");
request.setRequest_body(requestBody);

Map<String, Object> queryJson = Maps.newHashMap();
requestBody.setQueryJson(queryJson);

queryJson.put("query", query);

queryJson.put("from", offset);
queryJson.put("size", limit);

return request;
}


private String getUserServiceUrl() {
return serverConfigService.getValue("userService.url");
}

private String getUserServiceAccessToken() {
return serverConfigService.getValue("userService.accessToken");
}

static class UserServiceRequest {
private String access_token;
private String type;
private UserServiceRequestBody request_body;

public String getAccess_token() {
return access_token;
}

public void setAccess_token(String access_token) {
this.access_token = access_token;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public UserServiceRequestBody getRequest_body() {
return request_body;
}

public void setRequest_body(
UserServiceRequestBody request_body) {
this.request_body = request_body;
}
}

static class UserServiceRequestBody {
private String indexAlias;
private Map<String, Object> queryJson;

public String getIndexAlias() {
return indexAlias;
}

public void setIndexAlias(String indexAlias) {
this.indexAlias = indexAlias;
}

public Map<String, Object> getQueryJson() {
return queryJson;
}

public void setQueryJson(Map<String, Object> queryJson) {
this.queryJson = queryJson;
}
}

static class UserServiceResponse {
private String empaccount;
private String displayname;
private String email;

public String getEmpaccount() {
return empaccount;
}

public void setEmpaccount(String empaccount) {
this.empaccount = empaccount;
}

public String getDisplayname() {
return displayname;
}

public void setDisplayname(String displayname) {
this.displayname = displayname;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ctrip.framework.apollo.portal.auth;

import com.google.common.collect.Lists;

import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
import com.ctrip.framework.apollo.portal.service.UserService;

import java.util.Collections;
import java.util.List;

/**
* @author Jason Song([email protected])
*/
public class DefaultUserService implements UserService {

@Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
return Collections.emptyList();
}

@Override
public UserInfo findByUserId(String userId) {
if (userId.equals("apollo")) {
return assembleDefaultUser();
}
return null;
}

@Override
public List<UserInfo> findByUserIds(List<String> userIds) {
if (userIds.contains("apollo")) {
return Lists.newArrayList(assembleDefaultUser());
}
return null;
}

private UserInfo assembleDefaultUser() {
UserInfo defaultUser = new UserInfo();
defaultUser.setUserId("apollo");
defaultUser.setName("apollo");
defaultUser.setEmail("[email protected]");

return defaultUser;
}
}
Loading

0 comments on commit 566b6fb

Please sign in to comment.