|
5 | 5 | from django.urls import reverse
|
6 | 6 | from django.views.generic.edit import FormView
|
7 | 7 | from django.core.urlresolvers import reverse_lazy
|
| 8 | +from django.conf import settings |
| 9 | +from django.shortcuts import render |
8 | 10 |
|
9 | 11 | from forms import UnifiRegistrationForm
|
10 | 12 | from models import UnifiUser
|
11 | 13 |
|
| 14 | +def index_view(request, *args, **kwargs): |
| 15 | + _mac = request.GET.get('id', '') |
| 16 | + _ap = request.GET.get('ap', '') |
| 17 | + _url = request.GET.get('url', '') |
| 18 | + _t = request.GET.get('t', '') |
| 19 | + |
| 20 | + print "mac:", _mac |
| 21 | + print "ap:", _ap |
| 22 | + print "t:", _t |
| 23 | + print "url:", _url |
| 24 | + |
| 25 | + context = { |
| 26 | + } |
| 27 | + return render(request, 'index.html', context) |
| 28 | + |
| 29 | + |
| 30 | +def send_authorization(id, ap, minutes, url): |
| 31 | + unifiServer = settings.UNIFI_SERVER; |
| 32 | + unifiUser = settings.UNIFI_USER |
| 33 | + unifiPass = settings.UNIFI_USER |
| 34 | + |
| 35 | + # |
| 36 | + # 1) Login to Unifi Server |
| 37 | + # |
| 38 | + ''' |
| 39 | + //Start Curl for login |
| 40 | + $ch = curl_init(); |
| 41 | + // We are posting data |
| 42 | + curl_setopt($ch, CURLOPT_POST, TRUE); |
| 43 | + // Set up cookies |
| 44 | + $cookie_file = "/tmp/unifi_cookie"; |
| 45 | + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); |
| 46 | + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); |
| 47 | + // Allow Self Signed Certs |
| 48 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 49 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
| 50 | + // Force SSL3 only |
| 51 | + curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
| 52 | + // Login to the UniFi controller |
| 53 | + curl_setopt($ch, CURLOPT_URL, "$unifiServer/login"); |
| 54 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "login=login&username=$unifiUser&password=$unifiPass"); |
| 55 | + curl_exec ($ch); |
| 56 | + curl_close ($ch); |
| 57 | + ''' |
| 58 | + |
| 59 | + # |
| 60 | + # 2) Send user to authorize and the time allowed |
| 61 | + # |
| 62 | + data = { |
| 63 | + 'cmd': 'authorize-guest', |
| 64 | + 'mac': id, |
| 65 | + 'ap': ap, |
| 66 | + 'minutes': minutes |
| 67 | + } |
| 68 | + print "data->", data |
| 69 | + |
| 70 | + ''' |
| 71 | + $ch = curl_init(); |
| 72 | + // We are posting data |
| 73 | + curl_setopt($ch, CURLOPT_POST, TRUE); |
| 74 | + // Set up cookies |
| 75 | + $cookie_file = "/tmp/unifi_cookie"; |
| 76 | + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); |
| 77 | + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); |
| 78 | + // Allow Self Signed Certs |
| 79 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 80 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
| 81 | + // Force SSL3 only |
| 82 | + curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
| 83 | + // Make the API Call |
| 84 | + curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr'); |
| 85 | + curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data); |
| 86 | + curl_exec ($ch); |
| 87 | + curl_close ($ch); |
| 88 | + ''' |
| 89 | + |
| 90 | + # |
| 91 | + # 3) Logout of the connection |
| 92 | + # |
| 93 | + ''' |
| 94 | + $ch = curl_init(); |
| 95 | + // We are posting data |
| 96 | + curl_setopt($ch, CURLOPT_POST, TRUE); |
| 97 | + // Set up cookies |
| 98 | + $cookie_file = "/tmp/unifi_cookie"; |
| 99 | + curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); |
| 100 | + curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); |
| 101 | + // Allow Self Signed Certs |
| 102 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 103 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
| 104 | + // Force SSL3 only |
| 105 | + curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
| 106 | + // Make the API Call |
| 107 | + curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout'); |
| 108 | + curl_exec ($ch); |
| 109 | + curl_close ($ch); |
| 110 | + echo "Login successful, I should redirect to: ".$url; // $_SESSION['url']; |
| 111 | + sleep(8); // Small sleep to allow controller time to authorize |
| 112 | + header('Location: '.$url); // $_SESSION['url']); |
| 113 | + ''' |
12 | 114 |
|
13 | 115 | class UnifiUserRegistration(FormView):
|
14 | 116 | template_name = 'registration.html'
|
|
0 commit comments