-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStudentRegistrationFunction.cs
141 lines (124 loc) · 5.27 KB
/
StudentRegistrationFunction.cs
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
133
134
135
136
137
138
139
140
141
using System;
using System.Linq;
using System.Threading.Tasks;
using AzureAutomaticGradingEngineFunctionApp.Dao;
using AzureAutomaticGradingEngineFunctionApp.Helper;
using AzureAutomaticGradingEngineFunctionApp.Model;
using AzureAutomaticGradingEngineFunctionApp.Poco;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
namespace AzureAutomaticGradingEngineFunctionApp;
public static class StudentRegistrationFunction
{
[FunctionName(nameof(StudentRegistrationFunction))]
// ReSharper disable once UnusedMember.Global
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest req,
ILogger log, ExecutionContext context)
{
log.LogInformation($"Start {nameof(StudentRegistrationFunction)}");
if (req.Method == "GET")
{
if (!req.Query.ContainsKey("email") || !req.Query.ContainsKey("lab"))
{
return GetContentResult("Invalid Url and it should contain lab and email!");
}
string lab = req.Query["lab"];
string email = req.Query["email"];
var form = $@"
<form id='form' method='post'>
<input type='hidden' id='lab' name='lab' value='{lab}'>
<label for='email'>Email:</label><br>
<input type='email' id='email' name='email' size='50' value='{email}' required><br>
Azure Credentials<br/>
<textarea name='credentials' required rows='15' cols='100'></textarea>
<br/>
<button type='submit'>Register</button>
</form>
";
return GetContentResult(form);
}
if (req.Method == "POST")
{
log.LogInformation("POST Request");
string lab = req.Form["lab"];
string email = req.Form["email"];
string credentialJsonString = req.Form["credentials"];
log.LogInformation("Student Register: " + email + " Lab:" + lab);
if (string.IsNullOrWhiteSpace(email) ||
string.IsNullOrWhiteSpace(credentialJsonString))
return GetContentResult("Missing Data and Registration Failed!");
email = email.Trim().ToLower();
var config = new Config(context);
var subscriptionDao = new SubscriptionDao(config, log);
var labCredentialDao = new LabCredentialDao(config, log);
var credential = AppPrincipal.FromJson(credentialJsonString, log);
var azureCredentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(credential.appId, credential.password, credential.tenant, AzureEnvironment.AzureGlobalCloud);
var authenticated = Microsoft.Azure.Management.Fluent.Azure.Configure().Authenticate(azureCredentials);
string subscriptionId = authenticated.Subscriptions.List().First<ISubscription>().SubscriptionId;
if (string.IsNullOrWhiteSpace(lab))
{
lab = email.ToLower().Trim();
}
var subscription = new Subscription
{
PartitionKey = lab,
RowKey = subscriptionId,
Email = email
};
if (!subscriptionDao.IsNew(subscription))
return GetContentResult("You can only have one Subscription Id for one lab!");
var labCredential = new LabCredential
{
PartitionKey = lab,
RowKey = email.ToLower().Trim(),
Timestamp = DateTime.Now,
AppId = credential.appId,
DisplayName = credential.displayName,
Password = credential.password,
Tenant = credential.tenant,
SubscriptionId = subscriptionId,
Email = email.ToLower().Trim()
};
if (!await Helper.Azure.IsValidSubscriptionContributorRole(labCredential, subscriptionId))
return GetContentResult(
"Your services principal is not in the contributor role for your subscription! Check your subscription ID and Services principal!");
subscriptionDao.Add(subscription);
labCredentialDao.Upsert(labCredential);
return GetContentResult("Your credentials has been Registered!");
}
return new OkObjectResult("ok");
}
private static ContentResult GetContentResult(string content)
{
return new ContentResult
{
Content = GetHtml(content),
ContentType = "text/html",
StatusCode = 200
};
}
private static string GetHtml(string content)
{
return $@"
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta charset='utf-8' />
<title>Azure Automatic Grading Engine</title>
</head>
<body>
<h1>Azure Automatic Grading Engine</h1>
{content}
<footer>
<p>Developed by <a href='https://www.vtc.edu.hk/admission/en/programme/it114115-higher-diploma-in-cloud-and-data-centre-administration/'> Higher Diploma in Cloud and Data Centre Administration Team.</a></p>
</footer>
</body>
</html>";
}
}