1 /*
2 * Copyright (c) 2002-2014, 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.mylutece.modules.wssodatabase.authentication.business;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.spring.SpringContextService;
38
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
42
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.lang.StringUtils;
45
46
47 /**
48 * This class provides instances management methods (create, find, ...) for
49 * WssoUser objects
50 */
51 public final class WssoUserHome
52 {
53 // Static variable pointed at the DAO instance
54 private static IWssoUserDAO _dao = (IWssoUserDAO) SpringContextService
55 .getBean( "mylutece-wssodatabase.wssoUserDAO" );
56
57 /**
58 * Private constructor - this class need not be instantiated
59 */
60 private WssoUserHome( )
61 {
62 }
63
64 /**
65 * Creation of an instance of wssoUser
66 *
67 * @param wssoUser The instance of the wssoUser which contains the
68 * informations to store
69 * @param plugin The current plugin using this method
70 * @return The instance of wssoUser which has been created with its primary
71 * key.
72 */
73 public static WssoUser create( WssoUser wssoUser, Plugin plugin )
74 {
75 _dao.insert( wssoUser, plugin );
76
77 return wssoUser;
78 }
79
80 /**
81 * Update of the wssoUser which is specified in parameter
82 *
83 * @param wssoUser The instance of the wssoUser which contains the data to
84 * store
85 * @param plugin The current plugin using this method
86 * @return The instance of the wssoUser which has been updated
87 */
88 public static WssoUser update( WssoUser wssoUser, Plugin plugin )
89 {
90 _dao.store( wssoUser, plugin );
91
92 return wssoUser;
93 }
94
95 /**
96 * Remove the WssoUser whose identifier is specified in parameter
97 *
98 * @param wssoUser The WssoUser object to remove
99 * @param plugin The current plugin using this method
100 */
101 public static void remove( WssoUser wssoUser, Plugin plugin )
102 {
103 _dao.delete( wssoUser, plugin );
104 }
105
106 // /////////////////////////////////////////////////////////////////////////
107 // Finders
108
109 /**
110 * Returns an instance of a wssoUser whose identifier is specified in
111 * parameter
112 *
113 * @param nKey The Primary key of the wssoUser
114 * @param plugin The current plugin using this method
115 * @return An instance of wssoUser
116 */
117 public static WssoUser findByPrimaryKey( int nKey, Plugin plugin )
118 {
119 return _dao.load( nKey, plugin );
120 }
121
122 /**
123 * Returns a collection of wssoUsers objects
124 * @param plugin The current plugin using this method
125 * @return A collection of wssoUsers
126 */
127 public static Collection<WssoUser> findWssoUsersList( Plugin plugin )
128 {
129 return _dao.selectWssoUserList( plugin );
130 }
131
132 /**
133 * Returns a collection of wssoUsers objects for a role
134 * @param nIdRole The role id of the wssoUser
135 * @param plugin The current plugin using this method
136 * @return A collection of wssoUsers
137 */
138 public static Collection findWssoUsersListForRole( int nIdRole, Plugin plugin )
139 {
140 return _dao.selectWssoUsersListForRole( nIdRole, plugin );
141 }
142
143 /**
144 * Returns a collection of wssoUsers objects for a guid
145 * @param strGuid The guid of the wssoUser
146 * @param plugin The current plugin using this method
147 * @return A collection of wssoUsers
148 */
149 public static Collection findWssoUsersListForGuid( String strGuid, Plugin plugin )
150 {
151 return _dao.selectWssoUserListForGuid( strGuid, plugin );
152 }
153
154 /**
155 * Load the list of wssoUsers for a last name or first name and profil
156 *
157 * @param strLastName The guid of WssoUser
158 * @param codeProfil The Plugin using this data access service
159 * @param strFirstName the first name
160 * @param strEmail the email
161 * @param plugin the current plugin
162 * @return The Collection of the WssoUsers
163 */
164 public static List<WssoUser> findWssoUserssByLastNameOrFirtNameOrEmailByProfil( String codeProfil,
165 String strLastName, String strFirstName, String strEmail, Plugin plugin )
166 {
167 return _dao.findWssoUserssByLastNameOrFirtNameOrEmailByProfil( codeProfil, strLastName, strFirstName, strEmail,
168 plugin );
169 }
170
171 /**
172 * Load the list of wssoUsers for a last name or first name and profil
173 *
174 * @param strLastName The guid of WssoUser
175 * @param codeProfil The Plugin using this data access service
176 * @param strFirstName the first name
177 * @param strEmail the email
178 * @param plugin the current plugin
179 * @return The Collection of the WssoUsers
180 */
181 public static List<WssoUser> findWssoUsersByLastNameOrFirstNameOrEmailByProfil( String strLastName,
182 String strFirstName, String strEmail, Plugin plugin )
183 {
184 return _dao.findWssoUsersByLastNameOrFirstNameOrEmailByProfil( strLastName, strFirstName, strEmail, plugin );
185 }
186
187 /**
188 * Get a user id from his guid
189 * @param strGuid The guid of the user
190 * @param plugin The plugin
191 * @return The user id, or 0 if no user has this login.
192 */
193 public static int findDatabaseUserIdFromGuid( String strGuid, Plugin plugin )
194 {
195 return _dao.findDatabaseUserIdFromGuid( strGuid, plugin );
196 }
197
198 /**
199 * Returns a collection of wssoUsers ids with a role
200 *
201 * @param strRole
202 * The role of the wssoUser
203 * @param plugin
204 * The current plugin using this method
205 * @return A collection of wssoUsers ids
206 */
207 public static List<Integer> findWssoUserIdsListForRole( String strRole, Plugin plugin )
208 {
209 return _dao.selectWssoUserIdsListForRole( strRole, plugin );
210 }
211
212 /**
213 * Returns a collection of roles by profils for user
214 * @param nWssoUserId the user id
215 * @param plugin The current plugin using this method
216 * @return the list of roles for a user
217 */
218 public static List<String> findRolesByProfilsForUser( int nWssoUserId, Plugin plugin )
219 {
220 List<String> listProfils = WssoProfilHome.findWssoProfilsForUser( nWssoUserId, plugin );
221 List<String> listRoles = new ArrayList<String>( );
222
223 if ( CollectionUtils.isNotEmpty( listProfils ) )
224 {
225 for ( String codeProfil : listProfils )
226 {
227 List<String> profilRoleKeyList = IdxWSSODatabaseHome.findRolesFromProfil( codeProfil, plugin );
228
229 if ( CollectionUtils.isNotEmpty( profilRoleKeyList ) )
230 {
231 for ( String role : profilRoleKeyList )
232 {
233 if ( StringUtils.isNotBlank( role ) && !listRoles.contains( role ) )
234 {
235 listRoles.add( role );
236 }
237 }
238 }
239 }
240 }
241
242 return listRoles;
243 }
244 }