1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 }