diff --git a/src/main/java/xyz/playedu/api/PlayEduBContext.java b/src/main/java/xyz/playedu/api/PlayEduBContext.java index 9ac4476..2e70a67 100644 --- a/src/main/java/xyz/playedu/api/PlayEduBContext.java +++ b/src/main/java/xyz/playedu/api/PlayEduBContext.java @@ -18,7 +18,7 @@ public class PlayEduBContext { public PlayEduBContext() { } - public static void put(String key, Object val) { + private static void put(String key, Object val) { LinkedHashMap hashMap = THREAD_LOCAL.get(); if (hashMap == null) { hashMap = new LinkedHashMap<>(); @@ -27,7 +27,7 @@ public class PlayEduBContext { THREAD_LOCAL.set(hashMap); } - public static Object get(String key) { + private static Object get(String key) { return THREAD_LOCAL.get().getOrDefault(key, null); } diff --git a/src/main/java/xyz/playedu/api/bus/BackendBus.java b/src/main/java/xyz/playedu/api/bus/BackendBus.java index 8607782..118c66f 100644 --- a/src/main/java/xyz/playedu/api/bus/BackendBus.java +++ b/src/main/java/xyz/playedu/api/bus/BackendBus.java @@ -75,4 +75,16 @@ public class BackendBus { return PrivacyUtil.desValue(value, 1, 0, "*"); } + public boolean isSuperAdmin() { + AdminRole superRole = adminRoleService.getBySlug(BackendConstant.SUPER_ADMIN_ROLE); + if (superRole == null) { + return false; + } + List roleIds = adminUserService.getRoleIdsByUserId(PlayEduBContext.getAdminUserID()); + if (roleIds.size() == 0) { + return false; + } + return roleIds.contains(superRole.getId()); + } + } diff --git a/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java b/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java index 1901edf..ce0fe09 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java @@ -4,6 +4,8 @@ import org.apache.commons.collections4.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; +import xyz.playedu.api.PlayEduBContext; +import xyz.playedu.api.bus.BackendBus; import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.domain.Resource; import xyz.playedu.api.domain.ResourceVideo; @@ -35,6 +37,9 @@ public class ResourceController { @Autowired private MinioService minioService; + @Autowired + private BackendBus backendBus; + @GetMapping("/index") public JsonResponse index(@RequestParam HashMap params) { Integer page = MapUtils.getInteger(params, "page", 1); @@ -54,8 +59,10 @@ public class ResourceController { filter.setSortField(sortField); filter.setType(type); filter.setCategoryIds(categoryIds); - if (name != null && name.length() > 0) { - filter.setName(name); + filter.setName(name); + // 非超管只能读取它自己上传的资源 + if (!backendBus.isSuperAdmin()) { + filter.setAdminId(PlayEduBContext.getAdminUserID()); } PaginationResult result = resourceService.paginate(page, size, filter); diff --git a/src/main/java/xyz/playedu/api/controller/backend/UploadController.java b/src/main/java/xyz/playedu/api/controller/backend/UploadController.java index afb428a..2330b66 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/UploadController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/UploadController.java @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import xyz.playedu.api.PlayEduBContext; import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.domain.Resource; import xyz.playedu.api.exception.ServiceException; @@ -39,7 +40,7 @@ public class UploadController { @PostMapping("/minio") public JsonResponse uploadMinio(@RequestParam HashMap params, MultipartFile file) throws ServiceException { String categoryIds = MapUtils.getString(params, "category_ids"); - Resource res = uploadService.storeMinio(file, categoryIds); + Resource res = uploadService.storeMinio(PlayEduBContext.getAdminUserID(), file, categoryIds); return JsonResponse.data(res); } @@ -91,6 +92,7 @@ public class UploadController { // 视频素材保存 Resource videoResource = resourceService.create( + PlayEduBContext.getAdminUserID(), req.getCategoryIds(), type, req.getOriginalFilename(), @@ -102,7 +104,7 @@ public class UploadController { url ); // 视频封面素材保存 - Resource posterResource = uploadService.storeBase64Image(req.getPoster(), null); + Resource posterResource = uploadService.storeBase64Image(PlayEduBContext.getAdminUserID(), req.getPoster(), null); // 视频的封面素材改为[隐藏 && 属于视频的子素材] resourceService.changeParentId(posterResource.getId(), videoResource.getId()); // 视频信息 diff --git a/src/main/java/xyz/playedu/api/domain/Resource.java b/src/main/java/xyz/playedu/api/domain/Resource.java index b912fed..93fa4c8 100644 --- a/src/main/java/xyz/playedu/api/domain/Resource.java +++ b/src/main/java/xyz/playedu/api/domain/Resource.java @@ -4,25 +4,32 @@ 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 java.io.Serializable; import java.util.Date; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; /** + * * @TableName resources */ -@TableName(value = "resources") +@TableName(value ="resources") @Data public class Resource implements Serializable { /** - * + * */ @TableId(type = IdType.AUTO) private Integer id; + /** + * + */ + @JsonProperty("admin_id") + private Integer adminId; + /** * 类型 */ @@ -64,16 +71,22 @@ public class Resource implements Serializable { */ private String url; + /** + * + */ @JsonProperty("created_at") private Date createdAt; + /** + * 所属素材 + */ @JsonProperty("parent_id") private Integer parentId; /** * 隐藏[0:否,1:是] */ - @JsonProperty("is_hidden") + @JsonIgnore private Integer isHidden; @TableField(exist = false) @@ -92,17 +105,18 @@ public class Resource implements Serializable { } Resource other = (Resource) that; return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) - && (this.getType() == null ? other.getType() == null : this.getType().equals(other.getType())) - && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) - && (this.getExtension() == null ? other.getExtension() == null : this.getExtension().equals(other.getExtension())) - && (this.getSize() == null ? other.getSize() == null : this.getSize().equals(other.getSize())) - && (this.getDisk() == null ? other.getDisk() == null : this.getDisk().equals(other.getDisk())) - && (this.getFileId() == null ? other.getFileId() == null : this.getFileId().equals(other.getFileId())) - && (this.getPath() == null ? other.getPath() == null : this.getPath().equals(other.getPath())) - && (this.getUrl() == null ? other.getUrl() == null : this.getUrl().equals(other.getUrl())) - && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt())) - && (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId())) - && (this.getIsHidden() == null ? other.getIsHidden() == null : this.getIsHidden().equals(other.getIsHidden())); + && (this.getAdminId() == null ? other.getAdminId() == null : this.getAdminId().equals(other.getAdminId())) + && (this.getType() == null ? other.getType() == null : this.getType().equals(other.getType())) + && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) + && (this.getExtension() == null ? other.getExtension() == null : this.getExtension().equals(other.getExtension())) + && (this.getSize() == null ? other.getSize() == null : this.getSize().equals(other.getSize())) + && (this.getDisk() == null ? other.getDisk() == null : this.getDisk().equals(other.getDisk())) + && (this.getFileId() == null ? other.getFileId() == null : this.getFileId().equals(other.getFileId())) + && (this.getPath() == null ? other.getPath() == null : this.getPath().equals(other.getPath())) + && (this.getUrl() == null ? other.getUrl() == null : this.getUrl().equals(other.getUrl())) + && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt())) + && (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId())) + && (this.getIsHidden() == null ? other.getIsHidden() == null : this.getIsHidden().equals(other.getIsHidden())); } @Override @@ -110,6 +124,7 @@ public class Resource implements Serializable { final int prime = 31; int result = 1; result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getAdminId() == null) ? 0 : getAdminId().hashCode()); result = prime * result + ((getType() == null) ? 0 : getType().hashCode()); result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); result = prime * result + ((getExtension() == null) ? 0 : getExtension().hashCode()); @@ -131,6 +146,7 @@ public class Resource implements Serializable { sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); + sb.append(", adminId=").append(adminId); sb.append(", type=").append(type); sb.append(", name=").append(name); sb.append(", extension=").append(extension); diff --git a/src/main/java/xyz/playedu/api/mapper/ResourceMapper.java b/src/main/java/xyz/playedu/api/mapper/ResourceMapper.java index 1b2857d..b922d39 100644 --- a/src/main/java/xyz/playedu/api/mapper/ResourceMapper.java +++ b/src/main/java/xyz/playedu/api/mapper/ResourceMapper.java @@ -7,7 +7,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * @author tengteng * @description 针对表【resources】的数据库操作Mapper -* @createDate 2023-03-08 16:55:59 +* @createDate 2023-03-13 10:25:30 * @Entity xyz.playedu.api.domain.Resource */ @Mapper diff --git a/src/main/java/xyz/playedu/api/service/ResourceService.java b/src/main/java/xyz/playedu/api/service/ResourceService.java index 253d3d6..0826fe9 100644 --- a/src/main/java/xyz/playedu/api/service/ResourceService.java +++ b/src/main/java/xyz/playedu/api/service/ResourceService.java @@ -6,8 +6,6 @@ import xyz.playedu.api.exception.NotFoundException; import xyz.playedu.api.types.paginate.PaginationResult; import xyz.playedu.api.types.paginate.ResourcePaginateFilter; -import java.util.List; - /** * @author tengteng * @description 针对表【resources】的数据库操作Service @@ -17,7 +15,7 @@ public interface ResourceService extends IService { PaginationResult paginate(int page, int size, ResourcePaginateFilter filter); - Resource create(String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url); + Resource create(Integer adminId, String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url); Resource findOrFail(Integer id) throws NotFoundException; diff --git a/src/main/java/xyz/playedu/api/service/UploadService.java b/src/main/java/xyz/playedu/api/service/UploadService.java index 7e6038c..cf741c5 100644 --- a/src/main/java/xyz/playedu/api/service/UploadService.java +++ b/src/main/java/xyz/playedu/api/service/UploadService.java @@ -9,7 +9,7 @@ import xyz.playedu.api.exception.ServiceException; * @create 2023/3/8 14:02 */ public interface UploadService { - Resource storeMinio(MultipartFile file, String categoryIds) throws ServiceException; + Resource storeMinio(Integer adminId, MultipartFile file, String categoryIds) throws ServiceException; - Resource storeBase64Image(String content, String categoryIds) throws ServiceException; + Resource storeBase64Image(Integer adminId, String content, String categoryIds) throws ServiceException; } diff --git a/src/main/java/xyz/playedu/api/service/impl/ResourceServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/ResourceServiceImpl.java index 8005575..743cca8 100644 --- a/src/main/java/xyz/playedu/api/service/impl/ResourceServiceImpl.java +++ b/src/main/java/xyz/playedu/api/service/impl/ResourceServiceImpl.java @@ -54,6 +54,9 @@ public class ResourceServiceImpl extends ServiceImpl i if (filter.getType() != null) { wrapper.eq("type", filter.getType()); } + if (filter.getAdminId() != null && !filter.getAdminId().equals(0)) { + wrapper.eq("admin_id", filter.getAdminId()); + } if (filter.getCategoryIds() != null && filter.getCategoryIds().trim().length() > 0) { List categoryIds = Arrays.stream(filter.getCategoryIds().split(",")).map(Integer::valueOf).toList(); List ids = relationService.getRidsByCids(categoryIds); @@ -89,8 +92,9 @@ public class ResourceServiceImpl extends ServiceImpl i @Override @Transactional - public Resource create(String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url) { + public Resource create(Integer adminId, String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url) { Resource resource = new Resource(); + resource.setAdminId(adminId); resource.setType(type); resource.setName(filename); resource.setExtension(ext); diff --git a/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java index 3c5bb27..71e2fc8 100644 --- a/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java +++ b/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java @@ -8,14 +8,11 @@ import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.domain.Resource; import xyz.playedu.api.exception.ServiceException; import xyz.playedu.api.service.MinioService; -import xyz.playedu.api.service.ResourceCategoryService; import xyz.playedu.api.service.ResourceService; import xyz.playedu.api.service.UploadService; import xyz.playedu.api.util.Base64Util; import xyz.playedu.api.util.HelperUtil; -import java.util.List; - /** * @Author 杭州白书科技有限公司 * @create 2023/3/8 14:02 @@ -31,7 +28,7 @@ public class UploadServiceImpl implements UploadService { private MinioService minioService; @Override - public Resource storeMinio(MultipartFile file, String categoryIds) throws ServiceException { + public Resource storeMinio(Integer adminId, MultipartFile file, String categoryIds) throws ServiceException { if (file == null || file.isEmpty() || file.getOriginalFilename() == null) { throw new ServiceException("请上传文件"); } @@ -53,11 +50,11 @@ public class UploadServiceImpl implements UploadService { // 保存文件 String url = minioService.saveFile(file, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext)); // 上传记录 - return resourceService.create(categoryIds, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); + return resourceService.create(adminId, categoryIds, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); } @Override - public Resource storeBase64Image(String content, String categoryIds) throws ServiceException { + public Resource storeBase64Image(Integer adminId, String content, String categoryIds) throws ServiceException { // data:image/jpeg;base64, String[] base64Rows = content.split(","); // 解析出content-type @@ -78,6 +75,6 @@ public class UploadServiceImpl implements UploadService { // 保存文件 String url = minioService.saveBytes(binary, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext)); // 上传记录 - return resourceService.create(categoryIds, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); + return resourceService.create(adminId, categoryIds, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); } } diff --git a/src/main/java/xyz/playedu/api/types/paginate/ResourcePaginateFilter.java b/src/main/java/xyz/playedu/api/types/paginate/ResourcePaginateFilter.java index 9be5c70..9d1aa43 100644 --- a/src/main/java/xyz/playedu/api/types/paginate/ResourcePaginateFilter.java +++ b/src/main/java/xyz/playedu/api/types/paginate/ResourcePaginateFilter.java @@ -23,4 +23,6 @@ public class ResourcePaginateFilter { private String type; + private Integer adminId; + } diff --git a/src/main/resources/mapper/ResourceMapper.xml b/src/main/resources/mapper/ResourceMapper.xml index 1712aff..39e29f3 100644 --- a/src/main/resources/mapper/ResourceMapper.xml +++ b/src/main/resources/mapper/ResourceMapper.xml @@ -6,6 +6,7 @@ + @@ -20,9 +21,10 @@ - id,type,name, - extension,size,disk, - file_id,path,url, - created_at,parent_id,is_hidden + id,admin_id,type, + name,extension,size, + disk,file_id,path, + url,created_at,parent_id, + is_hidden