def _sync_user_credentials(self, data, addr): """ Received user credentials and send its uuid :param data: data send by client :param addr: client address """ self._logger.debug("User trying to login from %s", addr[0]) self._logger.debug("Checking credentials...") # format user information received data.replace("\n", "") user = User() user._email = data.split(":")[0] user._password = data.split(":")[1] # search user user_found = self._find_user(user) if user_found != None: # send the generated uuid to client self._socket.sendto(user_found._uuid, (addr[0], addr[1])) self._logger.debug("User found") self._logger.debug("User (email: %s) is logged", user_found._email) self._logger.debug("UUID: \"%s\" sent to %s", user_found._uuid, addr[0]) else: # send empty string to client self._socket.sendto("none", (addr[0], addr[1])) self._logger.debug("none string sent to %s", addr[0]) self._logger.debug("User not found") self._logger.debug("User (email: %s) failed to login", user._email)
def _login_user(self): """ Ask user login """ # send message to arduino to bling the LED self._send_message_to_arduino("3") self._logger.info("Please enter your email and password to login") user = User() sys.stdout.write("email: ") user._email = sys.stdin.readline() sys.stdout.write("password: "******"Login failed. Please check your internet and/or serial connection and retry") self._login_user() elif user._uuid == "none": self._logger.info("Login failed") self._login_user() elif re.compile("[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}").match(user._uuid): self._save_user_settings(user) self._logger.info("Login successful") # send message to arduino to put off LED self._send_message_to_arduino("4")
def add_user(self): """ Add user in data file """ self._logger.info("Please enter user information") user = User() # ask user information sys.stdout.write("first name: ") user._first_name = sys.stdin.readline() sys.stdout.write("last name: ") user._last_name = sys.stdin.readline() sys.stdout.write("email: ") user._email = sys.stdin.readline() sys.stdout.write("password: "******"\n", ""), 'lastName': user._last_name.replace("\n", ""), 'email': user._email.replace("\n", ""), 'password': user._password, 'uuid': user._uuid } # add user to the dict users.append(new_user) # save user settings in json file with open(DATA_FILE_LOCATION + DATA_FILE_NAME, "w") as file: json.dump(users, file, indent = 4, encoding = 'utf-8') # init user work data file with open(DATA_FILE_LOCATION + user._uuid + ".json", "w") as file: user_hours = { "uuid": user._uuid, "currentState": "", "workingHours": [] } json.dump(user_hours, file, indent = 4, encoding = 'utf-8') self._logger.info("User successfully added ") self._logger.debug("New user added (id: %s - email: %s)", user._id, user._email)