<s> """<STR_LIT>""" <EOL> from passlib . context import CryptContext <EOL> from flask import Blueprint , current_app , url_for <EOL> from flask_login import LoginManager , UserMixin as LoginUserMixin , make_secure_token <EOL> from flask_user . db_adapters import DBAdapter <EOL> from . db_adapters import SQLAlchemyAdapter <EOL> from . import emails <EOL> from . import forms <EOL> from . import passwords <EOL> from . import settings <EOL> from . import tokens <EOL> from . import translations <EOL> from . import views <EOL> from . import signals <EOL> from . translations import get_translations <EOL> from flask_login import current_user <EOL> from . decorators import * <EOL> from . signals import * <EOL> __version__ = '<STR_LIT>' <EOL> def _call_or_get ( function_or_property ) : <EOL> return function_or_property ( ) if callable ( function_or_property ) else function_or_property <EOL> def _flask_user_context_processor ( ) : <EOL> """<STR_LIT>""" <EOL> return dict ( <EOL> user_manager = current_app . user_manager , <EOL> call_or_get = _call_or_get ) <EOL> class UserManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db_adapter = None , app = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . db_adapter = db_adapter <EOL> self . app = app <EOL> if db_adapter is not None and app is not None : <EOL> self . init_app ( app , db_adapter , ** kwargs ) <EOL> def init_app ( self , app , db_adapter = None , <EOL> add_email_form = forms . AddEmailForm , <EOL> change_password_form = forms . ChangePasswordForm , <EOL> change_username_form = forms . ChangeUsernameForm , <EOL> forgot_password_form = forms . ForgotPasswordForm , <EOL> login_form = forms . LoginForm , <EOL> register_form = forms . RegisterForm , <EOL> resend_confirm_email_form = forms . ResendConfirmEmailForm , <EOL> reset_password_form = forms . ResetPasswordForm , <EOL> invite_form = forms . InviteForm , <EOL> username_validator = forms . username_validator , <EOL> password_validator = forms . password_validator , <EOL> change_password_view_function = views . change_password , <EOL> change_username_view_function = views . change_username , <EOL> confirm_email_view_function = views . confirm_email , <EOL> email_action_view_function = views . email_action , <EOL> forgot_password_view_function = views . forgot_password , <EOL> login_view_function = views . login , <EOL> logout_view_function = views . logout , <EOL> manage_emails_view_function = views . manage_emails , <EOL> register_view_function = views . register , <EOL> resend_confirm_email_view_function = views . resend_confirm_email , <EOL> reset_password_view_function = views . reset_password , <EOL> unconfirmed_email_view_function = views . unconfirmed , <EOL> unauthenticated_view_function = views . unauthenticated , <EOL> unauthorized_view_function = views . unauthorized , <EOL> user_profile_view_function = views . user_profile , <EOL> invite_view_function = views . invite , <EOL> login_manager = LoginManager ( ) , <EOL> password_crypt_context = None , <EOL> send_email_function = emails . send_email , <EOL> token_manager = tokens . TokenManager ( ) , <EOL> legacy_check_password_hash = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> self . app = app <EOL> if db_adapter is not None : <EOL> self . db_adapter = db_adapter <EOL> self . add_email_form = add_email_form <EOL> self . change_password_form = change_password_form <EOL> self . change_username_form = change_username_form <EOL> self . forgot_password_form = forgot_password_form <EOL> self . login_form = login_form <EOL> self . register_form = register_form <EOL> self . resend_confirm_email_form = resend_confirm_email_form <EOL> self . reset_password_form = reset_password_form <EOL> self . invite_form = invite_form <EOL> self . username_validator = username_validator <EOL> self . password_validator = password_validator <EOL> self . change_password_view_function = change_password_view_function <EOL> self . change_username_view_function = change_username_view_function <EOL> self . confirm_email_view_function = confirm_email_view_function <EOL> self . email_action_view_function = email_action_view_function <EOL> self . forgot_password_view_function = forgot_password_view_function <EOL> self . login_view_function = login_view_function <EOL> self . logout_view_function = logout_view_function <EOL> self . manage_emails_view_function = manage_emails_view_function <EOL> self . register_view_function = register_view_function <EOL> self . resend_confirm_email_view_function = resend_confirm_email_view_function <EOL> self . reset_password_view_function = reset_password_view_function <EOL> self . unconfirmed_email_view_function = unconfirmed_email_view_function <EOL> self . unauthenticated_view_function = unauthenticated_view_function <EOL> self . unauthorized_view_function = unauthorized_view_function <EOL> self . user_profile_view_function = user_profile_view_function <EOL> self . invite_view_function = invite_view_function <EOL> self . login_manager = login_manager <EOL> self . token_manager = token_manager <EOL> self . password_crypt_context = password_crypt_context <EOL> self . send_email_function = send_email_function <EOL> self . legacy_check_password_hash = legacy_check_password_hash <EOL> """<STR_LIT>""" <EOL> app . user_manager = self <EOL> if not hasattr ( app , '<STR_LIT>' ) : <EOL> app . extensions = { } <EOL> app . extensions [ '<STR_LIT:user>' ] = self <EOL> settings . set_default_settings ( self , app . config ) <EOL> settings . check_settings ( self ) <EOL> if hasattr ( app . jinja_env , '<STR_LIT>' ) : <EOL> app . jinja_env . install_gettext_callables ( <EOL> lambda x : get_translations ( ) . ugettext ( x ) , <EOL> lambda s , p , n : get_translations ( ) . ungettext ( s , p , n ) , <EOL> newstyle = True ) <EOL> else : <EOL> app . jinja_env . add_extension ( '<STR_LIT>' ) <EOL> app . jinja_env . install_null_translations ( ) <EOL> if not self . password_crypt_context : <EOL> self . password_crypt_context = CryptContext ( <EOL> schemes = [ app . config [ '<STR_LIT>' ] ] ) <EOL> self . setup_login_manager ( app ) <EOL> self . token_manager . setup ( app . config . get ( '<STR_LIT>' ) ) <EOL> blueprint = Blueprint ( '<STR_LIT>' , '<STR_LIT>' , template_folder = '<STR_LIT>' ) <EOL> app . register_blueprint ( blueprint ) <EOL> self . add_url_routes ( app ) <EOL> app . context_processor ( _flask_user_context_processor ) <EOL> _ = translations . gettext <EOL> def setup_login_manager ( self , app ) : <EOL> @ self . login_manager . user_loader <EOL> def load_user_by_id ( user_unicode_id ) : <EOL> user_id = int ( user_unicode_id ) <EOL> return self . get_user_by_id ( user_id ) <EOL> @ self . login_manager . token_loader <EOL> def load_user_by_token ( token ) : <EOL> user_id = self . token_manager . decrypt_id ( token ) <EOL> return self . get_user_by_id ( int ( user_id ) ) <EOL> self . login_manager . login_view = '<STR_LIT>' <EOL> self . login_manager . init_app ( app ) <EOL> def add_url_routes ( self , app ) : <EOL> """<STR_LIT>""" <EOL> app . add_url_rule ( self . login_url , '<STR_LIT>' , self . login_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> app . add_url_rule ( self . logout_url , '<STR_LIT>' , self . logout_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . enable_confirm_email : <EOL> app . add_url_rule ( self . confirm_email_url , '<STR_LIT>' , self . confirm_email_view_function ) <EOL> app . add_url_rule ( self . resend_confirm_email_url , '<STR_LIT>' , self . resend_confirm_email_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . enable_change_password : <EOL> app . add_url_rule ( self . change_password_url , '<STR_LIT>' , self . change_password_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . enable_change_username : <EOL> app . add_url_rule ( self . change_username_url , '<STR_LIT>' , self . change_username_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . enable_forgot_password : <EOL> app . add_url_rule ( self . forgot_password_url , '<STR_LIT>' , self . forgot_password_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> app . add_url_rule ( self . reset_password_url , '<STR_LIT>' , self . reset_password_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . enable_register : <EOL> app . add_url_rule ( self . register_url , '<STR_LIT>' , self . register_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . db_adapter . UserEmailClass : <EOL> app . add_url_rule ( self . email_action_url , '<STR_LIT>' , self . email_action_view_function ) <EOL> app . add_url_rule ( self . manage_emails_url , '<STR_LIT>' , self . manage_emails_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> app . add_url_rule ( self . user_profile_url , '<STR_LIT>' , self . user_profile_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> if self . enable_invitation : <EOL> app . add_url_rule ( self . invite_url , '<STR_LIT>' , self . invite_view_function , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def generate_password_hash ( self , password ) : <EOL> return passwords . hash_password ( self , password ) <EOL> def hash_password ( self , password ) : <EOL> return passwords . hash_password ( self , password ) <EOL> def get_password ( self , user ) : <EOL> use_auth_class = True if self . db_adapter . UserAuthClass and hasattr ( user , '<STR_LIT>' ) else False <EOL> if self . db_adapter . UserProfileClass : <EOL> hashed_password = user . password <EOL> else : <EOL> hashed_password = user . user_auth . password if use_auth_class else user . password <EOL> return hashed_password <EOL> def update_password ( self , user , hashed_password ) : <EOL> use_auth_class = True if self . db_adapter . UserAuthClass and hasattr ( user , '<STR_LIT>' ) else False <EOL> if use_auth_class : <EOL> user . user_auth . password = hashed_password <EOL> else : <EOL> user . password = hashed_password <EOL> self . db_adapter . commit ( ) <EOL> def verify_password ( self , password , user ) : <EOL> """<STR_LIT>""" <EOL> verified = False <EOL> hashed_password = self . get_password ( user ) <EOL> try : <EOL> verified = passwords . verify_password ( self , password , hashed_password ) <EOL> except ValueError : <EOL> legacy_check = self . legacy_check_password_hash <EOL> if legacy_check : <EOL> verified = legacy_check ( hashed_password , password ) <EOL> if verified : <EOL> new_hash = self . hash_password ( password ) <EOL> self . update_password ( user , new_hash ) <EOL> return verified <EOL> def generate_token ( self , user_id ) : <EOL> return self . token_manager . generate_token ( user_id ) <EOL> def verify_token ( self , token , expiration_in_seconds ) : <EOL> return self . token_manager . verify_token ( token , expiration_in_seconds ) <EOL> def get_user_by_id ( self , user_id ) : <EOL> ObjectClass = self . db_adapter . UserAuthClass if self . db_adapter . UserAuthClass and self . db_adapter . UserProfileClass else self . db_adapter . UserClass <EOL> return self . db_adapter . get_object ( ObjectClass , user_id ) <EOL> def find_user_by_id ( self , user_id ) : <EOL> print ( '<STR_LIT>' ) <EOL> return self . get_user_by_id ( user_id ) <EOL> def get_user_email_by_id ( self , user_email_id ) : <EOL> return self . db_adapter . get_object ( self . db_adapter . UserEmailClass , user_email_id ) <EOL> def find_user_email_by_id ( self , user_email_id ) : <EOL> print ( '<STR_LIT>' ) <EOL> return self . get_user_email_by_id ( user_email_id ) <EOL> def find_user_by_username ( self , username ) : <EOL> user_auth = None <EOL> if self . db_adapter . UserAuthClass and hasattr ( self . db_adapter . UserAuthClass , '<STR_LIT:username>' ) : <EOL> user_auth = self . db_adapter . ifind_first_object ( self . db_adapter . UserAuthClass , username = username ) <EOL> if self . db_adapter . UserProfileClass : return user_auth <EOL> user = user_auth . user if user_auth else None <EOL> else : <EOL> user = self . db_adapter . ifind_first_object ( self . db_adapter . UserClass , username = username ) <EOL> return user <EOL> def find_user_by_email ( self , email ) : <EOL> user_email = None <EOL> user_auth = None <EOL> if self . db_adapter . UserEmailClass : <EOL> user_email = self . db_adapter . ifind_first_object ( self . db_adapter . UserEmailClass , email = email ) <EOL> user = user_email . user if user_email else None <EOL> else : <EOL> if self . db_adapter . UserAuthClass and hasattr ( self . db_adapter . UserAuthClass , '<STR_LIT:email>' ) : <EOL> user_auth = self . db_adapter . ifind_first_object ( self . db_adapter . UserAuthClass , email = email ) <EOL> if self . db_adapter . UserProfileClass : return ( user_auth , user_email ) <EOL> user = user_auth . user if user_auth else None <EOL> else : <EOL> user = self . db_adapter . ifind_first_object ( self . db_adapter . UserClass , email = email ) <EOL> return ( user , user_email ) <EOL> def email_is_available ( self , new_email ) : <EOL> """<STR_LIT>""" <EOL> user , user_email = self . find_user_by_email ( new_email ) <EOL> return ( user == None ) <EOL> def username_is_available ( self , new_username ) : <EOL> """<STR_LIT>""" <EOL> if _call_or_get ( current_user . is_authenticated ) : <EOL> current_username = current_user . user_auth . username if self . db_adapter . UserAuthClass and hasattr ( current_user , '<STR_LIT>' ) else current_user . username <EOL> if new_username == current_username : <EOL> return True <EOL> return self . find_user_by_username ( new_username ) == None <EOL> def send_reset_password_email ( self , email ) : <EOL> user , user_email = self . find_user_by_email ( email ) <EOL> if user : <EOL> token = self . generate_token ( int ( user . get_id ( ) ) ) <EOL> reset_password_link = url_for ( '<STR_LIT>' , token = token , _external = True ) <EOL> emails . send_forgot_password_email ( user , user_email , reset_password_link ) <EOL> if hasattr ( user , '<STR_LIT>' ) : <EOL> self . db_adapter . update_object ( user , reset_password_token = token ) <EOL> self . db_adapter . commit ( ) <EOL> signals . user_forgot_password . send ( current_app . _get_current_object ( ) , user = user ) <EOL> class UserMixin ( LoginUserMixin ) : <EOL> """<STR_LIT>""" <EOL> def is_active ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . active <EOL> else : <EOL> return self . is_enabled <EOL> def set_active ( self , active ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . active = active <EOL> else : <EOL> self . is_enabled = active <EOL> def has_role ( self , * specified_role_names ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> roles = self . roles <EOL> else : <EOL> if hasattr ( self , '<STR_LIT>' ) and hasattr ( self . user_profile , '<STR_LIT>' ) : <EOL> roles = self . user_profile . roles <EOL> else : <EOL> roles = None <EOL> if not roles : return False <EOL> user_role_names = [ role . name for role in roles ] <EOL> for role_name in specified_role_names : <EOL> if role_name in user_role_names : <EOL> return True <EOL> return False <EOL> def has_roles ( self , * requirements ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> roles = self . roles <EOL> else : <EOL> if hasattr ( self , '<STR_LIT>' ) and hasattr ( self . user_profile , '<STR_LIT>' ) : <EOL> roles = self . user_profile . roles <EOL> else : <EOL> roles = None <EOL> if not roles : return False <EOL> user_role_names = [ role . name for role in roles ] <EOL> for requirement in requirements : <EOL> if isinstance ( requirement , ( list , tuple ) ) : <EOL> tuple_of_role_names = requirement <EOL> authorized = False <EOL> for role_name in tuple_of_role_names : <EOL> if role_name in user_role_names : <EOL> authorized = True <EOL> break <EOL> if not authorized : <EOL> return False <EOL> else : <EOL> role_name = requirement <EOL> if not role_name in user_role_names : <EOL> return False <EOL> return True <EOL> def get_auth_token ( self ) : <EOL> token_manager = current_app . user_manager . token_manager <EOL> user_id = int ( self . get_id ( ) ) <EOL> token = token_manager . encrypt_id ( user_id ) <EOL> return token <EOL> def has_confirmed_email ( self ) : <EOL> db_adapter = current_app . user_manager . db_adapter <EOL> if db_adapter . UserEmailClass : <EOL> has_confirmed_email = False <EOL> user_emails = db_adapter . find_all_objects ( db_adapter . UserEmailClass , user_id = self . id ) <EOL> for user_email in user_emails : <EOL> if user_email . confirmed_at : <EOL> has_confirmed_email = True <EOL> break <EOL> else : <EOL> has_confirmed_email = True if self . confirmed_at else False <EOL> return has_confirmed_email </s>
