001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * dmetzler 018 */ 019package org.nuxeo.ecm.restapi.server.jaxrs.directory; 020 021import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; 022import static org.nuxeo.ecm.restapi.server.jaxrs.directory.DirectorySessionRunner.withDirectorySession; 023 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027 028import javax.ws.rs.GET; 029import javax.ws.rs.POST; 030import javax.ws.rs.Path; 031import javax.ws.rs.PathParam; 032import javax.ws.rs.Produces; 033import javax.ws.rs.core.MediaType; 034import javax.ws.rs.core.Response; 035import javax.ws.rs.core.Response.Status; 036 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.DocumentModelList; 039import org.nuxeo.ecm.core.api.NuxeoException; 040import org.nuxeo.ecm.core.api.NuxeoPrincipal; 041import org.nuxeo.ecm.directory.Directory; 042import org.nuxeo.ecm.directory.Session; 043import org.nuxeo.ecm.directory.api.DirectoryEntry; 044import org.nuxeo.ecm.directory.api.DirectoryService; 045import org.nuxeo.ecm.platform.usermanager.UserManager; 046import org.nuxeo.ecm.webengine.model.WebObject; 047import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 048import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException; 049import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 050import org.nuxeo.runtime.api.Framework; 051 052/** 053 * @since 5.7.3 054 */ 055@WebObject(type = "directoryObject") 056@Produces(MediaType.APPLICATION_JSON) 057public class DirectoryObject extends DefaultObject { 058 059 private Directory directory; 060 061 @Override 062 protected void initialize(Object... args) { 063 if (args.length < 1) { 064 throw new IllegalArgumentException("Directory Object takes one parameter"); 065 } 066 String dirName = (String) args[0]; 067 directory = Framework.getService(DirectoryService.class).getDirectory(dirName); 068 if (directory == null) { 069 throw new WebResourceNotFoundException("Directory " + dirName + " was not found"); 070 } 071 } 072 073 @GET 074 public List<DirectoryEntry> getDirectoryEntries() { 075 return withDirectorySession(directory, new DirectorySessionRunner<List<DirectoryEntry>>() { 076 077 @Override 078 List<DirectoryEntry> run(Session session) { 079 DocumentModelList entries = session.query(Collections.emptyMap()); 080 List<DirectoryEntry> dirEntries = new ArrayList<>(); 081 for (DocumentModel doc : entries) { 082 dirEntries.add(new DirectoryEntry(directory.getName(), doc)); 083 } 084 return dirEntries; 085 } 086 }); 087 088 } 089 090 @POST 091 public Response addEntry(final DirectoryEntry entry) { 092 checkEditGuards(); 093 DirectoryEntry result = withDirectorySession(directory, new DirectorySessionRunner<DirectoryEntry>() { 094 095 @Override 096 DirectoryEntry run(Session session) { 097 DocumentModel docEntry = session.createEntry(entry.getDocumentModel()); 098 return new DirectoryEntry(directory.getName(), docEntry); 099 } 100 }); 101 102 return Response.ok(result).status(Status.CREATED).build(); 103 } 104 105 void checkEditGuards() { 106 NuxeoPrincipal currentUser = (NuxeoPrincipal) getContext().getCoreSession().getPrincipal(); 107 if (!(currentUser.isAdministrator() || currentUser.isMemberOf("powerusers"))) { 108 throw new WebSecurityException("Not allowed to edit directory"); 109 } 110 111 UserManager um = Framework.getService(UserManager.class); 112 if (directory.getName().equals(um.getUserDirectoryName()) 113 || directory.getName().equals(um.getGroupDirectoryName())) { 114 throw new NuxeoException("Not allowed to edit user/group directories, please use user/group endpoints", 115 SC_BAD_REQUEST); 116 } 117 } 118 119 @Path("{entryId:((?:(?!/@).)*)}") 120 public Object getEntry(@PathParam("entryId") final String entryId) { 121 return withDirectorySession(directory, new DirectorySessionRunner<Object>() { 122 123 @Override 124 Object run(Session session) { 125 DocumentModel entry = session.getEntry(entryId); 126 if (entry == null) { 127 throw new WebResourceNotFoundException("Entry not found"); 128 } 129 return newObject("directoryEntry", new DirectoryEntry(directory.getName(), entry), entryId); 130 } 131 }); 132 133 } 134 135}