Skip to content

Commit

Permalink
[INLONG-8440][Manager] Support list tenant info by user or given tena…
Browse files Browse the repository at this point in the history
…nt list (apache#8441)
  • Loading branch information
vernedeng authored Jul 7, 2023
1 parent b398084 commit 54a0383
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TenantUserRoleEntityMapper {

Expand All @@ -35,6 +37,8 @@ public interface TenantUserRoleEntityMapper {

Page<TenantUserRoleEntity> listByCondition(TenantRolePageRequest request);

List<String> listByUsername(String username);

int updateById(TenantUserRoleEntity record);

int deleteById(Integer id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@
from inlong_tenant
<where>
is_deleted = 0
<if test="name != null and name != ''">
and name = #{name, jdbcType=VARCHAR}
</if>
<if test="keyword != null and keyword != ''">
and name like CONCAT('%', #{keyword}, '%')
</if>
<if test="tenantList != null and tenantList.size() > 0">
and name in
<foreach collection="tenantList" item="targetTenant" index="index" open="(" close=")" separator=",">
#{targetTenant}
</foreach>
</if>
</where>
order by modify_time desc
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,22 @@
tenant like CONCAT('%', #{keyword}, '%')
)
</if>
<if test="tenantList != null and tenantList.size() > 0">
and tenant in
<foreach collection="tenantList" item="targetTenant" index="index" open="(" close=")" separator=",">
#{targetTenant}
</foreach>
</if>
<if test="disabled != null">
and disabled = #{disabled, jdbcType=SMALLINT}
</if>
</select>

<select id="listByUsername" resultType="java.lang.String">
select distinct tenant
from tenant_user_role
where is_deleted = 0
and username = #{username,jdbcType=VARCHAR}
</select>

<update id="updateById" parameterType="org.apache.inlong.manager.dao.entity.TenantUserRoleEntity">
update tenant_user_role
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
Expand All @@ -35,14 +34,15 @@ public class InlongTenantPageRequest extends PageRequest {
@ApiModelProperty(value = "Primary key")
private Integer id;

@ApiModelProperty(value = "Tenant name")
@Pattern(regexp = "^[A-Za-z0-9_-]{1,256}$", message = "only supports letters, numbers, '-', or '_'")
@NotBlank
private String name;
@ApiModelProperty(value = "Tenant list")
private List<String> tenantList;

@ApiModelProperty(value = "Keyword")
private String keyword;

@ApiModelProperty(value = "Whether list tenant info of login user")
private Boolean listByLoginUser = false;

@ApiModelProperty(value = "Current user", hidden = true)
private String currentUser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel("Tenant user paging query request")
Expand All @@ -35,6 +37,9 @@ public class TenantRolePageRequest extends PageRequest {
@ApiModelProperty(value = "User name")
private String username;

@ApiModelProperty(value = "Tenant list")
private List<String> tenantList;

@ApiModelProperty(value = "Role code")
private String roleCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.inlong.manager.pojo.tenant.InlongTenantInfo;
import org.apache.inlong.manager.pojo.tenant.InlongTenantPageRequest;
import org.apache.inlong.manager.pojo.tenant.InlongTenantRequest;
import org.apache.inlong.manager.pojo.user.UserInfo;

/**
* Inlong tenant service
Expand All @@ -47,9 +48,10 @@ public interface InlongTenantService {
* Paging query stream sink info based on conditions.
*
* @param request paging request
* @param userInfo query user info
* @return tenant page list
*/
PageResult<InlongTenantInfo> listByCondition(InlongTenantPageRequest request);
PageResult<InlongTenantInfo> listByCondition(InlongTenantPageRequest request, UserInfo userInfo);

/**
* Update one tenant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,29 @@
import org.apache.inlong.manager.pojo.tenant.InlongTenantPageRequest;
import org.apache.inlong.manager.pojo.tenant.InlongTenantRequest;
import org.apache.inlong.manager.pojo.user.LoginUserUtils;
import org.apache.inlong.manager.pojo.user.UserInfo;
import org.apache.inlong.manager.service.user.TenantRoleService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.apache.inlong.manager.pojo.user.UserRoleCode.INLONG_ADMIN;
import static org.apache.inlong.manager.pojo.user.UserRoleCode.INLONG_OPERATOR;

@Service
@Slf4j
public class InlongTenantServiceImpl implements InlongTenantService {

@Autowired
private InlongTenantEntityMapper inlongTenantEntityMapper;
@Autowired
private TenantRoleService tenantRoleService;

@Override
public InlongTenantInfo getByName(String name) {
Expand Down Expand Up @@ -72,8 +80,13 @@ public Integer save(InlongTenantRequest request) {
}

@Override
public PageResult<InlongTenantInfo> listByCondition(InlongTenantPageRequest request) {
public PageResult<InlongTenantInfo> listByCondition(InlongTenantPageRequest request, UserInfo userInfo) {
PageHelper.startPage(request.getPageNum(), request.getPageSize());

if (request.getListByLoginUser()) {
setTargetTenantList(request, userInfo);
}

Page<InlongTenantEntity> entityPage = inlongTenantEntityMapper.selectByCondition(request);

List<InlongTenantInfo> tenantList = CommonBeanUtils.copyListProperties(entityPage, InlongTenantInfo::new);
Expand Down Expand Up @@ -107,4 +120,23 @@ public Boolean update(InlongTenantRequest request) {
}
return true;
}

private void setTargetTenantList(InlongTenantPageRequest request, UserInfo userInfo) {
request.setKeyword(null);
if (isInlongRoles(userInfo)) {
request.setTenantList(null);
return;
}

List<String> tenants = tenantRoleService.listTenantByUsername(userInfo.getName());
if (CollectionUtils.isEmpty(tenants)) {
request.setTenantList(null);
return;
}
request.setTenantList(tenants);
}

private boolean isInlongRoles(UserInfo userInfo) {
return userInfo.getRoles().contains(INLONG_ADMIN) || userInfo.getRoles().contains(INLONG_OPERATOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.inlong.manager.pojo.user.TenantRolePageRequest;
import org.apache.inlong.manager.pojo.user.TenantRoleRequest;

import java.util.List;

/**
* Tenant Role service
*/
Expand Down Expand Up @@ -51,4 +53,9 @@ public interface TenantRoleService {
* Get one tenant role by name and tenant
*/
TenantRoleInfo getByUsernameAndTenant(String name, String tenant);

/**
* List tenant by given username
*/
List<String> listTenantByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ public TenantRoleInfo getByUsernameAndTenant(String name, String tenant) {
return CommonBeanUtils.copyProperties(entity, TenantRoleInfo::new);
}

@Override
public List<String> listTenantByUsername(String username) {
return tenantUserRoleEntityMapper.listByUsername(username);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public void testPage() {
}
InlongTenantPageRequest pageRequest = new InlongTenantPageRequest();
pageRequest.setKeyword("test");
PageResult<InlongTenantInfo> infoPage = tenantService.listByCondition(pageRequest);
PageResult<InlongTenantInfo> infoPage = tenantService.listByCondition(pageRequest,
LoginUserUtils.getLoginUser());
Assertions.assertEquals(size, infoPage.getList().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.inlong.manager.pojo.tenant.InlongTenantInfo;
import org.apache.inlong.manager.pojo.tenant.InlongTenantPageRequest;
import org.apache.inlong.manager.pojo.tenant.InlongTenantRequest;
import org.apache.inlong.manager.pojo.user.LoginUserUtils;
import org.apache.inlong.manager.service.operationlog.OperationLog;
import org.apache.inlong.manager.service.tenant.InlongTenantService;

Expand Down Expand Up @@ -69,9 +70,8 @@ public Response<Integer> save(@Validated @RequestBody InlongTenantRequest reques

@RequestMapping(value = "/tenant/list", method = RequestMethod.POST)
@ApiOperation(value = "List tenant by paginating")
@RequiresRoles(logical = Logical.OR, value = {INLONG_ADMIN, INLONG_OPERATOR})
public Response<PageResult<InlongTenantInfo>> listByCondition(@RequestBody InlongTenantPageRequest request) {
return Response.success(tenantService.listByCondition(request));
return Response.success(tenantService.listByCondition(request, LoginUserUtils.getLoginUser()));
}

@RequestMapping(value = "/tenant/update", method = RequestMethod.POST)
Expand Down

0 comments on commit 54a0383

Please sign in to comment.