Added person lookup by email and email auth url key.
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
from __future__ import annotations
|
||||
import datetime, random
|
||||
import datetime, random, secrets
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import redis_lookup_id_random, sql_insert, sql_select, sql_update
|
||||
from app.lib_general import log, logging
|
||||
from app.lib_general import log, logging, send_email
|
||||
|
||||
# from app.methods.account_methods import load_account_cfg_obj
|
||||
from app.methods.contact_methods import load_contact_obj, update_contact_obj
|
||||
# from app.methods.event_methods import get_event_rec_list
|
||||
from app.methods.order_methods import load_order_obj, get_order_rec_list
|
||||
@@ -15,6 +16,7 @@ from app.methods.person_methods import create_person_obj_v3, load_person_obj, up
|
||||
from app.methods.post_methods import get_post_rec_list, load_post_obj
|
||||
from app.methods.user_role_methods import get_user_role_rec_list, load_user_role_obj
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.user_models import User_Base, User_New_Base, User_Out_Base
|
||||
|
||||
|
||||
@@ -482,3 +484,120 @@ def get_user_rec_list(
|
||||
|
||||
return user_rec_li
|
||||
# ### END ### API User Methods ### get_user_rec_list() ###
|
||||
|
||||
|
||||
# ### BEGIN ### User Methods ### email_user_auth_key_url() ###
|
||||
# This emails the actual one time use sign in URL for a user.
|
||||
# Updated 2021-12-02
|
||||
def email_user_auth_key_url(
|
||||
account_id: int|str,
|
||||
user_id: int|str,
|
||||
root_url: str,
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else: return False
|
||||
|
||||
if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass
|
||||
else: return False
|
||||
|
||||
if user_obj := load_user_obj(
|
||||
user_id = user_id,
|
||||
):
|
||||
log.info('User object loaded')
|
||||
if user_obj.enable and user_obj.allow_auth_key:
|
||||
new_auth_key = secrets.token_urlsafe(default_num_bytes)
|
||||
update_user_data = {}
|
||||
update_user_data['auth_key'] = new_auth_key
|
||||
|
||||
if user_rec_update_result := sql_update(
|
||||
table_name = 'user',
|
||||
record_id = user_id,
|
||||
data = update_user_data
|
||||
):
|
||||
log.info('The user record was updated with a new auth_key')
|
||||
else:
|
||||
log.warning('The user record was not updated with a new auth_key')
|
||||
return False
|
||||
else:
|
||||
log.warning('The user record was not enabled or auth_key is not allowed')
|
||||
return False
|
||||
else: return False
|
||||
log.debug(user_obj)
|
||||
|
||||
from app.methods.account_cfg_methods import load_account_cfg_obj
|
||||
if account_cfg := load_account_cfg_obj(
|
||||
account_id = account_id,
|
||||
):
|
||||
log.info('Account config loaded')
|
||||
else: return False
|
||||
log.debug(account_cfg)
|
||||
|
||||
user_id_random = user_obj.id_random # NOTE: Not user_id_random because of alias
|
||||
|
||||
from_email = account_cfg.default_no_reply_email
|
||||
from_name = account_cfg.default_no_reply_name
|
||||
|
||||
to_name = user_obj.name
|
||||
to_email = user_obj.email
|
||||
|
||||
bcc_email = account_cfg.confirm_email
|
||||
bcc_name = account_cfg.confirm_name
|
||||
|
||||
help_tech_email = account_cfg.help_tech_email
|
||||
help_tech_name = account_cfg.help_tech_name
|
||||
|
||||
account_short_name = account_cfg.account_short_name
|
||||
|
||||
username = user_obj.username
|
||||
enable = user_obj.enable
|
||||
if enable_from := user_obj.enable_from:
|
||||
# enable_from_datetime = datetime.datetime.fromisoformat(enable_from).replace(tzinfo=datetime.timezone.utc)
|
||||
enable_from_datetime = enable_from.replace(tzinfo=datetime.timezone.utc)
|
||||
enable_from_datetime = enable_from
|
||||
enable_from_str = enable_from_datetime.strftime('%A, %B %-d, %Y %-I:%M %p %Z')
|
||||
else: enable_from_str = '-- Not Set --'
|
||||
if enable_to := user_obj.enable_to:
|
||||
# enable_to_datetime = datetime.datetime.fromisoformat(enable_to).replace(tzinfo=datetime.timezone.utc)
|
||||
enable_to_datetime = enable_to.replace(tzinfo=datetime.timezone.utc)
|
||||
enable_to_str = enable_to_datetime.strftime('%A, %B %-d, %Y %-I:%M %p %Z')
|
||||
else: enable_to_str = '-- Not Set --'
|
||||
auth_key = user_obj.auth_key
|
||||
|
||||
user_login_url = f'{root_url}user/login?username={username}'
|
||||
user_login_auth_key_url = f'{root_url}?user_id={user_id_random}&auth_key={new_auth_key}'
|
||||
|
||||
subject = f'{account_short_name}: One Time Use Sign In Link ({new_auth_key})'
|
||||
|
||||
body_html = f"""
|
||||
<p>{to_name},</p>
|
||||
|
||||
<p>If you did not request this sign in link, please delete this email. It is suggested that you delete this email after the sign in link has been used or if a new link has been requested.</p>
|
||||
|
||||
<p>The link below can only be used to sign in once. If you would like to sign in again using this method, you must <a href="{user_login_url}">request a new sign in link</a>. If you request multiple links, only the newest link will sign you in.</p>
|
||||
|
||||
<p><strong><a href="{user_login_auth_key_url}" style="appearance: button; display: inline-block; text-align: center; text-decoration: none; padding: .2rem .4rem; border: solid thin gray; border-radius: .2rem; background-color: lightyellow; color: black; font-size: larger;">Click to Sign In With One Time Use Link</a></strong></p>
|
||||
|
||||
<p>Or copy and paste the link:<br>
|
||||
<strong style="background-color: lightyellow; color: black; font-size: larger;"><a href="{user_login_auth_key_url}">{user_login_auth_key_url}</a></strong></p>
|
||||
|
||||
<p>Your username is: {username}<br>
|
||||
User account enabled: {enable}<br>
|
||||
User account enabled from: {enable_from_str}<br>
|
||||
User account enabled to: {enable_to_str}<br>
|
||||
Current one time use auth key for user account: {new_auth_key}</p>
|
||||
|
||||
<p>If you have questions about this email or trouble with this one time use link, you can email <a href="mailto:{help_tech_email}">{help_tech_name} ({help_tech_email})</a>.</p>
|
||||
|
||||
<p>Thank you!</p>
|
||||
"""
|
||||
|
||||
if send_email(from_email=from_email, from_name=from_name, to_email=to_email, to_name=to_name, bcc_email=bcc_email, bcc_name=bcc_name, subject=subject, body_text=None, body_html=body_html):
|
||||
log.info(f'An email with a one time use sign in link was sent to {to_email}.')
|
||||
return True
|
||||
else:
|
||||
log.info(f'An email with a one time use sign in link was not sent to {to_email}.')
|
||||
return False
|
||||
# ### END ### User ### email_user_auth_key_url() ###
|
||||
|
||||
Reference in New Issue
Block a user