Beispiel #1
0
	def authenticate(self, username=None, password=None):
		try:
			user = User.objects.get(username=username.lower())

			# If user is found, check the password using the django
			# methods alone.
			if user.check_password(password):
				return user

			# User found but password wrong --> tell django it is wrong
			return None
		except User.DoesNotExist:
			# User does not exist. See if it exists in the old system,
			# and if it does, migrate it to the new one.
			curs = connection.cursor()
			curs.execute('SELECT * FROM community_login_old(%s,%s)', (username.lower(), password))
			rows = curs.fetchall()

			if len(rows) != 1:
				# No rows returned, something clearly went wrong
				return None
			if rows[0][1] == 1:
				# Value 1 in field 1 means the login succeeded. In this case,
				# create a user in the django system, and migrate all settings
				# we can think of.
				namepieces = rows[0][2].split(None, 2)
				if len(namepieces) == 0: namepieces = ['', '']
				if len(namepieces) == 1: namepieces.append('')
				user = User(username=username.lower(), email=rows[0][3], first_name=namepieces[0], last_name=namepieces[1])
				user.set_password(password)
				user.save()

				# Create a userprofile if we have to
				if rows[0][8]:
					profile = UserProfile(user=user)
					profile.sshkey = rows[0][8]
					profile.save()

				# Now delete the user in the old system so nobody can use it
				curs.execute('SELECT * FROM community_login_old_delete(%s)', (username.lower(), ))

				return user
			# Any other value in field 1 means login failed, so tell django we did
			return None

		return None # Should never get here, but just in case...
			print "Ok, migrating..."
			break
		elif yn == "n":
			print "Aborting"
			sys.exit(1)
		else:
			continue

	pwd = GenPasswd(12)
	print "New password: %s" % pwd

	namepieces = rows[0][1].split(None, 2)
	if len(namepieces) == 0: namepieces = ['', '']
	if len(namepieces) == 1: namepieces.append('')
	print "Creating new user record..."
	user = User(username=u, email=rows[0][2], first_name=namepieces[0], last_name=namepieces[1])
	user.set_password(pwd)
	user.save()
	if rows[0][3]:
		print "Migrating SSH key..."
		profile = UserProfile(user=user)
		profile.sshkey = rows[0][3]
		profile.save()

	print "Removing user from the old system..."
	curs.execute("SELECT * FROM community_login_old_delete(%s)", (u, ))

	transaction.commit()

	print "Done. Don't forget to email the user at %s, informing him/her about the new password %s" % (rows[0][2], pwd)