def send_password_reset_code(self): """Create a password reset code""" reset_token = ''.join( random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(RESET_TOKEN_LEN)) self._data['_reset_token'] = reset_token self._set_modified(True) self.save() logger.info("Password reset requested for (%s) : %s", self.email, reset_token) with (Path(app.config.ASSETS_PATH) / 'html/recover.html').open() as html, \ (Path(app.config.ASSETS_PATH) / 'txt/recover.txt').open() as txt, \ (Path(app.config.ASSETS_PATH) / 'img/epmanage.png').open('rb') as logo: msg = Message( html=T(html.read()), text=T(txt.read()), subject="Account recovery for {}".format(self.email), mail_from=("EPManage", app.config.EMAIL_ADDRESS), message_id=make_msgid(domain=app.config.EMAIL_DOMAIN)) msg.attach(filename="epmanage.png", content_disposition="inline", data=logo) msg.send(to=self.email, render={ 'USER_EMAIL': self.email, 'URL_TO_RESET': "{}/{}".format(app.config.RECOVERY_URL, reset_token) }) return True
def invite(data: dict): """ Create a Client object ready for registration :return: the registration_token to be used for registration """ max_agents = data.pop('max_agents') if max_agents > app.config.INVITE_MAX_AGENTS: raise AuthException("Please set a maximum of {} agents".format(app.config.INVITE_MAX_AGENTS)) max_users = data.pop('max_users') if max_users > app.config.INVITE_MAX_USERS: raise AuthException("Please set a maximum of {} users".format(app.config.INVITE_MAX_USERS)) client = epmanage.lib.client.Client() client.invitation_email = data.pop('email') client.max_agents = max_agents client.max_users = max_users client.token = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(32)) client.registration_token = ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(20)) client.database_settings = dict( DBNAME='{}_corp'.format(client.token) ) client.save() client.insert_default_data() sender = AuthController.get_current_identity().get_user() user_name = "{} {}".format(sender.firstname, sender.lastname) user_email = sender.email with (Path(app.config.ASSETS_PATH) / 'html/invite.html').open() as html, \ (Path(app.config.ASSETS_PATH) / 'txt/invite.txt').open() as txt, \ (Path(app.config.ASSETS_PATH) / 'img/rocket.png').open('rb') as rocket, \ (Path(app.config.ASSETS_PATH) / 'img/epmanage.png').open('rb') as logo: msg = Message( html=T(html.read()), text=T(txt.read()), subject="Invitation to the EPManage", mail_from=(user_name, user_email), message_id=make_msgid(domain=app.config.EMAIL_DOMAIN)) msg.attach(filename="rocket.png", content_disposition="inline", data=rocket) msg.attach(filename="epmanage.png", content_disposition="inline", data=logo) msg.send(to=client.invitation_email, render={ 'USER_NAME': user_name, 'USER_EMAIL': user_email, 'URL_TO_JOIN': "{}/{}".format(app.config.REGISTRATION_URL, client.registration_token) }) return client.registration_token