1 /* 2 * Copyright (c) 2002-2022, City of Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.portal.service.rbac; 35 36 import fr.paris.lutece.portal.service.i18n.I18nService; 37 import fr.paris.lutece.portal.service.i18n.Localizable; 38 import fr.paris.lutece.portal.service.util.AppLogService; 39 40 import java.util.Collection; 41 import java.util.HashMap; 42 import java.util.Locale; 43 import java.util.Map; 44 45 /** 46 * This class provides the methods for resource types. It gives the key and label of the resource type It allows to define and access to the permissions 47 * available. It links with the resourceIdService class that retrieves specific data to be managed. 48 */ 49 public class ResourceType implements Localizable 50 { 51 private String _strResourceTypeKey; 52 private String _strResourceTypeLabel; 53 private Map<String, Permission> _mapPermissions; 54 private String _strResourceIdServiceClass; 55 private String _strPluginName; 56 private Locale _locale; 57 58 /** 59 * 60 */ 61 public ResourceType( ) 62 { 63 _mapPermissions = new HashMap<>( ); 64 } 65 66 /** 67 * Implements Localizable 68 * 69 * @param locale 70 * The current locale 71 */ 72 public void setLocale( Locale locale ) 73 { 74 _locale = locale; 75 } 76 77 /** 78 * Registers a permission 79 * 80 * @param permission 81 * the permission object to register 82 */ 83 public void registerPermission( Permission permission ) 84 { 85 _mapPermissions.put( permission.getPermissionKey( ), permission ); 86 } 87 88 /** 89 * Returns all registered permissions 90 * 91 * @return A collection containing all registered permissions 92 */ 93 public Collection<Permission> getPermissionList( ) 94 { 95 return _mapPermissions.values( ); 96 } 97 98 /** 99 * Get a particular permission 100 * 101 * @param strPermissionId 102 * Identifier of the seeked permission 103 * @return the selected resource type 104 */ 105 public Permission getPermission( String strPermissionId ) 106 { 107 return _mapPermissions.get( strPermissionId ); 108 } 109 110 /** 111 * Returns the resource type Key 112 * 113 * @return Returns the _strResourceTypeKey. 114 */ 115 public String getResourceTypeKey( ) 116 { 117 return _strResourceTypeKey; 118 } 119 120 /** 121 * Sets the resource type Key 122 * 123 * @param strResourceTypeCode 124 * The _strResourceTypeKey to set. 125 */ 126 public void setResourceTypeKey( String strResourceTypeCode ) 127 { 128 _strResourceTypeKey = strResourceTypeCode; 129 } 130 131 /** 132 * Returns the resource type label 133 * 134 * @return Returns the _strResourceTypeLabel. 135 */ 136 public String getResourceTypeLabel( ) 137 { 138 return I18nService.getLocalizedString( _strResourceTypeLabel, _locale ); 139 } 140 141 /** 142 * Sets the resource type label 143 * 144 * @param strResourceTypeLabel 145 * The _strResourceTypeLabel to set. 146 */ 147 public void setResourceTypeLabelKey( String strResourceTypeLabel ) 148 { 149 _strResourceTypeLabel = strResourceTypeLabel; 150 } 151 152 /** 153 * Returns the name of the resourceIdService class 154 * 155 * @return Returns the _strResourceIdServiceClass. 156 */ 157 public String getResourceIdServiceClass( ) 158 { 159 return _strResourceIdServiceClass; 160 } 161 162 /** 163 * Sets the name of the resourceIdService class 164 * 165 * @param strResourceIdServiceClass 166 * The _strResourceIdServiceClass to set. 167 */ 168 public void setResourceIdServiceClass( String strResourceIdServiceClass ) 169 { 170 _strResourceIdServiceClass = strResourceIdServiceClass; 171 } 172 173 /** 174 * Returns an instance of the resourceIdService class 175 * 176 * @return a ResourceIdService object with the plugin name initialised 177 */ 178 public ResourceIdService getResourceIdService( ) 179 { 180 try 181 { 182 ResourceIdService../../fr/paris/lutece/portal/service/rbac/ResourceIdService.html#ResourceIdService">ResourceIdService service = (ResourceIdService) Class.forName( getResourceIdServiceClass( ) ).newInstance( ); 183 if ( service != null ) 184 { 185 service.setPluginName( getPluginName( ) ); 186 return service; 187 } 188 } 189 catch( InstantiationException | IllegalAccessException | ClassNotFoundException e ) 190 { 191 AppLogService.error( e.getMessage( ), e ); 192 } 193 194 return null; 195 } 196 197 /** 198 * Sets the plugin name 199 * 200 * @param strPluginName 201 * The __strPluginName to set. 202 */ 203 public void setPluginName( String strPluginName ) 204 { 205 _strPluginName = strPluginName; 206 } 207 208 /** 209 * Returns the plugin name 210 * 211 * @return Returns the __strPluginName. 212 */ 213 public String getPluginName( ) 214 { 215 return _strPluginName; 216 } 217 }