← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17948: User controller, implemented method for replicating user with a new username and password. Useful...

 

------------------------------------------------------------
revno: 17948
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-01-12 18:23:39 +0100
message:
  User controller, implemented method for replicating user with a new username and password. Useful for debugging purposes when you need to reproduce issues reported by a given user.
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/TranslationController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java	2015-01-02 11:37:51 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/ChartController.java	2015-01-12 17:23:39 +0000
@@ -72,7 +72,6 @@
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
- * @author Lars Helge Overland
  */
 @Controller
 @RequestMapping( value = ChartSchemaDescriptor.API_ENDPOINT )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/TranslationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/TranslationController.java	2014-05-22 12:40:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/TranslationController.java	2015-01-12 17:23:39 +0000
@@ -1,5 +1,33 @@
 package org.hisp.dhis.webapi.controller;
 
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project 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 OWNER 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.
+ */
+
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.translation.TranslationService;
@@ -16,9 +44,8 @@
 import static org.hisp.dhis.webapi.utils.ContextUtils.CONTENT_TYPE_JSON;
 
 /**
- * Created by kprakash on 05/05/14.
+ * @author kprakash.
  */
-
 @Controller
 @RequestMapping(value = TranslationController.RESOURCE_PATH)
 public class TranslationController
@@ -46,5 +73,4 @@
         Translations translations = JacksonUtils.fromJson( request.getInputStream(), Translations.class );
         translationService.createOrUpdate( translations.getTranslations() );
     }
-
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java	2015-01-02 11:34:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java	2015-01-12 17:23:39 +0000
@@ -28,8 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
+import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
@@ -55,16 +64,14 @@
 import org.hisp.dhis.webapi.webdomain.WebMetaData;
 import org.hisp.dhis.webapi.webdomain.WebOptions;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.util.List;
-
-import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -75,8 +82,10 @@
     extends AbstractCrudController<User>
 {
     public static final String INVITE_PATH = "/invite";
-
     public static final String BULK_INVITE_PATH = "/invites";
+    
+    private static final String KEY_USERNAME = "username";
+    private static final String KEY_PASSWORD = "password";
 
     @Autowired
     private UserService userService;
@@ -250,6 +259,63 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
+    @PreAuthorize( "hasRole('ALL')" )
+    @RequestMapping( value = "/{uid}/replica", method = RequestMethod.POST )
+    public void replicateUser( @PathVariable String uid, 
+        HttpServletRequest request, HttpServletResponse response ) throws IOException
+    {
+        User existingUser = userService.getUser( uid );
+        
+        if ( existingUser == null || existingUser.getUserCredentials() == null )
+        {
+            ContextUtils.conflictResponse( response, "User not found: " + uid );
+            return;
+        }
+        
+        if ( !validateCreateUser( existingUser, response ) )
+        {
+            return;
+        }
+        
+        Map<String, String> auth = renderService.fromJson( request.getInputStream(),Map.class );
+        
+        if ( auth == null || !auth.containsKey( KEY_USERNAME ) || !auth.containsKey( KEY_PASSWORD ) )
+        {
+            ContextUtils.conflictResponse( response, "Username and password must be specified" );
+            return;
+        }
+        
+        String username = auth.get( KEY_USERNAME );
+        String password = auth.get( KEY_PASSWORD );
+        
+        if ( userService.getUserCredentialsByUsername( username ) != null )
+        {
+            ContextUtils.conflictResponse( response, "Username already taken: " + username );
+            return;
+        }
+        
+        User userReplica = new User();
+        userReplica.mergeWith( existingUser );
+        userReplica.setUid( CodeGenerator.generateCode() );
+        userReplica.setCreated( new Date() );
+        
+        UserCredentials credentialsReplica = new UserCredentials();
+        credentialsReplica.mergeWith( existingUser.getUserCredentials() );
+        
+        credentialsReplica.setUsername( username );
+        credentialsReplica.setPassword( password );
+        
+        userReplica.setUserCredentials( credentialsReplica );
+        credentialsReplica.setUser( userReplica );
+        
+        userService.addUser( userReplica );
+        userService.addUserCredentials( credentialsReplica );
+        userGroupService.addUserToGroups( userReplica, IdentifiableObjectUtils.getUids( existingUser.getGroups() ) );
+        
+        ContextUtils.createdResponse( response, "User replica created", UserSchemaDescriptor.API_ENDPOINT + "/" + userReplica.getUid() );
+    }
+    
     // -------------------------------------------------------------------------
     // PUT
     // -------------------------------------------------------------------------