Skip to content

Commit

Permalink
[Feature]support-token-manager (DataLinkDC#2292)
Browse files Browse the repository at this point in the history
* support-token-manager

* support-token-manager

* fix some bug

* add i18n
  • Loading branch information
Zzm0809 authored Sep 6, 2023
1 parent e7fc3b3 commit 6477fd2
Show file tree
Hide file tree
Showing 48 changed files with 1,636 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.lang.Dict;
Expand Down Expand Up @@ -84,4 +85,17 @@ public Result<Map<String, List<Configuration<?>>>> getAll() {
MapUtil.map(all, (k, v) -> v.stream().map(Configuration::show).collect(Collectors.toList()));
return Result.succeed(map);
}

@GetMapping("/getConfigByType")
@ApiOperation("Query One Type System Config List By Key")
public Result<List<Configuration<?>>> getOneTypeByKey(@RequestParam("type") String type) {
Map<String, List<Configuration<?>>> all = sysConfigService.getAll();
// 过滤出 以 type 开头的配置 返回 list
List<Configuration<?>> configList = all.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(type))
.map(Map.Entry::getValue)
.flatMap(List::stream)
.collect(Collectors.toList());
return Result.succeed(configList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,10 @@ public Result<Void> assignUserToTenant(@RequestBody AssignUserToTenantParams ass
public Result<List<User>> getUserListByTenantId(@RequestParam("id") Integer id) {
return Result.succeed(userService.getUserListByTenantId(id));
}

@GetMapping("/getTenantListByUserId")
@ApiOperation("Get Tenant List By User Id")
public Result<List<Tenant>> getTenantListByUserId(@RequestParam("id") Integer userId) {
return Result.succeed(tenantService.getTenantListByUserId(userId));
}
}
113 changes: 113 additions & 0 deletions dinky-admin/src/main/java/org/dinky/controller/TokenController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.controller;

import org.dinky.data.annotation.Log;
import org.dinky.data.enums.BusinessType;
import org.dinky.data.enums.Status;
import org.dinky.data.model.SysToken;
import org.dinky.data.model.UDFTemplate;
import org.dinky.data.result.ProTableResult;
import org.dinky.data.result.Result;
import org.dinky.service.TokenService;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;

import cn.hutool.core.lang.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/** TokenController */
@Slf4j
@Api(tags = "Token Controller")
@RestController
@RequestMapping("/api/token")
@RequiredArgsConstructor
public class TokenController {

private final TokenService tokenService;

/**
* get udf template list
*
* @param params {@link JsonNode}
* @return {@link ProTableResult} <{@link UDFTemplate}>
*/
@PostMapping("/list")
@ApiOperation("Get Token List")
public ProTableResult<SysToken> listToken(@RequestBody JsonNode params) {
return tokenService.selectForProTable(params);
}

/**
* save or update udf template
*
* @param sysToken {@link SysToken}
* @return {@link Result} <{@link String}>
*/
@PutMapping("/saveOrUpdateToken")
@ApiOperation("Insert or Update Token")
@Log(title = "Insert or Update Token", businessType = BusinessType.INSERT_OR_UPDATE)
public Result<Void> saveOrUpdateToken(@RequestBody SysToken sysToken) {
return tokenService.saveOrUpdate(sysToken)
? Result.succeed(Status.SAVE_SUCCESS)
: Result.failed(Status.SAVE_FAILED);
}

/**
* delete Token by id
*
* @param id {@link Integer}
* @return {@link Result} <{@link Void}>
*/
@DeleteMapping("/delete")
@Log(title = "Delete Token By Id", businessType = BusinessType.DELETE)
@ApiOperation("Delete Token By Id")
@Transactional(rollbackFor = Exception.class)
public Result<Void> deleteToken(@RequestParam Integer id) {
if (tokenService.removeTokenById(id)) {
return Result.succeed(Status.DELETE_SUCCESS);
} else {
return Result.failed(Status.DELETE_FAILED);
}
}

/**
* delete Token by id
* @return {@link Result} <{@link Void}>
*/
@PostMapping("/buildToken")
@Log(title = "Build Token", businessType = BusinessType.OTHER)
@ApiOperation("Build Token")
public Result<String> buildToken() {
return Result.succeed(UUID.fastUUID().toString(true), Status.SUCCESS);
}
}
86 changes: 86 additions & 0 deletions dinky-admin/src/main/java/org/dinky/data/model/SysToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.data.model;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("dinky_sys_token")
public class SysToken implements Serializable {
private static final long serialVersionUID = 1L;

@TableId(value = "id", type = IdType.AUTO)
private Integer id;

private String tokenValue;
private Integer userId;
private Integer roleId;
private Integer tenantId;

private Integer expireType;

private Date expireStartTime;
private Date expireEndTime;

@TableField(fill = FieldFill.INSERT)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createTime;

@TableField(fill = FieldFill.INSERT_UPDATE)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime updateTime;

private Integer creator;
private Integer updator;

@TableField(exist = false)
private String userName;

@TableField(exist = false)
private String roleName;

@TableField(exist = false)
private String tenantCode;

@TableField(exist = false)
private List<LocalDateTime> expireTimeRange = new ArrayList<>();
}
29 changes: 29 additions & 0 deletions dinky-admin/src/main/java/org/dinky/mapper/TokenMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.mapper;

import org.dinky.data.model.SysToken;
import org.dinky.mybatis.mapper.SuperMapper;

import org.apache.ibatis.annotations.Mapper;

/** TokenMapper */
@Mapper
public interface TokenMapper extends SuperMapper<SysToken> {}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.dinky.data.result.Result;
import org.dinky.mybatis.service.ISuperService;

import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;

public interface TenantService extends ISuperService<Tenant> {
Expand Down Expand Up @@ -60,6 +62,13 @@ public interface TenantService extends ISuperService<Tenant> {
*/
Tenant getTenantByTenantCode(String tenantCode);

/**
* query tenant list by user id
* @param userId user id
* @return tenant list
*/
List<Tenant> getTenantListByUserId(Integer userId);

/**
* @param tenant tenant info
* @return modify code
Expand Down
42 changes: 42 additions & 0 deletions dinky-admin/src/main/java/org/dinky/service/TokenService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dinky.service;

import org.dinky.data.model.SysToken;
import org.dinky.mybatis.service.ISuperService;

public interface TokenService extends ISuperService<SysToken> {

/**
* remove token by id
*
* @param tokenId token id
* @return delete result code
*/
boolean removeTokenById(Integer tokenId);

/**
* add or update token
*
* @param token token
* @return add or update code
*/
boolean saveOrUpdateSysToken(SysToken token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ public Tenant getTenantByTenantCode(String tenantCode) {
.eq(Tenant::getIsDelete, 0));
}

/**
* @param userId
* @return
*/
@Override
public List<Tenant> getTenantListByUserId(Integer userId) {
List<UserTenant> userTenants = userTenantService
.getBaseMapper()
.selectList(new LambdaQueryWrapper<UserTenant>().eq(UserTenant::getUserId, userId));
if (CollectionUtil.isNotEmpty(userTenants)) {
List<Integer> tenantIds = new ArrayList<>();
userTenants.forEach(userTenant -> tenantIds.add(userTenant.getTenantId()));
return getBaseMapper().selectList(new LambdaQueryWrapper<Tenant>().in(Tenant::getId, tenantIds));
}
return null;
}

@Override
public boolean modifyTenant(Tenant tenant) {
if (Asserts.isNull(tenant.getId())) {
Expand Down
Loading

0 comments on commit 6477fd2

Please sign in to comment.