def send_password_link():
    data_request = request.get_json()
    schema = Schema({Required("email"): Email})
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    email = data_request.get('email')
    master_user = MasterUserDelegate()
    user = master_user.get_by_email(str(email))
    if user is None:
        response_data = ERROR.copy()
        response_data["message"] = "Invalid email address."
        return generic_error_response(response_data)
    first_name = user.first_name
    email_resp = change_password_verification(
        first_name, email, generate_confirmation_token(email))
    if email_resp.status_code == 200:
        response_data = SUCCESS.copy()
        response_data["message"] = "Link sent!! Please check your email."
        response = generic_success_response(response_data)
    else:
        response_data = ERROR.copy()
        response_data["email_response"] = email_resp.text
        response_data[
            "message"] = "Sorry!! We are unable to send email at this time."
        response = generic_error_response(response_data)
    return response
Example #2
0
def add_master_user():
    data_request = request.get_json()
    schema = Schema({
        Required("first_name"): validate_name,
        Required("last_name"): validate_name,
        Required("email"): validate_email,
        Required("password"): validate_password,
        Required("auth_type"): validate_auth_type,
        Required("mgn_user_type"): validate_user_type,
        Optional("profile_pic"): Url
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    first_name = data_request.get('first_name')
    last_name = data_request.get('last_name')
    email = data_request.get('email')
    password = data_request.get('password')
    auth_type_id = data_request.get('auth_type')
    mgn_user_type_id = data_request.get('mgn_user_type')
    try:
        profile_pic = data_request.get('profile_pic')
    except:
        profile_pic = None
    master_user = MasterUserDelegate()
    social_id = None
    user = master_user.get_by_email(email)
    if user is not None:
        response_data = ERROR.copy()
        response_data[
            "message"] = "This email is already in use! Please try Forgot Password"
        response = generic_error_response(response_data)
        return response
    uuid = master_user.register(first_name, last_name, email, password,
                                auth_type_id, mgn_user_type_id, profile_pic,
                                social_id)
    if uuid is not None:
        if auth_type_id == EMAIL:
            email_resp = account_verification(
                first_name, email, generate_confirmation_token(email))
            if email_resp.status_code == 200:
                response_data = SUCCESS.copy()
                response_data["auth_type"] = auth_type_id
                response_data[
                    "message"] = "Account is been created. Please check your email for verification."
                response = generic_success_response(response_data)
            else:
                response_data = ERROR.copy()
                response_data["email_response"] = email_resp.text
                response_data[
                    "message"] = "We are unable to verify email at this time."
                response_data["attribute"] = "email_verification"
                response_data["verification_link"] = "/email-verification"
                response = generic_error_response(response_data)
        else:
            response = INVALID_RESPONSE
    else:
        response = FAILURE_RESPONSE
    return response
Example #3
0
def get_master_user(master_user_id=None):
    try:
        master_user = MasterUserDelegate(master_user_id)
        data = master_user.get()
    except:
        response = FAILURE_RESPONSE
        return response
    if data is not None:
        response_data = SUCCESS.copy()
        response_data["data"] = data
        response = generic_success_response(response_data)
    else:
        response_data = ERROR.copy()
        response_data["message"] = INVALID_ID
        response = generic_success_response(response_data)
    return response
Example #4
0
def update_master_user_profile_pic(master_user_id=None):
    data_request = request.get_json()
    schema = Schema({Required("profile_pic"): Url})
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    profile_pic = data_request.get('profile_pic')
    master_user = MasterUserDelegate(master_user_id)
    if master_user.update_profile_pic(profile_pic):
        response_data = SUCCESS.copy()
        response_data["message"] = UPDATED
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
Example #5
0
def search_master_user(q=None):
    try:
        master_user = MasterUserDelegate()
        data = master_user.search(q)
    except:
        response = FAILURE_RESPONSE
        return response
    if data is not None:
        response_data = SUCCESS.copy()
        if data != EMPTY_LIST:
            response_data["data"] = data
        else:
            response_data["message"] = NO_DATA
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
def oauth_callback(provider=None, mgn_user_type_id=None, auth_type_id=None):
    oauth = OAuthSignIn.get_provider(provider)
    social_id, email, first_name, last_name, fb_profile_pic = oauth.callback(
        mgn_user_type_id, auth_type_id)
    if social_id is None:
        response_data = ERROR.copy()
        response_data["message"] = "Authentication Failed."
        response = generic_success_response(response_data)
        return response
    master_user = MasterUserDelegate()
    user = master_user.get_by_email(str(email))
    try:
        profile_pic = fb_profile_pic["data"]["url"]
    except:
        profile_pic = None
    if user is None:
        uuid = master_user.register(first_name, last_name, email,
                                    generate_password(), auth_type_id,
                                    mgn_user_type_id, profile_pic, social_id)
        if uuid is not None:
            auth_type = auth_type_id
            token = generate_confirmation_token(email)
            uid = generate_confirmation_token(uuid)
            response = make_response(
                redirect(
                    url_for('auth.authorize',
                            auth_type=auth_type,
                            token=token,
                            uid=uid)))
        else:
            response_data = ERROR.copy()
            response_data["message"] = "Unable to create account."
            response = generic_error_response(response_data)
    else:
        auth_type = auth_type_id
        token = generate_confirmation_token(user.email)
        uid = generate_confirmation_token(user.master_user_id)
        response = make_response(
            redirect(
                url_for('auth.authorize',
                        auth_type=auth_type,
                        token=token,
                        uid=uid)))
    return response
Example #7
0
def update_master_user_is_blocked(master_user_id=None):
    data_request = request.get_json()
    schema = Schema({Required("is_blocked"): All(int, Range(min=0, max=1))})
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    is_blocked = data_request.get('is_blocked')
    master_user = MasterUserDelegate(master_user_id)
    if is_blocked != '':
        if master_user.update_user_is_active(is_blocked):
            response_data = SUCCESS.copy()
            response_data["message"] = UPDATED
            response = generic_success_response(response_data)
        else:
            response = ERROR_RESPONSE
    else:
        response = ERROR_RESPONSE
    return response
Example #8
0
def update_master_user_password(master_user_id=None):
    data_request = request.get_json()
    schema = Schema({Required("password"): validate_password})
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    password = data_request.get('password')
    master_user = MasterUserDelegate(master_user_id)
    if password != '':
        if master_user.update_user_password(password):
            response_data = SUCCESS.copy()
            response_data["message"] = UPDATED
            response = generic_success_response(response_data)
        else:
            response = ERROR_RESPONSE
    else:
        response = ERROR_RESPONSE
    return response
def login(auth_type=None, token=None, uid=None):
    data_request = request.get_json()
    schema = Schema({
        Required("email"): Email,
        Required("password"): validate_password
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    email = data_request.get('email')
    password = data_request.get('password')
    master_user = MasterUserDelegate()
    user = master_user.get_by_email(email)
    if user is not None and check_password_hash(user.password, password):
        if user.is_active == TRUE:
            response_data = SUCCESS.copy()
            user_json = user.serialize
            user_json["token"] = generate_confirmation_token(email)
            response_data["data"] = json.dumps(user_json)
            response_data["message"] = "Login Successful"
            response = generic_success_response(response_data)
        elif user.is_deleted == TRUE:
            response_data = ERROR.copy()
            response_data["message"] = "Account is been deleted"
            response = generic_error_response(response_data)
        elif user.is_blocked == TRUE:
            response_data = ERROR.copy()
            response_data["message"] = "Account is been blocked"
            response = generic_error_response(response_data)
        elif user.is_email_confirmed == FALSE:
            response_data = ERROR.copy()
            response_data[
                "message"] = "Please verify your email address. <a href='/email-verification'>Click Here</a> to resend verification link."
            response = generic_error_response(response_data)
        return response
    response_data = ERROR.copy()
    response_data["message"] = "Username or Password is wrong"
    response = generic_error_response(response_data)
    return response
Example #10
0
def update_master_user_name(master_user_id=None):
    data_request = request.get_json()
    schema = Schema({
        Required("first_name"): validate_name,
        Required("last_name"): validate_name
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    first_name = data_request.get('first_name')
    last_name = data_request.get('last_name')
    master_user = MasterUserDelegate(master_user_id)
    if first_name != '' and last_name != '':
        if master_user.update_user_name(first_name, last_name):
            response_data = SUCCESS.copy()
            response_data["message"] = UPDATED
            response = generic_success_response(response_data)
        else:
            response = ERROR_RESPONSE
    else:
        response = ERROR_RESPONSE
    return response
def email_verification():
    data_request = request.get_json()
    schema = Schema({Required("email"): Email, Required("token"): str})
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    email = data_request.get('email')
    token = data_request.get('token')
    if email == confirm_token(token, SHORT_TOKEN_VALIDITY):
        master_user = MasterUserDelegate()
        user = master_user.get_by_email(str(email))
        if user is not None:
            response_data = SUCCESS.copy()
            if user.is_active == ACTIVE:
                response_data[
                    "message"] = "This email address already confirmed! Please <a href='/login'>login</a> to continue."
            else:
                master_user = MasterUserDelegate(user.master_user_id)
                master_user.update_user_is_active(ACTIVE)
                response_data[
                    "message"] = "Email address confirmed! Please <a href='/login'>login</a> to continue."
                notification = UserNotificationsDelegate(user.id)
                notification.add('welcome',
                                 'Welcome to MGN! ' + user.first_name, '',
                                 str(datetime.datetime.utcnow()))
            response = generic_success_response(response_data)
        else:
            response_data = ERROR.copy()
            response_data["message"] = "Invalid email address."
            response = generic_error_response(response_data)
    else:
        response_data = ERROR.copy()
        response_data[
            "message"] = "Invalid token or token expired! <a href='/email-verification'>Click Here</a> to resend email."
        response = generic_error_response(response_data)
    return response
def forgot_password(master_user_id=None):
    data_request = request.get_json()
    schema = Schema({
        Required("password"): validate_password,
        Required("email"): Email,
        Required("token"): str
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    password = data_request.get('password')
    email = data_request.get('email')
    token = data_request.get('token')
    if email != confirm_token(token, SHORT_TOKEN_VALIDITY):
        response_data = ERROR.copy()
        response_data[
            "message"] = "Invalid token or token expired! Please use <a href='/forgot-password'>forgot password</a> to resend email."
        return generic_error_response(response_data)
    master_user = MasterUserDelegate()
    user = master_user.get_by_email(str(email))
    if user is None:
        response_data = ERROR.copy()
        response_data["message"] = "Invalid email address."
        return generic_error_response(response_data)
    master_user = MasterUserDelegate(user.id)
    if password != '':
        if master_user.update_user_password(password):
            response_data = SUCCESS.copy()
            response_data["message"] = UPDATED
            response = generic_success_response(response_data)
        else:
            response = ERROR_RESPONSE
    else:
        response = ERROR_RESPONSE
    return response
def validate_user_by_id(master_user_id=None):
    master_user = MasterUserDelegate(master_user_id).get()
    if master_user is None:
        raise Invalid("Invalid user id")
    return master_user_id