def confirm_migration(request): from tardis.tardis_portal.auth import auth_service migration_form = \ openid_user_migration_form() authForm = migration_form(request.POST) if not authForm.is_valid(): errorMessage = \ 'Please provide all the necessary information to authenticate.' return _getJsonFailedResponse(errorMessage) # let's try and authenticate here user = auth_service.authenticate(authMethod="None", request=request) if user is None: errorMessage = 'Wrong username or password. Please try again' return _getJsonFailedResponse(errorMessage) # do not allow migration from an inactive account if not user.is_active: errorMessage = 'Account is inactive' return _getJsonFailedResponse(errorMessage) # get request user auth method backend = request.session['_auth_user_backend'] # get key from backend class name auth_provider = get_matching_auth_provider(backend) data = _setupJsonData(user, request.user) data['auth_method'] = auth_provider[0] return _getJsonConfirmResponse(data)
def do_migration(request): """Migrating account from the account that the logged in user has provided in the Authentication Form. Migration involve relinking the UserAuthentication table entries, transferring ObjectACL entries to the migrated account, changing the Group memberships and making the old account inactive. :param Request request: the HTTP request object :returns: The HttpResponse which contains request.user's new list of authentication methods :rtype: HttpResponse """ from tardis.tardis_portal.auth import auth_service # let's try and authenticate here user = auth_service.authenticate(authMethod="None", request=request) if user is None: errorMessage = 'Wrong username or password. Please try again' return _getJsonFailedResponse(errorMessage) # if has already registered to use the provided auth method, then we can't # link the auth method to the user if user == request.user: errorMessage = "You can't migrate to the same account" return _getJsonFailedResponse(errorMessage) logger.info("starting migration from %s to %s", user.username, request.user.username) # get request user authentication method data = _setupJsonData(old_user=user, new_user=request.user) # get authenticated user backend backend = request.session['_auth_user_backend'] # get key from backend class name auth_provider = get_matching_auth_provider(backend) logger.info("linking user authentication") # in most of the case it should return one authentication method # but in case it returns more than one we need to throw an exception # and notify admin about this. try: userAuths = UserAuthentication.objects.filter(userProfile__user=user) if userAuths.count() > 1: logger.error("Multiple authentication methods found for user %s" % user) return _getJsonFailedResponse("Something went wrong") if userAuths.count() == 1: old_authentication_method = userAuths[0].authenticationMethod else: old_authentication_method = getattr(settings, 'DEFAULT_AUTH', 'localdb') except ValueError: logger.error("issue with authentication methods for user %s" % user) old_authentication_method = getattr(settings, 'DEFAULT_AUTH', 'localdb') logger.info("Old authentication method is %s", old_authentication_method) # let's search for the ACLs that refer to 'user' and transfer them # to request.user userIdToBeReplaced = user.id replacementUserId = request.user.id # for logging migration event user_migration_record = OpenidUserMigration( old_user=user, new_user=request.user, old_user_auth_method=old_authentication_method, new_user_auth_method=auth_provider[0]) user_migration_record.save() logger.info("Staring object ACL migration") acl_migration(userIdToBeReplaced, replacementUserId, user_migration_record) # let's also change the group memberships of all the groups that 'user' # is a member of logger.info("Migrating user groups") groups = Group.objects.filter(user=user) logger.info("Number of groups found : %s", groups.count()) for group in groups: request.user.groups.add(group) # change old user username to username_authmethod amd make it inactive old_username = user.username user.username = old_username + '_' + old_authentication_method logger.info("setting old user to inactive") user.is_active = False user.save() # change new user username to old user new_user = request.user # copy api key from old user to new user so that MyData works seamlessly post migration logger.info("migrating api key") migrate_api_key(user, request.user) # migrate user permissions logger.info("migrating user permissions") migrate_user_permissions(user, request.user) # Add migration event record user_migration_record.migration_status = True user_migration_record.save() # send email for successful migration # TODO : get request user auth method logger.info("sending email to %s", user.email) notify_migration_status.apply_async( args=[user.id, new_user.username, auth_provider[1]], priority=settings.DEFAULT_EMAIL_TASK_PRIORITY) logger.info("migration complete") if new_user.has_perm('openid_migration.add_openidusermigration'): perm = Permission.objects.get(codename='add_openidusermigration') new_user.user_permissions.remove(perm) # refresh permissions from db User.objects.get(pk=new_user.pk) message = ( "Your account has been migrated successfully. " "Please note that your old account has been deactivated and is no longer accessible. " "Please use Login via %s for all of your future logins to %s." % (auth_provider[1], getattr(settings, 'SITE_TITLE', 'MyTardis'))) messages.add_message(request, messages.INFO, message) data['auth_method'] = auth_provider[0] return _getJsonSuccessResponse(data=data)
def do_migration(request): """Migrating account from the account that the logged in user has provided in the Authentication Form. Migration involve relinking the UserAuthentication table entries, transferring ObjectACL entries to the migrated account, changing the Group memberships and making the old account inactive. :param Request request: the HTTP request object :returns: The HttpResponse which contains request.user's new list of authentication methods :rtype: HttpResponse """ from tardis.tardis_portal.auth import auth_service userAuthMethodList = [] # the list of supported non-local DB authentication methods supportedAuthMethods = getSupportedAuthMethods() # let's try and authenticate here user = auth_service.authenticate(authMethod="None", request=request) if user is None: errorMessage = 'Wrong username or password. Please try again' return _getJsonFailedResponse(errorMessage) # if has already registered to use the provided auth method, then we can't # link the auth method to the user if user == request.user: errorMessage = "You can't migrate to the same account" return _getJsonFailedResponse(errorMessage) logger.info("starting migration from %s to %s", user.username, request.user.username) # check if the "request.user" has a userProfile userProfile, created = UserProfile.objects.get_or_create( user=request.user) # get request user authentication method data = _setupJsonData(old_user=user, new_user=request.user) # get authenticated user backend backend = request.session['_auth_user_backend'] # get key from backend class name auth_provider = get_matching_auth_provider(backend) logger.info("linking user authentication") # in most of the case it should return one authentication method # but in case it returns more than one we need to throw an exception # and notify admin about this. try: userAuths = UserAuthentication.objects.filter( userProfile__user=user) if userAuths.count() > 1: logger.error("Multiple authentication methods found for user %s" % user) return _getJsonFailedResponse("Something went wrong") if userAuths.count() == 1: old_authentication_method = userAuths[0].authenticationMethod else: old_authentication_method = getattr(settings, 'DEFAULT_AUTH', 'localdb') except ValueError: logger.error("issue with authentication methods for user %s" % user) old_authentication_method = getattr(settings, 'DEFAULT_AUTH', 'localdb') logger.info("Old authentication method is %s", old_authentication_method) # let's search for the ACLs that refer to 'user' and transfer them # to request.user userIdToBeReplaced = user.id replacementUserId = request.user.id # for logging migration event user_migration_record = OpenidUserMigration( old_user=user, new_user=request.user, old_user_auth_method=old_authentication_method, new_user_auth_method=auth_provider[0]) user_migration_record.save() logger.info("Staring object ACL migration") acl_migration(userIdToBeReplaced, replacementUserId, user_migration_record) # let's also change the group memberships of all the groups that 'user' # is a member of logger.info("Migrating user groups") groups = Group.objects.filter(user=user) logger.info("Number of groups found : %s", groups.count()) for group in groups: request.user.groups.add(group) # change old user username to username_authmethod amd make it inactive old_username = user.username user.username = old_username + '_' + old_authentication_method logger.info("setting old user to inactive") user.is_active = False user.save() # change new user username to old user new_user = request.user # copy api key from old user to new user so that MyData works seamlessly post migration logger.info("migrating api key") migrate_api_key(user, request.user) # migrate user permissions logger.info("migrating user permissions") migrate_user_permissions(user, request.user) # Add migration event record user_migration_record.migration_status = True user_migration_record.save() # send email for successful migration # TODO : get request user auth method logger.info("sending email to %s", user.email) notify_migration_status.delay(user, new_user.username, auth_provider[1]) logger.info("migration complete") if new_user.has_perm('openid_migration.add_openidusermigration'): perm = Permission.objects.get(codename='add_openidusermigration') new_user.user_permissions.remove(perm) # refresh permissions from db User.objects.get(pk=new_user.pk) message = ( "Your account has been migrated successfully. " "Please note that your old account has been deactivated and is no longer accessible. " "Please use Login via %s for all of your future logins to %s." % (auth_provider[1], getattr(settings, 'SITE_TITLE', 'MyTardis'))) messages.add_message(request, messages.INFO, message) data['auth_method'] = auth_provider[0] return _getJsonSuccessResponse(data=data)