~stian-sandvold/dhis2/UserSettingFallback

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java

  • Committer: Stian Sandvold
  • Date: 2016-03-14 12:16:33 UTC
  • mfrom: (21871.1.422 dhis2)
  • Revision ID: stian.sandvold@gmail.com-20160314121633-ojggeaoszdunzltj
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.dxf2.metadata2.objectbundle.hooks;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2016, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
11
 *
 
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.
 
18
 *
 
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.
 
29
 */
 
30
 
 
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;
 
40
 
 
41
import java.util.List;
 
42
import java.util.Map;
 
43
import java.util.Set;
 
44
 
 
45
/**
 
46
 * @author Morten Olav Hansen <mortenoh@gmail.com>
 
47
 */
 
48
@Component
 
49
public class UserObjectBundleHook extends AbstractObjectBundleHook
 
50
{
 
51
    @Autowired
 
52
    private UserService userService;
 
53
 
 
54
    private UserCredentials userCredentials;
 
55
 
 
56
    @Override
 
57
    public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
 
58
    {
 
59
        if ( !User.class.isInstance( identifiableObject ) || ((User) identifiableObject).getUserCredentials() == null ) return;
 
60
 
 
61
        User user = (User) identifiableObject;
 
62
        userCredentials = user.getUserCredentials();
 
63
        user.setUserCredentials( null );
 
64
 
 
65
        if ( objectBundle.getPreheat().getUsernames().containsKey( userCredentials.getUsername() ) )
 
66
        {
 
67
            // Username exists, throw validation error
 
68
        }
 
69
    }
 
70
 
 
71
    @Override
 
72
    public void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
 
73
    {
 
74
        if ( !User.class.isInstance( identifiableObject ) || userCredentials == null ) return;
 
75
 
 
76
        User user = (User) identifiableObject;
 
77
 
 
78
        if ( !StringUtils.isEmpty( userCredentials.getPassword() ) )
 
79
        {
 
80
            userService.encodeAndSetPassword( userCredentials, userCredentials.getPassword() );
 
81
        }
 
82
 
 
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;
 
88
    }
 
89
 
 
90
    @Override
 
91
    public void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
 
92
    {
 
93
        if ( !User.class.isInstance( identifiableObject ) || ((User) identifiableObject).getUserCredentials() == null ) return;
 
94
        User user = (User) identifiableObject;
 
95
        userCredentials = user.getUserCredentials();
 
96
    }
 
97
 
 
98
    @Override
 
99
    public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
 
100
    {
 
101
        if ( !User.class.isInstance( identifiableObject ) || userCredentials == null ) return;
 
102
 
 
103
        User user = (User) identifiableObject;
 
104
        UserCredentials persistedUserCredentials = objectBundle.getPreheat().get( objectBundle.getPreheatIdentifier(), UserCredentials.class, user );
 
105
 
 
106
        if ( !StringUtils.isEmpty( userCredentials.getPassword() ) )
 
107
        {
 
108
            userService.encodeAndSetPassword( userCredentials, userCredentials.getPassword() );
 
109
        }
 
110
 
 
111
        persistedUserCredentials.mergeWith( userCredentials, objectBundle.getMergeMode() );
 
112
        preheatService.connectReferences( persistedUserCredentials, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
 
113
 
 
114
        persistedUserCredentials.setUserInfo( user );
 
115
        user.setUserCredentials( persistedUserCredentials );
 
116
 
 
117
        sessionFactory.getCurrentSession().update( user.getUserCredentials() );
 
118
        userCredentials = null;
 
119
    }
 
120
 
 
121
    @Override
 
122
    @SuppressWarnings( "unchecked" )
 
123
    public void postImport( ObjectBundle objectBundle )
 
124
    {
 
125
        if ( !objectBundle.getObjectMap().containsKey( User.class ) ) return;
 
126
 
 
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 );
 
130
 
 
131
        if ( userReferences == null || userReferences.isEmpty() || userCredentialsReferences == null || userCredentialsReferences.isEmpty() )
 
132
        {
 
133
            return;
 
134
        }
 
135
 
 
136
        for ( IdentifiableObject identifiableObject : objects )
 
137
        {
 
138
            identifiableObject = objectBundle.getPreheat().get( objectBundle.getPreheatIdentifier(), identifiableObject );
 
139
            Map<String, Object> userReferenceMap = userReferences.get( identifiableObject.getUid() );
 
140
 
 
141
            if ( userReferenceMap == null || userReferenceMap.isEmpty() )
 
142
            {
 
143
                continue;
 
144
            }
 
145
 
 
146
            User user = (User) identifiableObject;
 
147
            UserCredentials userCredentials = user.getUserCredentials();
 
148
            Map<String, Object> userCredentialsReferenceMap = userCredentialsReferences.get( userCredentials.getUid() );
 
149
 
 
150
            if ( userCredentialsReferenceMap == null || userCredentialsReferenceMap.isEmpty() )
 
151
            {
 
152
                continue;
 
153
            }
 
154
 
 
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 );
 
159
 
 
160
            preheatService.connectReferences( user, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
 
161
            preheatService.connectReferences( userCredentials, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
 
162
 
 
163
            user.setUserCredentials( userCredentials );
 
164
            sessionFactory.getCurrentSession().update( user );
 
165
        }
 
166
    }
 
167
}