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

feat(open-api): get authorized apps #3647

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ecda570
feat(open-api): get authorized apps
Anilople Apr 24, 2021
29fa187
Merge branch 'master' into feature/openapi/enhancement/20210417
Anilople Apr 24, 2021
1c1522d
Merge branch 'master' into feature/openapi/enhancement/20210417
nobodyiam May 15, 2021
f0931c6
Merge branch 'master' into feature/openapi/enhancement/20210417
Anilople Jun 13, 2021
4d41625
refactor: delete new class
Anilople Jun 16, 2021
f3adccd
fix: test failure
Anilople Jun 16, 2021
472dd45
rename method name
Anilople Jun 16, 2021
5cacf99
Merge branch 'master' into feature/openapi/enhancement/20210417
Anilople Jun 16, 2021
73f4c01
Merge branch 'master' into feature/openapi/enhancement/20210417
Anilople Jun 16, 2021
c1d7cbd
fix: extractAppIdFromMasterRoleName -> extractAppIdFromRoleName
Anilople Jun 16, 2021
f9cdb84
test: find app authorized
Anilople Jun 16, 2021
1d45338
Merge branch 'master' into feature/openapi/enhancement/20210417
Anilople Jun 19, 2021
f4d182b
Update CHANGES.md
Anilople Jun 19, 2021
11d39b3
refactor: create method findAppIdsAuthorizedByConsumerId in ConsumerS…
Anilople Jun 20, 2021
1ddc81c
test: use sql to prepare data
Anilople Jun 20, 2021
348af40
test: add more app consumer-test-app-id-2
Anilople Jun 21, 2021
99822cb
test: restTemplate call api with token
Anilople Jun 21, 2021
dea5f3c
test: append 000 to all id in sql
Anilople Jun 25, 2021
c284ae5
test: move sql to folder /sql/openapi
Anilople Jun 25, 2021
5a999dc
Merge branch 'master' into feature/openapi/enhancement/20210417
nobodyiam Jun 26, 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 @@ -71,6 +71,15 @@ public List<OpenAppDTO> getAllApps() {
return appService.getAppsInfo(null);
}

/**
* Get applications which can be operated by current open api client.
*
* @return app's information
*/
public List<OpenAppDTO> getAuthorizedApps() {
return this.appService.getAuthorizedApps();
}

/**
* Get App information by app ids
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ public List<OpenAppDTO> getAppsInfo(List<String> appIds) {
throw new RuntimeException(String.format("Load app information for appIds: %s failed", appIds), ex);
}
}

public List<OpenAppDTO> getAuthorizedApps() {
String path = "authorized/apps";
try(CloseableHttpResponse response = this.get(path)) {
return gson.fromJson(EntityUtils.toString(response.getEntity()), OPEN_APP_DTO_LIST_TYPE);
} catch (Throwable ex) {
throw new RuntimeException("Load authorized apps failed", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.ctrip.framework.apollo.openapi.service;

import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.openapi.dto.OpenAppDTO;
import com.ctrip.framework.apollo.openapi.entity.ConsumerRole;
import com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository;
import com.ctrip.framework.apollo.openapi.util.OpenApiBeanUtils;
import com.ctrip.framework.apollo.portal.entity.po.Role;
import com.ctrip.framework.apollo.portal.repository.RoleRepository;
import com.ctrip.framework.apollo.portal.service.AppService;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author wxq
*/
@Service
public class AuthorizationService {

private final ConsumerService consumerService;

private final AppService appService;

@Autowired
private ConsumerRoleRepository consumerRoleRepository;

@Autowired
private RoleRepository roleRepository;

public AuthorizationService(
ConsumerService consumerService,
AppService appService) {
this.consumerService = consumerService;
this.appService = appService;
}

private Set<String> getAppIdsByRoleIds(List<Long> roleIds) {
Iterable<Role> roleIterable = this.roleRepository.findAllById(roleIds);
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved

List<String> roleNames = new ArrayList<>();
for (Role role : roleIterable) {
if (!role.isDeleted()) {
roleNames.add(role.getRoleName());
}
}

Set<String> appIds = roleNames.stream().map(RoleUtils::extractAppIdFromRoleName)
.collect(Collectors.toSet());

return appIds;
}

public List<OpenAppDTO> getAuthorizedApps(String token) {
long consumerId = this.consumerService.getConsumerIdByToken(token);
List<ConsumerRole> consumerRoles = this.consumerRoleRepository.findByConsumerId(consumerId);
List<Long> roleIds = consumerRoles.stream().map(ConsumerRole::getRoleId)
.collect(Collectors.toList());

Set<String> appIds = this.getAppIdsByRoleIds(roleIds);

List<App> apps = this.appService.findByAppIds(appIds);
List<OpenAppDTO> openAppDTOS = OpenApiBeanUtils.transformFromApps(apps);
return openAppDTOS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ctrip.framework.apollo.openapi.v1.controller;

import com.ctrip.framework.apollo.openapi.dto.OpenAppDTO;
import com.ctrip.framework.apollo.openapi.service.AuthorizationService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Let open api know self's permission.
*
* @author wxq
* @since 1.9.0
*/
@RestController("openapiAuthorizationController")
@RequestMapping("/openapi/v1")
public class AuthorizationController {

@Autowired
private AuthorizationService authorizationService;

/**
* @return which apps can be operated by open api
*/
@GetMapping("/authorized/apps")
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
public List<OpenAppDTO> getAuthorizedApps(
@RequestHeader(HttpHeaders.AUTHORIZATION) String token) {
return this.authorizationService.getAuthorizedApps(token);
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
}
}