[13860] | 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 |
|
---|
[13740] | 22 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
[13862] | 23 | using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
|
---|
[13740] | 24 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels.User;
|
---|
[13860] | 25 | using Microsoft.AspNetCore.Hosting;
|
---|
| 26 | using Microsoft.AspNetCore.Http;
|
---|
| 27 | using Microsoft.AspNetCore.Mvc;
|
---|
[13689] | 28 | using System;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
| 31 | {
|
---|
[13805] | 32 | /// <summary>
|
---|
| 33 | /// Handles all the actions for user management
|
---|
| 34 | /// </summary>
|
---|
[13740] | 35 | public class UserController : Controller
|
---|
[13689] | 36 | {
|
---|
[13739] | 37 | private WebLoginService weblog;
|
---|
| 38 | private HiveServiceLocatorWeb serviceLocator;
|
---|
[13740] | 39 | private AccessAdministrationClient accessClient;
|
---|
[13739] | 40 | private Guid userId;
|
---|
| 41 |
|
---|
[13847] | 42 | private UserViewModel vm;
|
---|
[13689] | 43 | private IHostingEnvironment _environment;
|
---|
| 44 |
|
---|
| 45 | public UserController(IHostingEnvironment env)
|
---|
| 46 | {
|
---|
[13739] | 47 | weblog = WebLoginService.Instance;
|
---|
[13740] | 48 | _environment = env;
|
---|
| 49 | }
|
---|
[13805] | 50 | /// <summary>
|
---|
| 51 | /// Init required services and checks login
|
---|
| 52 | /// </summary>
|
---|
| 53 | /// <returns></returns>
|
---|
[13854] | 54 |
|
---|
[13754] | 55 | private bool init()
|
---|
[13740] | 56 | {
|
---|
[13739] | 57 | var u = HttpContext.Session.GetString("UserId");
|
---|
| 58 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
| 59 | {
|
---|
[13754] | 60 | return false;
|
---|
[13739] | 61 | }
|
---|
[13740] | 62 | else
|
---|
| 63 | {
|
---|
[13854] | 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 | {
|
---|
[13847] | 77 | return false;
|
---|
[13854] | 78 | }
|
---|
[13739] | 79 | }
|
---|
[13689] | 80 | }
|
---|
[13742] | 81 | #region Users
|
---|
[13805] | 82 | /// <summary>
|
---|
| 83 | /// Show users page
|
---|
| 84 | /// </summary>
|
---|
| 85 | /// <returns></returns>
|
---|
[13689] | 86 | public IActionResult Index()
|
---|
| 87 | {
|
---|
[13754] | 88 | if (init())
|
---|
[13689] | 89 | {
|
---|
[13740] | 90 | ViewBag.Title = "Users";
|
---|
[13854] | 91 | vm.refreshAll();
|
---|
[13805] | 92 | //Refreshall for users, groups and roles
|
---|
[13741] | 93 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13740] | 94 | return View("Index", vm);
|
---|
| 95 | }
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
[13847] | 98 | return RedirectToAction("Index", "Job");
|
---|
[13740] | 99 | }
|
---|
| 100 | }
|
---|
[13854] | 101 |
|
---|
[13740] | 102 | public IActionResult SelectUser(string id)
|
---|
| 103 | {
|
---|
[13754] | 104 | if (init())
|
---|
[13740] | 105 | {
|
---|
| 106 | Guid curr = Guid.Parse(id);
|
---|
[13754] | 107 |
|
---|
[13847] | 108 | vm.refreshAll();
|
---|
[13740] | 109 | if (curr == Guid.Empty)
|
---|
[13805] | 110 | {//new user
|
---|
[13740] | 111 | ViewBag.Title = "Add User";
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
[13805] | 114 | {//View an existing user
|
---|
[13741] | 115 | vm.SelectedUser = vm.getUserById(curr);
|
---|
[13742] | 116 | vm.SelectedUserSubscriptions = accessClient.getSubscribedGroups(curr);
|
---|
[13754] | 117 | vm.SelectedUserRoles = accessClient.getRoles(vm.SelectedUser);
|
---|
[13741] | 118 | ViewBag.title = vm.SelectedUser.UserName;
|
---|
[13740] | 119 | }
|
---|
| 120 | ViewBag.Title += " - Users";
|
---|
[13741] | 121 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13740] | 122 | return View("Index", vm);
|
---|
[13805] | 123 | //Uses Index page too, with the additional info in the Viewmodel
|
---|
[13740] | 124 | }
|
---|
| 125 | else
|
---|
| 126 | {
|
---|
[13847] | 127 | return RedirectToAction("Index", "Job");
|
---|
[13740] | 128 | }
|
---|
| 129 | }
|
---|
[13805] | 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>
|
---|
[13741] | 140 | [HttpPost]
|
---|
[13754] | 141 | public IActionResult saveUser(string inpusername, string inpfullname, string inpemail, string u, string[] rolestoadd, string[] groupstoadd)
|
---|
[13741] | 142 | {
|
---|
| 143 |
|
---|
[13754] | 144 | if (init())
|
---|
| 145 | {
|
---|
| 146 | var uid = Guid.Parse(u);
|
---|
| 147 | Access.User user;
|
---|
[13805] | 148 | if (uid == Guid.Empty)//checks if user is new or exists
|
---|
[13754] | 149 | user = new Access.User();
|
---|
| 150 | else
|
---|
| 151 | user = accessClient.Users.Find(x => x.Id == uid);
|
---|
[13741] | 152 |
|
---|
| 153 |
|
---|
[13754] | 154 | user.FullName = inpfullname;
|
---|
| 155 | user.Email = inpemail;
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | if (uid == Guid.Empty)
|
---|
[13805] | 159 | {//New user
|
---|
[13754] | 160 | user.UserName = inpusername;
|
---|
| 161 | uid = accessClient.addUser(user);
|
---|
[13827] | 162 | accessClient.RefreshUsers();
|
---|
[13805] | 163 | var newpass = accessClient.resetPassword(uid);
|
---|
| 164 | sendPassmail(accessClient.Users.Find(x => x.Id == uid), newpass);
|
---|
| 165 | //password received through mail
|
---|
[13754] | 166 | ViewBag.Title = user.UserName + " added - User";
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
[13805] | 169 | {//Existing user
|
---|
[13754] | 170 | accessClient.updateUser(user);
|
---|
| 171 | ViewBag.Title = user.UserName + " updated - User";
|
---|
| 172 | }
|
---|
[13805] | 173 | accessClient.RefreshUsers();//Refresh data from user
|
---|
[13754] | 174 | user = accessClient.Users.Find(x => x.Id == uid);
|
---|
| 175 | foreach (var s in rolestoadd)
|
---|
[13805] | 176 | {//Loop to add roles
|
---|
[13754] | 177 | var role = accessClient.Roles.Find(x => x.Name == s);
|
---|
| 178 |
|
---|
| 179 | accessClient.addRoleToUser(user, role);
|
---|
| 180 | }
|
---|
| 181 | accessClient.RefreshUserGroups();
|
---|
[13854] | 182 | foreach (var g in groupstoadd)
|
---|
[13805] | 183 | {//loop to subscribe to groups
|
---|
[13754] | 184 | var gid = Guid.Parse(g);
|
---|
| 185 | var group = accessClient.Groups.Find(x => x.Id == gid);
|
---|
| 186 | accessClient.addMember(user, group);
|
---|
| 187 | }
|
---|
[13862] | 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() });
|
---|
[13741] | 192 | }
|
---|
| 193 | else
|
---|
| 194 | {
|
---|
[13847] | 195 | return RedirectToAction("Index", "Job");
|
---|
[13741] | 196 | }
|
---|
| 197 |
|
---|
| 198 | }
|
---|
[13805] | 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 | {/*
|
---|
[13827] | 206 | SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587);
|
---|
[13805] | 207 | smtpClient.UseDefaultCredentials = true;
|
---|
[13827] | 208 | smtpClient.Credentials = new System.Net.NetworkCredential("trac.heuristiclab@gmail.com", "||||||||||||");
|
---|
[13805] | 209 | smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
|
---|
| 210 | smtpClient.EnableSsl = true;
|
---|
| 211 | MailMessage mail = new MailMessage();
|
---|
| 212 |
|
---|
| 213 | //Setting From , To and CC
|
---|
[13827] | 214 | mail.From = new MailAddress("trac.heuristiclab@gmail.com", "HeuristicLab");
|
---|
[13805] | 215 | mail.Subject = "Password changed - HeuristicLab Hive";
|
---|
[13827] | 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";
|
---|
[13805] | 220 | mail.To.Add(new MailAddress(u.Email));
|
---|
| 221 |
|
---|
| 222 | smtpClient.Send(mail);
|
---|
| 223 | */
|
---|
| 224 | }
|
---|
[13841] | 225 |
|
---|
| 226 | public IActionResult ApproveUser(string id)
|
---|
| 227 | {
|
---|
| 228 | if (init())
|
---|
| 229 | {
|
---|
| 230 | Guid curr = Guid.Parse(id);
|
---|
| 231 |
|
---|
[13847] | 232 | vm.refreshAll();
|
---|
[13854] | 233 |
|
---|
| 234 | var us = vm.getUserById(curr);
|
---|
[13841] | 235 | us.IsApproved = true;
|
---|
| 236 | accessClient.updateUser(us);
|
---|
| 237 | return RedirectToAction("SelectUser", new { id = id });
|
---|
| 238 |
|
---|
| 239 | }
|
---|
| 240 | else
|
---|
| 241 | {
|
---|
[13847] | 242 | return RedirectToAction("Index", "Job");
|
---|
[13841] | 243 | }
|
---|
| 244 | }
|
---|
[13805] | 245 | /// <summary>
|
---|
| 246 | /// Delete a user
|
---|
| 247 | /// </summary>
|
---|
| 248 | /// <param name="id">User ID to delete</param>
|
---|
| 249 | /// <returns></returns>
|
---|
[13742] | 250 | public IActionResult deleteUser(string id)
|
---|
| 251 | {
|
---|
[13754] | 252 | if (init())
|
---|
[13742] | 253 | {
|
---|
[13754] | 254 | var uid = Guid.Parse(id);
|
---|
[13847] | 255 | vm.refreshUsers();
|
---|
[13754] | 256 |
|
---|
| 257 | if (uid == Guid.Empty)
|
---|
[13805] | 258 | {//check for empty Guid, should not happen but still
|
---|
[13754] | 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);
|
---|
[13805] | 267 | vm.refreshAll();//refreshAll for index view
|
---|
[13754] | 268 | ViewBag.Title = "Users";
|
---|
[13805] | 269 | return View("Index", vm);//No redirect to save the viewbag message
|
---|
[13754] | 270 | }
|
---|
[13742] | 271 | }
|
---|
| 272 | else
|
---|
| 273 | {
|
---|
[13847] | 274 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 275 | }
|
---|
| 276 |
|
---|
| 277 | }
|
---|
[13805] | 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>
|
---|
[13754] | 284 | public IActionResult deleteUserRole(string id, string role)
|
---|
| 285 | {
|
---|
| 286 | if (init())
|
---|
| 287 | {
|
---|
| 288 | var uid = Guid.Parse(id);
|
---|
[13847] | 289 | vm.refreshAll();
|
---|
[13754] | 290 |
|
---|
[13742] | 291 | var user = vm.getUserById(uid);
|
---|
[13754] | 292 | var r = vm.getRoleByName(role);
|
---|
[13805] | 293 |
|
---|
[13754] | 294 | accessClient.deleteUserRole(user, r);
|
---|
| 295 | return RedirectToAction("SelectUser", new { id = id });
|
---|
[13742] | 296 | }
|
---|
[13754] | 297 | else
|
---|
| 298 | {
|
---|
[13847] | 299 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 300 | }
|
---|
| 301 | }
|
---|
[13805] | 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>
|
---|
[13754] | 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);
|
---|
[13847] | 314 | vm.refreshGroups().refreshUsers();
|
---|
[13741] | 315 |
|
---|
[13754] | 316 | var user = vm.getUserById(mid);
|
---|
| 317 | var gr = vm.getGroupById(gid);
|
---|
[13854] | 318 |
|
---|
[13754] | 319 | accessClient.deleteMember(user, gr);
|
---|
[13854] | 320 | return RedirectToAction("SelectUser", new { id = id });
|
---|
[13754] | 321 | }
|
---|
| 322 | else
|
---|
| 323 | {
|
---|
[13847] | 324 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 325 | }
|
---|
| 326 |
|
---|
[13742] | 327 | }
|
---|
[13740] | 328 | #endregion
|
---|
| 329 |
|
---|
| 330 | #region Groups
|
---|
[13805] | 331 | /// <summary>
|
---|
| 332 | /// Shows the groups overview, opening up with "Add group"
|
---|
| 333 | /// </summary>
|
---|
| 334 | /// <returns></returns>
|
---|
[13740] | 335 | public IActionResult Groups()
|
---|
| 336 | {
|
---|
[13754] | 337 | if (init())
|
---|
[13740] | 338 | {
|
---|
| 339 |
|
---|
| 340 | ViewBag.Title = "Groups";
|
---|
[13847] | 341 | vm.refreshGroups().refreshUsers();
|
---|
[13742] | 342 | return View("Groups", vm);
|
---|
[13689] | 343 | }
|
---|
| 344 | else
|
---|
| 345 | {
|
---|
[13847] | 346 | return RedirectToAction("Index", "Job");
|
---|
[13689] | 347 | }
|
---|
| 348 | }
|
---|
[13805] | 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>
|
---|
[13742] | 354 | public IActionResult SelectGroup(string id)
|
---|
| 355 | {
|
---|
[13754] | 356 | if (init())
|
---|
[13742] | 357 | {
|
---|
| 358 | Guid curr = Guid.Parse(id);
|
---|
| 359 |
|
---|
[13847] | 360 | vm
|
---|
[13742] | 361 | .refreshGroups()
|
---|
| 362 | .refreshUsers();
|
---|
| 363 | if (curr == Guid.Empty)
|
---|
[13805] | 364 | {//Add group
|
---|
[13742] | 365 | ViewBag.Title = "Add group";
|
---|
| 366 | }
|
---|
| 367 | else
|
---|
[13805] | 368 | {//Show existing group info and possibility to edit
|
---|
[13742] | 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 | {
|
---|
[13847] | 378 | return RedirectToAction("Index", "Job");
|
---|
[13742] | 379 | }
|
---|
| 380 | }
|
---|
[13805] | 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>
|
---|
[13742] | 389 | [HttpPost]
|
---|
| 390 | public IActionResult saveGroup(string inpgroupname, string u, string[] userstoadd, string[] groupstoadd)
|
---|
| 391 | {
|
---|
[13754] | 392 | if (init())
|
---|
| 393 | {
|
---|
| 394 | var uid = Guid.Parse(u);
|
---|
[13847] | 395 | vm.refreshGroups().refreshUsers();
|
---|
[13742] | 396 |
|
---|
[13754] | 397 | Access.UserGroup group;
|
---|
[13742] | 398 |
|
---|
[13805] | 399 | if (uid == Guid.Empty)//Create new group
|
---|
[13754] | 400 | group = new Access.UserGroup();
|
---|
[13805] | 401 | else//Find existing group
|
---|
[13754] | 402 | group = accessClient.Groups.Find(x => x.Id == uid);
|
---|
[13742] | 403 |
|
---|
[13754] | 404 | group.Name = inpgroupname;
|
---|
| 405 |
|
---|
| 406 | if (uid == Guid.Empty)
|
---|
[13805] | 407 | {//New Group add
|
---|
[13754] | 408 | uid = accessClient.addGroup(group);
|
---|
| 409 | ViewBag.Title = group.Name + " added - Group";
|
---|
| 410 | }
|
---|
| 411 | else
|
---|
[13805] | 412 | {//Existing group update
|
---|
[13754] | 413 | accessClient.updateGroup(group);
|
---|
| 414 | ViewBag.Title = group.Name + " updated - Group";
|
---|
| 415 | }
|
---|
| 416 | vm.refreshGroups();
|
---|
| 417 | group = vm.getGroupById(uid);
|
---|
[13805] | 418 | //refresh group info
|
---|
[13754] | 419 | foreach (var s in userstoadd)
|
---|
[13805] | 420 | {//Subscribes members to group
|
---|
[13754] | 421 | var tempid = Guid.Parse(s);
|
---|
| 422 | var member = vm.getUserById(tempid);
|
---|
| 423 | accessClient.addMember(member, group);
|
---|
| 424 | }
|
---|
| 425 | foreach (var g in groupstoadd)
|
---|
[13805] | 426 | {//Subscribes groups to group
|
---|
[13754] | 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() });
|
---|
[13742] | 433 | }
|
---|
| 434 | else
|
---|
| 435 | {
|
---|
[13847] | 436 | return RedirectToAction("Index", "Job");
|
---|
[13742] | 437 | }
|
---|
| 438 | }
|
---|
[13805] | 439 | /// <summary>
|
---|
| 440 | /// Delete an existing group
|
---|
| 441 | /// </summary>
|
---|
| 442 | /// <param name="id">Group to delete</param>
|
---|
| 443 | /// <returns></returns>
|
---|
[13742] | 444 | public IActionResult deleteGroup(string id)
|
---|
| 445 | {
|
---|
[13754] | 446 | if (init())
|
---|
| 447 | {
|
---|
| 448 | var uid = Guid.Parse(id);
|
---|
[13847] | 449 | vm.refreshGroups();
|
---|
[13742] | 450 |
|
---|
[13754] | 451 | if (uid == Guid.Empty)
|
---|
[13805] | 452 | {//Check for empty ID, should not happen but still..
|
---|
[13754] | 453 | vm.message = "Something went wrong, group is not deleted";
|
---|
| 454 | return View("Groups", vm);
|
---|
| 455 | }
|
---|
| 456 | else
|
---|
[13805] | 457 | {//Deletes the user group
|
---|
[13754] | 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 | }
|
---|
[13742] | 464 | }
|
---|
| 465 | else
|
---|
| 466 | {
|
---|
[13847] | 467 | return RedirectToAction("Index", "Job");
|
---|
[13742] | 468 | }
|
---|
| 469 |
|
---|
| 470 | }
|
---|
[13805] | 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>
|
---|
[13742] | 477 | public IActionResult deleteMember(string g, string m)
|
---|
| 478 | {
|
---|
[13754] | 479 | if (init())
|
---|
| 480 | {
|
---|
| 481 | var gid = Guid.Parse(g);
|
---|
| 482 | var mid = Guid.Parse(m);
|
---|
[13847] | 483 | vm.refreshGroups().refreshUsers();
|
---|
[13742] | 484 |
|
---|
[13754] | 485 | if (gid == Guid.Empty || mid == Guid.Empty)
|
---|
[13805] | 486 | {//Should not be empty but still..
|
---|
[13754] | 487 | vm.message = "Something went wrong, member is not deleted";
|
---|
| 488 | return View("Groups", vm);
|
---|
| 489 | }
|
---|
| 490 | else
|
---|
[13805] | 491 | {//Deletes the member
|
---|
[13754] | 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 | }
|
---|
[13742] | 503 | }
|
---|
| 504 | else
|
---|
| 505 | {
|
---|
[13847] | 506 | return RedirectToAction("Index", "Job");
|
---|
[13742] | 507 | }
|
---|
| 508 |
|
---|
| 509 | }
|
---|
[13740] | 510 | #endregion
|
---|
[13742] | 511 |
|
---|
[13740] | 512 | #region Roles
|
---|
[13805] | 513 | /// <summary>
|
---|
| 514 | /// Shows roles overview, opening with "Add role"
|
---|
| 515 | /// </summary>
|
---|
| 516 | /// <returns></returns>
|
---|
[13740] | 517 | public IActionResult Roles()
|
---|
| 518 | {
|
---|
[13754] | 519 | if (init())
|
---|
[13740] | 520 | {
|
---|
| 521 |
|
---|
| 522 | ViewBag.Title = "Roles";
|
---|
[13847] | 523 | vm.refreshRoles();
|
---|
[13740] | 524 |
|
---|
[13754] | 525 | return View("Roles", vm);
|
---|
[13740] | 526 | }
|
---|
| 527 | else
|
---|
| 528 | {
|
---|
[13847] | 529 | return RedirectToAction("Index", "Job");
|
---|
[13740] | 530 | }
|
---|
| 531 | }
|
---|
[13805] | 532 | /// <summary>
|
---|
| 533 | /// Shows information for a specific role
|
---|
| 534 | /// </summary>
|
---|
| 535 | /// <param name="name">Role name</param>
|
---|
| 536 | /// <returns></returns>
|
---|
[13754] | 537 | public IActionResult SelectRole(string name)
|
---|
| 538 | {
|
---|
| 539 | if (init())
|
---|
| 540 | {
|
---|
| 541 |
|
---|
[13847] | 542 | vm.refreshRoles();
|
---|
[13754] | 543 | if (name == "" || name == null)
|
---|
[13805] | 544 | {//Add role
|
---|
[13754] | 545 | ViewBag.Title = "Add Role";
|
---|
| 546 | }
|
---|
| 547 | else
|
---|
[13805] | 548 | {//Edit/view role
|
---|
[13754] | 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 | {
|
---|
[13847] | 559 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 560 | }
|
---|
| 561 | }
|
---|
[13805] | 562 | /// <summary>
|
---|
| 563 | /// Saves a role or adds a new one
|
---|
| 564 | /// </summary>
|
---|
| 565 | /// <param name="inprolename">Role name</param>
|
---|
| 566 | /// <returns></returns>
|
---|
[13754] | 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 | {
|
---|
[13847] | 585 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 586 | }
|
---|
| 587 | }
|
---|
[13805] | 588 | /// <summary>
|
---|
| 589 | /// Delete a role
|
---|
| 590 | /// </summary>
|
---|
| 591 | /// <param name="name">Role name</param>
|
---|
| 592 | /// <returns></returns>
|
---|
[13754] | 593 | public IActionResult deleteRole(string name)
|
---|
| 594 | {
|
---|
| 595 | if (init())
|
---|
| 596 | {
|
---|
| 597 | accessClient.deleteRole(name);
|
---|
| 598 | return RedirectToAction("Roles");
|
---|
| 599 | }
|
---|
| 600 | else
|
---|
| 601 | {
|
---|
[13847] | 602 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 603 | }
|
---|
| 604 | }
|
---|
[13805] | 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>
|
---|
[13754] | 611 | public IActionResult deleteRoleUser(string id, string role)
|
---|
| 612 | {
|
---|
| 613 | if (init())
|
---|
| 614 | {
|
---|
| 615 | var uid = Guid.Parse(id);
|
---|
[13847] | 616 | vm.refreshAll();
|
---|
[13754] | 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 | {
|
---|
[13847] | 626 | return RedirectToAction("Index", "Job");
|
---|
[13754] | 627 | }
|
---|
| 628 | }
|
---|
[13740] | 629 | #endregion
|
---|
[13689] | 630 | }
|
---|
| 631 | }
|
---|