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