001/* 002 * Copyright 2015 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.core.right.profile; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 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.SourceResolver; 029import org.apache.commons.lang3.StringUtils; 030 031import org.ametys.core.cocoon.JSonReader; 032import org.ametys.core.right.Profile; 033import org.ametys.core.right.RightProfilesDAO; 034 035/** 036 * Get profiles 037 * 038 */ 039public class ProfileSearchAction extends ServiceableAction 040{ 041 private RightProfilesDAO _profilesDAO; 042 043 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 044 { 045 if (_profilesDAO == null) 046 { 047 // Lazy loading for safe mode 048 _profilesDAO = (RightProfilesDAO) manager.lookup(RightProfilesDAO.ROLE); 049 } 050 051 Map<String, Object> result = new HashMap<>(); 052 053 List<Map<String, Object>> profilesAsJSON = new ArrayList<>(); 054 055 List<Profile> profiles = new ArrayList<>(); 056 profiles.addAll(_profilesDAO.getProfiles(null)); 057 058 String optionalContext = ObjectModelHelper.getRequest(objectModel).getParameter("context"); 059 if (StringUtils.isNotBlank(optionalContext)) 060 { 061 profiles.addAll(_profilesDAO.getProfiles(optionalContext)); 062 } 063 064 for (Profile profile : profiles) 065 { 066 Map<String, Object> profile2json = profile.toJSON(); 067 profile2json.put("rights", _profilesDAO.getRights(profile)); 068 profilesAsJSON.add(profile2json); 069 } 070 071 result.put("profiles", profilesAsJSON); 072 073 Request request = ObjectModelHelper.getRequest(objectModel); 074 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 075 076 return EMPTY_MAP; 077 } 078 079}