-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProfilesService.java
More file actions
590 lines (515 loc) · 18.6 KB
/
Copy pathProfilesService.java
File metadata and controls
590 lines (515 loc) · 18.6 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* Copyright (c) 2002-2020, City of Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.plugins.profiles.service;
import fr.paris.lutece.plugins.profiles.business.Profile;
import fr.paris.lutece.plugins.profiles.business.ProfileAction;
import fr.paris.lutece.plugins.profiles.business.ProfileFilter;
import fr.paris.lutece.plugins.profiles.business.ProfileHome;
import fr.paris.lutece.plugins.profiles.business.views.View;
import fr.paris.lutece.plugins.profiles.service.action.IProfileActionService;
import fr.paris.lutece.plugins.profiles.utils.constants.ProfilesConstants;
import fr.paris.lutece.portal.business.rbac.RBACRole;
import fr.paris.lutece.portal.business.right.Right;
import fr.paris.lutece.portal.business.user.AdminUser;
import fr.paris.lutece.portal.business.user.AdminUserHome;
import fr.paris.lutece.portal.business.user.attribute.AdminUserField;
import fr.paris.lutece.portal.business.user.attribute.AdminUserFieldHome;
import fr.paris.lutece.portal.business.user.attribute.AttributeField;
import fr.paris.lutece.portal.business.user.attribute.IAttribute;
import fr.paris.lutece.portal.business.workgroup.AdminWorkgroup;
import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
import fr.paris.lutece.portal.service.plugin.Plugin;
import fr.paris.lutece.portal.service.plugin.PluginService;
import fr.paris.lutece.portal.service.rbac.RBACService;
import fr.paris.lutece.portal.service.user.attribute.AttributeFieldService;
import fr.paris.lutece.portal.service.user.attribute.AttributeService;
import fr.paris.lutece.util.ReferenceList;
import fr.paris.lutece.util.html.ItemNavigator;
import fr.paris.lutece.util.url.UrlItem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
/**
*
* ProfilesService
*
*/
public class ProfilesService implements IProfilesService
{
@Inject
private IProfileActionService _profileActionService;
/**
* {@inheritDoc}
*/
@Override
public ItemNavigator getItemNavigator( ProfileFilter pFilter, Profile profile, UrlItem url )
{
Plugin plugin = PluginService.getPlugin( ProfilesPlugin.PLUGIN_NAME );
List<String> listItem = new ArrayList<String>( );
Collection<Profile> listAllProfiles = ProfileHome.findProfilesByFilter( pFilter, plugin );
int nIndex = 0;
int nCurrentItemId = 0;
for ( Profile allProfile : listAllProfiles )
{
listItem.add( allProfile.getKey( ) );
if ( allProfile.getKey( ).equals( profile.getKey( ) ) )
{
nCurrentItemId = nIndex;
}
nIndex++;
}
return new ItemNavigator( listItem, nCurrentItemId, url.getUrl( ), ProfilesConstants.PARAMETER_PROFILE_KEY );
}
/**
* {@inheritDoc}
*/
@Override
public List<ProfileAction> getListActions( AdminUser user, Profile profile, String strPermission, Locale locale, Plugin plugin )
{
List<ProfileAction> listActions = new ArrayList<ProfileAction>( );
for ( ProfileAction action : _profileActionService.selectActionsList( locale, plugin ) )
{
if ( !action.getPermission( ).equals( strPermission ) )
{
listActions.add( action );
}
}
listActions = (List<ProfileAction>) RBACService.getAuthorizedActionsCollection( listActions, profile, user );
return listActions;
}
/**
* {@inheritDoc}
*/
@Override
public void doAssignUserToProfile( int nIdUser, HttpServletRequest request, Locale locale )
{
AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
if ( user != null )
{
ProfilesAdminUserFieldListenerService.getService( ).doCreateUserFields( user, request, locale );
}
}
/**
* {@inheritDoc}
*/
@Override
public void doUnassignUserFromProfile( int nIdUser, String strProfileKey, AdminUser currentUser, HttpServletRequest request, Locale locale, Plugin plugin )
{
AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
// Remove User Fields
List<IAttribute> listAttributes = AttributeService.getInstance( ).getPluginAttributesWithoutFields( ProfilesPlugin.PLUGIN_NAME, locale );
IAttribute attribute = listAttributes.get( 0 );
List<AdminUserField> listUserFields = AdminUserFieldHome.selectUserFieldsByIdUserIdAttribute( user.getUserId( ), attribute.getIdAttribute( ) );
for ( AdminUserField userField : listUserFields )
{
AdminUserFieldHome.remove( userField );
}
// Remove profile
removeUserFromProfile( strProfileKey, nIdUser, plugin );
List<Profile> listProfiles = findProfileByIdUser( nIdUser, plugin );
Set<Right> listProfilesRights = new HashSet<Right>( );
Set<RBACRole> listProfilesRoles = new HashSet<RBACRole>( );
Set<AdminWorkgroup> listProfilesWorkgroups = new HashSet<AdminWorkgroup>( );
for ( Profile profile : listProfiles )
{
if ( !StringUtils.equals( profile.getKey( ), strProfileKey ) )
{
listProfilesRights.addAll( getRightsListForProfile( profile.getKey( ), plugin ) );
listProfilesRoles.addAll( getRolesListForProfile( profile.getKey( ), plugin ) );
listProfilesWorkgroups.addAll( getWorkgroupsListForProfile( profile.getKey( ), plugin ) );
}
}
// Remove rights to the user
for ( Right right : getRightsListForProfile( strProfileKey, plugin ) )
{
if ( !listProfilesRights.contains( right ) && AdminUserHome.hasRight( user, right.getId( ) )
&& ( ( user.getUserLevel( ) > currentUser.getUserLevel( ) ) || currentUser.isAdmin( ) ) )
{
AdminUserHome.removeRightForUser( nIdUser, right.getId( ) );
}
}
// Remove roles to the user
for ( RBACRole role : getRolesListForProfile( strProfileKey, plugin ) )
{
if ( !listProfilesRoles.contains( role ) && AdminUserHome.hasRole( user, role.getKey( ) ) )
{
AdminUserHome.removeRoleForUser( nIdUser, role.getKey( ) );
}
}
// Remove workgroups to the user
for ( AdminWorkgroup workgroup : getWorkgroupsListForProfile( strProfileKey, plugin ) )
{
if ( !listProfilesWorkgroups.contains( workgroup ) && AdminWorkgroupHome.isUserInWorkgroup( user, workgroup.getKey( ) ) )
{
AdminWorkgroupHome.removeUserFromWorkgroup( user, workgroup.getKey( ) );
}
}
}
/**
* {@inheritDoc}
*/
@Override
public Profile create( Profile profile, Locale locale, Plugin plugin )
{
if ( profile != null )
{
ProfileHome.create( profile, plugin );
// Create user field
List<IAttribute> listAttributes = AttributeService.getInstance( ).getPluginAttributesWithoutFields( ProfilesPlugin.PLUGIN_NAME, locale );
AttributeField attributeField = new AttributeField( );
attributeField.setTitle( profile.getKey( ) );
attributeField.setValue( profile.getDescription( ) );
attributeField.setDefaultValue( false );
attributeField.setAttribute( listAttributes.get( 0 ) );
AttributeFieldService.getInstance( ).createAttributeField( attributeField );
}
return profile;
}
/**
* {@inheritDoc}
*/
@Override
public Profile update( Profile profile, Locale locale, Plugin plugin )
{
if ( profile != null )
{
ProfileHome.update( profile, plugin );
// Modify user field
List<IAttribute> listAttributes = AttributeService.getInstance( ).getPluginAttributesWithFields( ProfilesPlugin.PLUGIN_NAME, locale );
for ( IAttribute attribute : listAttributes )
{
if ( ( attribute == null ) || ( attribute.getListAttributeFields( ) == null ) || attribute.getListAttributeFields( ).isEmpty( ) )
{
continue;
}
for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
{
if ( ( attributeField.getTitle( ) != null ) && attributeField.getTitle( ).equals( profile.getKey( ) ) )
{
attributeField.setValue( profile.getDescription( ) );
AttributeFieldService.getInstance( ).updateAttributeField( attributeField );
break;
}
}
}
}
return profile;
}
/**
* {@inheritDoc}
*/
@Override
public void remove( String strProfileKey, Locale locale, Plugin plugin )
{
ProfileHome.remove( strProfileKey, plugin );
// Remove user field
List<IAttribute> listAttributes = AttributeService.getInstance( ).getPluginAttributesWithFields( ProfilesPlugin.PLUGIN_NAME, locale );
for ( IAttribute attribute : listAttributes )
{
if ( ( attribute == null ) || ( attribute.getListAttributeFields( ) == null ) || attribute.getListAttributeFields( ).isEmpty( ) )
{
continue;
}
for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
{
if ( ( attributeField.getTitle( ) != null ) && attributeField.getTitle( ).equals( strProfileKey ) )
{
AttributeFieldService.getInstance( ).removeAttributeFieldFromIdField( attributeField.getIdField( ) );
break;
}
}
}
}
///////////////////////////////////////////////////////////////////////////
// Finders
/**
* {@inheritDoc}
*/
@Override
public Profile findByPrimaryKey( String strProfileKey, Plugin plugin )
{
return ProfileHome.findByPrimaryKey( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public List<Profile> findAll( Plugin plugin )
{
return ProfileHome.findAll( plugin );
}
/**
* {@inheritDoc}
*/
@Override
public List<Profile> findProfilesByFilter( ProfileFilter pFilter, Plugin plugin )
{
return ProfileHome.findProfilesByFilter( pFilter, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean checkExistProfile( String strProfileKey, Plugin plugin )
{
return ProfileHome.checkExistProfile( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public ReferenceList getProfilesList( Plugin plugin )
{
return ProfileHome.getProfilesList( plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean checkProfileAttributed( String strProfileKey, Plugin plugin )
{
return ProfileHome.checkProfileAttributed( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public List<Profile> findProfileByIdUser( int nIdUser, Plugin plugin )
{
return ProfileHome.findProfileByIdUser( nIdUser, plugin );
}
/* RIGHTS */
/**
* {@inheritDoc}
*/
@Override
public List<Right> getRightsListForProfile( String strProfileKey, Plugin plugin )
{
return ProfileHome.getRightsListForProfile( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasRight( String strProfileKey, String strIdRight, Plugin plugin )
{
return ProfileHome.hasRight( strProfileKey, strIdRight, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void addRightForProfile( String strProfileKey, String strIdRight, Plugin plugin )
{
ProfileHome.addRightForProfile( strProfileKey, strIdRight, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeRightFromProfile( String strProfileKey, String strIdRight, Plugin plugin )
{
ProfileHome.removeRightFromProfile( strProfileKey, strIdRight, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeRights( String strProfileKey, Plugin plugin )
{
ProfileHome.removeRights( strProfileKey, plugin );
}
/* WORKGROUPS */
/**
* {@inheritDoc}
*/
@Override
public List<AdminWorkgroup> getWorkgroupsListForProfile( String strProfileKey, Plugin plugin )
{
return ProfileHome.getWorkgroupsListForProfile( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasWorkgroup( String strProfileKey, String strWorkgroupKey, Plugin plugin )
{
return ProfileHome.hasWorkgroup( strProfileKey, strWorkgroupKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void addWorkgroupForProfile( String strProfileKey, String strWorkgroupKey, Plugin plugin )
{
ProfileHome.addWorkgroupForProfile( strProfileKey, strWorkgroupKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeWorkgroupFromProfile( String strProfileKey, String strWorkgroupKey, Plugin plugin )
{
ProfileHome.removeWorkgroupFromProfile( strProfileKey, strWorkgroupKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeWorkgroups( String strProfileKey, Plugin plugin )
{
ProfileHome.removeWorkgroups( strProfileKey, plugin );
}
/* ROLES */
/**
* {@inheritDoc}
*/
@Override
public List<RBACRole> getRolesListForProfile( String strProfileKey, Plugin plugin )
{
return ProfileHome.getRolesListForProfile( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasRole( String strProfileKey, String strRoleKey, Plugin plugin )
{
return ProfileHome.hasRole( strProfileKey, strRoleKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void addRoleForProfile( String strProfileKey, String strRoleKey, Plugin plugin )
{
ProfileHome.addRoleForProfile( strProfileKey, strRoleKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeRoleFromProfile( String strProfileKey, String strRoleKey, Plugin plugin )
{
ProfileHome.removeRoleFromProfile( strProfileKey, strRoleKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeRoles( String strProfileKey, Plugin plugin )
{
ProfileHome.removeRoles( strProfileKey, plugin );
}
/* USERS */
/**
* {@inheritDoc}
*/
@Override
public List<AdminUser> getUsersListForProfile( String strProfileKey, Plugin plugin )
{
return ProfileHome.getUsersListForProfile( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasUser( String strProfileKey, int nIdUser, Plugin plugin )
{
return ProfileHome.hasUser( strProfileKey, nIdUser, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void addUserForProfile( String strProfileKey, int nIdUser, Plugin plugin )
{
ProfileHome.addUserForProfile( strProfileKey, nIdUser, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeUserFromProfile( String strProfileKey, int nIdUser, Plugin plugin )
{
ProfileHome.removeUserFromProfile( strProfileKey, nIdUser, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeUsers( String strProfileKey, Plugin plugin )
{
ProfileHome.removeUsers( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeProfilesFromUser( int nIdUser, Plugin plugin )
{
ProfileHome.removeProfilesFromUser( nIdUser, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasProfile( String strProfileKey, int nIdUser, Plugin plugin )
{
return ProfileHome.hasProfile( strProfileKey, nIdUser, plugin );
}
/* VIEW */
/**
* {@inheritDoc}
*/
@Override
public View getViewForProfile( String strProfileKey, Plugin plugin )
{
return ProfileHome.getViewForProfile( strProfileKey, plugin );
}
/**
* {@inheritDoc}
*/
@Override
public void removeView( String strProfileKey, Plugin plugin )
{
ProfileHome.removeView( strProfileKey, plugin );
}
}