1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
23 | using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
|
---|
24 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels.User;
|
---|
25 | using Microsoft.AspNetCore.Hosting;
|
---|
26 | using Microsoft.AspNetCore.Http;
|
---|
27 | using Microsoft.AspNetCore.Mvc;
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
31 | {
|
---|
32 | /// <summary>
|
---|
33 | /// Handles all the actions for user management
|
---|
34 | /// </summary>
|
---|
35 | public class UserController : Controller
|
---|
36 | {
|
---|
37 | private WebLoginService weblog;
|
---|
38 | private HiveServiceLocatorWeb serviceLocator;
|
---|
39 | private AccessAdministrationClient accessClient;
|
---|
40 | private Guid userId;
|
---|
41 |
|
---|
42 | private UserViewModel vm;
|
---|
43 | private IHostingEnvironment _environment;
|
---|
44 |
|
---|
45 | public UserController(IHostingEnvironment env)
|
---|
46 | {
|
---|
47 | weblog = WebLoginService.Instance;
|
---|
48 | _environment = env;
|
---|
49 | }
|
---|
50 | /// <summary>
|
---|
51 | /// Init required services and checks login
|
---|
52 | /// </summary>
|
---|
53 | /// <returns></returns>
|
---|
54 |
|
---|
55 | private bool init()
|
---|
56 | {
|
---|
57 | var u = HttpContext.Session.GetString("UserId");
|
---|
58 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
59 | {
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | try
|
---|
65 | {
|
---|
66 | userId = Guid.Parse(u);
|
---|
67 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
68 | accessClient = weblog.getAccessAdminClient(userId);
|
---|
69 | vm = new UserViewModel(accessClient, weblog.getCurrentUser(userId));
|
---|
70 | if (weblog.getCurrentUser(userId).HasUserAdminAccess())
|
---|
71 | return serviceLocator.CheckLogin();
|
---|
72 | else
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | catch (NullReferenceException)
|
---|
76 | {
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | #region Users
|
---|
82 | /// <summary>
|
---|
83 | /// Show users page
|
---|
84 | /// </summary>
|
---|
85 | /// <returns></returns>
|
---|
86 | public IActionResult Index()
|
---|
87 | {
|
---|
88 | if (init())
|
---|
89 | {
|
---|
90 | ViewBag.Title = "Users";
|
---|
91 | vm.refreshAll();
|
---|
92 | //Refreshall for users, groups and roles
|
---|
93 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
94 | return View("Index", vm);
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | return RedirectToAction("Index", "Job");
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public IActionResult SelectUser(string id)
|
---|
103 | {
|
---|
104 | if (init())
|
---|
105 | {
|
---|
106 | Guid curr = Guid.Parse(id);
|
---|
107 |
|
---|
108 | vm.refreshAll();
|
---|
109 | if (curr == Guid.Empty)
|
---|
110 | {//new user
|
---|
111 | ViewBag.Title = "Add User";
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {//View an existing user
|
---|
115 | vm.SelectedUser = vm.getUserById(curr);
|
---|
116 | vm.SelectedUserSubscriptions = accessClient.getSubscribedGroups(curr);
|
---|
117 | vm.SelectedUserRoles = accessClient.getRoles(vm.SelectedUser);
|
---|
118 | ViewBag.title = vm.SelectedUser.UserName;
|
---|
119 | }
|
---|
120 | ViewBag.Title += " - Users";
|
---|
121 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
122 | return View("Index", vm);
|
---|
123 | //Uses Index page too, with the additional info in the Viewmodel
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | return RedirectToAction("Index", "Job");
|
---|
128 | }
|
---|
129 | }
|
---|
130 | /// <summary>
|
---|
131 | /// Saves an existing user or adds a new user
|
---|
132 | /// </summary>
|
---|
133 | /// <param name="inpusername">Username</param>
|
---|
134 | /// <param name="inpfullname">Full name </param>
|
---|
135 | /// <param name="inpemail">Email</param>
|
---|
136 | /// <param name="u">User ID</param>
|
---|
137 | /// <param name="rolestoadd">Roles to add to the user</param>
|
---|
138 | /// <param name="groupstoadd">Groups to subscribe the user to</param>
|
---|
139 | /// <returns></returns>
|
---|
140 | [HttpPost]
|
---|
141 | public IActionResult saveUser(string inpusername, string inpfullname, string inpemail, string u, string[] rolestoadd, string[] groupstoadd)
|
---|
142 | {
|
---|
143 |
|
---|
144 | if (init())
|
---|
145 | {
|
---|
146 | var uid = Guid.Parse(u);
|
---|
147 | Access.User user;
|
---|
148 | if (uid == Guid.Empty)//checks if user is new or exists
|
---|
149 | user = new Access.User();
|
---|
150 | else
|
---|
151 | user = accessClient.Users.Find(x => x.Id == uid);
|
---|
152 |
|
---|
153 |
|
---|
154 | user.FullName = inpfullname;
|
---|
155 | user.Email = inpemail;
|
---|
156 |
|
---|
157 |
|
---|
158 | if (uid == Guid.Empty)
|
---|
159 | {//New user
|
---|
160 | user.UserName = inpusername;
|
---|
161 | uid = accessClient.addUser(user);
|
---|
162 | accessClient.RefreshUsers();
|
---|
163 | var newpass = accessClient.resetPassword(uid);
|
---|
164 | sendPassmail(accessClient.Users.Find(x => x.Id == uid), newpass);
|
---|
165 | //password received through mail
|
---|
166 | ViewBag.Title = user.UserName + " added - User";
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {//Existing user
|
---|
170 | accessClient.updateUser(user);
|
---|
171 | ViewBag.Title = user.UserName + " updated - User";
|
---|
172 | }
|
---|
173 | accessClient.RefreshUsers();//Refresh data from user
|
---|
174 | user = accessClient.Users.Find(x => x.Id == uid);
|
---|
175 | foreach (var s in rolestoadd)
|
---|
176 | {//Loop to add roles
|
---|
177 | var role = accessClient.Roles.Find(x => x.Name == s);
|
---|
178 |
|
---|
179 | accessClient.addRoleToUser(user, role);
|
---|
180 | }
|
---|
181 | accessClient.RefreshUserGroups();
|
---|
182 | foreach (var g in groupstoadd)
|
---|
183 | {//loop to subscribe to groups
|
---|
184 | var gid = Guid.Parse(g);
|
---|
185 | var group = accessClient.Groups.Find(x => x.Id == gid);
|
---|
186 | accessClient.addMember(user, group);
|
---|
187 | }
|
---|
188 | if (uid != Guid.Empty && uid == weblog.getCurrentUser(userId).currentUser.Id)
|
---|
189 | return RedirectToAction("Index", "Home");//Logout if current user
|
---|
190 | else
|
---|
191 | return RedirectToAction("SelectUser", new { id = uid.ToString() });
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | return RedirectToAction("Index", "Job");
|
---|
196 | }
|
---|
197 |
|
---|
198 | }
|
---|
199 | /// <summary>
|
---|
200 | /// Sends email for a specific user, containg a password
|
---|
201 | /// </summary>
|
---|
202 | /// <param name="u">User to mail</param>
|
---|
203 | /// <param name="pass">Password to send</param>
|
---|
204 | public static void sendPassmail(Access.User u, string pass)
|
---|
205 | {/*
|
---|
206 | SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587);
|
---|
207 | smtpClient.UseDefaultCredentials = true;
|
---|
208 | smtpClient.Credentials = new System.Net.NetworkCredential("trac.heuristiclab@gmail.com", "||||||||||||");
|
---|
209 | smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
|
---|
210 | smtpClient.EnableSsl = true;
|
---|
211 | MailMessage mail = new MailMessage();
|
---|
212 |
|
---|
213 | //Setting From , To and CC
|
---|
214 | mail.From = new MailAddress("trac.heuristiclab@gmail.com", "HeuristicLab");
|
---|
215 | mail.Subject = "Password changed - HeuristicLab Hive";
|
---|
216 | mail.Body = "Hello " + u.FullName + ", " + Environment.NewLine + Environment.NewLine+
|
---|
217 | "Your password was changed through the HeuristicLab Hive web application." + Environment.NewLine+
|
---|
218 | "The new password for " + u.UserName + " is ' " + pass + " '." + Environment.NewLine + Environment.NewLine +
|
---|
219 | "HeuristicLab Hive";
|
---|
220 | mail.To.Add(new MailAddress(u.Email));
|
---|
221 |
|
---|
222 | smtpClient.Send(mail);
|
---|
223 | */
|
---|
224 | }
|
---|
225 |
|
---|
226 | public IActionResult ApproveUser(string id)
|
---|
227 | {
|
---|
228 | if (init())
|
---|
229 | {
|
---|
230 | Guid curr = Guid.Parse(id);
|
---|
231 |
|
---|
232 | vm.refreshAll();
|
---|
233 |
|
---|
234 | var us = vm.getUserById(curr);
|
---|
235 | us.IsApproved = true;
|
---|
236 | accessClient.updateUser(us);
|
---|
237 | return RedirectToAction("SelectUser", new { id = id });
|
---|
238 |
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | return RedirectToAction("Index", "Job");
|
---|
243 | }
|
---|
244 | }
|
---|
245 | /// <summary>
|
---|
246 | /// Delete a user
|
---|
247 | /// </summary>
|
---|
248 | /// <param name="id">User ID to delete</param>
|
---|
249 | /// <returns></returns>
|
---|
250 | public IActionResult deleteUser(string id)
|
---|
251 | {
|
---|
252 | if (init())
|
---|
253 | {
|
---|
254 | var uid = Guid.Parse(id);
|
---|
255 | vm.refreshUsers();
|
---|
256 |
|
---|
257 | if (uid == Guid.Empty)
|
---|
258 | {//check for empty Guid, should not happen but still
|
---|
259 | vm.message = "Something went wrong, user is not deleted";
|
---|
260 | return View("Index", vm);
|
---|
261 | }
|
---|
262 | else
|
---|
263 | {
|
---|
264 | var user = vm.getUserById(uid);
|
---|
265 | vm.message = user.UserName + " (" + user.FullName + ") has been deleted";
|
---|
266 | accessClient.DeleteUser(user);
|
---|
267 | vm.refreshAll();//refreshAll for index view
|
---|
268 | ViewBag.Title = "Users";
|
---|
269 | return View("Index", vm);//No redirect to save the viewbag message
|
---|
270 | }
|
---|
271 | }
|
---|
272 | else
|
---|
273 | {
|
---|
274 | return RedirectToAction("Index", "Job");
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 | /// <summary>
|
---|
279 | /// Deletes a role from a user
|
---|
280 | /// </summary>
|
---|
281 | /// <param name="id">User Id</param>
|
---|
282 | /// <param name="role">Role name</param>
|
---|
283 | /// <returns></returns>
|
---|
284 | public IActionResult deleteUserRole(string id, string role)
|
---|
285 | {
|
---|
286 | if (init())
|
---|
287 | {
|
---|
288 | var uid = Guid.Parse(id);
|
---|
289 | vm.refreshAll();
|
---|
290 |
|
---|
291 | var user = vm.getUserById(uid);
|
---|
292 | var r = vm.getRoleByName(role);
|
---|
293 |
|
---|
294 | accessClient.deleteUserRole(user, r);
|
---|
295 | return RedirectToAction("SelectUser", new { id = id });
|
---|
296 | }
|
---|
297 | else
|
---|
298 | {
|
---|
299 | return RedirectToAction("Index", "Job");
|
---|
300 | }
|
---|
301 | }
|
---|
302 | /// <summary>
|
---|
303 | /// Delete user from a group
|
---|
304 | /// </summary>
|
---|
305 | /// <param name="id">User Id</param>
|
---|
306 | /// <param name="group">Group Id</param>
|
---|
307 | /// <returns></returns>
|
---|
308 | public IActionResult deleteUserGroup(string id, string group)
|
---|
309 | {
|
---|
310 | if (init())
|
---|
311 | {
|
---|
312 | var gid = Guid.Parse(group);
|
---|
313 | var mid = Guid.Parse(id);
|
---|
314 | vm.refreshGroups().refreshUsers();
|
---|
315 |
|
---|
316 | var user = vm.getUserById(mid);
|
---|
317 | var gr = vm.getGroupById(gid);
|
---|
318 |
|
---|
319 | accessClient.deleteMember(user, gr);
|
---|
320 | return RedirectToAction("SelectUser", new { id = id });
|
---|
321 | }
|
---|
322 | else
|
---|
323 | {
|
---|
324 | return RedirectToAction("Index", "Job");
|
---|
325 | }
|
---|
326 |
|
---|
327 | }
|
---|
328 | #endregion
|
---|
329 |
|
---|
330 | #region Groups
|
---|
331 | /// <summary>
|
---|
332 | /// Shows the groups overview, opening up with "Add group"
|
---|
333 | /// </summary>
|
---|
334 | /// <returns></returns>
|
---|
335 | public IActionResult Groups()
|
---|
336 | {
|
---|
337 | if (init())
|
---|
338 | {
|
---|
339 |
|
---|
340 | ViewBag.Title = "Groups";
|
---|
341 | vm.refreshGroups().refreshUsers();
|
---|
342 | return View("Groups", vm);
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | return RedirectToAction("Index", "Job");
|
---|
347 | }
|
---|
348 | }
|
---|
349 | /// <summary>
|
---|
350 | /// Show information for a specific group (Guid.empty = new group)
|
---|
351 | /// </summary>
|
---|
352 | /// <param name="id">Group ID</param>
|
---|
353 | /// <returns></returns>
|
---|
354 | public IActionResult SelectGroup(string id)
|
---|
355 | {
|
---|
356 | if (init())
|
---|
357 | {
|
---|
358 | Guid curr = Guid.Parse(id);
|
---|
359 |
|
---|
360 | vm
|
---|
361 | .refreshGroups()
|
---|
362 | .refreshUsers();
|
---|
363 | if (curr == Guid.Empty)
|
---|
364 | {//Add group
|
---|
365 | ViewBag.Title = "Add group";
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {//Show existing group info and possibility to edit
|
---|
369 | vm.SelectedGroup = vm.getGroupById(curr);
|
---|
370 | vm.SelectedGroupMembers = accessClient.getGroupMembers(curr);
|
---|
371 | ViewBag.title = vm.SelectedGroup.Name;
|
---|
372 | }
|
---|
373 | ViewBag.Title += " - Group";
|
---|
374 | return View("Groups", vm);
|
---|
375 | }
|
---|
376 | else
|
---|
377 | {
|
---|
378 | return RedirectToAction("Index", "Job");
|
---|
379 | }
|
---|
380 | }
|
---|
381 | /// <summary>
|
---|
382 | /// Saves an exisiting group or adds a new one.
|
---|
383 | /// </summary>
|
---|
384 | /// <param name="inpgroupname">Group name</param>
|
---|
385 | /// <param name="u">Group id (empty for new)</param>
|
---|
386 | /// <param name="userstoadd">Users to add to group</param>
|
---|
387 | /// <param name="groupstoadd">Groups to add to group</param>
|
---|
388 | /// <returns></returns>
|
---|
389 | [HttpPost]
|
---|
390 | public IActionResult saveGroup(string inpgroupname, string u, string[] userstoadd, string[] groupstoadd)
|
---|
391 | {
|
---|
392 | if (init())
|
---|
393 | {
|
---|
394 | var uid = Guid.Parse(u);
|
---|
395 | vm.refreshGroups().refreshUsers();
|
---|
396 |
|
---|
397 | Access.UserGroup group;
|
---|
398 |
|
---|
399 | if (uid == Guid.Empty)//Create new group
|
---|
400 | group = new Access.UserGroup();
|
---|
401 | else//Find existing group
|
---|
402 | group = accessClient.Groups.Find(x => x.Id == uid);
|
---|
403 |
|
---|
404 | group.Name = inpgroupname;
|
---|
405 |
|
---|
406 | if (uid == Guid.Empty)
|
---|
407 | {//New Group add
|
---|
408 | uid = accessClient.addGroup(group);
|
---|
409 | ViewBag.Title = group.Name + " added - Group";
|
---|
410 | }
|
---|
411 | else
|
---|
412 | {//Existing group update
|
---|
413 | accessClient.updateGroup(group);
|
---|
414 | ViewBag.Title = group.Name + " updated - Group";
|
---|
415 | }
|
---|
416 | vm.refreshGroups();
|
---|
417 | group = vm.getGroupById(uid);
|
---|
418 | //refresh group info
|
---|
419 | foreach (var s in userstoadd)
|
---|
420 | {//Subscribes members to group
|
---|
421 | var tempid = Guid.Parse(s);
|
---|
422 | var member = vm.getUserById(tempid);
|
---|
423 | accessClient.addMember(member, group);
|
---|
424 | }
|
---|
425 | foreach (var g in groupstoadd)
|
---|
426 | {//Subscribes groups to group
|
---|
427 | var tempid = Guid.Parse(g);
|
---|
428 | var member = vm.getGroupById(tempid);
|
---|
429 | accessClient.addMember(member, group);
|
---|
430 | }
|
---|
431 |
|
---|
432 | return RedirectToAction("SelectGroup", new { id = uid.ToString() });
|
---|
433 | }
|
---|
434 | else
|
---|
435 | {
|
---|
436 | return RedirectToAction("Index", "Job");
|
---|
437 | }
|
---|
438 | }
|
---|
439 | /// <summary>
|
---|
440 | /// Delete an existing group
|
---|
441 | /// </summary>
|
---|
442 | /// <param name="id">Group to delete</param>
|
---|
443 | /// <returns></returns>
|
---|
444 | public IActionResult deleteGroup(string id)
|
---|
445 | {
|
---|
446 | if (init())
|
---|
447 | {
|
---|
448 | var uid = Guid.Parse(id);
|
---|
449 | vm.refreshGroups();
|
---|
450 |
|
---|
451 | if (uid == Guid.Empty)
|
---|
452 | {//Check for empty ID, should not happen but still..
|
---|
453 | vm.message = "Something went wrong, group is not deleted";
|
---|
454 | return View("Groups", vm);
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {//Deletes the user group
|
---|
458 | var group = vm.getGroupById(uid);
|
---|
459 | vm.message = group.Name + " has been deleted";
|
---|
460 | accessClient.deleteUserGroup(group);
|
---|
461 | vm.refreshGroups();
|
---|
462 | return View("Groups", vm);
|
---|
463 | }
|
---|
464 | }
|
---|
465 | else
|
---|
466 | {
|
---|
467 | return RedirectToAction("Index", "Job");
|
---|
468 | }
|
---|
469 |
|
---|
470 | }
|
---|
471 | /// <summary>
|
---|
472 | /// Delete a member from a group
|
---|
473 | /// </summary>
|
---|
474 | /// <param name="g">Group id</param>
|
---|
475 | /// <param name="m">User id</param>
|
---|
476 | /// <returns>Group selected</returns>
|
---|
477 | public IActionResult deleteMember(string g, string m)
|
---|
478 | {
|
---|
479 | if (init())
|
---|
480 | {
|
---|
481 | var gid = Guid.Parse(g);
|
---|
482 | var mid = Guid.Parse(m);
|
---|
483 | vm.refreshGroups().refreshUsers();
|
---|
484 |
|
---|
485 | if (gid == Guid.Empty || mid == Guid.Empty)
|
---|
486 | {//Should not be empty but still..
|
---|
487 | vm.message = "Something went wrong, member is not deleted";
|
---|
488 | return View("Groups", vm);
|
---|
489 | }
|
---|
490 | else
|
---|
491 | {//Deletes the member
|
---|
492 | var group = vm.getGroupById(gid);
|
---|
493 | Access.UserGroupBase member = vm.getUserById(mid);
|
---|
494 | if (member == null)
|
---|
495 | {
|
---|
496 | member = vm.getGroupById(mid);
|
---|
497 | }
|
---|
498 | vm.message = " Member deleted";
|
---|
499 | accessClient.deleteMember(member, group);
|
---|
500 | vm.refreshGroups();
|
---|
501 | return RedirectToAction("SelectGroup", new { id = g });
|
---|
502 | }
|
---|
503 | }
|
---|
504 | else
|
---|
505 | {
|
---|
506 | return RedirectToAction("Index", "Job");
|
---|
507 | }
|
---|
508 |
|
---|
509 | }
|
---|
510 | #endregion
|
---|
511 |
|
---|
512 | #region Roles
|
---|
513 | /// <summary>
|
---|
514 | /// Shows roles overview, opening with "Add role"
|
---|
515 | /// </summary>
|
---|
516 | /// <returns></returns>
|
---|
517 | public IActionResult Roles()
|
---|
518 | {
|
---|
519 | if (init())
|
---|
520 | {
|
---|
521 |
|
---|
522 | ViewBag.Title = "Roles";
|
---|
523 | vm.refreshRoles();
|
---|
524 |
|
---|
525 | return View("Roles", vm);
|
---|
526 | }
|
---|
527 | else
|
---|
528 | {
|
---|
529 | return RedirectToAction("Index", "Job");
|
---|
530 | }
|
---|
531 | }
|
---|
532 | /// <summary>
|
---|
533 | /// Shows information for a specific role
|
---|
534 | /// </summary>
|
---|
535 | /// <param name="name">Role name</param>
|
---|
536 | /// <returns></returns>
|
---|
537 | public IActionResult SelectRole(string name)
|
---|
538 | {
|
---|
539 | if (init())
|
---|
540 | {
|
---|
541 |
|
---|
542 | vm.refreshRoles();
|
---|
543 | if (name == "" || name == null)
|
---|
544 | {//Add role
|
---|
545 | ViewBag.Title = "Add Role";
|
---|
546 | }
|
---|
547 | else
|
---|
548 | {//Edit/view role
|
---|
549 | vm.SelectedRole = vm.getRoleByName(name);
|
---|
550 | vm.SelectedRoleEnrolled = accessClient.getEnrolled(vm.SelectedRole);
|
---|
551 |
|
---|
552 | ViewBag.title = vm.SelectedRole.Name;
|
---|
553 | }
|
---|
554 | ViewBag.Title += " - Roles";
|
---|
555 | return View("Roles", vm);
|
---|
556 | }
|
---|
557 | else
|
---|
558 | {
|
---|
559 | return RedirectToAction("Index", "Job");
|
---|
560 | }
|
---|
561 | }
|
---|
562 | /// <summary>
|
---|
563 | /// Saves a role or adds a new one
|
---|
564 | /// </summary>
|
---|
565 | /// <param name="inprolename">Role name</param>
|
---|
566 | /// <returns></returns>
|
---|
567 | [HttpPost]
|
---|
568 | public IActionResult saveRole(string inprolename)
|
---|
569 | {
|
---|
570 | if (init())
|
---|
571 | {
|
---|
572 | Access.Role role = accessClient.Roles.Find(x => x.Name == inprolename);
|
---|
573 | if (role == null)
|
---|
574 | {
|
---|
575 | role = new Access.Role();
|
---|
576 | role.Name = inprolename;
|
---|
577 | accessClient.addRole(role);
|
---|
578 | }
|
---|
579 |
|
---|
580 | accessClient.RefreshRoles();
|
---|
581 | return RedirectToAction("SelectRole", new { name = inprolename });
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | return RedirectToAction("Index", "Job");
|
---|
586 | }
|
---|
587 | }
|
---|
588 | /// <summary>
|
---|
589 | /// Delete a role
|
---|
590 | /// </summary>
|
---|
591 | /// <param name="name">Role name</param>
|
---|
592 | /// <returns></returns>
|
---|
593 | public IActionResult deleteRole(string name)
|
---|
594 | {
|
---|
595 | if (init())
|
---|
596 | {
|
---|
597 | accessClient.deleteRole(name);
|
---|
598 | return RedirectToAction("Roles");
|
---|
599 | }
|
---|
600 | else
|
---|
601 | {
|
---|
602 | return RedirectToAction("Index", "Job");
|
---|
603 | }
|
---|
604 | }
|
---|
605 | /// <summary>
|
---|
606 | /// Deletes a user from a role
|
---|
607 | /// </summary>
|
---|
608 | /// <param name="id">User id</param>
|
---|
609 | /// <param name="role">Role name</param>
|
---|
610 | /// <returns></returns>
|
---|
611 | public IActionResult deleteRoleUser(string id, string role)
|
---|
612 | {
|
---|
613 | if (init())
|
---|
614 | {
|
---|
615 | var uid = Guid.Parse(id);
|
---|
616 | vm.refreshAll();
|
---|
617 |
|
---|
618 | var user = vm.getUserById(uid);
|
---|
619 | var r = vm.getRoleByName(role);
|
---|
620 | vm.message = role + " role has been deleted from " + user.FullName;
|
---|
621 | accessClient.deleteUserRole(user, r);
|
---|
622 | return RedirectToAction("SelectRole", new { name = role });
|
---|
623 | }
|
---|
624 | else
|
---|
625 | {
|
---|
626 | return RedirectToAction("Index", "Job");
|
---|
627 | }
|
---|
628 | }
|
---|
629 | #endregion
|
---|
630 | }
|
---|
631 | }
|
---|