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.extraction.execution;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.commons.collections.CollectionUtils;
026
027import org.ametys.core.group.GroupIdentity;
028import org.ametys.core.group.GroupManager;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.plugins.extraction.ExtractionConstants;
031import org.ametys.plugins.extraction.component.ExtractionComponent;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * Object representing the extraction definition file content
036 */
037public class Extraction
038{
039    /**
040     * Visibility of a Query
041     */
042    public enum Visibility
043    {
044        /** Private visibility. */
045        PRIVATE,
046        /** Shared visibility. */
047        SHARED,
048        /** Public visibility. */
049        PUBLIC;
050        
051        @Override
052        public String toString()
053        {
054            return name().toLowerCase();
055        }
056    }
057    
058    /**
059     * Rights profiles
060     */
061    public enum ExtractionProfile
062    {
063        /** Read access */
064        READ_ACCESS,
065        /** Write access */
066        WRITE_ACCESS;
067        
068        @Override
069        public String toString()
070        {
071            return name().toLowerCase();
072        }
073        
074        /**
075         * Extraction profile title getter.
076         * @return The title of the profile
077         */
078        public I18nizableText getTitle()
079        {
080            switch (this)
081            {
082                case READ_ACCESS: 
083                    return new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_RIGHT_PROFILE_READ_TITLE");
084                case WRITE_ACCESS: 
085                    return new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_RIGHT_PROFILE_WRITE_TITLE");
086                default: 
087                    return null;
088            }
089        }
090
091        /**
092         * Extraction profile description getter.
093         * @return The description of the profile
094         */
095        public I18nizableText getDescription()
096        {
097            switch (this)
098            {
099                case READ_ACCESS: 
100                    return new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_RIGHT_PROFILE_READ_DESCRIPTION");
101                case WRITE_ACCESS: 
102                    return new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_RIGHT_PROFILE_WRITE_DESCRIPTION");
103                default: 
104                    return null;
105            }
106        }
107    }
108    
109    private String _descriptionId;
110    private List<ExtractionComponent> _extractionComponents;
111    private List<String> _displayOptionalColumnsNames;
112    private Map<String, String> _queryVariablesNamesAndContentTypes;
113    private Visibility _visibility;
114    private UserIdentity _author;
115    private Map<ExtractionProfile, Set<UserIdentity>> _grantedUsers = new HashMap<>();
116    private Map<ExtractionProfile, Set<GroupIdentity>> _grantedGroups = new HashMap<>();
117    
118    /**
119     * Retrieves the identifier of the extraction's description
120     * @return the identifier of the extraction's description
121     */
122    public String getDescriptionId()
123    {
124        return _descriptionId;
125    }
126    
127    /**
128     * Set the identifier of the extraction's description
129     * @param descriptionId the identifier to set
130     */
131    public void setDescriptionId(String descriptionId)
132    {
133        _descriptionId = descriptionId;
134    }
135    
136    /**
137     * Retrieves the list of the extraction components
138     * @return The extraction components 
139     */
140    public List<ExtractionComponent> getExtractionComponents()
141    {
142        return null != _extractionComponents ? _extractionComponents : new ArrayList<>();
143    }
144    
145    /**
146     * Add an extraction component 
147     * @param extractionComponent The extraction component to add
148     */
149    public void addExtractionComponent(ExtractionComponent extractionComponent)
150    {
151        if (null == _extractionComponents)
152        {
153            _extractionComponents = new ArrayList<>();
154        }
155        _extractionComponents.add(extractionComponent);
156    }
157    
158    /**
159     * Retrieves the list of variables names controlling display of optional columns
160     * @return The variables names
161     */
162    public List<String> getDisplayOptionalColumnsNames()
163    {
164        return null != _displayOptionalColumnsNames ? _displayOptionalColumnsNames : new ArrayList<>();
165    }
166    
167    /**
168     * Set variables names controlling display of optional columns
169     * @param displayOptionalColumnsNames the variables for the optional columns to set
170     */
171    public void setDisplayOptionalColumnsNames(List<String> displayOptionalColumnsNames)
172    {
173        _displayOptionalColumnsNames = displayOptionalColumnsNames;
174    }
175    
176    /**
177     * Retrieves the list of variables names and content type to use in queries
178     * @return A Map containing variables names and content types
179     */
180    public Map<String, String> getQueryVariablesNamesAndContentTypes()
181    {
182        return null != _queryVariablesNamesAndContentTypes ? _queryVariablesNamesAndContentTypes : new HashMap<>();
183    }
184    
185    /**
186     * Set variables names and content types to use in queries
187     * @param queryVariablesNamesAndContentTypes A Map containing variables names and content types to set
188     */
189    public void setQueryVariablesNamesAndContentTypes(Map<String, String> queryVariablesNamesAndContentTypes)
190    {
191        _queryVariablesNamesAndContentTypes = queryVariablesNamesAndContentTypes;
192    }
193    
194    /**
195     * Get the visibility of the extraction
196     * @return The visibility
197     */
198    public Visibility getVisibility()
199    {
200        return _visibility != null ? _visibility : Visibility.PUBLIC;
201    }
202    
203    /**
204     * Set the extraction's visibility
205     * @param visibility the visibility
206     */
207    public void setVisibility (Visibility visibility)
208    {
209        _visibility = visibility;
210    }
211
212    /**
213     * Get the author of the extraction
214     * @return The author
215     */
216    public UserIdentity getAuthor ()
217    {
218        return _author;
219    }
220    
221    /**
222     * Set the author of this extraction.
223     * @param author the author
224     */
225    public void setAuthor(UserIdentity author)
226    {
227        _author = author;
228    }
229    
230    /**
231     * Get the granted users
232     * @param profile the extraction profile
233     * @return the granted users
234     */
235    public Set<UserIdentity> getGrantedUsers(ExtractionProfile profile)
236    {
237        Set<UserIdentity> grantedUsers = new HashSet<>();
238        grantedUsers.add(getAuthor());
239        
240        if (_grantedUsers.containsKey(profile))
241        {
242            grantedUsers.addAll(_grantedUsers.get(profile));
243        }
244
245        return grantedUsers;
246    }
247    
248    /**
249     * Get the granted groups
250     * @param profile the extraction profile
251     * @return the granted groups
252     */
253    public Set<GroupIdentity> getGrantedGroups(ExtractionProfile profile)
254    {
255        if (_grantedGroups.containsKey(profile))
256        {
257            return _grantedGroups.get(profile);
258        }
259        else
260        {
261            return Set.of();
262        }
263    }
264    
265    /**
266     * Determines if an user has READ access to this extraction.
267     * @param user The user
268     * @param groupManager The group manager to determine the user's groups
269     * @return <code>true</code> if the user has read access, <code>false</code> otherwise
270     */
271    public boolean canRead(UserIdentity user, GroupManager groupManager)
272    {
273        Visibility visibility = getVisibility();
274        
275        switch (visibility)
276        {
277            case PRIVATE:
278                return getAuthor().equals(user);
279                
280            case SHARED:
281                return getGrantedUsers(ExtractionProfile.READ_ACCESS).contains(user) || CollectionUtils.containsAny(getGrantedGroups(ExtractionProfile.READ_ACCESS), groupManager.getUserGroups(user)) || canWrite(user, groupManager);
282
283            case PUBLIC:
284            default:
285                return true;
286        }
287    }
288    
289    /**
290     * Determines if an user has WRITE access to this extraction.
291     * @param user The user
292     * @param groupManager The group manager to determine the user's groups
293     * @return <code>true</code> if the user has write access, <code>false</code> otherwise
294     */
295    public boolean canWrite(UserIdentity user, GroupManager groupManager)
296    {
297        Visibility visibility = getVisibility();
298        
299        switch (visibility)
300        {
301            case PRIVATE:
302                return getAuthor().equals(user);
303                
304            case SHARED:
305                return getGrantedUsers(ExtractionProfile.WRITE_ACCESS).contains(user) || CollectionUtils.containsAny(getGrantedGroups(ExtractionProfile.WRITE_ACCESS), groupManager.getUserGroups(user));
306
307            case PUBLIC:
308            default:
309                return true;
310        }
311    }
312    
313    /**
314     * Set the granted users
315     * @param profile the extraction profile
316     * @param users the granted users
317     */
318    public void setGrantedUsers (ExtractionProfile profile, Set<UserIdentity> users)
319    {
320        UserIdentity author = this.getAuthor();
321        users.remove(author);
322        _grantedUsers.put(profile, users);
323    }
324    
325    /**
326     * Set the granted groups
327     * @param profile the extraction profile
328     * @param groups the granted groups
329     */
330    public void setGrantedGroups(ExtractionProfile profile, Set<GroupIdentity> groups)
331    {
332        _grantedGroups.put(profile, groups);
333    }
334}