1 | using HeuristicLab.Clients.Access.Administration;
|
---|
2 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
3 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels.User;
|
---|
4 | using Microsoft.AspNet.Hosting;
|
---|
5 | using Microsoft.AspNet.Http;
|
---|
6 | using Microsoft.AspNet.Mvc;
|
---|
7 | using System;
|
---|
8 | using System.Collections.Generic;
|
---|
9 | using System.Linq;
|
---|
10 | using System.ServiceModel.Security;
|
---|
11 | using System.Threading.Tasks;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
14 | {
|
---|
15 | public class UserController : Controller
|
---|
16 | {
|
---|
17 | private WebLoginService weblog;
|
---|
18 | private HiveServiceLocatorWeb serviceLocator;
|
---|
19 | private AccessAdministrationClient accessClient;
|
---|
20 | private Guid userId;
|
---|
21 |
|
---|
22 | private IHostingEnvironment _environment;
|
---|
23 |
|
---|
24 | public UserController(IHostingEnvironment env)
|
---|
25 | {
|
---|
26 | weblog = WebLoginService.Instance;
|
---|
27 | _environment = env;
|
---|
28 | }
|
---|
29 | private bool init()
|
---|
30 | {
|
---|
31 | var u = HttpContext.Session.GetString("UserId");
|
---|
32 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
33 | {
|
---|
34 | return false;
|
---|
35 | }
|
---|
36 | else
|
---|
37 | {
|
---|
38 | userId = Guid.Parse(u);
|
---|
39 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
40 | accessClient = weblog.getAccessAdminClient(userId);
|
---|
41 | return serviceLocator.CheckLogin();
|
---|
42 |
|
---|
43 | }
|
---|
44 | }
|
---|
45 | #region Users
|
---|
46 |
|
---|
47 | public IActionResult Index()
|
---|
48 | {
|
---|
49 | if (init())
|
---|
50 | {
|
---|
51 | ViewBag.Title = "Users";
|
---|
52 | UserViewModel vm = new UserViewModel(accessClient).refreshAll() ;
|
---|
53 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
54 | return View("Index", vm);
|
---|
55 | }
|
---|
56 | else
|
---|
57 | {
|
---|
58 | return RedirectToAction("Index", "Home");
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public IActionResult SelectUser(string id)
|
---|
63 | {
|
---|
64 | ;
|
---|
65 | if (init())
|
---|
66 | {
|
---|
67 | Guid curr = Guid.Parse(id);
|
---|
68 |
|
---|
69 | UserViewModel vm = new UserViewModel(accessClient).refreshAll();
|
---|
70 | if (curr == Guid.Empty)
|
---|
71 | {
|
---|
72 | ViewBag.Title = "Add User";
|
---|
73 | }
|
---|
74 | else
|
---|
75 | {
|
---|
76 | vm.SelectedUser = vm.getUserById(curr);
|
---|
77 | vm.SelectedUserSubscriptions = accessClient.getSubscribedGroups(curr);
|
---|
78 | vm.SelectedUserRoles = accessClient.getRoles(vm.SelectedUser);
|
---|
79 | ViewBag.title = vm.SelectedUser.UserName;
|
---|
80 | }
|
---|
81 | ViewBag.Title += " - Users";
|
---|
82 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
83 | return View("Index", vm);
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | return RedirectToAction("Index", "Home");
|
---|
88 | }
|
---|
89 | }
|
---|
90 | [HttpPost]
|
---|
91 | public IActionResult saveUser(string inpusername, string inpfullname, string inpemail, string u, string[] rolestoadd, string[] groupstoadd)
|
---|
92 | {
|
---|
93 |
|
---|
94 | if (init())
|
---|
95 | {
|
---|
96 | var uid = Guid.Parse(u);
|
---|
97 | Access.User user;
|
---|
98 | if (uid == Guid.Empty)
|
---|
99 | user = new Access.User();
|
---|
100 | else
|
---|
101 | user = accessClient.Users.Find(x => x.Id == uid);
|
---|
102 |
|
---|
103 |
|
---|
104 | user.FullName = inpfullname;
|
---|
105 | user.Email = inpemail;
|
---|
106 |
|
---|
107 |
|
---|
108 | if (uid == Guid.Empty)
|
---|
109 | {
|
---|
110 | user.UserName = inpusername;
|
---|
111 | uid = accessClient.addUser(user);
|
---|
112 | ViewBag.Title = user.UserName + " added - User";
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | accessClient.updateUser(user);
|
---|
117 | ViewBag.Title = user.UserName + " updated - User";
|
---|
118 | }
|
---|
119 | accessClient.RefreshUsers();
|
---|
120 | user = accessClient.Users.Find(x => x.Id == uid);
|
---|
121 | foreach (var s in rolestoadd)
|
---|
122 | {
|
---|
123 | var role = accessClient.Roles.Find(x => x.Name == s);
|
---|
124 |
|
---|
125 | accessClient.addRoleToUser(user, role);
|
---|
126 | }
|
---|
127 | accessClient.RefreshUserGroups();
|
---|
128 | foreach(var g in groupstoadd)
|
---|
129 | {
|
---|
130 | var gid = Guid.Parse(g);
|
---|
131 | var group = accessClient.Groups.Find(x => x.Id == gid);
|
---|
132 | accessClient.addMember(user, group);
|
---|
133 | }
|
---|
134 | return RedirectToAction("SelectUser", new { id = uid.ToString() });
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | return RedirectToAction("Index", "Home");
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|
142 | public IActionResult deleteUser(string id)
|
---|
143 | {
|
---|
144 | if (init())
|
---|
145 | {
|
---|
146 | var uid = Guid.Parse(id);
|
---|
147 | UserViewModel vm = new UserViewModel(accessClient).refreshUsers();
|
---|
148 |
|
---|
149 | if (uid == Guid.Empty)
|
---|
150 | {
|
---|
151 | vm.message = "Something went wrong, user is not deleted";
|
---|
152 | return View("Index", vm);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | var user = vm.getUserById(uid);
|
---|
157 | vm.message = user.UserName + " (" + user.FullName + ") has been deleted";
|
---|
158 | accessClient.DeleteUser(user);
|
---|
159 | vm.refreshAll();
|
---|
160 | ViewBag.Title = "Users";
|
---|
161 | return View("Index", vm);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | return RedirectToAction("Index", "Home");
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|
170 |
|
---|
171 | public IActionResult deleteUserRole(string id, string role)
|
---|
172 | {
|
---|
173 | if (init())
|
---|
174 | {
|
---|
175 | var uid = Guid.Parse(id);
|
---|
176 | UserViewModel vm = new UserViewModel(accessClient).refreshAll();
|
---|
177 |
|
---|
178 | var user = vm.getUserById(uid);
|
---|
179 | var r = vm.getRoleByName(role);
|
---|
180 | accessClient.deleteUserRole(user, r);
|
---|
181 | return RedirectToAction("SelectUser", new { id = id });
|
---|
182 | }
|
---|
183 | else
|
---|
184 | {
|
---|
185 | return RedirectToAction("Index", "Home");
|
---|
186 | }
|
---|
187 | }
|
---|
188 | public IActionResult deleteUserGroup(string id, string group)
|
---|
189 | {
|
---|
190 | if (init())
|
---|
191 | {
|
---|
192 | var gid = Guid.Parse(group);
|
---|
193 | var mid = Guid.Parse(id);
|
---|
194 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
195 |
|
---|
196 | var user = vm.getUserById(mid);
|
---|
197 | var gr = vm.getGroupById(gid);
|
---|
198 | accessClient.deleteMember(user, gr);
|
---|
199 | return RedirectToAction("SelectUser", new { id = id});
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | return RedirectToAction("Index", "Home");
|
---|
204 | }
|
---|
205 |
|
---|
206 | }
|
---|
207 | #endregion
|
---|
208 |
|
---|
209 | #region Groups
|
---|
210 | public IActionResult Groups()
|
---|
211 | {
|
---|
212 | if (init())
|
---|
213 | {
|
---|
214 |
|
---|
215 | ViewBag.Title = "Groups";
|
---|
216 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
217 | return View("Groups", vm);
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | return RedirectToAction("Index", "Home");
|
---|
222 | }
|
---|
223 | }
|
---|
224 | public IActionResult SelectGroup(string id)
|
---|
225 | {
|
---|
226 | if (init())
|
---|
227 | {
|
---|
228 | Guid curr = Guid.Parse(id);
|
---|
229 |
|
---|
230 | UserViewModel vm = new UserViewModel(accessClient)
|
---|
231 | .refreshGroups()
|
---|
232 | .refreshUsers();
|
---|
233 | if (curr == Guid.Empty)
|
---|
234 | {
|
---|
235 | ViewBag.Title = "Add group";
|
---|
236 | }
|
---|
237 | else
|
---|
238 | {
|
---|
239 | vm.SelectedGroup = vm.getGroupById(curr);
|
---|
240 | vm.SelectedGroupMembers = accessClient.getGroupMembers(curr);
|
---|
241 | ViewBag.title = vm.SelectedGroup.Name;
|
---|
242 | }
|
---|
243 | ViewBag.Title += " - Group";
|
---|
244 | return View("Groups", vm);
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | return RedirectToAction("Index", "Home");
|
---|
249 | }
|
---|
250 | }
|
---|
251 | [HttpPost]
|
---|
252 | public IActionResult saveGroup(string inpgroupname, string u, string[] userstoadd, string[] groupstoadd)
|
---|
253 | {
|
---|
254 | if (init())
|
---|
255 | {
|
---|
256 | var uid = Guid.Parse(u);
|
---|
257 | var vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
258 |
|
---|
259 | Access.UserGroup group;
|
---|
260 |
|
---|
261 | if (uid == Guid.Empty)
|
---|
262 | group = new Access.UserGroup();
|
---|
263 | else
|
---|
264 | group = accessClient.Groups.Find(x => x.Id == uid);
|
---|
265 |
|
---|
266 | group.Name = inpgroupname;
|
---|
267 |
|
---|
268 | if (uid == Guid.Empty)
|
---|
269 | {
|
---|
270 | uid = accessClient.addGroup(group);
|
---|
271 | ViewBag.Title = group.Name + " added - Group";
|
---|
272 | }
|
---|
273 | else
|
---|
274 | {
|
---|
275 | accessClient.updateGroup(group);
|
---|
276 | ViewBag.Title = group.Name + " updated - Group";
|
---|
277 | }
|
---|
278 | vm.refreshGroups();
|
---|
279 | group = vm.getGroupById(uid);
|
---|
280 | foreach (var s in userstoadd)
|
---|
281 | {
|
---|
282 | var tempid = Guid.Parse(s);
|
---|
283 | var member = vm.getUserById(tempid);
|
---|
284 | accessClient.addMember(member, group);
|
---|
285 | }
|
---|
286 | foreach (var g in groupstoadd)
|
---|
287 | {
|
---|
288 | var tempid = Guid.Parse(g);
|
---|
289 | var member = vm.getGroupById(tempid);
|
---|
290 | accessClient.addMember(member, group);
|
---|
291 | }
|
---|
292 |
|
---|
293 | return RedirectToAction("SelectGroup", new { id = uid.ToString() });
|
---|
294 | }
|
---|
295 | else
|
---|
296 | {
|
---|
297 | return RedirectToAction("Index", "Home");
|
---|
298 | }
|
---|
299 | }
|
---|
300 | public IActionResult deleteGroup(string id)
|
---|
301 | {
|
---|
302 | if (init())
|
---|
303 | {
|
---|
304 | var uid = Guid.Parse(id);
|
---|
305 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups();
|
---|
306 |
|
---|
307 | if (uid == Guid.Empty)
|
---|
308 | {
|
---|
309 | vm.message = "Something went wrong, group is not deleted";
|
---|
310 | return View("Groups", vm);
|
---|
311 | }
|
---|
312 | else
|
---|
313 | {
|
---|
314 | var group = vm.getGroupById(uid);
|
---|
315 | vm.message = group.Name + " has been deleted";
|
---|
316 | accessClient.deleteUserGroup(group);
|
---|
317 | vm.refreshGroups();
|
---|
318 | return View("Groups", vm);
|
---|
319 | }
|
---|
320 | }
|
---|
321 | else
|
---|
322 | {
|
---|
323 | return RedirectToAction("Index", "Home");
|
---|
324 | }
|
---|
325 |
|
---|
326 | }
|
---|
327 | public IActionResult deleteMember(string g, string m)
|
---|
328 | {
|
---|
329 | if (init())
|
---|
330 | {
|
---|
331 | var gid = Guid.Parse(g);
|
---|
332 | var mid = Guid.Parse(m);
|
---|
333 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
334 |
|
---|
335 | if (gid == Guid.Empty || mid == Guid.Empty)
|
---|
336 | {
|
---|
337 | vm.message = "Something went wrong, member is not deleted";
|
---|
338 | return View("Groups", vm);
|
---|
339 | }
|
---|
340 | else
|
---|
341 | {
|
---|
342 | var group = vm.getGroupById(gid);
|
---|
343 | Access.UserGroupBase member = vm.getUserById(mid);
|
---|
344 | if (member == null)
|
---|
345 | {
|
---|
346 | member = vm.getGroupById(mid);
|
---|
347 | }
|
---|
348 | vm.message = " Member deleted";
|
---|
349 | accessClient.deleteMember(member, group);
|
---|
350 | vm.refreshGroups();
|
---|
351 | return RedirectToAction("SelectGroup", new { id = g });
|
---|
352 | }
|
---|
353 | }
|
---|
354 | else
|
---|
355 | {
|
---|
356 | return RedirectToAction("Index", "Home");
|
---|
357 | }
|
---|
358 |
|
---|
359 | }
|
---|
360 | #endregion
|
---|
361 |
|
---|
362 | #region Roles
|
---|
363 | public IActionResult Roles()
|
---|
364 | {
|
---|
365 | if (init())
|
---|
366 | {
|
---|
367 |
|
---|
368 | ViewBag.Title = "Roles";
|
---|
369 | UserViewModel vm = new UserViewModel(accessClient).refreshRoles();
|
---|
370 |
|
---|
371 | return View("Roles", vm);
|
---|
372 | }
|
---|
373 | else
|
---|
374 | {
|
---|
375 | return RedirectToAction("Index", "Home");
|
---|
376 | }
|
---|
377 | }
|
---|
378 | public IActionResult SelectRole(string name)
|
---|
379 | {
|
---|
380 | if (init())
|
---|
381 | {
|
---|
382 |
|
---|
383 | UserViewModel vm = new UserViewModel(accessClient).refreshRoles();
|
---|
384 | if (name == "" || name == null)
|
---|
385 | {
|
---|
386 | ViewBag.Title = "Add Role";
|
---|
387 | }
|
---|
388 | else
|
---|
389 | {
|
---|
390 | vm.SelectedRole = vm.getRoleByName(name);
|
---|
391 | vm.SelectedRoleEnrolled = accessClient.getEnrolled(vm.SelectedRole);
|
---|
392 |
|
---|
393 | ViewBag.title = vm.SelectedRole.Name;
|
---|
394 | }
|
---|
395 | ViewBag.Title += " - Roles";
|
---|
396 | return View("Roles", vm);
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | return RedirectToAction("Index", "Home");
|
---|
401 | }
|
---|
402 | }
|
---|
403 | [HttpPost]
|
---|
404 | public IActionResult saveRole(string inprolename)
|
---|
405 | {
|
---|
406 | if (init())
|
---|
407 | {
|
---|
408 | Access.Role role = accessClient.Roles.Find(x => x.Name == inprolename);
|
---|
409 | if (role == null)
|
---|
410 | {
|
---|
411 | role = new Access.Role();
|
---|
412 | role.Name = inprolename;
|
---|
413 | accessClient.addRole(role);
|
---|
414 | }
|
---|
415 |
|
---|
416 | accessClient.RefreshRoles();
|
---|
417 | return RedirectToAction("SelectRole", new { name = inprolename });
|
---|
418 | }
|
---|
419 | else
|
---|
420 | {
|
---|
421 | return RedirectToAction("Index", "Home");
|
---|
422 | }
|
---|
423 | }
|
---|
424 | public IActionResult deleteRole(string name)
|
---|
425 | {
|
---|
426 | if (init())
|
---|
427 | {
|
---|
428 | accessClient.deleteRole(name);
|
---|
429 | return RedirectToAction("Roles");
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | return RedirectToAction("Index", "Home");
|
---|
434 | }
|
---|
435 | }
|
---|
436 | public IActionResult deleteRoleUser(string id, string role)
|
---|
437 | {
|
---|
438 | if (init())
|
---|
439 | {
|
---|
440 | var uid = Guid.Parse(id);
|
---|
441 | UserViewModel vm = new UserViewModel(accessClient).refreshAll();
|
---|
442 |
|
---|
443 | var user = vm.getUserById(uid);
|
---|
444 | var r = vm.getRoleByName(role);
|
---|
445 | vm.message = role + " role has been deleted from " + user.FullName;
|
---|
446 | accessClient.deleteUserRole(user, r);
|
---|
447 | return RedirectToAction("SelectRole", new { name = role });
|
---|
448 | }
|
---|
449 | else
|
---|
450 | {
|
---|
451 | return RedirectToAction("Index", "Home");
|
---|
452 | }
|
---|
453 | }
|
---|
454 | #endregion
|
---|
455 | }
|
---|
456 | }
|
---|