Wenjun Ruan
11 months ago
committed by
GitHub
69 changed files with 334 additions and 2497 deletions
@ -1,106 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.api.permission; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
|
||||||
import org.apache.dolphinscheduler.common.enums.UserType; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Resource; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.User; |
|
||||||
import org.apache.dolphinscheduler.dao.mapper.ResourceMapper; |
|
||||||
import org.apache.dolphinscheduler.dao.mapper.ResourceUserMapper; |
|
||||||
|
|
||||||
import java.util.Arrays; |
|
||||||
import java.util.HashSet; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Set; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions; |
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
import org.junit.jupiter.api.extension.ExtendWith; |
|
||||||
import org.mockito.InjectMocks; |
|
||||||
import org.mockito.Mock; |
|
||||||
import org.mockito.Mockito; |
|
||||||
import org.mockito.junit.jupiter.MockitoExtension; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class) |
|
||||||
public class FilePermissionCheckTest { |
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(FilePermissionCheckTest.class); |
|
||||||
@InjectMocks |
|
||||||
private ResourcePermissionCheckServiceImpl.FilePermissionCheck filePermissionCheck; |
|
||||||
|
|
||||||
@Mock |
|
||||||
private ResourceMapper resourceMapper; |
|
||||||
|
|
||||||
@Mock |
|
||||||
private ResourceUserMapper resourceUserMapper; |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testPermissionCheck() { |
|
||||||
User user = getLoginUser(); |
|
||||||
Assertions.assertTrue(filePermissionCheck.permissionCheck(user.getId(), null, logger)); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testAuthorizationTypes() { |
|
||||||
List<AuthorizationType> authorizationTypes = filePermissionCheck.authorizationTypes(); |
|
||||||
Assertions.assertEquals(Arrays.asList(AuthorizationType.RESOURCE_FILE_ID, AuthorizationType.UDF_FILE), |
|
||||||
authorizationTypes); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testListAuthorizedResourceIds() { |
|
||||||
// ADMIN_USER
|
|
||||||
User user = getAdminUser(); |
|
||||||
Resource resource = new Resource(); |
|
||||||
Set<Integer> ids = new HashSet(); |
|
||||||
ids.add(resource.getId()); |
|
||||||
List<Resource> resources = Arrays.asList(resource); |
|
||||||
|
|
||||||
Mockito.when(resourceMapper.queryResourceListAuthored(user.getId(), -1)).thenReturn(resources); |
|
||||||
Assertions.assertEquals(ids, filePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
|
||||||
|
|
||||||
// GENERAL_USER
|
|
||||||
user = getLoginUser(); |
|
||||||
Resource resource1 = new Resource(); |
|
||||||
ids.add(resource1.getId()); |
|
||||||
Mockito.when(resourceMapper.queryResourceListAuthored(user.getId(), -1)).thenReturn(resources); |
|
||||||
Mockito.when(resourceUserMapper.queryResourcesIdListByUserIdAndPerm(user.getId(), 0)) |
|
||||||
.thenReturn(Arrays.asList(resource1.getId())); |
|
||||||
|
|
||||||
Assertions.assertEquals(ids, filePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
|
||||||
} |
|
||||||
|
|
||||||
private User getLoginUser() { |
|
||||||
User loginUser = new User(); |
|
||||||
loginUser.setUserType(UserType.GENERAL_USER); |
|
||||||
loginUser.setUserName("test"); |
|
||||||
loginUser.setId(1); |
|
||||||
return loginUser; |
|
||||||
} |
|
||||||
|
|
||||||
private User getAdminUser() { |
|
||||||
User loginUser = new User(); |
|
||||||
loginUser.setUserType(UserType.ADMIN_USER); |
|
||||||
loginUser.setUserName("test"); |
|
||||||
loginUser.setId(0); |
|
||||||
return loginUser; |
|
||||||
} |
|
||||||
} |
|
@ -1,166 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.dao.entity; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.spi.enums.ResourceType; |
|
||||||
|
|
||||||
import java.util.Date; |
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
import lombok.Data; |
|
||||||
import lombok.NoArgsConstructor; |
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType; |
|
||||||
import com.baomidou.mybatisplus.annotation.TableField; |
|
||||||
import com.baomidou.mybatisplus.annotation.TableId; |
|
||||||
import com.baomidou.mybatisplus.annotation.TableName; |
|
||||||
|
|
||||||
@Data |
|
||||||
@NoArgsConstructor |
|
||||||
@TableName("t_ds_resources") |
|
||||||
public class Resource { |
|
||||||
|
|
||||||
/** |
|
||||||
* id |
|
||||||
*/ |
|
||||||
@TableId(value = "id", type = IdType.AUTO) |
|
||||||
private Integer id; |
|
||||||
|
|
||||||
/** |
|
||||||
* parent id |
|
||||||
*/ |
|
||||||
private int pid; |
|
||||||
|
|
||||||
/** |
|
||||||
* resource alias |
|
||||||
*/ |
|
||||||
private String alias; |
|
||||||
|
|
||||||
/** |
|
||||||
* full name |
|
||||||
*/ |
|
||||||
private String fullName; |
|
||||||
|
|
||||||
/** |
|
||||||
* is directory |
|
||||||
*/ |
|
||||||
private boolean isDirectory = false; |
|
||||||
|
|
||||||
/** |
|
||||||
* description |
|
||||||
*/ |
|
||||||
private String description; |
|
||||||
|
|
||||||
/** |
|
||||||
* file alias |
|
||||||
*/ |
|
||||||
private String fileName; |
|
||||||
|
|
||||||
/** |
|
||||||
* user id |
|
||||||
*/ |
|
||||||
private int userId; |
|
||||||
|
|
||||||
/** |
|
||||||
* resource type |
|
||||||
*/ |
|
||||||
private ResourceType type; |
|
||||||
|
|
||||||
/** |
|
||||||
* resource size |
|
||||||
*/ |
|
||||||
private long size; |
|
||||||
|
|
||||||
/** |
|
||||||
* create time |
|
||||||
*/ |
|
||||||
private Date createTime; |
|
||||||
|
|
||||||
/** |
|
||||||
* update time |
|
||||||
*/ |
|
||||||
private Date updateTime; |
|
||||||
|
|
||||||
/** |
|
||||||
* user name |
|
||||||
*/ |
|
||||||
@TableField(exist = false) |
|
||||||
private String userName; |
|
||||||
|
|
||||||
public Resource(int id, String alias, String fileName, String description, int userId, |
|
||||||
ResourceType type, long size, |
|
||||||
Date createTime, Date updateTime) { |
|
||||||
this.id = id; |
|
||||||
this.alias = alias; |
|
||||||
this.fileName = fileName; |
|
||||||
this.description = description; |
|
||||||
this.userId = userId; |
|
||||||
this.type = type; |
|
||||||
this.size = size; |
|
||||||
this.createTime = createTime; |
|
||||||
this.updateTime = updateTime; |
|
||||||
} |
|
||||||
|
|
||||||
public Resource(int id, int pid, String alias, String fullName, boolean isDirectory) { |
|
||||||
this.id = id; |
|
||||||
this.pid = pid; |
|
||||||
this.alias = alias; |
|
||||||
this.fullName = fullName; |
|
||||||
this.isDirectory = isDirectory; |
|
||||||
} |
|
||||||
|
|
||||||
public Resource(int pid, String alias, String fullName, boolean isDirectory, String description, String fileName, |
|
||||||
int userId, ResourceType type, long size, Date createTime, Date updateTime) { |
|
||||||
this.pid = pid; |
|
||||||
this.alias = alias; |
|
||||||
this.fullName = fullName; |
|
||||||
this.isDirectory = isDirectory; |
|
||||||
this.description = description; |
|
||||||
this.fileName = fileName; |
|
||||||
this.userId = userId; |
|
||||||
this.type = type; |
|
||||||
this.size = size; |
|
||||||
this.createTime = createTime; |
|
||||||
this.updateTime = updateTime; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean equals(Object o) { |
|
||||||
if (this == o) { |
|
||||||
return true; |
|
||||||
} |
|
||||||
if (o == null || getClass() != o.getClass()) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
|
|
||||||
Resource resource = (Resource) o; |
|
||||||
|
|
||||||
if (!Objects.equals(id, resource.id)) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
return alias.equals(resource.alias); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public int hashCode() { |
|
||||||
int result = id; |
|
||||||
result = 31 * result + alias.hashCode(); |
|
||||||
return result; |
|
||||||
} |
|
||||||
} |
|
@ -1,61 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.dao.entity; |
|
||||||
|
|
||||||
import java.util.Date; |
|
||||||
|
|
||||||
import lombok.Data; |
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType; |
|
||||||
import com.baomidou.mybatisplus.annotation.TableId; |
|
||||||
import com.baomidou.mybatisplus.annotation.TableName; |
|
||||||
|
|
||||||
@Data |
|
||||||
@TableName("t_ds_relation_resources_user") |
|
||||||
public class ResourcesUser { |
|
||||||
|
|
||||||
/** |
|
||||||
* id |
|
||||||
*/ |
|
||||||
@TableId(value = "id", type = IdType.AUTO) |
|
||||||
private Integer id; |
|
||||||
|
|
||||||
/** |
|
||||||
* user id |
|
||||||
*/ |
|
||||||
private int userId; |
|
||||||
|
|
||||||
/** |
|
||||||
* resource id |
|
||||||
*/ |
|
||||||
private int resourcesId; |
|
||||||
|
|
||||||
/** |
|
||||||
* permission |
|
||||||
*/ |
|
||||||
private int perm; |
|
||||||
|
|
||||||
/** |
|
||||||
* create time |
|
||||||
*/ |
|
||||||
private Date createTime; |
|
||||||
|
|
||||||
/** |
|
||||||
* update time |
|
||||||
*/ |
|
||||||
private Date updateTime; |
|
||||||
} |
|
@ -1,157 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.dao.mapper; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.dao.entity.Resource; |
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Param; |
|
||||||
|
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
||||||
|
|
||||||
/** |
|
||||||
* resource mapper interface
|
|
||||||
*/ |
|
||||||
public interface ResourceMapper extends BaseMapper<Resource> { |
|
||||||
|
|
||||||
/** |
|
||||||
* query resource list |
|
||||||
* @param fullName full name |
|
||||||
* @param userId userId |
|
||||||
* @param type type |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
List<Resource> queryResourceList(@Param("fullName") String fullName, |
|
||||||
@Param("userId") int userId, |
|
||||||
@Param("type") int type); |
|
||||||
|
|
||||||
/** |
|
||||||
* query resource list |
|
||||||
* @param userId userId |
|
||||||
* @param type type |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
List<Resource> queryResourceListAuthored(@Param("userId") int userId, |
|
||||||
@Param("type") int type); |
|
||||||
|
|
||||||
/** |
|
||||||
* resource page |
|
||||||
* @param page page |
|
||||||
* @param id id |
|
||||||
* @param type type |
|
||||||
* @param searchVal searchVal |
|
||||||
* @param resIds resIds |
|
||||||
* @return resource page |
|
||||||
*/ |
|
||||||
IPage<Resource> queryResourcePaging(IPage<Resource> page, |
|
||||||
@Param("id") int id, |
|
||||||
@Param("type") int type, |
|
||||||
@Param("searchVal") String searchVal, |
|
||||||
@Param("resIds") List<Integer> resIds); |
|
||||||
|
|
||||||
/** |
|
||||||
* query resource except userId |
|
||||||
* @param userId userId |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
List<Resource> queryResourceExceptUserId(@Param("userId") int userId); |
|
||||||
|
|
||||||
/** |
|
||||||
* list authorized resource |
|
||||||
* @param userId userId |
|
||||||
* @param resNames resNames |
|
||||||
* @param <T> T |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
<T> List<Resource> listAuthorizedResource(@Param("userId") int userId, @Param("resNames") T[] resNames); |
|
||||||
|
|
||||||
/** |
|
||||||
* list resources by id |
|
||||||
* @param resIds resIds |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
List<Resource> queryResourceListById(@Param("resIds") List<Integer> resIds); |
|
||||||
|
|
||||||
/** |
|
||||||
* list authorized resource |
|
||||||
* @param userId userId |
|
||||||
* @param resIds resIds |
|
||||||
* @param <T> T |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
<T> List<Resource> listAuthorizedResourceById(@Param("userId") int userId, @Param("resIds") T[] resIds); |
|
||||||
|
|
||||||
/** |
|
||||||
* delete resource by id array |
|
||||||
* @param resIds resource id array |
|
||||||
* @return delete num |
|
||||||
*/ |
|
||||||
int deleteIds(@Param("resIds") Integer[] resIds); |
|
||||||
|
|
||||||
/** |
|
||||||
* list children |
|
||||||
* @param direcotyId directory id |
|
||||||
* @return resource id array |
|
||||||
*/ |
|
||||||
List<Integer> listChildren(@Param("direcotyId") int direcotyId); |
|
||||||
|
|
||||||
/** |
|
||||||
* query resource by full name or pid |
|
||||||
* @param fullName full name |
|
||||||
* @param type resource type |
|
||||||
* @return resource |
|
||||||
*/ |
|
||||||
List<Resource> queryResource(@Param("fullName") String fullName, @Param("type") int type); |
|
||||||
|
|
||||||
/** |
|
||||||
* list resource by id array |
|
||||||
* @param resIds resource id array |
|
||||||
* @return resource list |
|
||||||
*/ |
|
||||||
List<Resource> listResourceByIds(@Param("resIds") Integer[] resIds); |
|
||||||
|
|
||||||
/** |
|
||||||
* update resource |
|
||||||
* @param resourceList resource list |
|
||||||
* @return update num |
|
||||||
*/ |
|
||||||
int batchUpdateResource(@Param("resourceList") List<Resource> resourceList); |
|
||||||
|
|
||||||
/** |
|
||||||
* check resource exist |
|
||||||
* @param fullName full name |
|
||||||
* @param userId userId |
|
||||||
* @param type type |
|
||||||
* @return true if exist else return null |
|
||||||
*/ |
|
||||||
Boolean existResourceByUser(@Param("fullName") String fullName, |
|
||||||
@Param("userId") int userId, |
|
||||||
@Param("type") int type); |
|
||||||
|
|
||||||
/** |
|
||||||
* check resource exist |
|
||||||
* @param fullName full name |
|
||||||
* @param type type |
|
||||||
* @return true if exist else return null |
|
||||||
*/ |
|
||||||
Boolean existResource(@Param("fullName") String fullName, |
|
||||||
@Param("type") int type); |
|
||||||
|
|
||||||
} |
|
@ -1,60 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.dao.mapper; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.dao.entity.ResourcesUser; |
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Param; |
|
||||||
|
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|
||||||
|
|
||||||
/** |
|
||||||
* resource user relation mapper interface
|
|
||||||
*/ |
|
||||||
public interface ResourceUserMapper extends BaseMapper<ResourcesUser> { |
|
||||||
|
|
||||||
/** |
|
||||||
* query resourcesId list by userId and perm |
|
||||||
* @param userId userId |
|
||||||
* @param perm perm |
|
||||||
* @return resourcesId list result |
|
||||||
*/ |
|
||||||
List<Integer> queryResourcesIdListByUserIdAndPerm(@Param("userId") int userId, |
|
||||||
@Param("perm") int perm); |
|
||||||
|
|
||||||
/** |
|
||||||
* delete resource user relation |
|
||||||
* @param userId userId |
|
||||||
* @param resourceId resourceId |
|
||||||
* @return delete result |
|
||||||
*/ |
|
||||||
int deleteResourceUser(@Param("userId") int userId, |
|
||||||
@Param("resourceId") int resourceId); |
|
||||||
|
|
||||||
/** |
|
||||||
* delete resource user relation |
|
||||||
* @param userId userId |
|
||||||
* @param resIds resource Ids |
|
||||||
* @return delete result |
|
||||||
*/ |
|
||||||
int deleteResourceUserArray(@Param("userId") int userId, |
|
||||||
@Param("resIds") Integer[] resIds); |
|
||||||
|
|
||||||
} |
|
@ -1,198 +0,0 @@ |
|||||||
<?xml version="1.0" encoding="UTF-8" ?> |
|
||||||
<!-- |
|
||||||
~ 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. |
|
||||||
--> |
|
||||||
|
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > |
|
||||||
<mapper namespace="org.apache.dolphinscheduler.dao.mapper.ResourceMapper"> |
|
||||||
<sql id="baseSqlV2"> |
|
||||||
${alias}.id, ${alias}.alias, ${alias}.file_name, ${alias}.description, ${alias}.user_id, ${alias}.type, ${alias}.size, ${alias}.create_time, ${alias}.update_time, |
|
||||||
${alias}.pid, ${alias}.full_name, ${alias}.is_directory |
|
||||||
</sql> |
|
||||||
<select id="queryResourceList" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where 1= 1 |
|
||||||
<if test="fullName != null and fullName != ''"> |
|
||||||
and r.full_name = #{fullName} |
|
||||||
</if> |
|
||||||
<if test="type != -1"> |
|
||||||
and r.type = #{type} |
|
||||||
</if> |
|
||||||
<if test="userId != 0"> |
|
||||||
and r.user_id = #{userId} |
|
||||||
</if> |
|
||||||
</select> |
|
||||||
<select id="queryResourceListAuthored" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where 1 = 1 |
|
||||||
<if test="type != -1"> |
|
||||||
and r.type=#{type} |
|
||||||
</if> |
|
||||||
<if test="userId != 0"> |
|
||||||
and r.user_id=#{userId} |
|
||||||
</if> |
|
||||||
</select> |
|
||||||
<select id="queryResourcePaging" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="d"/> |
|
||||||
</include> |
|
||||||
,u.user_name |
|
||||||
from t_ds_resources d |
|
||||||
left join t_ds_user u on u.id = d.user_id |
|
||||||
where 1=1 and d.type=#{type} and d.pid=#{id} |
|
||||||
<if test="resIds != null and resIds.size() > 0"> |
|
||||||
and d.id in |
|
||||||
<foreach collection="resIds" item="i" open="(" close=")" separator=","> |
|
||||||
#{i} |
|
||||||
</foreach> |
|
||||||
</if> |
|
||||||
<if test="searchVal != null and searchVal != ''"> |
|
||||||
and d.alias like concat('%', #{searchVal}, '%') |
|
||||||
</if> |
|
||||||
order by d.update_time desc |
|
||||||
</select> |
|
||||||
|
|
||||||
<select id="queryResourceExceptUserId" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where r.user_id <![CDATA[ <> ]]> #{userId} |
|
||||||
</select> |
|
||||||
|
|
||||||
<select id="listAuthorizedResource" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where r.type = 0 |
|
||||||
and r.user_id=#{userId} |
|
||||||
|
|
||||||
<if test="resNames != null and resNames.length > 0"> |
|
||||||
and full_name in |
|
||||||
<foreach collection="resNames" item="i" open="(" close=")" separator=","> |
|
||||||
#{i} |
|
||||||
</foreach> |
|
||||||
</if> |
|
||||||
</select> |
|
||||||
<select id="queryResourceListById" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where 1 = 1 |
|
||||||
<if test="resIds != null and resIds.size() > 0"> |
|
||||||
and r.id in |
|
||||||
<foreach collection="resIds" item="i" open="(" close=")" separator=","> |
|
||||||
#{i} |
|
||||||
</foreach> |
|
||||||
</if> |
|
||||||
</select> |
|
||||||
<select id="listAuthorizedResourceById" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where r.user_id=#{userId} |
|
||||||
<if test="resIds != null and resIds.length > 0"> |
|
||||||
and id in |
|
||||||
<foreach collection="resIds" item="i" open="(" close=")" separator=","> |
|
||||||
#{i} |
|
||||||
</foreach> |
|
||||||
</if> |
|
||||||
</select> |
|
||||||
|
|
||||||
<delete id="deleteIds" parameterType="java.lang.Integer"> |
|
||||||
delete from t_ds_resources |
|
||||||
<if test="resIds != null and resIds.length != 0"> |
|
||||||
where id in |
|
||||||
<foreach collection="resIds" item="i" open="(" close=")" separator=","> |
|
||||||
#{i} |
|
||||||
</foreach> |
|
||||||
</if> |
|
||||||
</delete> |
|
||||||
|
|
||||||
<select id="listChildren" resultType="java.lang.Integer"> |
|
||||||
select id |
|
||||||
from t_ds_resources |
|
||||||
where pid = #{direcotyId} |
|
||||||
</select> |
|
||||||
|
|
||||||
<select id="queryResource" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
where r.type = #{type} |
|
||||||
and r.full_name = #{fullName} |
|
||||||
</select> |
|
||||||
|
|
||||||
<update id="batchUpdateResource" parameterType="java.util.List"> |
|
||||||
<foreach collection="resourceList" item="resource" index="index" open="" close="" separator=";"> |
|
||||||
update t_ds_resources |
|
||||||
<set> |
|
||||||
full_name=#{resource.fullName}, |
|
||||||
update_time=#{resource.updateTime} |
|
||||||
</set> |
|
||||||
<where> |
|
||||||
id=#{resource.id} |
|
||||||
</where> |
|
||||||
</foreach> |
|
||||||
</update> |
|
||||||
|
|
||||||
<select id="listResourceByIds" resultType="org.apache.dolphinscheduler.dao.entity.Resource"> |
|
||||||
select |
|
||||||
<include refid="baseSqlV2"> |
|
||||||
<property name="alias" value="r"/> |
|
||||||
</include> |
|
||||||
from t_ds_resources r |
|
||||||
<if test="resIds != null and resIds.length != 0"> |
|
||||||
where r.id in |
|
||||||
<foreach collection="resIds" item="i" open="(" close=")" separator=","> |
|
||||||
#{i} |
|
||||||
</foreach> |
|
||||||
</if> |
|
||||||
</select> |
|
||||||
|
|
||||||
<select id="existResourceByUser" resultType="java.lang.Boolean"> |
|
||||||
select 1 |
|
||||||
from t_ds_resources |
|
||||||
where full_name = #{fullName} |
|
||||||
and type = #{type} |
|
||||||
and user_id = #{userId} limit 1 |
|
||||||
</select> |
|
||||||
|
|
||||||
<select id="existResource" resultType="java.lang.Boolean"> |
|
||||||
select 1 |
|
||||||
from t_ds_resources |
|
||||||
where full_name = #{fullName} |
|
||||||
and type = #{type} limit 1 |
|
||||||
</select> |
|
||||||
</mapper> |
|
@ -1,440 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.dao.mapper; |
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.constants.Constants; |
|
||||||
import org.apache.dolphinscheduler.common.enums.UserType; |
|
||||||
import org.apache.dolphinscheduler.dao.BaseDaoTest; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Resource; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.ResourcesUser; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.Tenant; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.User; |
|
||||||
import org.apache.dolphinscheduler.spi.enums.ResourceType; |
|
||||||
|
|
||||||
import org.apache.commons.collections4.CollectionUtils; |
|
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.Arrays; |
|
||||||
import java.util.Date; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions; |
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
||||||
|
|
||||||
public class ResourceMapperTest extends BaseDaoTest { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private ResourceMapper resourceMapper; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private ResourceUserMapper resourceUserMapper; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private TenantMapper tenantMapper; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private UserMapper userMapper; |
|
||||||
|
|
||||||
/** |
|
||||||
* insert |
|
||||||
* |
|
||||||
* @return Resource |
|
||||||
*/ |
|
||||||
private Resource insertOne() { |
|
||||||
// insertOne
|
|
||||||
Resource resource = new Resource(); |
|
||||||
resource.setAlias("ut-resource"); |
|
||||||
resource.setFullName("/ut-resource"); |
|
||||||
resource.setPid(-1); |
|
||||||
resource.setDirectory(false); |
|
||||||
resource.setType(ResourceType.FILE); |
|
||||||
resource.setUserId(111); |
|
||||||
int status = resourceMapper.insert(resource); |
|
||||||
if (status != 1) { |
|
||||||
Assertions.fail("insert data error"); |
|
||||||
} |
|
||||||
return resource; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* create resource by user |
|
||||||
* |
|
||||||
* @param user user |
|
||||||
* @return Resource |
|
||||||
*/ |
|
||||||
private Resource createResource(User user, boolean isDirectory, ResourceType resourceType, int pid, String alias, |
|
||||||
String fullName) { |
|
||||||
// insertOne
|
|
||||||
Resource resource = new Resource(); |
|
||||||
resource.setDirectory(isDirectory); |
|
||||||
resource.setType(resourceType); |
|
||||||
resource.setAlias(alias); |
|
||||||
resource.setFullName(fullName); |
|
||||||
resource.setUserId(user.getId()); |
|
||||||
int status = resourceMapper.insert(resource); |
|
||||||
if (status != 1) { |
|
||||||
Assertions.fail("insert data error"); |
|
||||||
} |
|
||||||
return resource; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* create resource by user |
|
||||||
* |
|
||||||
* @param user user |
|
||||||
* @return Resource |
|
||||||
*/ |
|
||||||
private Resource createResource(User user) { |
|
||||||
// insertOne
|
|
||||||
String alias = String.format("ut-resource-%s", user.getUserName()); |
|
||||||
String fullName = String.format("/%s", alias); |
|
||||||
|
|
||||||
Resource resource = createResource(user, false, ResourceType.FILE, -1, alias, fullName); |
|
||||||
return resource; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* create user |
|
||||||
* |
|
||||||
* @return User |
|
||||||
*/ |
|
||||||
private User createGeneralUser(String userName) { |
|
||||||
User user = new User(); |
|
||||||
user.setUserName(userName); |
|
||||||
user.setUserPassword("1"); |
|
||||||
user.setEmail("xx@123.com"); |
|
||||||
user.setUserType(UserType.GENERAL_USER); |
|
||||||
user.setCreateTime(new Date()); |
|
||||||
user.setTenantId(1); |
|
||||||
user.setUpdateTime(new Date()); |
|
||||||
int status = userMapper.insert(user); |
|
||||||
|
|
||||||
if (status != 1) { |
|
||||||
Assertions.fail("insert data error"); |
|
||||||
} |
|
||||||
return user; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* create resource user |
|
||||||
* |
|
||||||
* @return ResourcesUser |
|
||||||
*/ |
|
||||||
private ResourcesUser createResourcesUser(Resource resource, User user) { |
|
||||||
// insertOne
|
|
||||||
ResourcesUser resourcesUser = new ResourcesUser(); |
|
||||||
resourcesUser.setCreateTime(new Date()); |
|
||||||
resourcesUser.setUpdateTime(new Date()); |
|
||||||
resourcesUser.setUserId(user.getId()); |
|
||||||
resourcesUser.setResourcesId(resource.getId()); |
|
||||||
resourcesUser.setPerm(7); |
|
||||||
resourceUserMapper.insert(resourcesUser); |
|
||||||
return resourcesUser; |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testInsert() { |
|
||||||
Resource resource = insertOne(); |
|
||||||
Assertions.assertNotNull(resource.getId()); |
|
||||||
Assertions.assertTrue(resource.getId() > 0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test update |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testUpdate() { |
|
||||||
// insertOne
|
|
||||||
Resource resource = insertOne(); |
|
||||||
resource.setCreateTime(new Date()); |
|
||||||
// update
|
|
||||||
int update = resourceMapper.updateById(resource); |
|
||||||
Assertions.assertEquals(1, update); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test delete |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testDelete() { |
|
||||||
Resource resourceMap = insertOne(); |
|
||||||
int delete = resourceMapper.deleteById(resourceMap.getId()); |
|
||||||
Assertions.assertEquals(1, delete); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test query |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQuery() { |
|
||||||
Resource resource = insertOne(); |
|
||||||
// query
|
|
||||||
List<Resource> resources = resourceMapper.selectList(null); |
|
||||||
Assertions.assertNotEquals(resources.size(), 0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test query resource list |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQueryResourceList() { |
|
||||||
|
|
||||||
Resource resource = insertOne(); |
|
||||||
|
|
||||||
String alias = ""; |
|
||||||
int userId = resource.getUserId(); |
|
||||||
int type = resource.getType().ordinal(); |
|
||||||
List<Resource> resources = resourceMapper.queryResourceList( |
|
||||||
alias, |
|
||||||
userId, |
|
||||||
type); |
|
||||||
|
|
||||||
Assertions.assertNotEquals(resources.size(), 0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test page |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQueryResourcePaging() { |
|
||||||
User user = new User(); |
|
||||||
user.setUserName("11"); |
|
||||||
user.setUserPassword("1"); |
|
||||||
user.setEmail("xx@123.com"); |
|
||||||
user.setUserType(UserType.GENERAL_USER); |
|
||||||
user.setCreateTime(new Date()); |
|
||||||
user.setTenantId(1); |
|
||||||
user.setUpdateTime(new Date()); |
|
||||||
userMapper.insert(user); |
|
||||||
Resource resource = new Resource(); |
|
||||||
resource.setAlias("ut-resource"); |
|
||||||
resource.setFullName("/ut-resource"); |
|
||||||
resource.setPid(-1); |
|
||||||
resource.setDirectory(false); |
|
||||||
resource.setType(ResourceType.FILE); |
|
||||||
resource.setUserId(user.getId()); |
|
||||||
resourceMapper.insert(resource); |
|
||||||
|
|
||||||
Page<Resource> page = new Page(1, 3); |
|
||||||
|
|
||||||
IPage<Resource> resourceIPage = resourceMapper.queryResourcePaging( |
|
||||||
page, |
|
||||||
-1, |
|
||||||
resource.getType().ordinal(), |
|
||||||
"", |
|
||||||
new ArrayList<>(resource.getId())); |
|
||||||
IPage<Resource> resourceIPage1 = resourceMapper.queryResourcePaging( |
|
||||||
page, |
|
||||||
-1, |
|
||||||
resource.getType().ordinal(), |
|
||||||
"", |
|
||||||
null); |
|
||||||
Assertions.assertEquals(resourceIPage.getTotal(), 1); |
|
||||||
Assertions.assertEquals(resourceIPage1.getTotal(), 1); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test authed resource list |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQueryResourceListAuthored() { |
|
||||||
Resource resource = insertOne(); |
|
||||||
|
|
||||||
List<Integer> resIds = resourceUserMapper.queryResourcesIdListByUserIdAndPerm(resource.getUserId(), |
|
||||||
Constants.AUTHORIZE_WRITABLE_PERM); |
|
||||||
List<Resource> resources = |
|
||||||
CollectionUtils.isEmpty(resIds) ? new ArrayList<>() : resourceMapper.queryResourceListById(resIds); |
|
||||||
|
|
||||||
ResourcesUser resourcesUser = new ResourcesUser(); |
|
||||||
|
|
||||||
resourcesUser.setResourcesId(resource.getId()); |
|
||||||
resourcesUser.setUserId(1110); |
|
||||||
resourcesUser.setPerm(Constants.AUTHORIZE_WRITABLE_PERM); |
|
||||||
resourceUserMapper.insert(resourcesUser); |
|
||||||
|
|
||||||
List<Integer> resIds1 = |
|
||||||
resourceUserMapper.queryResourcesIdListByUserIdAndPerm(1110, Constants.AUTHORIZE_WRITABLE_PERM); |
|
||||||
List<Resource> resources1 = |
|
||||||
CollectionUtils.isEmpty(resIds1) ? new ArrayList<>() : resourceMapper.queryResourceListById(resIds1); |
|
||||||
|
|
||||||
Assertions.assertEquals(0, resources.size()); |
|
||||||
Assertions.assertNotEquals(0, resources1.size()); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test authed resource list |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQueryAuthorizedResourceList() { |
|
||||||
Resource resource = insertOne(); |
|
||||||
|
|
||||||
List<Integer> resIds = resourceUserMapper.queryResourcesIdListByUserIdAndPerm(resource.getUserId(), |
|
||||||
Constants.AUTHORIZE_WRITABLE_PERM); |
|
||||||
List<Resource> resources = |
|
||||||
CollectionUtils.isEmpty(resIds) ? new ArrayList<>() : resourceMapper.queryResourceListById(resIds); |
|
||||||
|
|
||||||
resourceMapper.deleteById(resource.getId()); |
|
||||||
Assertions.assertEquals(0, resources.size()); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test query resource expect userId |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQueryResourceExceptUserId() { |
|
||||||
Resource resource = insertOne(); |
|
||||||
List<Resource> resources = resourceMapper.queryResourceExceptUserId( |
|
||||||
11111); |
|
||||||
Assertions.assertNotEquals(resources.size(), 0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test query tenant code by resource name |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQueryTenantCodeByResourceName() { |
|
||||||
|
|
||||||
Tenant tenant = new Tenant(); |
|
||||||
tenant.setTenantCode("ut tenant code for resource"); |
|
||||||
int tenantInsertStatus = tenantMapper.insert(tenant); |
|
||||||
|
|
||||||
if (tenantInsertStatus != 1) { |
|
||||||
Assertions.fail("insert tenant data error"); |
|
||||||
} |
|
||||||
|
|
||||||
User user = new User(); |
|
||||||
user.setTenantId(tenant.getId()); |
|
||||||
user.setUserName("ut user"); |
|
||||||
int userInsertStatus = userMapper.insert(user); |
|
||||||
|
|
||||||
if (userInsertStatus != 1) { |
|
||||||
Assertions.fail("insert user data error"); |
|
||||||
} |
|
||||||
|
|
||||||
Resource resource = insertOne(); |
|
||||||
resource.setUserId(user.getId()); |
|
||||||
int userUpdateStatus = resourceMapper.updateById(resource); |
|
||||||
if (userUpdateStatus != 1) { |
|
||||||
Assertions.fail("update user data error"); |
|
||||||
} |
|
||||||
|
|
||||||
List<Resource> resourceList = resourceMapper.queryResource(resource.getFullName(), ResourceType.FILE.ordinal()); |
|
||||||
|
|
||||||
int resourceUserId = resourceList.get(0).getUserId(); |
|
||||||
User resourceUser = userMapper.selectById(resourceUserId); |
|
||||||
Tenant resourceTenant = tenantMapper.selectById(resourceUser.getTenantId()); |
|
||||||
|
|
||||||
Assertions.assertEquals("ut tenant code for resource", resourceTenant.getTenantCode()); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testListAuthorizedResource() { |
|
||||||
// create a general user
|
|
||||||
User generalUser1 = createGeneralUser("user1"); |
|
||||||
User generalUser2 = createGeneralUser("user2"); |
|
||||||
// create one resource
|
|
||||||
Resource resource = createResource(generalUser2); |
|
||||||
Resource unauthorizedResource = createResource(generalUser1); |
|
||||||
|
|
||||||
// need download resources
|
|
||||||
String[] resNames = new String[]{resource.getFullName(), unauthorizedResource.getFullName()}; |
|
||||||
|
|
||||||
List<Resource> resources = resourceMapper.listAuthorizedResource(generalUser2.getId(), resNames); |
|
||||||
|
|
||||||
Assertions.assertEquals(generalUser2.getId().intValue(), resource.getUserId()); |
|
||||||
Assertions.assertFalse( |
|
||||||
resources.stream().map(t -> t.getFullName()).collect(toList()).containsAll(Arrays.asList(resNames))); |
|
||||||
|
|
||||||
// authorize object unauthorizedResource to generalUser
|
|
||||||
createResourcesUser(unauthorizedResource, generalUser2); |
|
||||||
List<Resource> authorizedResources = resourceMapper.listAuthorizedResource(generalUser2.getId(), resNames); |
|
||||||
Assertions.assertTrue(authorizedResources.stream().map(t -> t.getFullName()).collect(toList()) |
|
||||||
.containsAll(Arrays.asList(resource.getFullName()))); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void deleteIdsTest() { |
|
||||||
// create a general user
|
|
||||||
User generalUser1 = createGeneralUser("user1"); |
|
||||||
User generalUser = createGeneralUser("user"); |
|
||||||
|
|
||||||
Resource resource = createResource(generalUser); |
|
||||||
Resource resource1 = createResource(generalUser1); |
|
||||||
|
|
||||||
List<Integer> resourceList = new ArrayList<>(); |
|
||||||
resourceList.add(resource.getId()); |
|
||||||
resourceList.add(resource1.getId()); |
|
||||||
int result = resourceMapper.deleteIds(resourceList.toArray(new Integer[resourceList.size()])); |
|
||||||
Assertions.assertEquals(result, 2); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void queryResourceListAuthoredTest() { |
|
||||||
// create a general user
|
|
||||||
User generalUser1 = createGeneralUser("user1"); |
|
||||||
User generalUser2 = createGeneralUser("user2"); |
|
||||||
// create resource
|
|
||||||
Resource resource = createResource(generalUser1); |
|
||||||
createResourcesUser(resource, generalUser2); |
|
||||||
|
|
||||||
List<Resource> resourceList = |
|
||||||
resourceMapper.queryResourceListAuthored(generalUser2.getId(), ResourceType.FILE.ordinal()); |
|
||||||
Assertions.assertNotNull(resourceList); |
|
||||||
|
|
||||||
resourceList = resourceMapper.queryResourceListAuthored(generalUser2.getId(), ResourceType.FILE.ordinal()); |
|
||||||
Assertions.assertFalse(resourceList.contains(resource)); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void batchUpdateResourceTest() { |
|
||||||
// create a general user
|
|
||||||
User generalUser1 = createGeneralUser("user1"); |
|
||||||
// create resource
|
|
||||||
Resource resource = createResource(generalUser1); |
|
||||||
resource.setFullName(String.format("%s-update", resource.getFullName())); |
|
||||||
resource.setUpdateTime(new Date()); |
|
||||||
List<Resource> resourceList = new ArrayList<>(); |
|
||||||
resourceList.add(resource); |
|
||||||
int result = resourceMapper.batchUpdateResource(resourceList); |
|
||||||
if (result != resourceList.size()) { |
|
||||||
Assertions.fail("batch update resource data error"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void existResourceTest() { |
|
||||||
String fullName = "/ut-resource"; |
|
||||||
int userId = 111; |
|
||||||
int type = ResourceType.FILE.getCode(); |
|
||||||
Assertions.assertNull(resourceMapper.existResourceByUser(fullName, userId, type)); |
|
||||||
Assertions.assertNull(resourceMapper.existResource(fullName, type)); |
|
||||||
insertOne(); |
|
||||||
Assertions.assertTrue(resourceMapper.existResourceByUser(fullName, userId, type)); |
|
||||||
Assertions.assertTrue(resourceMapper.existResource(fullName, type)); |
|
||||||
} |
|
||||||
} |
|
@ -1,111 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.apache.dolphinscheduler.dao.mapper; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.constants.Constants; |
|
||||||
import org.apache.dolphinscheduler.dao.BaseDaoTest; |
|
||||||
import org.apache.dolphinscheduler.dao.entity.ResourcesUser; |
|
||||||
|
|
||||||
import java.util.Date; |
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions; |
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
|
|
||||||
public class ResourceUserMapperTest extends BaseDaoTest { |
|
||||||
|
|
||||||
@Autowired |
|
||||||
private ResourceUserMapper resourceUserMapper; |
|
||||||
|
|
||||||
/** |
|
||||||
* insert |
|
||||||
* @return ResourcesUser |
|
||||||
*/ |
|
||||||
private ResourcesUser insertOne() { |
|
||||||
// insertOne
|
|
||||||
ResourcesUser resourcesUser = new ResourcesUser(); |
|
||||||
resourcesUser.setCreateTime(new Date()); |
|
||||||
resourcesUser.setUpdateTime(new Date()); |
|
||||||
resourcesUser.setUserId(11111); |
|
||||||
resourcesUser.setResourcesId(1110); |
|
||||||
resourcesUser.setPerm(Constants.AUTHORIZE_WRITABLE_PERM); |
|
||||||
resourceUserMapper.insert(resourcesUser); |
|
||||||
return resourcesUser; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test update |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testUpdate() { |
|
||||||
// insertOne
|
|
||||||
ResourcesUser queue = insertOne(); |
|
||||||
queue.setCreateTime(new Date()); |
|
||||||
// update
|
|
||||||
int update = resourceUserMapper.updateById(queue); |
|
||||||
Assertions.assertEquals(1, update); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test delete |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testDelete() { |
|
||||||
ResourcesUser queue = insertOne(); |
|
||||||
int delete = resourceUserMapper.deleteById(queue.getId()); |
|
||||||
Assertions.assertEquals(1, delete); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test query |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testQuery() { |
|
||||||
ResourcesUser queue = insertOne(); |
|
||||||
// query
|
|
||||||
List<ResourcesUser> queues = resourceUserMapper.selectList(null); |
|
||||||
Assertions.assertNotEquals(0, queues.size()); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test delete |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testDeleteResourceUser() { |
|
||||||
|
|
||||||
ResourcesUser queue = insertOne(); |
|
||||||
int delete = resourceUserMapper.deleteResourceUser( |
|
||||||
queue.getUserId(), |
|
||||||
queue.getResourcesId()); |
|
||||||
Assertions.assertNotEquals(0, delete); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* test delete |
|
||||||
*/ |
|
||||||
@Test |
|
||||||
public void testDeleteResourceUserArray() { |
|
||||||
|
|
||||||
ResourcesUser resourcesUser = insertOne(); |
|
||||||
Integer[] resourceIdArray = new Integer[]{resourcesUser.getResourcesId()}; |
|
||||||
int delete = resourceUserMapper.deleteResourceUserArray( |
|
||||||
resourcesUser.getUserId(), |
|
||||||
resourceIdArray); |
|
||||||
Assertions.assertNotEquals(0, delete); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,67 @@ |
|||||||
|
/* |
||||||
|
* 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.apache.dolphinscheduler.plugin.task.api.resource; |
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import lombok.Builder; |
||||||
|
import lombok.Data; |
||||||
|
import lombok.NoArgsConstructor; |
||||||
|
import lombok.ToString; |
||||||
|
|
||||||
|
@ToString |
||||||
|
public class ResourceContext { |
||||||
|
|
||||||
|
/** |
||||||
|
* ResourceAbsolutePathInStorage -> ResourceItem |
||||||
|
*/ |
||||||
|
private final Map<String, ResourceItem> resourceItemMap; |
||||||
|
|
||||||
|
public ResourceContext() { |
||||||
|
this.resourceItemMap = new HashMap<>(); |
||||||
|
} |
||||||
|
|
||||||
|
public void addResourceItem(ResourceItem resourceItem) { |
||||||
|
checkNotNull(resourceItem); |
||||||
|
resourceItemMap.put(resourceItem.getResourceAbsolutePathInStorage(), resourceItem); |
||||||
|
} |
||||||
|
|
||||||
|
public ResourceItem getResourceItem(String resourceAbsolutePathInStorage) { |
||||||
|
ResourceItem resourceItem = resourceItemMap.get(resourceAbsolutePathInStorage); |
||||||
|
if (resourceItem == null) { |
||||||
|
throw new IllegalArgumentException("Cannot find the resourceItem: " + resourceAbsolutePathInStorage); |
||||||
|
} |
||||||
|
return resourceItem; |
||||||
|
} |
||||||
|
|
||||||
|
@Data |
||||||
|
@Builder |
||||||
|
@AllArgsConstructor |
||||||
|
@NoArgsConstructor |
||||||
|
public static class ResourceItem { |
||||||
|
|
||||||
|
private String resourceAbsolutePathInStorage; |
||||||
|
private String resourceRelativePath; |
||||||
|
private String resourceAbsolutePathInLocal; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue