1
package org.hisp.dhis.dxf2.metadata2.objectbundle.hooks;
4
* Copyright (c) 2004-2016, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright notice,
13
* this list of conditions and the following disclaimer in the documentation
14
* and/or other materials provided with the distribution.
15
* Neither the name of the HISP project nor the names of its contributors may
16
* be used to endorse or promote products derived from this software without
17
* specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
import org.hisp.dhis.common.IdentifiableObject;
32
import org.hisp.dhis.dxf2.metadata2.objectbundle.ObjectBundle;
33
import org.hisp.dhis.organisationunit.OrganisationUnit;
34
import org.hisp.dhis.user.User;
35
import org.hisp.dhis.user.UserCredentials;
36
import org.hisp.dhis.user.UserService;
37
import org.springframework.beans.factory.annotation.Autowired;
38
import org.springframework.stereotype.Component;
39
import org.springframework.util.StringUtils;
41
import java.util.List;
46
* @author Morten Olav Hansen <mortenoh@gmail.com>
49
public class UserObjectBundleHook extends AbstractObjectBundleHook
52
private UserService userService;
54
private UserCredentials userCredentials;
57
public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
59
if ( !User.class.isInstance( identifiableObject ) || ((User) identifiableObject).getUserCredentials() == null ) return;
61
User user = (User) identifiableObject;
62
userCredentials = user.getUserCredentials();
63
user.setUserCredentials( null );
65
if ( objectBundle.getPreheat().getUsernames().containsKey( userCredentials.getUsername() ) )
67
// Username exists, throw validation error
72
public void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
74
if ( !User.class.isInstance( identifiableObject ) || userCredentials == null ) return;
76
User user = (User) identifiableObject;
78
if ( !StringUtils.isEmpty( userCredentials.getPassword() ) )
80
userService.encodeAndSetPassword( userCredentials, userCredentials.getPassword() );
83
preheatService.connectReferences( userCredentials, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
84
sessionFactory.getCurrentSession().save( userCredentials );
85
user.setUserCredentials( userCredentials );
86
sessionFactory.getCurrentSession().update( user );
87
userCredentials = null;
91
public void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
93
if ( !User.class.isInstance( identifiableObject ) || ((User) identifiableObject).getUserCredentials() == null ) return;
94
User user = (User) identifiableObject;
95
userCredentials = user.getUserCredentials();
99
public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
101
if ( !User.class.isInstance( identifiableObject ) || userCredentials == null ) return;
103
User user = (User) identifiableObject;
104
UserCredentials persistedUserCredentials = objectBundle.getPreheat().get( objectBundle.getPreheatIdentifier(), UserCredentials.class, user );
106
if ( !StringUtils.isEmpty( userCredentials.getPassword() ) )
108
userService.encodeAndSetPassword( userCredentials, userCredentials.getPassword() );
111
persistedUserCredentials.mergeWith( userCredentials, objectBundle.getMergeMode() );
112
preheatService.connectReferences( persistedUserCredentials, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
114
persistedUserCredentials.setUserInfo( user );
115
user.setUserCredentials( persistedUserCredentials );
117
sessionFactory.getCurrentSession().update( user.getUserCredentials() );
118
userCredentials = null;
122
@SuppressWarnings( "unchecked" )
123
public void postImport( ObjectBundle objectBundle )
125
if ( !objectBundle.getObjectMap().containsKey( User.class ) ) return;
127
List<IdentifiableObject> objects = objectBundle.getObjectMap().get( User.class );
128
Map<String, Map<String, Object>> userReferences = objectBundle.getObjectReferences( User.class );
129
Map<String, Map<String, Object>> userCredentialsReferences = objectBundle.getObjectReferences( UserCredentials.class );
131
if ( userReferences == null || userReferences.isEmpty() || userCredentialsReferences == null || userCredentialsReferences.isEmpty() )
136
for ( IdentifiableObject identifiableObject : objects )
138
identifiableObject = objectBundle.getPreheat().get( objectBundle.getPreheatIdentifier(), identifiableObject );
139
Map<String, Object> userReferenceMap = userReferences.get( identifiableObject.getUid() );
141
if ( userReferenceMap == null || userReferenceMap.isEmpty() )
146
User user = (User) identifiableObject;
147
UserCredentials userCredentials = user.getUserCredentials();
148
Map<String, Object> userCredentialsReferenceMap = userCredentialsReferences.get( userCredentials.getUid() );
150
if ( userCredentialsReferenceMap == null || userCredentialsReferenceMap.isEmpty() )
155
user.setOrganisationUnits( (Set<OrganisationUnit>) userReferenceMap.get( "organisationUnits" ) );
156
user.setDataViewOrganisationUnits( (Set<OrganisationUnit>) userReferenceMap.get( "dataViewOrganisationUnits" ) );
157
userCredentials.setUser( (User) userCredentialsReferenceMap.get( "user" ) );
158
userCredentials.setUserInfo( user );
160
preheatService.connectReferences( user, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
161
preheatService.connectReferences( userCredentials, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
163
user.setUserCredentials( userCredentials );
164
sessionFactory.getCurrentSession().update( user );