Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix-16578] Fix environment resource permission #16579

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.dolphinscheduler.dao.mapper.TaskGroupMapper;
import org.apache.dolphinscheduler.dao.mapper.TenantMapper;
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper;
import org.apache.dolphinscheduler.dao.repository.UserDao;
import org.apache.dolphinscheduler.service.process.ProcessService;

import java.util.Arrays;
Expand Down Expand Up @@ -240,8 +241,11 @@ public static class EnvironmentResourcePermissionCheck implements ResourceAcquis

private final EnvironmentMapper environmentMapper;

public EnvironmentResourcePermissionCheck(EnvironmentMapper environmentMapper) {
private final UserDao userDao;

public EnvironmentResourcePermissionCheck(EnvironmentMapper environmentMapper, UserDao userDao) {
this.environmentMapper = environmentMapper;
this.userDao = userDao;
}

@Override
Expand All @@ -251,7 +255,12 @@ public List<AuthorizationType> authorizationTypes() {

@Override
public boolean permissionCheck(int userId, String url, Logger logger) {
return true;
User user = userDao.queryById(userId);
if (user == null) {
logger.error("User does not exist, userId:{}.", userId);
return false;
}
return user.getUserType() == UserType.ADMIN_USER;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.apache.dolphinscheduler.api.permission;

import static org.mockito.Mockito.when;

import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.Environment;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.EnvironmentMapper;
import org.apache.dolphinscheduler.dao.repository.UserDao;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -34,7 +37,6 @@
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;
Expand All @@ -46,14 +48,24 @@ public class EnvironmentResourcePermissionCheckTest {
@InjectMocks
private ResourcePermissionCheckServiceImpl.EnvironmentResourcePermissionCheck environmentResourcePermissionCheck;

@Mock
private UserDao userDao;

@Mock
private EnvironmentMapper environmentMapper;

@Test
public void testPermissionCheck() {
User user = getLoginUser();
User user = getLoginAdminUser();
when(userDao.queryById(user.getId())).thenReturn(user);
Assertions.assertTrue(environmentResourcePermissionCheck.permissionCheck(user.getId(), null, logger));
}
@Test
public void testPermissionCheckFail() {
User user = getLoginAdminUser();
when(userDao.queryById(user.getId())).thenReturn(null);
Assertions.assertFalse(environmentResourcePermissionCheck.permissionCheck(user.getId(), null, logger));
}

@Test
public void testAuthorizationTypes() {
Expand All @@ -69,7 +81,7 @@ public void testListAuthorizedResourceIds() {
ids.add(environment.getId());
List<Environment> environments = Arrays.asList(environment);

Mockito.when(environmentMapper.queryAllEnvironmentList()).thenReturn(environments);
when(environmentMapper.queryAllEnvironmentList()).thenReturn(environments);

Assertions.assertEquals(ids,
environmentResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger));
Expand All @@ -82,4 +94,11 @@ private User getLoginUser() {
loginUser.setId(1);
return loginUser;
}
private User getLoginAdminUser() {
User loginUser = new User();
loginUser.setUserType(UserType.ADMIN_USER);
loginUser.setUserName("test");
loginUser.setId(1);
return loginUser;
}
}
Loading