[13740] | 1 | using HeuristicLab.Clients.Access.Administration;
|
---|
| 2 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
| 3 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels.User;
|
---|
[13689] | 4 | using Microsoft.AspNet.Hosting;
|
---|
[13739] | 5 | using Microsoft.AspNet.Http;
|
---|
[13689] | 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 | {
|
---|
[13740] | 15 | public class UserController : Controller
|
---|
[13689] | 16 | {
|
---|
[13739] | 17 | private WebLoginService weblog;
|
---|
| 18 | private HiveServiceLocatorWeb serviceLocator;
|
---|
[13740] | 19 | private AccessAdministrationClient accessClient;
|
---|
[13739] | 20 | private Guid userId;
|
---|
| 21 |
|
---|
[13689] | 22 | private IHostingEnvironment _environment;
|
---|
| 23 |
|
---|
| 24 | public UserController(IHostingEnvironment env)
|
---|
| 25 | {
|
---|
[13739] | 26 | weblog = WebLoginService.Instance;
|
---|
[13740] | 27 | _environment = env;
|
---|
| 28 | }
|
---|
[13754] | 29 | private bool init()
|
---|
[13740] | 30 | {
|
---|
[13739] | 31 | var u = HttpContext.Session.GetString("UserId");
|
---|
| 32 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
| 33 | {
|
---|
[13754] | 34 | return false;
|
---|
[13739] | 35 | }
|
---|
[13740] | 36 | else
|
---|
| 37 | {
|
---|
[13739] | 38 | userId = Guid.Parse(u);
|
---|
| 39 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
[13740] | 40 | accessClient = weblog.getAccessAdminClient(userId);
|
---|
[13754] | 41 | return serviceLocator.CheckLogin();
|
---|
| 42 |
|
---|
[13739] | 43 | }
|
---|
[13689] | 44 | }
|
---|
[13742] | 45 | #region Users
|
---|
| 46 |
|
---|
[13689] | 47 | public IActionResult Index()
|
---|
| 48 | {
|
---|
[13754] | 49 | if (init())
|
---|
[13689] | 50 | {
|
---|
[13740] | 51 | ViewBag.Title = "Users";
|
---|
[13754] | 52 | UserViewModel vm = new UserViewModel(accessClient).refreshAll() ;
|
---|
[13741] | 53 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13740] | 54 | return View("Index", vm);
|
---|
| 55 | }
|
---|
| 56 | else
|
---|
| 57 | {
|
---|
| 58 | return RedirectToAction("Index", "Home");
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
[13741] | 61 |
|
---|
[13740] | 62 | public IActionResult SelectUser(string id)
|
---|
| 63 | {
|
---|
[13754] | 64 | ;
|
---|
| 65 | if (init())
|
---|
[13740] | 66 | {
|
---|
| 67 | Guid curr = Guid.Parse(id);
|
---|
[13754] | 68 |
|
---|
| 69 | UserViewModel vm = new UserViewModel(accessClient).refreshAll();
|
---|
[13740] | 70 | if (curr == Guid.Empty)
|
---|
| 71 | {
|
---|
| 72 | ViewBag.Title = "Add User";
|
---|
| 73 | }
|
---|
| 74 | else
|
---|
| 75 | {
|
---|
[13741] | 76 | vm.SelectedUser = vm.getUserById(curr);
|
---|
[13742] | 77 | vm.SelectedUserSubscriptions = accessClient.getSubscribedGroups(curr);
|
---|
[13754] | 78 | vm.SelectedUserRoles = accessClient.getRoles(vm.SelectedUser);
|
---|
[13741] | 79 | ViewBag.title = vm.SelectedUser.UserName;
|
---|
[13740] | 80 | }
|
---|
| 81 | ViewBag.Title += " - Users";
|
---|
[13741] | 82 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13740] | 83 | return View("Index", vm);
|
---|
| 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
| 87 | return RedirectToAction("Index", "Home");
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[13741] | 90 | [HttpPost]
|
---|
[13754] | 91 | public IActionResult saveUser(string inpusername, string inpfullname, string inpemail, string u, string[] rolestoadd, string[] groupstoadd)
|
---|
[13741] | 92 | {
|
---|
| 93 |
|
---|
[13754] | 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);
|
---|
[13741] | 102 |
|
---|
| 103 |
|
---|
[13754] | 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() });
|
---|
[13741] | 135 | }
|
---|
| 136 | else
|
---|
| 137 | {
|
---|
[13754] | 138 | return RedirectToAction("Index", "Home");
|
---|
[13741] | 139 | }
|
---|
| 140 |
|
---|
| 141 | }
|
---|
[13742] | 142 | public IActionResult deleteUser(string id)
|
---|
| 143 | {
|
---|
[13754] | 144 | if (init())
|
---|
[13742] | 145 | {
|
---|
[13754] | 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 | }
|
---|
[13742] | 163 | }
|
---|
| 164 | else
|
---|
| 165 | {
|
---|
[13754] | 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 |
|
---|
[13742] | 178 | var user = vm.getUserById(uid);
|
---|
[13754] | 179 | var r = vm.getRoleByName(role);
|
---|
| 180 | accessClient.deleteUserRole(user, r);
|
---|
| 181 | return RedirectToAction("SelectUser", new { id = id });
|
---|
[13742] | 182 | }
|
---|
[13754] | 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();
|
---|
[13741] | 195 |
|
---|
[13754] | 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 |
|
---|
[13742] | 206 | }
|
---|
[13740] | 207 | #endregion
|
---|
| 208 |
|
---|
| 209 | #region Groups
|
---|
| 210 | public IActionResult Groups()
|
---|
| 211 | {
|
---|
[13754] | 212 | if (init())
|
---|
[13740] | 213 | {
|
---|
| 214 |
|
---|
| 215 | ViewBag.Title = "Groups";
|
---|
[13742] | 216 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
| 217 | return View("Groups", vm);
|
---|
[13689] | 218 | }
|
---|
| 219 | else
|
---|
| 220 | {
|
---|
| 221 | return RedirectToAction("Index", "Home");
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
[13742] | 224 | public IActionResult SelectGroup(string id)
|
---|
| 225 | {
|
---|
[13754] | 226 | if (init())
|
---|
[13742] | 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 | {
|
---|
[13754] | 254 | if (init())
|
---|
| 255 | {
|
---|
| 256 | var uid = Guid.Parse(u);
|
---|
| 257 | var vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
[13742] | 258 |
|
---|
[13754] | 259 | Access.UserGroup group;
|
---|
[13742] | 260 |
|
---|
[13754] | 261 | if (uid == Guid.Empty)
|
---|
| 262 | group = new Access.UserGroup();
|
---|
| 263 | else
|
---|
| 264 | group = accessClient.Groups.Find(x => x.Id == uid);
|
---|
[13742] | 265 |
|
---|
[13754] | 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() });
|
---|
[13742] | 294 | }
|
---|
| 295 | else
|
---|
| 296 | {
|
---|
[13754] | 297 | return RedirectToAction("Index", "Home");
|
---|
[13742] | 298 | }
|
---|
| 299 | }
|
---|
| 300 | public IActionResult deleteGroup(string id)
|
---|
| 301 | {
|
---|
[13754] | 302 | if (init())
|
---|
| 303 | {
|
---|
| 304 | var uid = Guid.Parse(id);
|
---|
| 305 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups();
|
---|
[13742] | 306 |
|
---|
[13754] | 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 | }
|
---|
[13742] | 320 | }
|
---|
| 321 | else
|
---|
| 322 | {
|
---|
[13754] | 323 | return RedirectToAction("Index", "Home");
|
---|
[13742] | 324 | }
|
---|
| 325 |
|
---|
| 326 | }
|
---|
| 327 | public IActionResult deleteMember(string g, string m)
|
---|
| 328 | {
|
---|
[13754] | 329 | if (init())
|
---|
| 330 | {
|
---|
| 331 | var gid = Guid.Parse(g);
|
---|
| 332 | var mid = Guid.Parse(m);
|
---|
| 333 | UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
|
---|
[13742] | 334 |
|
---|
[13754] | 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 | }
|
---|
[13742] | 353 | }
|
---|
| 354 | else
|
---|
| 355 | {
|
---|
[13754] | 356 | return RedirectToAction("Index", "Home");
|
---|
[13742] | 357 | }
|
---|
| 358 |
|
---|
| 359 | }
|
---|
[13740] | 360 | #endregion
|
---|
[13742] | 361 |
|
---|
[13740] | 362 | #region Roles
|
---|
| 363 | public IActionResult Roles()
|
---|
| 364 | {
|
---|
[13754] | 365 | if (init())
|
---|
[13740] | 366 | {
|
---|
| 367 |
|
---|
| 368 | ViewBag.Title = "Roles";
|
---|
[13754] | 369 | UserViewModel vm = new UserViewModel(accessClient).refreshRoles();
|
---|
[13740] | 370 |
|
---|
[13754] | 371 | return View("Roles", vm);
|
---|
[13740] | 372 | }
|
---|
| 373 | else
|
---|
| 374 | {
|
---|
| 375 | return RedirectToAction("Index", "Home");
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
[13754] | 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 | }
|
---|
[13740] | 454 | #endregion
|
---|
[13689] | 455 | }
|
---|
| 456 | }
|
---|