View Javadoc
1   /*
2    * Copyright (c) 2002-2019, Mairie de 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.appcenter.modules.identitystore.service;
35  
36  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.Attribute;
37  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.AttributeHome;
38  import fr.paris.lutece.plugins.appcenter.modules.identitystore.business.AttributeRight;
39  import java.util.HashMap;
40  import java.util.Iterator;
41  import java.util.Map;
42  import java.util.regex.Matcher;
43  import java.util.regex.Pattern;
44  import javax.servlet.http.HttpServletRequest;
45  import org.apache.commons.lang.StringUtils;
46  
47  public class IdentityStoreDemandService
48  {
49      private static final String CONSTANT_READABLE = "_readable";
50      private static final String CONSTANT_WRITABLE = "_writable";
51      private static final String CONSTANT_CERTIFIABLE = "_certifiable";
52  
53      private static final String REGEX_ATTRIBUTE = "(.*)_[^_]*";
54  
55      /**
56       * Get the attribute rights map of the request
57       * 
58       * @param request
59       *            the httpservletrequest
60       * @return the map of attribute/attributeRight contains in the request
61       */
62      public static Map<String, AttributeRight> getMapAttributeRights( HttpServletRequest request )
63      {
64          Map<String, AttributeRight> mapAttributeRight = new HashMap<>( );
65  
66          Map<String, String [ ]> colParameters = request.getParameterMap( );
67  
68          for ( Map.Entry<String, String [ ]> entry : colParameters.entrySet( ) )
69          {
70              boolean bIsReadable = false;
71              boolean bIsWritable = false;
72              boolean bIsCertifiable = false;
73              String strAttributeKey = StringUtils.EMPTY;
74  
75              if ( entry.getKey( ).endsWith( CONSTANT_READABLE ) )
76              {
77                  bIsReadable = true;
78              }
79              if ( entry.getKey( ).endsWith( CONSTANT_WRITABLE ) )
80              {
81                  bIsWritable = true;
82              }
83              if ( entry.getKey( ).endsWith( CONSTANT_CERTIFIABLE ) )
84              {
85                  bIsCertifiable = true;
86              }
87              if ( bIsCertifiable || bIsReadable || bIsWritable )
88              {
89                  Pattern p = Pattern.compile( REGEX_ATTRIBUTE );
90                  Matcher m = p.matcher( entry.getKey( ) );
91                  if ( m.matches( ) )
92                  {
93                      if ( m.group( 1 ) != null )
94                      {
95                          strAttributeKey = m.group( 1 );
96                          Attribute attribute = AttributeHome.findByAttributeKey( strAttributeKey );
97                          if ( attribute != null )
98                          {
99                              AttributeRight right = mapAttributeRight.get( attribute.getKey( ) );
100 
101                             if ( right == null )
102                             {
103                                 right = new AttributeRight( );
104                             }
105                             if ( bIsReadable == true )
106                             {
107                                 right.setReadable( true );
108                             }
109                             if ( bIsWritable == true )
110                             {
111                                 right.setWritable( true );
112                             }
113                             if ( bIsCertifiable == true )
114                             {
115                                 right.setCertifiable( true );
116                             }
117 
118                             mapAttributeRight.put( attribute.getKey( ), right );
119                         }
120                     }
121                 }
122             }
123         }
124         return mapAttributeRight;
125     }
126 }