/******************************************************************************* * Copyright (c) 2012-2015 Codenvy, S.A. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Codenvy, S.A. - initial API and implementation *******************************************************************************/ package org.eclipse.che.api.vfs.server.impl.memory; import org.eclipse.che.api.vfs.server.VirtualFile; import org.eclipse.che.api.vfs.shared.dto.AccessControlEntry; import org.eclipse.che.api.vfs.shared.dto.Principal; import org.eclipse.che.api.vfs.shared.dto.VirtualFileSystemInfo.BasicPermissions; import org.eclipse.che.commons.env.EnvironmentContext; import org.eclipse.che.commons.user.User; import org.eclipse.che.commons.user.UserImpl; import com.google.common.collect.Sets; import org.everrest.core.impl.ContainerResponse; import org.everrest.core.tools.ByteArrayContainerResponseWriter; import java.io.ByteArrayInputStream; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** @author andrew00x */ public class GetACLTest extends MemoryFileSystemTest { private VirtualFile file; private String fileId; @Override protected void setUp() throws Exception { super.setUp(); String name = getClass().getName(); VirtualFile getAclTestFolder = mountPoint.getRoot().createFolder(name); file = getAclTestFolder.createFile(name, "text/plain", new ByteArrayInputStream(DEFAULT_CONTENT.getBytes())); Principal adminPrincipal = createPrincipal("admin", Principal.Type.USER); Principal userPrincipal = createPrincipal("john", Principal.Type.USER); Map<Principal, Set<String>> permissions = new HashMap<>(2); permissions.put(adminPrincipal, Sets.newHashSet(BasicPermissions.ALL.value())); permissions.put(userPrincipal, Sets.newHashSet(BasicPermissions.READ.value())); file.updateACL(createAcl(permissions), true, null); fileId = file.getId(); } public void testGetACL() throws Exception { String path = SERVICE_URI + "acl/" + fileId; ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, null); assertEquals(200, response.getStatus()); @SuppressWarnings("unchecked") List<AccessControlEntry> acl = (List<AccessControlEntry>)response.getEntity(); for (AccessControlEntry ace : acl) { if ("root".equals(ace.getPrincipal().getName())) { ace.getPermissions().contains("all"); } if ("john".equals(ace.getPrincipal().getName())) { ace.getPermissions().contains("read"); } } } public void testGetACLNoPermissions() throws Exception { Principal adminPrincipal = createPrincipal("admin", Principal.Type.USER); Map<Principal, Set<String>> permissions = new HashMap<>(1); permissions.put(adminPrincipal, Sets.newHashSet(BasicPermissions.ALL.value())); User previousUser = EnvironmentContext.getCurrent().getUser(); EnvironmentContext.getCurrent().setUser(new UserImpl("admin", "admin", null, Arrays.asList("workspace/admin"))); file.updateACL(createAcl(permissions), true, null); EnvironmentContext.getCurrent().setUser(previousUser); // restore ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter(); String path = SERVICE_URI + "acl/" + fileId; ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null); assertEquals(403, response.getStatus()); log.info(new String(writer.getBody())); } }