[Cherry-pick-2.0.2][Feature-7408][dolphinscheduler-api] Built GenerateToken into the UpdateToken API (#7412)
* to feature 7408 * trigger e2e test Co-authored-by: ouyangyewei <yewei.oyyw@alibaba-inc.com>
This commit is contained in:
parent
49089d3dc1
commit
895b0fa9b8
|
|
@ -193,10 +193,16 @@ public class AccessTokenController extends BaseController {
|
|||
* @param id token id
|
||||
* @param userId token for user
|
||||
* @param expireTime token expire time
|
||||
* @param token token string
|
||||
* @param token token string (if it is absent, it will be automatically generated)
|
||||
* @return update result code
|
||||
*/
|
||||
@ApiIgnore
|
||||
@ApiOperation(value = "updateToken", notes = "UPDATE_TOKEN_NOTES")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "TOKEN_ID", required = true, dataType = "Int"),
|
||||
@ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int"),
|
||||
@ApiImplicitParam(name = "expireTime", value = "EXPIRE_TIME", required = true, dataType = "String", example = "2021-12-31 00:00:00"),
|
||||
@ApiImplicitParam(name = "token", value = "TOKEN", required = false, dataType = "String", example = "xxxx")
|
||||
})
|
||||
@PutMapping(value = "/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ApiException(UPDATE_ACCESS_TOKEN_ERROR)
|
||||
|
|
@ -205,7 +211,7 @@ public class AccessTokenController extends BaseController {
|
|||
@PathVariable(value = "id") int id,
|
||||
@RequestParam(value = "userId") int userId,
|
||||
@RequestParam(value = "expireTime") String expireTime,
|
||||
@RequestParam(value = "token") String token) {
|
||||
@RequestParam(value = "token", required = false) String token) {
|
||||
|
||||
Map<String, Object> result = accessTokenService.updateToken(loginUser, id, userId, expireTime, token);
|
||||
return returnDataList(result);
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ public interface AccessTokenService {
|
|||
* @param id token id
|
||||
* @param userId token for user
|
||||
* @param expireTime token expire time
|
||||
* @param token token string
|
||||
* @param token token string (if it is absent, it will be automatically generated)
|
||||
* @return update result code
|
||||
*/
|
||||
Map<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token);
|
||||
|
|
|
|||
|
|
@ -207,22 +207,33 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
|
|||
* @param id token id
|
||||
* @param userId token for user
|
||||
* @param expireTime token expire time
|
||||
* @param token token string
|
||||
* @param token token string (if it is absent, it will be automatically generated)
|
||||
* @return update result code
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> updateToken(User loginUser, int id, int userId, String expireTime, String token) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
// 1. check permission
|
||||
if (!hasPerm(loginUser,userId)) {
|
||||
putMsg(result, Status.USER_NO_OPERATION_PERM);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 2. check if token is existed
|
||||
AccessToken accessToken = accessTokenMapper.selectById(id);
|
||||
if (accessToken == null) {
|
||||
logger.error("access token not exist, access token id {}", id);
|
||||
putMsg(result, Status.ACCESS_TOKEN_NOT_EXIST);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 3. generate access token if absent
|
||||
if (StringUtils.isBlank(token)) {
|
||||
token = EncryptionUtils.getMd5(userId + expireTime + System.currentTimeMillis());
|
||||
}
|
||||
|
||||
// 4. persist to the database
|
||||
accessToken.setUserId(userId);
|
||||
accessToken.setExpireTime(DateUtils.stringToDate(expireTime));
|
||||
accessToken.setToken(token);
|
||||
|
|
|
|||
|
|
@ -144,8 +144,10 @@ QUERY_AUTHORIZED_USER_NOTES=query authorized user
|
|||
TASK_RECORD_TAG=task record related operation
|
||||
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
|
||||
CREATE_TOKEN_NOTES=create access token for specified user
|
||||
UPDATE_TOKEN_NOTES=update access token for specified user
|
||||
TOKEN=access token string, it will be automatically generated when it absent
|
||||
EXPIRE_TIME=expire time for the token
|
||||
TOKEN_ID=access token id
|
||||
QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging
|
||||
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
|
||||
SCHEDULE=schedule
|
||||
|
|
@ -227,7 +229,7 @@ GRANT_PROJECT_NOTES=GRANT PROJECT
|
|||
PROJECT_IDS=project ids(string format, multiple projects separated by ",")
|
||||
GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE
|
||||
REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER
|
||||
PROJECT_CODE=project codes
|
||||
PROJECT_CODE=project code
|
||||
GRANT_RESOURCE_NOTES=grant resource file
|
||||
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
|
||||
GET_USER_INFO_NOTES=get user info
|
||||
|
|
|
|||
|
|
@ -160,8 +160,10 @@ QUERY_AUTHORIZED_USER_NOTES=query authorized user
|
|||
TASK_RECORD_TAG=task record related operation
|
||||
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
|
||||
CREATE_TOKEN_NOTES=create access token for specified user
|
||||
UPDATE_TOKEN_NOTES=update access token for specified user
|
||||
TOKEN=access token string, it will be automatically generated when it absent
|
||||
EXPIRE_TIME=expire time for the token
|
||||
TOKEN_ID=access token id
|
||||
QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging
|
||||
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
|
||||
SCHEDULE=schedule
|
||||
|
|
@ -274,7 +276,7 @@ GRANT_PROJECT_NOTES=GRANT PROJECT
|
|||
PROJECT_IDS=project ids(string format, multiple projects separated by ",")
|
||||
GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE
|
||||
REVOKE_PROJECT_NOTES=REVOKE PROJECT FOR USER
|
||||
PROJECT_CODE=project codes
|
||||
PROJECT_CODE=project code
|
||||
GRANT_RESOURCE_NOTES=grant resource file
|
||||
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
|
||||
GET_USER_INFO_NOTES=get user info
|
||||
|
|
|
|||
|
|
@ -149,8 +149,10 @@ QUERY_AUTHORIZED_USER_NOTES=查询拥有项目授权的用户
|
|||
TASK_RECORD_TAG=任务记录相关操作
|
||||
QUERY_TASK_RECORD_LIST_PAGING_NOTES=分页查询任务记录列表
|
||||
CREATE_TOKEN_NOTES=为指定用户创建安全令牌
|
||||
UPDATE_TOKEN_NOTES=更新指定用户的安全令牌
|
||||
TOKEN=安全令牌字符串,若未显式指定将会自动生成
|
||||
EXPIRE_TIME=安全令牌的过期时间
|
||||
TOKEN_ID=安全令牌的ID
|
||||
QUERY_ACCESS_TOKEN_LIST_NOTES=分页查询access token列表
|
||||
QUERY_ACCESS_TOKEN_BY_USER_NOTES=查询指定用户的access token
|
||||
SCHEDULE=定时
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.api.controller;
|
|||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
|
|
@ -60,7 +61,7 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTokenWhenTokenAbsent() throws Exception {
|
||||
public void testCreateTokenIfAbsent() throws Exception {
|
||||
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
|
||||
paramsMap.add("userId", "4");
|
||||
paramsMap.add("expireTime", "2019-12-18 00:00:00");
|
||||
|
|
@ -175,4 +176,25 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
|
|||
logger.info(mvcResult.getResponse().getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateTokenIfAbsent() throws Exception {
|
||||
this.testCreateTokenIfAbsent();
|
||||
|
||||
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
|
||||
paramsMap.add("userId", "4");
|
||||
paramsMap.add("expireTime", "2019-12-20 00:00:00");
|
||||
paramsMap.add("token", null);
|
||||
|
||||
MvcResult mvcResult = this.mockMvc
|
||||
.perform(put("/access-tokens/2")
|
||||
.header("sessionId", this.sessionId)
|
||||
.params(paramsMap))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andReturn();
|
||||
|
||||
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
|
||||
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
|
||||
logger.info(mvcResult.getResponse().getContentAsString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -147,16 +147,21 @@ public class AccessTokenServiceTest {
|
|||
|
||||
@Test
|
||||
public void testUpdateToken() {
|
||||
|
||||
// Given Token
|
||||
when(accessTokenMapper.selectById(1)).thenReturn(getEntity());
|
||||
Map<String, Object> result = accessTokenService.updateToken(getLoginUser(), 1,Integer.MAX_VALUE,getDate(),"token");
|
||||
logger.info(result.toString());
|
||||
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
|
||||
// not exist
|
||||
|
||||
// Token is absent
|
||||
result = accessTokenService.updateToken(getLoginUser(), 1, Integer.MAX_VALUE,getDate(),null);
|
||||
logger.info(result.toString());
|
||||
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
|
||||
|
||||
// ACCESS_TOKEN_NOT_EXIST
|
||||
result = accessTokenService.updateToken(getLoginUser(), 2,Integer.MAX_VALUE,getDate(),"token");
|
||||
logger.info(result.toString());
|
||||
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST, result.get(Constants.STATUS));
|
||||
|
||||
}
|
||||
|
||||
private User getLoginUser() {
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ import com.google.common.collect.Lists;
|
|||
/**
|
||||
* users service test
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class UsersServiceTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UsersServiceTest.class);
|
||||
|
|
|
|||
Loading…
Reference in New Issue