aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/utils.py
blob: 9a9c26065795c5f8073a5d34c8db01b37d2b465b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# These functions are called from views and perform the actual request to LibLaaS

import json
from django.http.response import JsonResponse, HttpResponse
import requests
import os

from dashboard.forms import *
liblaas_base_url = os.environ.get("LIBLAAS_BASE_URL")

# IPA Stuff
def ipa_query_user(ipa_username):
    url = liblaas_base_url + "user/" + ipa_username
    print("Getting ipa user for", ipa_username, url)
    try:
        response = requests.get(url)
        data = response.json()
        print("ipa user is", data)
        return data # Expects a dict
    except Exception as e:
        print(e)
        return None
    
# Queries for an IPA user using dashboard username
# Returns a result
def get_ipa_migration_form(user, profile):
    # ipa_user = ipa_query_user(str(dashboard_user))
    # if (ipa_user and ipa_user.mail is )
    # pass
    dashboard_username = str(user)
    dashboard_email = profile.email_addr
    first_name = user.first_name
    last_name = user.last_name

    ipa_user = ipa_query_user(dashboard_username)
    print("Attempting auto migration with", dashboard_username, dashboard_email, ipa_user)
    if (ipa_user):
        if (dashboard_email == ipa_user["mail"]):
        # User is found and email match
            print("User is found and email match")
            return {
                "form": ReadOnlyIPAAccountForm(initial={'ipa_username': ipa_user['uid'],'first_name': ipa_user["givenname"], 'last_name': ipa_user["sn"], 'email': ipa_user["mail"], 'company': ipa_user["ou"]}),
                "message": "We have located the following IPA account matching your username and email. Please confirm to link your account. You may change these details at any time.",
                "action": "api/ipa/confirm",
                "button": "Link"
            }

        else:
        # User is found and emails don't match
            print("User is found and emails don't match")
            return {
                "form": ConflictIPAAcountForm(initial={'first_name': first_name, 'last_name': last_name, 'email': dashboard_email}),
                "message": "Our records indicate that you do not currently have an account in our IPA system, or your emails do not match. Please enter the following details to enroll your account.",
                "action": "/",
                "button": "Submit"
            }
    else:
    # User is not found
        print("User is not found")
        return {
            "form": NewIPAAccountForm(initial={'first_name': first_name, 'last_name': last_name, 'email': dashboard_email}),
            "message": "Our records indicate that you do not currently have an account in our IPA system, or your usernames do not match. Please enter the following details to enroll your account.",
            "action": "api/ipa/create",
            "button": "Submit"
        }

# Removes leading and trailing white space from a list of ssh keys and returns the cleaned list
def clean_ssh_keys(ssh_key_list):
    cleaned = []
    for key in ssh_key_list:
        cleaned.append(key.strip())
    return cleaned

# Take a list of strings, sends it to liblaas, replacing the IPA keys with the new keys
def ipa_set_ssh(user_profile, ssh_key_list):
    url = liblaas_base_url + "user/" + user_profile.ipa_username + "/ssh"
    ssh_key_list = clean_ssh_keys(ssh_key_list)
    try:
        requests.post(url, data=json.dumps(ssh_key_list), headers={'Content-Type': 'application/json'})
        return HttpResponse(status=200)
    except Exception as e:
        print(e)
        return HttpResponse(status=500)
    
def ipa_set_company(user_profile, company_name):
    url = liblaas_base_url + "user/" + user_profile.ipa_username + "/company"
    print("Setting company with URL", url)
    try:
        requests.post(url, data=json.dumps(company_name), headers={'Content-Type': 'application/json'})
        return HttpResponse(status=200)
    except Exception as e:
        print(e)
        return HttpResponse(status=500)

def get_booking_prereqs_validator(user_profile):
    ipa_user = None
    if (user_profile.ipa_username != None and user_profile.ipa_username != ""):
        ipa_user = ipa_query_user(user_profile.ipa_username)

    if ipa_user == None:
        print("No user")
        return {
            "form": None,
            "exists": "false",
            "action": "no user",
            "result": 0
        }

    if (not "ou" in ipa_user) or (ipa_user["ou"] == ""):
        print("Missing company")
        return {
            "form": SetCompanyForm(),
            "exists": "true",
            "action": "/api/ipa/workflow-company",
            "result": 1
        }
    
    if (not "ipasshpubkey" in ipa_user) or (ipa_user["ipasshpubkey"] == []):
        print("Missing SSH key")
        return {
            "form": SetSSHForm(),
            "exists": "true",
            "action": "/api/ipa/workflow-ssh",
            "result": 2,
        }

    return {
        "form": None,
        "exists": "false",
        "action": "",
        "result": -1
    }