def userManagementNewGuestSession(request, response): #VARIABLE DECLARATION userInstance = None daoInstance = None try: #**************************************************************** # Step 1.GENERATE RANDOM PASSWORD AND A RANDOM EMAIL FOR GUEST USER #**************************************************************** logging.info("STEP1 - GETTING RANDOM PASS AND USER...") password = getRandowWord(6) #GENERATE A RANDOM PASSWORD USING A WORD daoInstance = UserDAO() valid = False userName = "" from random import randrange while valid == False: userName = "******" + str(randrange(99999)) valid = daoInstance.findByEmail(userName + "@paintomics.org") == None #**************************************************************** # Step 2. ADD NEW USER TO DATABASE #**************************************************************** logging.info( "STEP2 - CREATING USER INSTANCE AND SAVING TO DATABASE...") userInstance = User("") userInstance.setEmail(userName + "@paintomics.org") from hashlib import sha1 userInstance.setPassword(sha1(password.encode('ascii')).hexdigest()) userInstance.setUserName(userName) userInstance.setAffiliation("GUEST USER") #Update the last login date at the database from time import strftime today = strftime("%Y%m%d") userInstance.setCreationDate(today) userInstance.setLastLogin(today) userInstance.setIsGuest(True) userID = daoInstance.insert(userInstance) #**************************************************************** # Step 3. Create user directories #**************************************************************** logging.info("STEP3 - INITIALIZING DIRECTORIES...") initializeUserDirectories(str(userID)) #**************************************************************** # Step 4. Create new session #**************************************************************** logging.info("STEP4 - GETTING A NEW SESSION TOKEN...") sessionToken = UserSessionManager().registerNewUser("" + str(userID)) response.setContent({ "success": True, "userID": userID, "userName": userInstance.getUserName(), "sessionToken": sessionToken, "p": password }) except Exception as ex: handleException(response, ex, __file__, "userManagementNewGuestSession") finally: if (daoInstance != None): daoInstance.closeConnection() return response
def userManagementSignUp(request, response, ROOT_DIRECTORY): #VARIABLE DECLARATION userInstance = None daoInstance = None try: #**************************************************************** # Step 1.READ PARAMS AND CHECK IF USER ALREADY EXISTS #**************************************************************** logging.info("STEP1 - READ PARAMS AND CHECK IF USER ALREADY EXISTS...") formFields = request.form email = formFields.get("email") email = email.lower() password = formFields.get("password") userName = adapt_string(formFields.get("userName")) affiliation = adapt_string(formFields.get("affiliation")) daoInstance = UserDAO() userInstance = daoInstance.findByEmail(email) if userInstance != None: logging.info("STEP1 - ERROR! EMAIL ALREADY AT THE DATABASE...") raise CredentialException("Email is already registered") #**************************************************************** # Step 2. Add user to database #**************************************************************** logging.info( "STEP2 - CREATING USER INSTANCE AND SAVING TO DATABASE...") userInstance = User("") userInstance.setEmail(email) from hashlib import sha1 userInstance.setPassword(sha1(password.encode('ascii')).hexdigest()) userInstance.setUserName(userName) userInstance.setAffiliation(affiliation) #Update the last login date at the database from time import strftime today = strftime("%Y%m%d") userInstance.setCreationDate(today) userInstance.setLastLogin(today) userID = daoInstance.insert(userInstance) #**************************************************************** # Step 3. Sending confirmation email #**************************************************************** logging.info("STEP3 - SENDING CONFIRMATION EMAIL... TODO!!") try: #TODO: SERVER ADDRESS AND ADMIN EMAIL message = '<html><body>' message += "<a href='" + "http://bioinfo.cipf.es/paintomics/" + "' target='_blank'>" message += " <img src='" + "http://bioinfo.cipf.es/paintomics/" + "resources/images/paintomics_white_300x66' border='0' width='150' height='33' alt='Paintomics 3 logo'>" message += "</a>" message += "<div style='width:100%; height:10px; border-top: 1px dotted #333; margin-top:20px; margin-bottom:30px;'></div>" message += "<h1>Welcome to Paintomics 3!</h1>" message += "<p>Thanks for joining, " + userInstance.getUserName( ) + "! You're already able to work with Paintomics.</p>" message += "<p>Your user name is as follows:</p>" message += "<p><b>Username:</b> " + userInstance.getEmail( ) + "</p></br>" message += "<p>Login in to Paintomics 3 at </p><a href='" + "http://bioinfo.cipf.es/paintomics/" + "'>" + "http://bioinfo.cipf.es/paintomics/" + "</a>" message += "<div style='width:100%; height:10px; border-top: 1px dotted #333; margin-top:20px; margin-bottom:30px;'></div>" message += "<p>Problems? E-mail <a href='mailto:" + "*****@*****.**" + "'>" + "*****@*****.**" + "</a></p>" message += '</body></html>' sendEmail(ROOT_DIRECTORY, userInstance.getEmail(), userInstance.getUserName(), "Welcome to Paintomics 3", message, isHTML=True) except Exception: logging.error("Failed to send the email.") #**************************************************************** # Step 4. Create user directories #**************************************************************** logging.info("STEP4 - INITIALIZING DIRECTORIES...") initializeUserDirectories(str(userID)) response.setContent({"success": True}) except Exception as ex: handleException(response, ex, __file__, "userManagementSignUp") finally: if (daoInstance != None): daoInstance.closeConnection() return response