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.Models;
|
---|
23 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
24 | using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
|
---|
25 | using Microsoft.AspNetCore.Hosting;
|
---|
26 | using Microsoft.AspNetCore.Http;
|
---|
27 | using Microsoft.AspNetCore.Mvc;
|
---|
28 | using System;
|
---|
29 | using System.Linq;
|
---|
30 | using System.Threading;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
33 | {
|
---|
34 | /// <summary>
|
---|
35 | /// Controller for the resources. Only handles requests that refresh the page.
|
---|
36 | /// </summary>
|
---|
37 | public class ResourceController : Controller
|
---|
38 | {
|
---|
39 | private WebLoginService weblog;
|
---|
40 | private HiveServiceLocatorWeb serviceLocator;
|
---|
41 | private AccessAdministrationClient accessClient;
|
---|
42 | private HiveAdminClientWeb adminClient;
|
---|
43 | private Guid userId;
|
---|
44 | private HiveWebUser currentUser;
|
---|
45 |
|
---|
46 | private IHostingEnvironment _environment;
|
---|
47 |
|
---|
48 | public ResourceController(IHostingEnvironment env)
|
---|
49 | {
|
---|
50 | weblog = WebLoginService.Instance;
|
---|
51 | _environment = env;
|
---|
52 | }
|
---|
53 | /// <summary>
|
---|
54 | /// Initialize and check the login. Gets all the required services for the user.
|
---|
55 | /// </summary>
|
---|
56 | /// <returns></returns>
|
---|
57 | private bool init()
|
---|
58 | {
|
---|
59 | var u = HttpContext.Session.GetString("UserId");
|
---|
60 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
61 | {
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | try {
|
---|
67 | userId = Guid.Parse(u);
|
---|
68 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
69 | adminClient = weblog.getAdminClient(userId);
|
---|
70 | accessClient = weblog.getAccessAdminClient(userId);
|
---|
71 | currentUser = weblog.getCurrentUser(userId);
|
---|
72 | if (currentUser.hasResourceAdminAccess())
|
---|
73 | return serviceLocator.CheckLogin();
|
---|
74 | else
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | catch (NullReferenceException)
|
---|
78 | {
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | /// <summary>
|
---|
84 | /// Loads the main page for the resources
|
---|
85 | /// </summary>
|
---|
86 | /// <returns></returns>
|
---|
87 | public IActionResult Index()
|
---|
88 | {
|
---|
89 |
|
---|
90 | if (init())
|
---|
91 | {
|
---|
92 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
93 | ViewBag.Title = "Resources";
|
---|
94 | return View("Index", currentUser);
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | return RedirectToAction("Index", "Job");
|
---|
99 | }
|
---|
100 | }
|
---|
101 | /// <summary>
|
---|
102 | /// Changes the parent for a specfic resource
|
---|
103 | /// </summary>
|
---|
104 | /// <param name="inpid">Resource to edit</param>
|
---|
105 | /// <param name="inpidpar">New parent resource</param>
|
---|
106 | /// <returns></returns>
|
---|
107 | [HttpPost]
|
---|
108 | public IActionResult changeParent(string inpid, string inpidpar)
|
---|
109 | {
|
---|
110 | if (init())
|
---|
111 | {
|
---|
112 | var gid = Guid.Parse(inpid);
|
---|
113 |
|
---|
114 |
|
---|
115 | adminClient.Refresh();
|
---|
116 | var tochange = adminClient.Resources.ToList().Find(x => x.Id == gid);
|
---|
117 | //gets current resource
|
---|
118 | if (inpidpar != null && inpidpar != "") {
|
---|
119 | Guid tempid = Guid.Parse(inpidpar);
|
---|
120 | while (tempid != null && tempid != Guid.Empty)
|
---|
121 | {//Check if a loop could occur (some parent of the parent being the parent)
|
---|
122 | if (tempid != gid)
|
---|
123 | {//Not equal so move up a level
|
---|
124 | var t = adminClient.Resources.ToList().Find(x => x.Id == tempid);
|
---|
125 | if (t.ParentResourceId == null)
|
---|
126 | tempid = Guid.Empty;
|
---|
127 | else
|
---|
128 | tempid = (Guid)t.ParentResourceId;
|
---|
129 |
|
---|
130 | }
|
---|
131 | else
|
---|
132 | break; //breaks loop if equal
|
---|
133 | }
|
---|
134 | if(tempid == null || tempid == Guid.Empty)//Broken loop => the tempid is not empty so no change
|
---|
135 | tochange.ParentResourceId = Guid.Parse(inpidpar);
|
---|
136 | }
|
---|
137 | else
|
---|
138 | tochange.ParentResourceId = null;
|
---|
139 | adminClient.Store(tochange, CancellationToken.None);
|
---|
140 | //Save to server
|
---|
141 |
|
---|
142 | return RedirectToAction("Index", "Resource");
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | return RedirectToAction("Index", "Job");
|
---|
147 | }
|
---|
148 | }
|
---|
149 | /// <summary>
|
---|
150 | /// Adds recources to a resource group
|
---|
151 | /// </summary>
|
---|
152 | /// <param name="addres">Array containing all the resources ID's from the group</param>
|
---|
153 | /// <param name="groupid">Group id</param>
|
---|
154 | /// <returns></returns>
|
---|
155 | [HttpPost]
|
---|
156 | public IActionResult addResourcesToGroup(string[] addres, string groupid)
|
---|
157 | {
|
---|
158 | if (init())
|
---|
159 | {
|
---|
160 | Guid gid = Guid.Parse(groupid);
|
---|
161 | adminClient.Refresh();
|
---|
162 | var exist = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
|
---|
163 | //Current existing member of the group
|
---|
164 |
|
---|
165 | foreach(var a in addres)
|
---|
166 | {//loop on each ID
|
---|
167 | Guid tempid = Guid.Parse(a);
|
---|
168 | var elemid = exist.FindIndex(x => x.Id == tempid);
|
---|
169 | if (elemid != -1)
|
---|
170 | {//If element already is a member of the group: scramble temporary ID (for future reference)
|
---|
171 | exist[elemid].Id = Guid.Empty;
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {//If element is not yet in list => change parentresourceid from that element
|
---|
175 | var obj = adminClient.Resources.ToList().Find(x => x.Id == tempid);
|
---|
176 | obj.ParentResourceId = gid;
|
---|
177 | adminClient.Store(obj, CancellationToken.None);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | foreach(var e in exist)
|
---|
181 | {//Loop on existing and check the ID's
|
---|
182 | if(e.Id != Guid.Empty)
|
---|
183 | {//ID != Empty means that element should not be a member of the list anymore
|
---|
184 | e.ParentResourceId = null; //remove ParentResourceID from element
|
---|
185 | adminClient.Store(e, CancellationToken.None);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return RedirectToAction("Index", "Resource");
|
---|
190 |
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | return RedirectToAction("Index", "Job");
|
---|
195 | }
|
---|
196 | }
|
---|
197 | /// <summary>
|
---|
198 | /// Delete a client group (relinks the parent from every member to possible grandparent)
|
---|
199 | /// </summary>
|
---|
200 | /// <param name="inpid">Group ID to delete</param>
|
---|
201 | /// <returns></returns>
|
---|
202 | [HttpPost]
|
---|
203 | public IActionResult deleteClientGroup(string inpid)
|
---|
204 | {
|
---|
205 | if (init())
|
---|
206 | {
|
---|
207 | var gid = Guid.Parse(inpid);
|
---|
208 |
|
---|
209 | adminClient.Refresh();
|
---|
210 | var group = adminClient.Resources.ToList().Find(x => x.Id == gid);
|
---|
211 | //Group to delete
|
---|
212 | var list = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
|
---|
213 | //All members from the group
|
---|
214 | foreach(var cl in list)
|
---|
215 | {//Links parent from every child to parent from the group to delete.
|
---|
216 | cl.ParentResourceId = group.ParentResourceId;
|
---|
217 | adminClient.Store(cl, CancellationToken.None);
|
---|
218 | }
|
---|
219 | adminClient.Delete(group);
|
---|
220 |
|
---|
221 | return RedirectToAction("Index", "Resource");
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | return RedirectToAction("Index", "Job");
|
---|
226 | }
|
---|
227 | }
|
---|
228 | /// <summary>
|
---|
229 | /// Create a new Client Group
|
---|
230 | /// </summary>
|
---|
231 | /// <param name="inpname">Name for the group</param>
|
---|
232 | /// <param name="inpheart">Heartbeat</param>
|
---|
233 | /// <param name="inpparent">Possible parent for the group</param>
|
---|
234 | /// <param name="clientstoadd">Clients to be added to the group</param>
|
---|
235 | /// <returns></returns>
|
---|
236 | [HttpPost]
|
---|
237 | public IActionResult newClientGroup(string inpname, int inpheart, string inpparent, string[] clientstoadd)
|
---|
238 | {
|
---|
239 | if (init()) {
|
---|
240 | adminClient.Refresh();
|
---|
241 |
|
---|
242 | SlaveGroup sg = new SlaveGroup();
|
---|
243 | //init
|
---|
244 | sg.Id = Guid.Empty;
|
---|
245 | sg.Name = inpname;
|
---|
246 | sg.HbInterval = inpheart;
|
---|
247 | if (inpparent != null)
|
---|
248 | sg.ParentResourceId = Guid.Parse(inpparent);
|
---|
249 |
|
---|
250 | sg.OwnerUserId = serviceLocator.getHiveServiceClient().GetUserIdByUsername(serviceLocator.getHiveServiceClient().ClientCredentials.UserName.UserName);
|
---|
251 | adminClient.Store(sg, CancellationToken.None);
|
---|
252 | adminClient.Refresh();
|
---|
253 | //Reloads the resources
|
---|
254 | var id = adminClient.Resources.ToList().Find(x => x.Name == inpname).Id;
|
---|
255 | foreach(var s in clientstoadd)
|
---|
256 | {//Loop so each client can be added to the group
|
---|
257 | var cid = Guid.Parse(s);
|
---|
258 | var client = adminClient.Resources.ToList().Find(x => x.Id == cid);
|
---|
259 | client.ParentResourceId = id;
|
---|
260 | adminClient.Store(client, CancellationToken.None);
|
---|
261 | }
|
---|
262 |
|
---|
263 | return RedirectToAction("Index", "Resource");
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | return RedirectToAction("Index", "Job");
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|