-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathInvitationsController.cs
More file actions
226 lines (181 loc) · 7.39 KB
/
InvitationsController.cs
File metadata and controls
226 lines (181 loc) · 7.39 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.Linq;
using QueryTree.Models;
using QueryTree.ViewModels;
using QueryTree.Managers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace QueryTree.Controllers
{
[Authorize]
public class InvitationsController : Controller
{
private UserManager<ApplicationUser> _userManager;
private ApplicationDbContext db;
public InvitationsController(
ApplicationDbContext dbContext,
UserManager<ApplicationUser> userManager)
{
db = dbContext;
_userManager = userManager;
}
private ApplicationUser _currentUser;
private ApplicationUser CurrentUser
{
get
{
if (_currentUser == null)
{
string userId = _userManager.GetUserId(User);
_currentUser = db.ApplicationUsers.FirstOrDefault(u => u.Id == userId);
}
return _currentUser;
}
}
public ActionResult Index()
{
if (CurrentUser == null)
{
return NotFound("Could not find user");
}
var invites = db
.OrganisationInvites
.Include(c => c.CreatedBy)
.Where(uc => uc.AcceptedOn == null && uc.RejectedOn == null)
.ToList()
.Where(uc => string.Compare(uc.InviteEmail, CurrentUser.Email, StringComparison.OrdinalIgnoreCase) == 0);
if (invites.Any() == false)
{
// No invites to look at, redirect to home
return RedirectToAction("Index", "Home");
}
List<InvitationViewModel> viewModels = new List<InvitationViewModel>();
foreach (var orgGrp in invites.GroupBy(c => c.OrganisationId))
{
InvitationViewModel viewModel = new InvitationViewModel
{
OrganisationInviteId = orgGrp.First().OrganisationInviteId
};
Organisation organisation = db.Organisations.First(ba => ba.OrganisationId == orgGrp.Key);
viewModel.OrganisationId = organisation.OrganisationId;
viewModel.Invitees = orgGrp
.Select(g => g.CreatedBy.UserName)
.OrderBy(_ => _)
.Distinct()
.ToList();
viewModel.OrganisationName = organisation.OrganisationName;
viewModels.Add(viewModel);
}
if (viewModels.Any())
{
List<string> databasesMerged = new List<string>();
List<string> databasesLost = new List<string>();
List<string> databases = db.DatabaseConnections
.Where(d => d.OrganisationId == CurrentUser.OrganisationId)
.ToList()
.Select(d => d.Name)
.OrderBy(d => d)
.ToList();
// are the the sole owner of the organisation?
// if so we should transfer databases to the new organisation
bool soleOwner = db.ApplicationUsers.Any(u => u.OrganisationId == CurrentUser.OrganisationId && CurrentUser.Id != u.Id) == false;
if (soleOwner)
{
databasesMerged = databases;
}
else
{
databasesLost = databases;
}
foreach(var viewModel in viewModels)
{
viewModel.DatabasesMerged = databasesMerged;
viewModel.DatabasesLost = databasesLost;
}
}
return View(viewModels);
}
public ActionResult Accept(int id)
{
if (CurrentUser == null)
{
return NotFound("Could not find user");
}
var invite = db.OrganisationInvites.FirstOrDefault(uc => uc.InviteEmail.ToLower() == CurrentUser.Email.ToLower() && uc.AcceptedOn == null && uc.RejectedOn == null && uc.OrganisationInviteId == id);
if (invite == null)
{
return NotFound("Invite not found");
}
var organisation = db.Organisations.FirstOrDefault(ba => ba.OrganisationId == invite.OrganisationId);
if (organisation == null)
{
return NotFound("Organisation not found");
}
List<DatabaseConnection> leave = new List<DatabaseConnection>();
List<DatabaseConnection> migrate = new List<DatabaseConnection>();
// Migrate or cut connections with databases as necessary.
List<DatabaseConnection> databases = db.DatabaseConnections
.Where(d => d.OrganisationId == CurrentUser.OrganisationId)
.ToList()
.ToList();
// are the the sole owner of the organisation?
// if so we should transfer databases to the new organisation
bool soleOwner = db.ApplicationUsers.Any(u => u.OrganisationId == CurrentUser.OrganisationId && CurrentUser.Id != u.Id) == false;
if (soleOwner)
{
migrate = databases;
}
else
{
leave = databases;
}
foreach(var database in leave)
{
// there shouldn't be any of these, but do it just in case
db.UserDatabaseConnections.RemoveWhere(uc => uc.DatabaseConnectionID == database.DatabaseConnectionID && uc.ApplicationUserID == CurrentUser.Id);
}
foreach (var database in migrate)
{
// there shouldn't be any of these, but do it just in case
db.UserDatabaseConnections.RemoveWhere(uc => uc.DatabaseConnectionID == database.DatabaseConnectionID && uc.ApplicationUserID == CurrentUser.Id);
database.Organisation = organisation;
}
CurrentUser.OrganisationId = organisation.OrganisationId;
invite.AcceptedOn = DateTime.Now;
// reject other invitations to other organisations
var invitesToReject = db.OrganisationInvites.Where(uc => uc.InviteEmail.ToLower() == CurrentUser.Email.ToLower() && uc.OrganisationInviteId != invite.OrganisationInviteId);
foreach (var inviteToReject in invitesToReject)
{
inviteToReject.RejectedOn = DateTime.Now;
}
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
public ActionResult Reject(int id)
{
if (CurrentUser == null)
{
return NotFound("Could not find user");
}
var invite = db.OrganisationInvites.FirstOrDefault(uc => uc.InviteEmail.ToLower() == CurrentUser.Email.ToLower() && uc.AcceptedOn == null && uc.RejectedOn == null && uc.OrganisationInviteId == id);
if (invite == null)
{
return NotFound("Invite not found");
}
invite.RejectedOn = DateTime.Now;
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}