001/* 002 * Copyright 2017 Anyware Services 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 */ 016package org.ametys.plugins.linkdirectory.link; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.parameters.Parameters; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.acting.ServiceableAction; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.Response; 029import org.apache.cocoon.environment.SourceResolver; 030 031import org.ametys.core.cocoon.JSonReader; 032import org.ametys.core.user.CurrentUserProvider; 033import org.ametys.core.user.UserIdentity; 034import org.ametys.plugins.linkdirectory.Link; 035import org.ametys.plugins.linkdirectory.Link.LinkType; 036import org.ametys.plugins.repository.RepositoryConstants; 037import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector; 038 039/** 040 * Adds a new user link in Ametys. 041 */ 042public class AddUserLinkAction extends ServiceableAction 043{ 044 /** The DAO for {@link Link}s */ 045 protected LinkDAO _linkDAO; 046 /** The current user provider */ 047 protected CurrentUserProvider _currentUserProvider; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 054 _linkDAO = (LinkDAO) smanager.lookup(LinkDAO.ROLE); 055 } 056 057 @Override 058 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 059 { 060 Request request = ObjectModelHelper.getRequest(objectModel); 061 Map<String, Object> result = new HashMap<>(); 062 063 UserIdentity user = _currentUserProvider.getUser(); 064 065 // Check user authenticated 066 if (user != null) 067 { 068 // Create link 069 Map<String, Object> createParameters = new HashMap<>(); 070 createParameters.put("siteName", request.getParameter("siteName")); 071 createParameters.put("lang", request.getParameter("lang")); 072 createParameters.put("url-type", LinkType.URL.toString()); 073 createParameters.put("url", request.getParameter("url")); 074 createParameters.put("title", request.getParameter("title")); 075 076 if (_createUserLink(request, createParameters).containsKey("already-exists")) 077 { 078 result.put("error", "already-exists"); 079 } 080 else 081 { 082 result.put("error", "none"); 083 } 084 } 085 else 086 { 087 result.put("error", "unauthenticated-user"); 088 } 089 090 Response response = ObjectModelHelper.getResponse(objectModel); 091 response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); 092 response.setHeader("Access-Control-Allow-Credentials", "true"); 093 094 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 095 return EMPTY_MAP; 096 } 097 098 private Map<String, Object> _createUserLink(Request request, Map<String, Object> parameters) 099 { 100 // Retrieve the current workspace. 101 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 102 103 try 104 { 105 // Force the workspace. 106 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE); 107 108 return _linkDAO.createUserLink(parameters); 109 } 110 finally 111 { 112 // Restore context 113 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 114 } 115 } 116}