Rework workspace service to return all workspace which user can read

6.19.x
Sergii Leschenko 2016-05-25 18:38:08 +03:00
parent 862e971fac
commit 3d9eabaedf
7 changed files with 23 additions and 25 deletions

View File

@ -43,7 +43,7 @@ public interface Subject {
@Override
public void checkPermission(String domain, String instance, String action) throws ForbiddenException {
throw new ForbiddenException("User is not authorized to perform " + action + " of " + domain + " with id '" + instance + "'");
}
@Override

View File

@ -11,8 +11,7 @@
Codenvy, S.A. - initial API and implementation
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>che-maven-server</artifactId>

View File

@ -246,22 +246,22 @@ public class WorkspaceManager {
}
/**
* Gets all user's workspaces(workspaces where user is owner).
* Gets list of workspaces which user can read
*
* <p>Returned workspaces have either {@link WorkspaceStatus#STOPPED} status
* or status defined by their runtime instances(if those exist).
*
* @param namespace
* the id of the user whose workspaces should be fetched
* @return the list of workspaces or empty list if user doesn't own any workspace
* @param user
* the id of the user
* @return the list of workspaces or empty list if user can't read any workspace
* @throws NullPointerException
* when {@code owner} is null
* @throws ServerException
* when any server error occurs while getting workspaces with {@link WorkspaceDao#getByNamespace(String)}
* when any server error occurs while getting workspaces with {@link WorkspaceDao#getWorkspaces(String)}
*/
public List<WorkspaceImpl> getWorkspaces(String namespace) throws ServerException {
requireNonNull(namespace, "Required non-null workspace namespace");
final List<WorkspaceImpl> workspaces = workspaceDao.getByNamespace(namespace);
public List<WorkspaceImpl> getWorkspaces(String user) throws ServerException {
requireNonNull(user, "Required non-null user id");
final List<WorkspaceImpl> workspaces = workspaceDao.getWorkspaces(user);
workspaces.forEach(this::normalizeState);
return workspaces;
}
@ -701,9 +701,9 @@ public class WorkspaceManager {
if (parts.length == 1) {
return workspaceDao.get(key);
}
final String userName = parts[0];
final String userId = parts[0];
final String wsName = parts[1];
final String ownerId = userName.isEmpty() ? sessionUser().getUserId() : userManager.getByName(userName).getId();
final String ownerId = userId.isEmpty() ? sessionUser().getUserId() : userManager.getByName(userId).getId();
return workspaceDao.get(wsName, ownerId);
}

View File

@ -179,7 +179,7 @@ public class WorkspaceService extends Service {
@Produces(APPLICATION_JSON)
@RolesAllowed("user")
@GenerateLink(rel = LINK_REL_GET_WORKSPACES)
@ApiOperation(value = "Get the workspaces owned by the current user",
@ApiOperation(value = "Get workspaces which user can read",
notes = "This operation can be performed only by authorized user",
response = WorkspaceDto.class,
responseContainer = "List")

View File

@ -133,13 +133,13 @@ public interface WorkspaceDao {
List<WorkspaceImpl> getByNamespace(String namespace) throws ServerException;
/**
* Gets list of workspaces where user is worker
* Gets list of workspaces which user can read
*
* @param username
* name of user
* @return list of workspaces where user is worker
* @param userId
* id of user
* @return list of workspaces which user can read
* @throws ServerException
* when any other error occurs during workspaces fetching
*/
List<WorkspaceImpl> getWorkspaces(String username) throws ServerException;
List<WorkspaceImpl> getWorkspaces(String userId) throws ServerException;
}

View File

@ -17,12 +17,12 @@ import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.machine.server.MachineManager;
import org.eclipse.che.api.machine.server.exception.MachineException;
import org.eclipse.che.api.machine.server.model.impl.SnapshotImpl;
import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl;
import org.eclipse.che.api.machine.server.model.impl.MachineImpl;
import org.eclipse.che.api.machine.server.model.impl.MachineRuntimeInfoImpl;
import org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl;
import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl;
import org.eclipse.che.api.machine.server.model.impl.SnapshotImpl;
import org.eclipse.che.api.user.server.UserManager;
import org.eclipse.che.api.workspace.server.WorkspaceRuntimes.RuntimeDescriptor;
import org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl;
@ -208,14 +208,14 @@ public class WorkspaceManagerTest {
}
@Test
public void shouldBeAbleToGetWorkspacesByOwner() throws Exception {
public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception {
// given
final WorkspaceConfig config = createConfig();
final WorkspaceImpl workspace1 = workspaceManager.createWorkspace(config, "user123", null);
final WorkspaceImpl workspace2 = workspaceManager.createWorkspace(config, "user123", null);
final WorkspaceImpl workspace2 = workspaceManager.createWorkspace(config, "user321", null);
when(workspaceDao.getByNamespace("user123")).thenReturn(asList(workspace1, workspace2));
when(workspaceDao.getWorkspaces("user123")).thenReturn(asList(workspace1, workspace2));
final RuntimeDescriptor descriptor = createDescriptor(workspace2, RUNNING);
when(runtimes.get(workspace2.getId())).thenReturn(descriptor);

View File

@ -31,7 +31,6 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -140,7 +139,7 @@ public class LocalWorkspaceDaoImpl implements WorkspaceDao {
}
@Override
public List<WorkspaceImpl> getWorkspaces(String username) throws ServerException {
public List<WorkspaceImpl> getWorkspaces(String userId) throws ServerException {
return new ArrayList<>(workspaces.values());
}