-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupDetails.cshtml.cs
144 lines (119 loc) · 5.28 KB
/
GroupDetails.cshtml.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
142
143
144
using hack_together_groups_manager.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Graph;
using System.Diagnostics.Metrics;
namespace hack_together_groups_manager.Pages
{
public class GroupDetailsModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly GraphServiceClient _graphServiceClient;
public Models.M365Group M365GroupDetails { get; set; }
[BindProperty]
public string? GroupId { get; set; }
[BindProperty]
public string? MemberToAdd { get; set; }
[BindProperty]
public string? OwnerToAdd { get; set; }
public GroupDetailsModel(ILogger<IndexModel> logger, GraphServiceClient graphServiceClient)
{
_logger = logger;
_graphServiceClient = graphServiceClient;
}
public async Task OnGetAsync()
{
GroupId = HttpContext.Request.Query["groupId"];
M365Group groupInfo = await GetGroupInfo(GroupId);
M365GroupDetails = groupInfo;
}
private async Task<M365Group> GetGroupInfo(string GroupId)
{
var groupInfo = new Models.M365Group();
if (GroupId != null)
{
var resultGroupInfo = await this._graphServiceClient.Groups[GroupId].Request().GetAsync();
if (resultGroupInfo != null)
{
var group = resultGroupInfo as Microsoft.Graph.Group;
groupInfo.Id = group.Id;
groupInfo.Description = group.Description;
groupInfo.DisplayName = group.DisplayName;
groupInfo.Visibility = group.Visibility;
}
var resultMembersInfo = await this._graphServiceClient.Groups[GroupId].Members.Request().GetAsync();
if (resultMembersInfo != null)
{
groupInfo.Members = new List<string>();
foreach (var member in resultMembersInfo)
{
groupInfo.Members.Add((member as Microsoft.Graph.User).UserPrincipalName);
}
}
var me = await this._graphServiceClient.Me.Request().GetAsync();
var resultOwnersInfo = await this._graphServiceClient.Groups[GroupId].Owners.Request().GetAsync();
if (resultOwnersInfo != null)
{
groupInfo.Owners = new List<string>();
foreach (var owner in resultOwnersInfo)
{
var upn = (owner as Microsoft.Graph.User).UserPrincipalName;
if (me.UserPrincipalName == upn)
{
groupInfo.UserRole = "Owner";
}
groupInfo.Owners.Add(upn);
}
}
}
return groupInfo;
}
public async Task<IActionResult> OnPostDelete()
{
var groupIdValue = Request.Form["groupId"];
await this._graphServiceClient.Groups[groupIdValue.ToString()].Request().DeleteAsync();
return RedirectToPage("Index");
}
public async Task<IActionResult> OnPostLeave()
{
var groupIdValue = Request.Form["groupId"];
var me = await this._graphServiceClient.Me.Request().GetAsync();
M365Group groupInfo = await GetGroupInfo(groupIdValue);
if (groupInfo.UserRole == "Owner")
{
await this._graphServiceClient.Groups[groupIdValue.ToString()].Owners[me.Id].Reference.Request().DeleteAsync();
}
await this._graphServiceClient.Groups[groupIdValue.ToString()].Members[me.Id].Reference.Request().DeleteAsync();
return RedirectToPage("Index");
}
public async Task<IActionResult> OnPostAddOwner()
{
var ownerQuery = await this._graphServiceClient.Users.Request().Filter($"userPrincipalName eq '{OwnerToAdd}'").GetAsync();
var owner = ownerQuery.FirstOrDefault();
if (owner != null)
{
var groupIdValue = Request.Form["groupId"];
await this._graphServiceClient.Groups[groupIdValue].Owners.References.Request().AddAsync(owner);
M365Group groupInfo = await GetGroupInfo(groupIdValue);
M365GroupDetails = groupInfo;
OwnerToAdd = string.Empty;
}
return Page();
}
public async Task<IActionResult> OnPostAddMember()
{
var memberQuery = await this._graphServiceClient.Users.Request().Filter($"userPrincipalName eq '{MemberToAdd}'").GetAsync();
var member = memberQuery.FirstOrDefault();
if (member != null)
{
var groupIdValue = Request.Form["groupId"];
await this._graphServiceClient.Groups[groupIdValue].Members.References.Request().AddAsync(member);
M365Group groupInfo = await GetGroupInfo(groupIdValue);
M365GroupDetails = groupInfo;
MemberToAdd = string.Empty;
}
return Page();
}
}
}