def authorize_external_user(request, data): """ Authorizes and returns a user based on a trusted JSON string """ username = data['username'] # get the user users = models.User.objects.filter(username=username) if users: # this user already exists in the database user = users[0] if user.profile.type != USER_EXTERNAL: raise Exception("this username already exists in BioStar for a local user") else: # create a new user email = data.get("email","no-email") user = models.User(username=username, email=email) user.save() # now update the profile user.profile.display_name = data.get("display_name", "Biostar User") user.profile.type = USER_EXTERNAL user.profile.my_tags = "galaxy" user.profile.save() # login the user password = models.make_uuid() user.set_password(password) user.save() user = authenticate(username=user.username, password=password) login(request=request, user=user) return user
def get_user(name, email, website='', about_me=''): users = models.User.objects.filter(email=email) if users: print '*** email %s already exists' % email return users[0] else: print '*** creating user %s:%s' % (name, email) username = models.make_uuid()[:10] user = models.User.objects.create(username=username, first_name=name, email=email) user.profile.display_name = name user.profile.website = website user.profile.about_me = about_me user.profile.save() return user
def test_login(request, uid, token): "This will allow test logins. Don't turn it on during production!" from django.contrib.auth import authenticate, login allow = (token == settings.SELENIUM_TEST_LOGIN_TOKEN) if settings.DEBUG and settings.SELENIUM_TEST_LOGIN_TOKEN and allow: user = models.User.objects.get(id=uid) password = models.make_uuid() user.set_password(password) user.save() user = authenticate(username=user.username, password=password) login(request=request, user=user) messages.info(request, "Test login complete.") else: messages.error(request, "Test login failed.") return html.redirect("/")