summaryrefslogtreecommitdiffstats
path: root/js/gsec.js
blob: 55d9dab46d266f9e00037395c92144da61ef0e15 (plain) (blame)
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
// tab = 2 || any spaces; use tabs
const GSE_URL = "https://zgimsis.gimb.tk/gse/";
class gsec {
	constructor() {
	}
	postback(getUrl, params, formId = null) {
		return new Promise( (resolve, reject) => {
			$.ajax({
				crossDomain: true,
				url: getUrl,
				cache: false,
				type: "GET",
				dataType: "html",
				success: (getData) => {
					var parsed = document.createElement("html");
					parsed.innerHTML = getData;
					if(formId == null) {
						var form = parsed.getElementsByTagName("form")[0];
					} else {
						var form = parsed.getElementById(formId);
					}
					var otherParams = $(form).serializeArray();
					for(const input of otherParams) {
						if(!(input.name in params)) {
							params[input.name] = input.value; // so we don't overwrite existing values
						}
					}
					var action = new URL($(form).attr("action"), GSE_URL); // absolute == relative + base
					$.ajax({
						crossDomain: true,
						url: action,
						cache: false,
						type: "POST",
						data: params,
						dataType: "text",
						success: (postData) => {
							resolve(postData);
						}
					});
				}
			});
		});
	}
	login(usernameToLogin, passwordToLogin) {
		return new Promise((resolve, reject) => {
		var dataToSend = {"edtGSEUserId": usernameToLogin, "edtGSEUserPassword": passwordToLogin, "btnLogin": "Prijava"};
			this.postback(GSE_URL+"Logon.aspx", dataToSend).then( (response) => {
				var parsed = document.createElement("html");
				parsed.innerHTML = response;
				var simpleResponse = parsed.getElementById("lblMsg");
					if( simpleResponse = "Napaka pri prijavi.") {
						reject(new Error(false));
					} else {
						resolve(true);
					}
			});
		});
	}
}