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.ctv.service;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
42  import fr.paris.lutece.plugins.profiles.business.Profile;
43  import fr.paris.lutece.plugins.profiles.service.IProfilesService;
44  import fr.paris.lutece.plugins.profiles.utils.constants.ProfilesConstants;
45  import fr.paris.lutece.portal.business.right.Right;
46  import fr.paris.lutece.portal.business.user.AdminUser;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  
50  public class CtvProfileService implements ICtvProfileService
51  {
52  
53      private IProfilesService _profilesService = SpringContextService.getBean( ProfilesConstants.BEAN_PROFILES_SERVICE );
54  
55      private static final ICtvProfileService CTV_PROFILE_SERVICE = new CtvProfileService( );
56  
57      public static ICtvProfileService getInstance( )
58      {
59          return CTV_PROFILE_SERVICE;
60      }
61  
62      public class ProfileCtv extends Profile
63      {
64  
65          public ProfileCtv( TypeProfileCtv key )
66          {
67              super( );
68              this.setKey( key.getKey( ) );
69          }
70  
71          @Override
72          public boolean equals( Object o )
73          {
74              return this.getKey( ).equals( ( (Profile) o ).getKey( ) );
75          }
76  
77          @Override
78          public int hashCode( )
79          {
80              return super.hashCode( );
81          }
82  
83      }
84  
85      @Override
86      public boolean hasUserProfile( AdminUser user, TypeProfileCtv typeProfile, Plugin plugin, HttpServletRequest request )
87      {
88          boolean hasProfile = ( typeProfile.getKey( ).equals( getCurrentProfile( user, plugin, request ) ) );
89  
90          return hasProfile;
91      }
92  
93      @Override
94      public String getCurrentProfile( AdminUser user, Plugin plugin, HttpServletRequest request )
95      {
96          String currentProfile = "";
97  
98          List<String> userProfiles = getUserProfilesKeys( user, plugin );
99          if ( _profilesService.findProfileByIdUser( user.getUserId( ), plugin ).size( ) < 2 )
100         {
101             currentProfile = _profilesService.findProfileByIdUser( user.getUserId( ), plugin ).get( 0 ).getKey( );
102         }
103         else
104             if ( request.getSession( ).getAttribute( "profile_default" ) != null )
105             {
106                 currentProfile = ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( );
107             }
108             else
109             {
110                 currentProfile = selectDefaultProfileByPriority( request, userProfiles );
111             }
112 
113         return currentProfile;
114     }
115 
116     private String selectDefaultProfileByPriority( HttpServletRequest request, List<String> userProfiles )
117     {
118         String strSelectedProfile = "";
119         if ( userProfiles.size( ) > 1 )
120         {
121             String selectedProfileInRequest = request.getParameter( "profile_default" );
122             TypeProfileCtv selectedProfil = null;
123             if ( selectedProfileInRequest == null
124                     || ( selectedProfileInRequest != null && !userProfiles.contains( TypeProfileCtv.getEnum( selectedProfileInRequest ) ) ) )
125             {
126 
127                 if ( userProfiles.contains( TypeProfileCtv.ADMIN_FONC.getKey( ) ) )
128                 {
129                     selectedProfil = TypeProfileCtv.ADMIN_FONC;
130                 }
131                 else
132                     if ( userProfiles.contains( TypeProfileCtv.SUPERVISEUR_2.getKey( ) ) )
133                     {
134                         selectedProfil = TypeProfileCtv.SUPERVISEUR_2;
135                     }
136                     else
137                         if ( userProfiles.contains( TypeProfileCtv.SUPERVISEUR_1.getKey( ) ) )
138                         {
139                             selectedProfil = TypeProfileCtv.SUPERVISEUR_1;
140                         }
141                         else
142                             if ( userProfiles.contains( TypeProfileCtv.INSTRUCTEUR.getKey( ) ) )
143                             {
144                                 selectedProfil = TypeProfileCtv.INSTRUCTEUR;
145                             }
146                             else
147                                 if ( userProfiles.contains( TypeProfileCtv.CONCESSIONNAIRE.getKey( ) ) )
148                                 {
149                                     selectedProfil = TypeProfileCtv.CONCESSIONNAIRE;
150                                 }
151 
152                 if ( selectedProfil != null )
153                 {
154                     request.getSession( ).setAttribute( "profile_default", selectedProfil );
155                     strSelectedProfile = selectedProfil.getKey( );
156                 }
157             }
158         }
159         else
160         {
161             strSelectedProfile = userProfiles.get( 0 );
162         }
163 
164         return strSelectedProfile;
165     }
166 
167     @Override
168     public List<String> getUserProfilesKeys( AdminUser user, Plugin plugin, HttpServletRequest request )
169     {
170         List<String> profilesKey = new ArrayList<>( );
171         Object profileParam = request.getSession( ).getAttribute( "profile_default" );
172         if ( profileParam != null )
173         {
174             profilesKey.add( ( (TypeProfileCtv) profileParam ).getKey( ) );
175             return profilesKey;
176         }
177         return getUserProfilesKeys( user, plugin );
178     }
179 
180     @Override
181     public List<String> getUserProfilesKeys( AdminUser user, Plugin plugin )
182     {
183         List<String> profilesKey = new ArrayList<>( );
184         List<Profile> profiles = _profilesService.findProfileByIdUser( user.getUserId( ), plugin );
185 
186         for ( Profile profile : profiles )
187         {
188             profilesKey.add( profile.getKey( ) );
189         }
190         return profilesKey;
191     }
192 
193     @Override
194     public boolean hasRight( String profileKey, String idRight, Plugin plugin )
195     {
196         List<Right> rightList = _profilesService.getRightsListForProfile( profileKey, plugin );
197 
198         boolean hasRight = false;
199 
200         for ( Right right : rightList )
201         {
202             if ( right.getId( ).equals( idRight ) )
203             {
204                 hasRight = true;
205             }
206         }
207         return hasRight;
208     }
209 
210 }