1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.ServiceModel;
|
---|
26 | using System.ServiceModel.Channels;
|
---|
27 | using System.Web.Security;
|
---|
28 | using HeuristicLab.GeoIP;
|
---|
29 | using DA = HeuristicLab.Services.Access.DataAccess;
|
---|
30 | using DT = HeuristicLab.Services.Access.DataTransfer;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Services.Access {
|
---|
33 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
|
---|
34 | public class AccessService : IAccessService {
|
---|
35 | private IUserManager userManager;
|
---|
36 | private IUserManager UserManager {
|
---|
37 | get {
|
---|
38 | if (userManager == null) userManager = new UserManager();
|
---|
39 | return userManager;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | private IRoleVerifier roleVerifier;
|
---|
44 | private IRoleVerifier RoleVerifier {
|
---|
45 | get {
|
---|
46 | if (roleVerifier == null) roleVerifier = new RoleVerifier();
|
---|
47 | return roleVerifier;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | #region Client Members
|
---|
52 | public bool ClientExists(Guid id) {
|
---|
53 | if (id != Guid.Empty) {
|
---|
54 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
55 | return (context.Resources.Where(x => x.Id == id).Count() != 0);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public DT.Client GetClient(Guid id) {
|
---|
62 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
63 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
64 | where c.Id == id
|
---|
65 | select c;
|
---|
66 | if (query.Count() > 0) {
|
---|
67 | return Convert.ToDto(query.FirstOrDefault());
|
---|
68 | } else {
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IEnumerable<DT.Client> GetClients(IEnumerable<Guid> ids) {
|
---|
75 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
76 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
77 | where ids.Contains(c.Id)
|
---|
78 | select Convert.ToDto(c);
|
---|
79 | return query.ToList();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IEnumerable<DT.Client> GetAllClients() {
|
---|
84 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
85 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
86 | select Convert.ToDto(c);
|
---|
87 | return query.ToList();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void AddClient(DT.Client client) {
|
---|
92 | string country = string.Empty;
|
---|
93 |
|
---|
94 | OperationContext opContext = OperationContext.Current;
|
---|
95 |
|
---|
96 | if (opContext != null) {
|
---|
97 | MessageProperties properties = opContext.IncomingMessageProperties;
|
---|
98 | RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
|
---|
99 | string ipAdr = endpoint.Address;
|
---|
100 | country = GeoIPLookupService.Instance.GetCountryName(ipAdr);
|
---|
101 | }
|
---|
102 |
|
---|
103 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
104 | DA.Client entity = Convert.ToEntity(client);
|
---|
105 |
|
---|
106 | if (country != string.Empty) {
|
---|
107 | var query = from c in context.GetTable<DA.Country>()
|
---|
108 | where c.Name == country
|
---|
109 | select c;
|
---|
110 | if (query.Count() > 0) {
|
---|
111 | entity.CountryId = query.First().Id;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (entity.OperatingSystem != null) {
|
---|
116 | string osversion = entity.OperatingSystem.Name;
|
---|
117 | var query = from os in context.GetTable<DA.OperatingSystem>()
|
---|
118 | where os.Name == osversion
|
---|
119 | select os;
|
---|
120 | if (query.Count() > 0) {
|
---|
121 | entity.OperatingSystem = query.First();
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (entity.ClientType != null) {
|
---|
126 | string cType = entity.ClientType.Name;
|
---|
127 | var query = from t in context.GetTable<DA.ClientType>()
|
---|
128 | where t.Name == cType
|
---|
129 | select t;
|
---|
130 | if (query.Count() > 0) {
|
---|
131 | entity.ClientType = query.First();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | context.Resources.InsertOnSubmit(entity);
|
---|
136 | context.SubmitChanges();
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void UpdateClient(DT.Client client) {
|
---|
141 | AddClient(client);
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void DeleteClient(DT.Client client) {
|
---|
145 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
146 | //load client because we could get a detached object
|
---|
147 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
148 | where c.Id == client.Id
|
---|
149 | select c;
|
---|
150 | if (query.Count() > 0) {
|
---|
151 |
|
---|
152 | //delete affiliation first
|
---|
153 | var queryMapping = context.ResourceResourceGroups.Where(x => x.ResourceId == client.Id);
|
---|
154 | if (queryMapping.Count() > 0) {
|
---|
155 | context.ResourceResourceGroups.DeleteAllOnSubmit(queryMapping);
|
---|
156 | }
|
---|
157 |
|
---|
158 | context.Resources.DeleteOnSubmit(query.First());
|
---|
159 | context.SubmitChanges();
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | #endregion
|
---|
164 |
|
---|
165 | #region ClientGroup
|
---|
166 | public IEnumerable<DT.ClientGroup> GetAllClientGroups() {
|
---|
167 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
168 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
169 | select Convert.ToDto(c);
|
---|
170 | return query.ToList();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | public IEnumerable<DT.ClientGroup> GetClientGroups(IEnumerable<Guid> ids) {
|
---|
175 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
176 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
177 | where ids.Contains(c.Id)
|
---|
178 | select Convert.ToDto(c);
|
---|
179 | return query.ToList();
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public Guid AddClientGroup(DT.ClientGroup group) {
|
---|
184 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
185 | if (group.Id == Guid.Empty)
|
---|
186 | group.Id = Guid.NewGuid();
|
---|
187 |
|
---|
188 | var entity = Convert.ToEntity(group);
|
---|
189 | context.Resources.InsertOnSubmit(entity);
|
---|
190 | context.SubmitChanges();
|
---|
191 | return entity.Id;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void UpdateClientGroup(DT.ClientGroup group) {
|
---|
196 | AddClientGroup(group);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void DeleteClientGroup(DT.ClientGroup clientGroup) {
|
---|
200 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
201 | //load clientGroup because we could get a detached object
|
---|
202 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
203 | where c.Id == clientGroup.Id
|
---|
204 | select c;
|
---|
205 | if (query.Count() > 0) {
|
---|
206 | context.Resources.DeleteOnSubmit(query.First());
|
---|
207 | context.SubmitChanges();
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void AddResourceToGroup(DT.Resource resource, DT.ClientGroup group) {
|
---|
213 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
214 | DA.ResourceResourceGroup rrg = new DA.ResourceResourceGroup() {
|
---|
215 | ResourceId = resource.Id,
|
---|
216 | ResourceGroupId = group.Id
|
---|
217 | };
|
---|
218 |
|
---|
219 | context.ResourceResourceGroups.InsertOnSubmit(rrg);
|
---|
220 | context.SubmitChanges();
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void RemoveResourceFromGroup(DT.Resource resource, DT.ClientGroup group) {
|
---|
225 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
226 | var query = context.ResourceResourceGroups.Where(x => x.ResourceId == resource.Id && x.ResourceGroupId == group.Id);
|
---|
227 | if (query.Count() > 0) {
|
---|
228 | context.ResourceResourceGroups.DeleteOnSubmit(query.First());
|
---|
229 | context.SubmitChanges();
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | #endregion
|
---|
234 |
|
---|
235 | #region ClientGroupMapping
|
---|
236 | public IEnumerable<DT.ClientGroupMapping> GetClientGroupMapping() {
|
---|
237 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
238 | var query = from c in context.GetTable<DA.ResourceResourceGroup>()
|
---|
239 | select Convert.ToDto(c);
|
---|
240 | return query.ToList();
|
---|
241 | }
|
---|
242 | }
|
---|
243 | #endregion
|
---|
244 |
|
---|
245 | #region Resource
|
---|
246 | public IEnumerable<DT.Resource> GetResources() {
|
---|
247 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
248 | var query = from r in context.Resources
|
---|
249 | select Convert.ToDto(r);
|
---|
250 | return query.ToList();
|
---|
251 | }
|
---|
252 | }
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | #region ClientLog
|
---|
256 | public DT.ClientLog GetLastClientLog(Guid clientId) {
|
---|
257 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
258 | var query = from r in context.ClientLogs
|
---|
259 | where r.ResourceId == clientId
|
---|
260 | select r;
|
---|
261 | return Convert.ToDto(query.OrderBy(x => x.Timestamp).LastOrDefault());
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | public IEnumerable<DT.ClientLog> GetClientLogs(Guid clientId) {
|
---|
266 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
267 | var query = from r in context.ClientLogs
|
---|
268 | where r.ResourceId == clientId
|
---|
269 | select Convert.ToDto(r);
|
---|
270 | return query.ToList();
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | public IEnumerable<DT.ClientLog> GetClientLogsSince(DateTime startDate) {
|
---|
275 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
276 | var query = from r in context.ClientLogs
|
---|
277 | where r.Timestamp >= startDate
|
---|
278 | select Convert.ToDto(r);
|
---|
279 | return query.ToList();
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | public void AddClientLog(DT.ClientLog log) {
|
---|
284 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
285 | context.ClientLogs.InsertOnSubmit(Convert.ToEntity(log));
|
---|
286 | context.SubmitChanges();
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | public void DeleteClientLog(DT.ClientLog log) {
|
---|
291 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
292 | context.ClientLogs.DeleteOnSubmit(Convert.ToEntity(log));
|
---|
293 | context.SubmitChanges();
|
---|
294 | }
|
---|
295 | }
|
---|
296 | #endregion
|
---|
297 |
|
---|
298 | #region User
|
---|
299 | private DT.User BuildUserDto(Guid userId) {
|
---|
300 | DA.aspnet_User aspUser = null;
|
---|
301 | DA.aspnet_Membership aspMembership = null;
|
---|
302 | DA.User accessUser = null;
|
---|
303 |
|
---|
304 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
305 | var userQuery = from u in context.aspnet_Users
|
---|
306 | where u.UserId == userId
|
---|
307 | select u;
|
---|
308 | if (userQuery.Count() == 1) {
|
---|
309 | aspUser = userQuery.First();
|
---|
310 | }
|
---|
311 |
|
---|
312 | var memQuery = from u in context.aspnet_Memberships
|
---|
313 | where u.UserId == userId
|
---|
314 | select u;
|
---|
315 | if (memQuery.Count() == 1) {
|
---|
316 | aspMembership = memQuery.First();
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | if (aspUser == null || aspMembership == null) {
|
---|
321 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
322 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
323 | where u.Id == userId
|
---|
324 | select u;
|
---|
325 | if (query.Count() == 1) {
|
---|
326 | accessUser = query.First();
|
---|
327 | } else {
|
---|
328 | //if the user is not in the access db add it (this makes it easy to upgrade with an existing asp.net authentication db)
|
---|
329 | DA.User user = new DA.User();
|
---|
330 | user.Id = userId;
|
---|
331 | user.FullName = "Not set";
|
---|
332 | context.UserGroupBases.InsertOnSubmit(user);
|
---|
333 | context.SubmitChanges();
|
---|
334 | accessUser = user;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | if (aspUser == null || aspMembership == null || accessUser == null) {
|
---|
340 | throw new Exception("User with id " + userId + " not found.");
|
---|
341 | } else {
|
---|
342 | return Convert.ToDto(accessUser, aspUser, aspMembership);
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | private DT.LightweightUser BuildLightweightUserDto(Guid userId) {
|
---|
347 | DA.aspnet_User aspUser = null;
|
---|
348 | DA.User accessUser = null;
|
---|
349 |
|
---|
350 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
351 | var userQuery = from u in context.aspnet_Users
|
---|
352 | where u.UserId == userId
|
---|
353 | select u;
|
---|
354 | if (userQuery.Count() == 1) {
|
---|
355 | aspUser = userQuery.First();
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (aspUser != null) {
|
---|
360 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
361 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
362 | where u.Id == userId
|
---|
363 | select u;
|
---|
364 | if (query.Count() == 1) {
|
---|
365 | accessUser = query.First();
|
---|
366 | } else {
|
---|
367 | //if the user is not in the access db add it (this makes it easy to upgrade with an existing asp.net authentication db)
|
---|
368 | DA.User user = new DA.User();
|
---|
369 | user.Id = userId;
|
---|
370 | user.FullName = "Not set";
|
---|
371 | context.UserGroupBases.InsertOnSubmit(user);
|
---|
372 | context.SubmitChanges();
|
---|
373 | accessUser = user;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | if (aspUser == null || accessUser == null) {
|
---|
379 | throw new Exception("User with id " + userId + " not found.");
|
---|
380 | } else {
|
---|
381 | return Convert.ToDto(accessUser, aspUser);
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | public DT.LightweightUser Login() {
|
---|
386 | Guid userId = UserManager.CurrentUserId;
|
---|
387 | return BuildLightweightUserDto(userId);
|
---|
388 | }
|
---|
389 |
|
---|
390 | public IEnumerable<DT.UserGroup> GetGroupsOfCurrentUser() {
|
---|
391 | Guid userId = UserManager.CurrentUserId;
|
---|
392 |
|
---|
393 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
394 | //TODO: this has to be done recursive, so check if a group is in another
|
---|
395 | //group because then the user is also in this group...
|
---|
396 | var query = from g in context.UserGroupUserGroups
|
---|
397 | from ug in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
398 | where g.UserGroupId == userId && g.UserGroupUserGroupId == ug.Id
|
---|
399 | select Convert.ToDto(ug);
|
---|
400 | return query.ToList();
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | public IEnumerable<DT.Role> GetRolesOfCurrentUser() {
|
---|
405 | Guid userId = UserManager.CurrentUserId;
|
---|
406 |
|
---|
407 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
408 | var query = from ur in context.aspnet_UsersInRoles
|
---|
409 | from r in context.aspnet_Roles
|
---|
410 | where ur.UserId == userId && ur.RoleId == r.RoleId
|
---|
411 | select Convert.ToDto(r);
|
---|
412 | return query.ToList();
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | public IEnumerable<DT.LightweightUser> GetAllLightweightUsers() {
|
---|
418 | List<Guid> accessUserGuids = null;
|
---|
419 |
|
---|
420 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
421 | var query = from u in context.aspnet_Users
|
---|
422 | select u.UserId;
|
---|
423 | accessUserGuids = query.ToList();
|
---|
424 | }
|
---|
425 | return accessUserGuids.Select(x => BuildLightweightUserDto(x));
|
---|
426 | }
|
---|
427 |
|
---|
428 | public IEnumerable<DT.User> GetAllUsers() {
|
---|
429 | List<Guid> accessUserGuids = null;
|
---|
430 |
|
---|
431 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
432 | var query = from u in context.aspnet_Users
|
---|
433 | select u.UserId;
|
---|
434 | accessUserGuids = query.ToList();
|
---|
435 | }
|
---|
436 |
|
---|
437 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
438 | }
|
---|
439 |
|
---|
440 | public IEnumerable<DT.User> GetUsers(IEnumerable<Guid> ids) {
|
---|
441 | List<Guid> accessUserGuids = null;
|
---|
442 |
|
---|
443 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
444 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
445 | where ids.Contains(u.Id)
|
---|
446 | select u.Id;
|
---|
447 | accessUserGuids = query.ToList();
|
---|
448 | }
|
---|
449 |
|
---|
450 | if (accessUserGuids.Count() != ids.Count()) {
|
---|
451 | throw new Exception("Couldn't find one or more users for the given user ids.");
|
---|
452 | }
|
---|
453 |
|
---|
454 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
455 | }
|
---|
456 |
|
---|
457 | public IEnumerable<DT.LightweightUser> GetLightweightUsers(IEnumerable<Guid> ids) {
|
---|
458 | List<Guid> accessUserGuids = null;
|
---|
459 |
|
---|
460 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
461 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
462 | where ids.Contains(u.Id)
|
---|
463 | select u.Id;
|
---|
464 | accessUserGuids = query.ToList();
|
---|
465 | }
|
---|
466 |
|
---|
467 | if (accessUserGuids.Count() != ids.Count()) {
|
---|
468 | throw new Exception("Couldn't find one or more users for the given user ids.");
|
---|
469 | }
|
---|
470 |
|
---|
471 | return accessUserGuids.Select(x => BuildLightweightUserDto(x));
|
---|
472 | }
|
---|
473 |
|
---|
474 | public DT.User AddUser(DT.User user) {
|
---|
475 | DA.User accessUser;
|
---|
476 | DA.aspnet_User aspUser;
|
---|
477 | DA.aspnet_Membership aspMembership;
|
---|
478 | bool userExistsInASP;
|
---|
479 |
|
---|
480 | Convert.ToEntity(user, out accessUser, out aspUser, out aspMembership, out userExistsInASP);
|
---|
481 |
|
---|
482 | if (userExistsInASP) {
|
---|
483 | if (accessUser.Id == null || accessUser.Id == Guid.Empty) {
|
---|
484 | accessUser.Id = aspMembership.UserId;
|
---|
485 | }
|
---|
486 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
487 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
488 | context.SubmitChanges();
|
---|
489 | }
|
---|
490 | MembershipUser membershipUser = Membership.GetUser((object)accessUser.Id);
|
---|
491 | if (membershipUser != null) {
|
---|
492 | membershipUser.Email = aspMembership.Email;
|
---|
493 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
494 | membershipUser.Comment = aspMembership.Comment;
|
---|
495 | Membership.UpdateUser(membershipUser);
|
---|
496 | }
|
---|
497 | } else {
|
---|
498 | MembershipUser membershipUser = Membership.CreateUser(aspUser.UserName, aspUser.UserName, aspMembership.Email);
|
---|
499 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
500 | membershipUser.Comment = aspMembership.Comment;
|
---|
501 | Membership.UpdateUser(membershipUser);
|
---|
502 |
|
---|
503 | Guid userId = (Guid)membershipUser.ProviderUserKey;
|
---|
504 | accessUser.Id = userId;
|
---|
505 |
|
---|
506 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
507 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
508 | context.SubmitChanges();
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
513 | var newAspUser = context.aspnet_Users.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
514 | var newAspMembership = context.aspnet_Memberships.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
515 | return Convert.ToDto(accessUser, newAspUser, newAspMembership);
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | public void DeleteUser(DT.User user) {
|
---|
520 | if (user.Id != null && user.Id != Guid.Empty) {
|
---|
521 | //delete asp.net user
|
---|
522 | Membership.DeleteUser(user.UserName);
|
---|
523 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
524 | var query = context.UserGroupBases.OfType<DA.User>().Where(x => x.Id == user.Id);
|
---|
525 | if (query.Count() > 0) {
|
---|
526 |
|
---|
527 | //delete affiliation first
|
---|
528 | var queryMapping = context.UserGroupUserGroups.Where(x => x.UserGroupId == user.Id);
|
---|
529 | if (queryMapping.Count() > 0) {
|
---|
530 | context.UserGroupUserGroups.DeleteAllOnSubmit(queryMapping);
|
---|
531 | }
|
---|
532 |
|
---|
533 | //delete user from access db
|
---|
534 | context.UserGroupBases.DeleteOnSubmit(query.First());
|
---|
535 | context.SubmitChanges();
|
---|
536 | }
|
---|
537 | }
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | public void UpdateUser(DT.User user) {
|
---|
542 | AddUser(user);
|
---|
543 | }
|
---|
544 |
|
---|
545 | public void AddUserToRole(DT.Role role, DT.User user) {
|
---|
546 | //TODO: usernames and rolenames have to be unique!
|
---|
547 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
548 | if (msUser != null) {
|
---|
549 | Roles.AddUserToRole(msUser.UserName, role.Name);
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | public void RemoveUserFromRole(DT.Role role, DT.User user) {
|
---|
554 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
555 | if (msUser != null) {
|
---|
556 | Roles.RemoveUserFromRole(msUser.UserName, role.Name);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | public bool ResetPassword(DT.User user, string oldPassword, string newPassword) {
|
---|
561 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
562 | if (msUser != null) {
|
---|
563 | return msUser.ChangePassword(oldPassword, newPassword);
|
---|
564 | }
|
---|
565 | return false;
|
---|
566 | }
|
---|
567 | #endregion
|
---|
568 |
|
---|
569 | #region UserGroup
|
---|
570 | public IEnumerable<DT.UserGroup> GetAllUserGroups() {
|
---|
571 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
572 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
573 | select Convert.ToDto(u);
|
---|
574 | return query.ToList();
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | public IEnumerable<DT.UserGroup> GetUserGroupsOfUser(Guid userId) {
|
---|
579 |
|
---|
580 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
581 | var groupIds = from g in context.UserGroupUserGroups
|
---|
582 | where g.UserGroupId == userId
|
---|
583 | select g.UserGroupUserGroupId;
|
---|
584 |
|
---|
585 | var query = from g in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
586 | where groupIds.Contains(g.Id)
|
---|
587 | select Convert.ToDto(g);
|
---|
588 |
|
---|
589 | return query.ToList();
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | public IEnumerable<DT.UserGroup> GetUserGroups(IEnumerable<Guid> ids) {
|
---|
594 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
595 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
596 | where ids.Contains(u.Id)
|
---|
597 | select Convert.ToDto(u);
|
---|
598 | return query.ToList();
|
---|
599 | }
|
---|
600 | }
|
---|
601 |
|
---|
602 | public Guid AddUserGroup(DT.UserGroup group) {
|
---|
603 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
604 | //because id is not automatically set because of user, we have to do it here manually for group
|
---|
605 | group.Id = Guid.NewGuid();
|
---|
606 | context.UserGroupBases.InsertOnSubmit(Convert.ToEntity(group));
|
---|
607 | context.SubmitChanges();
|
---|
608 | return group.Id;
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | public void UpdateUserGroup(DT.UserGroup group) {
|
---|
613 | AddUserGroup(group);
|
---|
614 | }
|
---|
615 |
|
---|
616 | public void DeleteUserGroup(DT.UserGroup group) {
|
---|
617 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
618 | context.UserGroupBases.DeleteOnSubmit(Convert.ToEntity(group));
|
---|
619 | context.SubmitChanges();
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | public void AddUserGroupBaseToGroup(DT.UserGroupBase resource, DT.UserGroup group) {
|
---|
624 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
625 | DA.UserGroupUserGroup ugug = new DA.UserGroupUserGroup();
|
---|
626 | ugug.UserGroupId = resource.Id;
|
---|
627 | ugug.UserGroupUserGroupId = group.Id;
|
---|
628 | context.UserGroupUserGroups.InsertOnSubmit(ugug);
|
---|
629 | context.SubmitChanges();
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 | public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup userGroup) {
|
---|
634 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
635 | var query = from u in context.UserGroupUserGroups
|
---|
636 | where u.UserGroupId == resource.Id && u.UserGroupUserGroupId == userGroup.Id
|
---|
637 | select u;
|
---|
638 |
|
---|
639 | if (query.Count() == 1) {
|
---|
640 | context.UserGroupUserGroups.DeleteOnSubmit(query.First());
|
---|
641 | context.SubmitChanges();
|
---|
642 | }
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | public IEnumerable<DT.UserGroupBase> GetUsersAndGroups() {
|
---|
647 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
648 | var query = from u in context.UserGroupBases
|
---|
649 | select Convert.ToDto(u);
|
---|
650 | return query.ToList();
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | public IEnumerable<DT.UserGroupMapping> GetUserGroupMapping() {
|
---|
655 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
656 | var query = from u in context.UserGroupUserGroups
|
---|
657 | select Convert.ToDto(u);
|
---|
658 | return query.ToList();
|
---|
659 | }
|
---|
660 | }
|
---|
661 | #endregion
|
---|
662 |
|
---|
663 | #region UserGroupBase
|
---|
664 | public IEnumerable<DT.UserGroupBase> GetAllLeightweightUsersAndGroups() {
|
---|
665 | //TODO: it must be possible to include a role so not all users are returned but only the ones who are allowed to use a certain service
|
---|
666 | List<DT.UserGroup> userGroups = new List<DT.UserGroup>();
|
---|
667 | List<DT.UserGroupBase> result = new List<DT.UserGroupBase>();
|
---|
668 |
|
---|
669 | // this is just for generating users from asp.net authenticaton db; we should maybe provide an updatescript instead
|
---|
670 | List<Guid> accessUserGuids = null;
|
---|
671 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
672 | var query = from u in context.aspnet_Users
|
---|
673 | select u.UserId;
|
---|
674 | accessUserGuids = query.ToList();
|
---|
675 | }
|
---|
676 | var lightweightUsers = accessUserGuids.Select(x => BuildLightweightUserDto(x));
|
---|
677 |
|
---|
678 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
679 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
680 | select Convert.ToDto(u);
|
---|
681 | userGroups = query.ToList();
|
---|
682 | }
|
---|
683 |
|
---|
684 | result.AddRange(lightweightUsers);
|
---|
685 | result.AddRange(userGroups);
|
---|
686 |
|
---|
687 | return result;
|
---|
688 | }
|
---|
689 |
|
---|
690 | public IEnumerable<DT.UserGroupBase> GetLeightweightUsersAndGroups(IEnumerable<Guid> ids) {
|
---|
691 | List<DA.UserGroupBase> dbUserGroupsBases = new List<DA.UserGroupBase>();
|
---|
692 | List<DT.UserGroupBase> result = new List<DT.UserGroupBase>();
|
---|
693 |
|
---|
694 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
695 | var query = from u in context.UserGroupBases
|
---|
696 | where ids.Contains(u.Id)
|
---|
697 | select u;
|
---|
698 | dbUserGroupsBases = query.ToList();
|
---|
699 | }
|
---|
700 |
|
---|
701 | foreach (var ugb in dbUserGroupsBases) {
|
---|
702 | if (ugb.GetType() == typeof(DA.User)) {
|
---|
703 | var user = BuildLightweightUserDto(ugb.Id);
|
---|
704 | result.Add(user);
|
---|
705 | } else if (ugb.GetType() == typeof(DA.UserGroup)) {
|
---|
706 | var group = Convert.ToDto(ugb as DA.UserGroup);
|
---|
707 | result.Add(group);
|
---|
708 | }
|
---|
709 | }
|
---|
710 | return result;
|
---|
711 | }
|
---|
712 | #endregion
|
---|
713 |
|
---|
714 | #region Roles
|
---|
715 | public IEnumerable<DT.Role> GetRoles() {
|
---|
716 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
717 | var query = from u in context.aspnet_Roles
|
---|
718 | select Convert.ToDto(u);
|
---|
719 | return query.ToList();
|
---|
720 | }
|
---|
721 | }
|
---|
722 |
|
---|
723 | public DT.Role AddRole(DT.Role role) {
|
---|
724 | Roles.CreateRole(role.Name);
|
---|
725 | return role;
|
---|
726 | }
|
---|
727 |
|
---|
728 | public void DeleteRole(DT.Role role) {
|
---|
729 | Roles.DeleteRole(role.Name);
|
---|
730 | }
|
---|
731 |
|
---|
732 | public IEnumerable<DT.Role> GetUserRoles(DT.User user) {
|
---|
733 | var roles = Roles.GetRolesForUser(user.UserName);
|
---|
734 | return roles.Select(x => new DT.Role() { Name = x });
|
---|
735 | }
|
---|
736 |
|
---|
737 | public void AddRoleToGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
738 | Guid[] userIds;
|
---|
739 | string[] aspUsers;
|
---|
740 |
|
---|
741 | using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
|
---|
742 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
743 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
744 | select u.UserGroupId).ToArray();
|
---|
745 | }
|
---|
746 |
|
---|
747 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
748 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
749 | where userIds.Contains(u.UserId)
|
---|
750 | select u.UserName).ToArray();
|
---|
751 | }
|
---|
752 |
|
---|
753 | Roles.AddUsersToRole(aspUsers, role.Name);
|
---|
754 |
|
---|
755 | }
|
---|
756 |
|
---|
757 | public void RemoveRoleFromGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
758 | Guid[] userIds;
|
---|
759 | string[] aspUsers;
|
---|
760 |
|
---|
761 | using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
|
---|
762 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
763 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
764 | select u.UserGroupId).ToArray();
|
---|
765 | }
|
---|
766 |
|
---|
767 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
768 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
769 | where userIds.Contains(u.UserId)
|
---|
770 | select u.UserName).ToArray();
|
---|
771 | }
|
---|
772 |
|
---|
773 | Roles.RemoveUsersFromRole(aspUsers.ToArray(), role.Name);
|
---|
774 | }
|
---|
775 | #endregion
|
---|
776 |
|
---|
777 | #region Error Reporting
|
---|
778 | public void ReportError(DT.ClientError error) {
|
---|
779 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
780 | context.ClientErrors.InsertOnSubmit(Convert.ToEntity(error));
|
---|
781 | context.SubmitChanges();
|
---|
782 | }
|
---|
783 | }
|
---|
784 |
|
---|
785 | public IEnumerable<DT.ClientError> GetClientErrors() {
|
---|
786 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
787 | var query = from c in context.ClientErrors
|
---|
788 | select Convert.ToDto(c);
|
---|
789 | return query.ToList();
|
---|
790 | }
|
---|
791 | }
|
---|
792 |
|
---|
793 | public IEnumerable<DT.ClientError> GetLastClientErrors(DateTime startDate) {
|
---|
794 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
795 | var query = from c in context.ClientErrors
|
---|
796 | where c.Timestamp >= startDate
|
---|
797 | select Convert.ToDto(c);
|
---|
798 | return query.ToList();
|
---|
799 | }
|
---|
800 | }
|
---|
801 |
|
---|
802 | public void DeleteError(DT.ClientError error) {
|
---|
803 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
804 | var query = context.ClientErrors.Where(x => x.Id == error.Id);
|
---|
805 | if (query.Count() > 0) {
|
---|
806 | context.ClientErrors.DeleteOnSubmit(query.First());
|
---|
807 | context.SubmitChanges();
|
---|
808 | }
|
---|
809 | }
|
---|
810 | }
|
---|
811 | #endregion
|
---|
812 | }
|
---|
813 | }
|
---|