Skip to content

Commit

Permalink
[Feature-7301][dolphinscheduler-api] Support query access token for s…
Browse files Browse the repository at this point in the history
…pecified user
  • Loading branch information
ouyangyewei committed Dec 13, 2021
1 parent 77fd225 commit 8672acc
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.QUERY_ACCESSTOKEN_BY_USER_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_ACCESS_TOKEN_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.DELETE_ACCESS_TOKEN_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.GENERATE_TOKEN_ERROR;
Expand Down Expand Up @@ -140,6 +141,27 @@ public Result queryAccessTokenList(@ApiIgnore @RequestAttribute(value = Constant
return result;
}

/**
* query access token for specified user
*
* @param loginUser login user
* @param userId user id
* @return token list for specified user
*/
@ApiOperation(value = "queryAccessTokenByUser", notes = "QUERY_ACCESS_TOKEN_BY_USER_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "USER_ID", dataType = "Int")
})
@GetMapping(value = "/user/{userId}")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_ACCESSTOKEN_BY_USER_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAccessTokenByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("userId") Integer userId) {
Map<String, Object> result = this.accessTokenService.queryAccessTokenByUser(loginUser, userId);
return this.returnDataList(result);
}

/**
* delete access token by id
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public enum Status {
UPDATE_ACCESS_TOKEN_ERROR(70013, "update access token error", "更新访问token错误"),
DELETE_ACCESS_TOKEN_ERROR(70014, "delete access token error", "删除访问token错误"),
ACCESS_TOKEN_NOT_EXIST(70015, "access token not exist", "访问token不存在"),
QUERY_ACCESSTOKEN_BY_USER_ERROR(70016, "query access token by user error", "查询访问指定用户的token错误"),


COMMAND_STATE_COUNT_ERROR(80001, "task instance state count error", "查询各状态任务实例数错误"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public interface AccessTokenService {
*/
Result queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, Integer pageSize);

/**
* query access token for specified user
*
* @param loginUser login user
* @param userId user id
* @return token list for specified user
*/
Map<String, Object> queryAccessTokenByUser(User loginUser, Integer userId);

/**
* create token
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
Expand Down Expand Up @@ -78,6 +79,30 @@ public Result queryAccessTokenList(User loginUser, String searchVal, Integer pag
return result;
}

/**
* query access token for specified user
*
* @param loginUser login user
* @param userId user id
* @return token list for specified user
*/
@Override
public Map<String, Object> queryAccessTokenByUser(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>();
result.put(Constants.STATUS, false);

// only admin can operate
if (isNotAdmin(loginUser, result)) {
return result;
}

// query access token for specified user
List<AccessToken> accessTokenList = this.accessTokenMapper.queryAccessTokenByUser(userId);
result.put(Constants.DATA_LIST, accessTokenList);
this.putMsg(result, Status.SUCCESS);
return result;
}

/**
* create token
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public Map<String, Object> revokeProject(User loginUser, int userId, long projec
return result;
}

// 4. delete th relationship between project and user
// 4. delete the relationship between project and user
this.projectUserMapper.deleteProjectRelation(project.getId(), user.getId());
this.putMsg(result, Status.SUCCESS);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ TASK_RECORD_TAG=task record related operation
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
CREATE_TOKEN_NOTES=create token ,note: please login first
QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
SCHEDULE=schedule
WARNING_TYPE=warning type(sending strategy)
WARNING_GROUP_ID=warning group id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ TASK_RECORD_TAG=task record related operation
QUERY_TASK_RECORD_LIST_PAGING_NOTES=query task record list paging
CREATE_TOKEN_NOTES=create token ,note: please login first
QUERY_ACCESS_TOKEN_LIST_NOTES=query access token list paging
QUERY_ACCESS_TOKEN_BY_USER_NOTES=query access token for specified user
SCHEDULE=schedule
WARNING_TYPE=warning type(sending strategy)
WARNING_GROUP_ID=warning group id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ TASK_RECORD_TAG=任务记录相关操作
QUERY_TASK_RECORD_LIST_PAGING_NOTES=分页查询任务记录列表
CREATE_TOKEN_NOTES=创建token,注意需要先登录
QUERY_ACCESS_TOKEN_LIST_NOTES=分页查询access token列表
QUERY_ACCESS_TOKEN_BY_USER_NOTES=查询指定用户的access token
SCHEDULE=定时
WARNING_TYPE=发送策略
WARNING_GROUP_ID=发送组ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public void testQueryAccessTokenList() throws Exception {
logger.info(mvcResult.getResponse().getContentAsString());
}

@Test
public void testQueryAccessTokenByUser() throws Exception {
MvcResult mvcResult = this.mockMvc
.perform(get("/access-tokens/user/1")
.header("sessionId", this.sessionId))
.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());
}

@Test
public void testDelAccessTokenById() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
import java.util.List;
import java.util.Map;

import org.assertj.core.util.Lists;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -79,6 +81,25 @@ public void testQueryAccessTokenList() {
Assert.assertTrue(pageInfo.getTotal() > 0);
}

@Test
public void testQueryAccessTokenByUser() {
List<AccessToken> accessTokenList = Lists.newArrayList(this.getEntity());
Mockito.when(this.accessTokenMapper.queryAccessTokenByUser(1)).thenReturn(accessTokenList);

// USER_NO_OPERATION_PERM
User user = this.getLoginUser();
user.setUserType(UserType.GENERAL_USER);
Map<String, Object> result = this.accessTokenService.queryAccessTokenByUser(user, 1);
logger.info(result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS));

// SUCCESS
user.setUserType(UserType.ADMIN_USER);
result = this.accessTokenService.queryAccessTokenByUser(user, 1);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}

@Test
public void testCreateToken() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
* accesstoken mapper interface
*/
Expand All @@ -44,6 +46,14 @@ IPage<AccessToken> selectAccessTokenPage(Page page,
@Param("userId") int userId
);

/**
* Query access token for specified user
*
* @param userId userId
* @return access token for specified user
*/
List<AccessToken> queryAccessTokenByUser(@Param("userId") int userId);

/**
* delete by userId
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
</if>
order by t.update_time desc
</select>

<select id="queryAccessTokenByUser" resultType="org.apache.dolphinscheduler.dao.entity.AccessToken">
select id, user_id, token, expire_time, create_time, update_time
from t_ds_access_token
where user_id = #{userId}
</select>

<delete id="deleteAccessTokenByUserId">
delete from t_ds_access_token
where user_id = #{userId}
Expand Down

0 comments on commit 8672acc

Please sign in to comment.