Return-Path: X-Original-To: apmail-usergrid-commits-archive@minotaur.apache.org Delivered-To: apmail-usergrid-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9B5DC18B7E for ; Fri, 16 Oct 2015 21:20:43 +0000 (UTC) Received: (qmail 10972 invoked by uid 500); 16 Oct 2015 21:20:43 -0000 Delivered-To: apmail-usergrid-commits-archive@usergrid.apache.org Received: (qmail 10944 invoked by uid 500); 16 Oct 2015 21:20:43 -0000 Mailing-List: contact commits-help@usergrid.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@usergrid.apache.org Delivered-To: mailing list commits@usergrid.apache.org Received: (qmail 10935 invoked by uid 99); 16 Oct 2015 21:20:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 16 Oct 2015 21:20:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 50935E0231; Fri, 16 Oct 2015 21:20:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: grey@apache.org To: commits@usergrid.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: usergrid git commit: Added a tool to fix the "Could not find organization for email". This is primarily a 1.0 problem but if it does appear in 2.0 then this tool can be applied there as well. Date: Fri, 16 Oct 2015 21:20:43 +0000 (UTC) Repository: usergrid Updated Branches: refs/heads/AdminPointerFix [created] 6e2ff9613 Added a tool to fix the "Could not find organization for email". This is primarily a 1.0 problem but if it does appear in 2.0 then this tool can be applied there as well. Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/6e2ff961 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/6e2ff961 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/6e2ff961 Branch: refs/heads/AdminPointerFix Commit: 6e2ff96132ec2a95b540405bbb811cf3eb1b8180 Parents: 6c1d22e Author: George Reyes Authored: Fri Oct 16 14:20:40 2015 -0700 Committer: George Reyes Committed: Fri Oct 16 14:20:40 2015 -0700 ---------------------------------------------------------------------- .../apache/usergrid/tools/AdminPointerFix.java | 148 +++++++++++++++++++ 1 file changed, 148 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/6e2ff961/stack/tools/src/main/java/org/apache/usergrid/tools/AdminPointerFix.java ---------------------------------------------------------------------- diff --git a/stack/tools/src/main/java/org/apache/usergrid/tools/AdminPointerFix.java b/stack/tools/src/main/java/org/apache/usergrid/tools/AdminPointerFix.java new file mode 100644 index 0000000..72ae4d7 --- /dev/null +++ b/stack/tools/src/main/java/org/apache/usergrid/tools/AdminPointerFix.java @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.usergrid.tools; + + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; + +import org.apache.usergrid.management.UserInfo; +import org.apache.usergrid.persistence.Entity; +import org.apache.usergrid.persistence.EntityManager; +import org.apache.usergrid.persistence.Query; +import org.apache.usergrid.persistence.Results; +import org.apache.usergrid.persistence.cassandra.CassandraService; +import org.apache.usergrid.persistence.entities.Application; +import org.apache.usergrid.persistence.entities.User; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; + + +/** + * Fixes issues where admin data is in organizations but can't accessed or reset the password of. This usually returns + * the error "Could not find organization for email" because the user can't be found but when the org is queried the + * user shows up there. If you see that then run this tool. + */ +public class AdminPointerFix extends ExportingToolBase { + private static final int PAGE_SIZE = 100; + + private static final Logger logger = LoggerFactory.getLogger( AdminPointerFix.class ); + + + @Override + @SuppressWarnings( "static-access" ) + public Options createOptions() { + + Option hostOption = + OptionBuilder.withArgName( "host" ).hasArg().isRequired( true ).withDescription( "Cassandra host" ) + .create( "host" ); + + + + Options options = new Options(); + options.addOption( hostOption ); + + return options; + } + + + @Override + public void runTool( CommandLine line ) throws Exception { + + + startSpring(); + + logger.info( "Starting crawl of all admins" ); + + EntityManager em = emf.getEntityManager( CassandraService.MANAGEMENT_APPLICATION_ID ); + Application app = em.getApplication(); + + // search for all orgs + + Query query = new Query(); + query.setLimit( PAGE_SIZE ); + Results r = null; + + Multimap emails = HashMultimap.create(); + Multimap usernames = HashMultimap.create(); + do { + + //get all users in the management app and page for each set of a PAGE_SIZE + r = em.searchCollection( app, "users", query ); + + for ( Entity entity : r.getEntities() ) { + //grab all emails returned + emails.put( entity.getProperty( "email" ).toString().toLowerCase(), entity.getUuid() ); + //grab all usernames returned. + usernames.put( entity.getProperty( "username" ).toString().toLowerCase(), entity.getUuid() ); + } + + query.setCursor( r.getCursor() ); + + logger.info( "Searching next page" ); + } + while ( r != null && r.size() == PAGE_SIZE ); + + + //do a get on a specific username, if it shows up more than once then remove it + for ( String username : usernames.keySet() ) { + Collection ids = usernames.get( username ); + + if ( ids.size() > 1 ) { + logger.info( "Found multiple users with the username {}", username ); + } + } + + for ( String email : emails.keySet() ) { + Collection ids = emails.get( email ); + + if ( ids.size() > 1 ) { + logger.info( "Found multiple users with the email {}", email ); + } + + + UserInfo targetUser = managementService.getAdminUserByEmail( email ); + + if ( targetUser == null ) { + //This means that the org is mis associated with the user. + List tempIds = new ArrayList( ids ); + //Collections.sort( tempIds ); + + UUID toLoad = tempIds.get( 0 ); + + logger.warn( "Could not load target user by email {}, loading by UUID {} instead", email, toLoad ); + targetUser = managementService.getAdminUserByUuid( toLoad ); + User targetUserEntity = em.get( targetUser.getUuid(), User.class ); + em.update( targetUserEntity ); + + } + } + + logger.info( "Repair complete" ); + } +}