Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Controllers/UserController.cs @ 13742

Last change on this file since 13742 was 13742, checked in by jlodewyc, 8 years ago

#2582 User delete, subscriptions. Groups add, delete, edit, add members

File size: 9.6 KB
Line 
1using HeuristicLab.Clients.Access.Administration;
2using HeuristicLab.Clients.Hive.WebJobManager.Services;
3using HeuristicLab.Clients.Hive.WebJobManager.ViewModels.User;
4using Microsoft.AspNet.Hosting;
5using Microsoft.AspNet.Http;
6using Microsoft.AspNet.Mvc;
7using System;
8using System.Collections.Generic;
9using System.Linq;
10using System.ServiceModel.Security;
11using System.Threading.Tasks;
12
13namespace 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 void init()
30        {
31            var u = HttpContext.Session.GetString("UserId");
32            if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
33            {
34                userId = Guid.Empty;
35                serviceLocator = new HiveServiceLocatorWeb();
36            }
37            else
38            {
39                userId = Guid.Parse(u);
40                serviceLocator = weblog.getServiceLocator(userId);
41                accessClient = weblog.getAccessAdminClient(userId);
42            }
43        }
44        #region Users
45
46        public IActionResult Index()
47        {
48            init();
49            if (serviceLocator.CheckLogin())
50            {
51
52                ViewBag.Title = "Users";
53                UserViewModel vm = new UserViewModel(accessClient).refreshUsers();
54                ViewBag.SessionId = HttpContext.Session.GetString("UserId");
55                return View("Index", vm);
56            }
57            else
58            {
59                return RedirectToAction("Index", "Home");
60            }
61        }
62
63        public IActionResult SelectUser(string id)
64        {
65            init();
66            if (serviceLocator.CheckLogin())
67            {
68                Guid curr = Guid.Parse(id);
69               
70                UserViewModel vm = new UserViewModel(accessClient).refreshUsers();
71                if (curr == Guid.Empty)
72                {
73                    ViewBag.Title = "Add User";
74                }
75                else
76                {
77                    vm.SelectedUser = vm.getUserById(curr);
78                    vm.SelectedUserSubscriptions = accessClient.getSubscribedGroups(curr);
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)
92        {
93            init();
94            var uid = Guid.Parse(u);
95            Access.User user;
96            if (uid == Guid.Empty)
97                user = new Access.User();
98            else
99                user = accessClient.Users.Find(x => x.Id == uid);
100
101           
102            user.FullName = inpfullname;
103            user.Email = inpemail;
104
105
106            if (uid == Guid.Empty)
107            {
108                user.UserName = inpusername;
109                uid = accessClient.addUser(user);
110                ViewBag.Title = user.UserName + " added - User";
111            }
112            else
113            {
114                accessClient.updateUser(user);
115                ViewBag.Title = user.UserName + " updated - User";
116            }
117
118            return RedirectToAction( "SelectUser", new {  id =  uid.ToString()});
119
120        }
121        public IActionResult deleteUser(string id)
122        {
123            init();
124            var uid = Guid.Parse(id);
125            UserViewModel vm = new UserViewModel(accessClient).refreshUsers();
126           
127            if (uid == Guid.Empty)
128            {
129                vm.message = "Something went wrong, user is not deleted";
130                return View("Index", vm);
131            }
132            else
133            {
134                var user = vm.getUserById(uid);
135                vm.message = user.UserName + " ("+user.FullName+") has been deleted";
136                accessClient.DeleteUser(user);
137                vm.refreshUsers();
138                return View("Index", vm);
139            }
140
141        }
142
143        #endregion
144
145        #region Groups
146        public IActionResult Groups()
147        {
148            init();
149            if (serviceLocator.CheckLogin())
150            {
151
152                ViewBag.Title = "Groups";
153                UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
154                return View("Groups", vm);
155            }
156            else
157            {
158                return RedirectToAction("Index", "Home");
159            }
160        }
161        public IActionResult SelectGroup(string id)
162        {
163            init();
164            if (serviceLocator.CheckLogin())
165            {
166                Guid curr = Guid.Parse(id);
167
168                UserViewModel vm = new UserViewModel(accessClient)
169                    .refreshGroups()
170                    .refreshUsers();
171                if (curr == Guid.Empty)
172                {
173                    ViewBag.Title = "Add group";
174                }
175                else
176                {
177                    vm.SelectedGroup = vm.getGroupById(curr);
178                    vm.SelectedGroupMembers = accessClient.getGroupMembers(curr);
179                    ViewBag.title = vm.SelectedGroup.Name;
180                }
181                ViewBag.Title += " - Group";
182                return View("Groups", vm);
183            }
184            else
185            {
186                return RedirectToAction("Index", "Home");
187            }
188        }
189        [HttpPost]
190        public IActionResult saveGroup(string inpgroupname, string u, string[] userstoadd, string[] groupstoadd)
191        {
192            init();
193            var uid = Guid.Parse(u);
194            var vm = new UserViewModel(accessClient).refreshGroups().refreshUsers();
195
196            Access.UserGroup group;
197           
198            if (uid == Guid.Empty)
199                group = new Access.UserGroup();
200            else
201                group = accessClient.Groups.Find(x => x.Id == uid);
202
203            group.Name = inpgroupname;
204
205            if (uid == Guid.Empty)
206            {
207                uid = accessClient.addGroup(group);
208                ViewBag.Title = group.Name + " added - Group";
209            }
210            else
211            {
212                accessClient.updateGroup(group);
213                ViewBag.Title = group.Name + " updated - Group";
214            }
215            vm.refreshGroups();
216            group = vm.getGroupById(uid);
217            foreach(var s in userstoadd)
218            {
219                var tempid = Guid.Parse(s);
220                var member = vm.getUserById(tempid);
221                accessClient.addMember(member, group);
222            }
223            foreach (var g in groupstoadd)
224            {
225                var tempid = Guid.Parse(g);
226                var member = vm.getGroupById(tempid);
227                accessClient.addMember(member, group);
228            }
229
230            return RedirectToAction("SelectGroup", new { id = uid.ToString() });
231
232        }
233        public IActionResult deleteGroup(string id)
234        {
235            init();
236            var uid = Guid.Parse(id);
237            UserViewModel vm = new UserViewModel(accessClient).refreshGroups();
238
239            if (uid == Guid.Empty)
240            {
241                vm.message = "Something went wrong, group is not deleted";
242                return View("Groups", vm);
243            }
244            else
245            {
246                var group = vm.getGroupById(uid);
247                vm.message = group.Name + " has been deleted";
248                accessClient.deleteUserGroup(group);
249                vm.refreshGroups();
250                return View("Groups", vm);
251            }
252
253        }
254        public IActionResult deleteMember(string g, string m)
255        {
256            init();
257            var gid = Guid.Parse(g);
258            var mid = Guid.Parse(m);
259            UserViewModel vm = new UserViewModel(accessClient).refreshGroups().refreshUsers() ;
260
261            if (gid == Guid.Empty || mid == Guid.Empty)
262            {
263                vm.message = "Something went wrong, member is not deleted";
264                return View("Groups", vm);
265            }
266            else
267            {
268                var group = vm.getGroupById(gid);
269                Access.UserGroupBase member = vm.getUserById(mid);
270                if(member == null)
271                {
272                    member = vm.getGroupById(mid);
273                }
274                vm.message = " Member deleted";
275                accessClient.deleteMember(member,group);
276                vm.refreshGroups();
277                return RedirectToAction("SelectGroup", new { id = g });
278            }
279
280        }
281        #endregion
282
283        #region Roles
284        public IActionResult Roles()
285        {
286            init();
287            if (serviceLocator.CheckLogin())
288            {
289
290                ViewBag.Title = "Roles";
291                // accessClient.RefreshUsers();
292
293                return View("Roles");
294            }
295            else
296            {
297                return RedirectToAction("Index", "Home");
298            }
299        }
300        #endregion
301    }
302}
Note: See TracBrowser for help on using the repository browser.