View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.newsletter.service;
35  
36  import fr.paris.lutece.plugins.newsletter.business.NewsLetterTemplate;
37  import fr.paris.lutece.plugins.newsletter.business.NewsLetterTemplateHome;
38  import fr.paris.lutece.portal.service.plugin.PluginService;
39  import fr.paris.lutece.portal.service.rbac.Permission;
40  import fr.paris.lutece.portal.service.rbac.ResourceIdService;
41  import fr.paris.lutece.portal.service.rbac.ResourceType;
42  import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;
43  import fr.paris.lutece.util.ReferenceList;
44  
45  import java.util.Locale;
46  
47  /**
48   * Resource Id service for RBAC features to control access to newsletter templates
49   */
50  public class NewsletterTemplateResourceIdService extends ResourceIdService
51  {
52      /** Permission for creating a newsletter template */
53      public static final String PERMISSION_CREATE = "CREATE";
54  
55      /** Permission for deleting a newsletter template */
56      public static final String PERMISSION_DELETE = "DELETE";
57  
58      /** Permission for modifying a newsletter template */
59      public static final String PERMISSION_MODIFY = "MODIFY";
60  
61      private static final String REGEX_ID = "^[\\d]+$";
62  
63      // i18n properties
64      private static final String PROPERTY_LABEL_RESOURCE_TYPE = "newsletter.newsletter_template.resourceType";
65      private static final String PROPERTY_LABEL_CREATE = "newsletter.permission.newsletter_template.label.create";
66      private static final String PROPERTY_LABEL_DELETE = "newsletter.permission.newsletter_template.label.delete";
67      private static final String PROPERTY_LABEL_MODIFY = "newsletter.permission.newsletter_template.label.modify";
68  
69      /** Creates a new instance of NewsletterTemplateResourceIdService */
70      public NewsletterTemplateResourceIdService( )
71      {
72          setPluginName( NewsletterPlugin.PLUGIN_NAME );
73      }
74  
75      /**
76       * Initializes the service
77       */
78      public void register( )
79      {
80          ResourceType rt = new ResourceType( );
81          rt.setResourceIdServiceClass( NewsletterTemplateResourceIdService.class.getName( ) );
82          rt.setPluginName( NewsletterPlugin.PLUGIN_NAME );
83          rt.setResourceTypeKey( NewsLetterTemplate.RESOURCE_TYPE );
84          rt.setResourceTypeLabelKey( PROPERTY_LABEL_RESOURCE_TYPE );
85  
86          Permission p = new Permission( );
87          p.setPermissionKey( PERMISSION_CREATE );
88          p.setPermissionTitleKey( PROPERTY_LABEL_CREATE );
89          rt.registerPermission( p );
90  
91          p = new Permission( );
92          p.setPermissionKey( PERMISSION_DELETE );
93          p.setPermissionTitleKey( PROPERTY_LABEL_DELETE );
94          rt.registerPermission( p );
95  
96          p = new Permission( );
97          p.setPermissionKey( PERMISSION_MODIFY );
98          p.setPermissionTitleKey( PROPERTY_LABEL_MODIFY );
99          rt.registerPermission( p );
100 
101         ResourceTypeManager.registerResourceType( rt );
102     }
103 
104     /**
105      * Returns a list of resource ids
106      * 
107      * @param locale
108      *            The current locale
109      * @return A list of resource ids
110      */
111     public ReferenceList getResourceIdList( Locale locale )
112     {
113         return NewsLetterTemplateHome.getTemplateListByRef( PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) );
114     }
115 
116     /**
117      * Returns the Title of a given resource
118      * 
119      * @param strId
120      *            The Id of the resource
121      * @param locale
122      *            The current locale
123      * @return The Title of a given resource
124      */
125     public String getTitle( String strId, Locale locale )
126     {
127         if ( ( strId == null ) || !strId.matches( REGEX_ID ) )
128         {
129             return null;
130         }
131 
132         NewsLetterTemplate newsletterTemplate = NewsLetterTemplateHome.findByPrimaryKey( Integer.parseInt( strId ),
133                 PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) );
134 
135         return ( newsletterTemplate != null ) ? newsletterTemplate.getDescription( ) : null;
136     }
137 }