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.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
281 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
282 | where u.Id == userId
|
---|
283 | select u;
|
---|
284 | if (query.Count() == 1) {
|
---|
285 | accessUser = query.First();
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
290 | var userQuery = from u in context.aspnet_Users
|
---|
291 | where u.UserId == userId
|
---|
292 | select u;
|
---|
293 | if (userQuery.Count() == 1) {
|
---|
294 | aspUser = userQuery.First();
|
---|
295 | }
|
---|
296 |
|
---|
297 | var memQuery = from u in context.aspnet_Memberships
|
---|
298 | where u.UserId == userId
|
---|
299 | select u;
|
---|
300 | if (memQuery.Count() == 1) {
|
---|
301 | aspMembership = memQuery.First();
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | if (aspUser == null || aspMembership == null || accessUser == null) {
|
---|
306 | //TODO: error handling
|
---|
307 | return null;
|
---|
308 | } else {
|
---|
309 | return Convert.ToDto(accessUser, aspUser, aspMembership);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | public DT.User Login() {
|
---|
314 | Guid userId = UserManager.CurrentUserId;
|
---|
315 | return BuildUserDto(userId);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public IEnumerable<DT.User> GetAllUsers() {
|
---|
319 | List<Guid> accessUserGuids = null;
|
---|
320 |
|
---|
321 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
322 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
323 | select u.Id;
|
---|
324 | accessUserGuids = query.ToList();
|
---|
325 | }
|
---|
326 |
|
---|
327 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
328 | }
|
---|
329 |
|
---|
330 | public IEnumerable<DT.User> GetUsers(IEnumerable<Guid> ids) {
|
---|
331 | List<Guid> accessUserGuids = null;
|
---|
332 |
|
---|
333 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
334 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
335 | where ids.Contains(u.Id)
|
---|
336 | select u.Id;
|
---|
337 | accessUserGuids = query.ToList();
|
---|
338 | }
|
---|
339 |
|
---|
340 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
341 | }
|
---|
342 |
|
---|
343 | public DT.User AddUser(DT.User user) {
|
---|
344 | DA.User accessUser;
|
---|
345 | DA.aspnet_User aspUser;
|
---|
346 | DA.aspnet_Membership aspMembership;
|
---|
347 | bool userExistsInASP;
|
---|
348 |
|
---|
349 | Convert.ToEntity(user, out accessUser, out aspUser, out aspMembership, out userExistsInASP);
|
---|
350 |
|
---|
351 | if (userExistsInASP) {
|
---|
352 | if (accessUser.Id == null || accessUser.Id == Guid.Empty) {
|
---|
353 | accessUser.Id = aspMembership.UserId;
|
---|
354 | }
|
---|
355 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
356 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
357 | context.SubmitChanges();
|
---|
358 | }
|
---|
359 | MembershipUser membershipUser = Membership.GetUser((object)accessUser.Id);
|
---|
360 | if (membershipUser != null) {
|
---|
361 | membershipUser.Email = aspMembership.Email;
|
---|
362 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
363 | membershipUser.Comment = aspMembership.Comment;
|
---|
364 | Membership.UpdateUser(membershipUser);
|
---|
365 | }
|
---|
366 | } else {
|
---|
367 | MembershipUser membershipUser = Membership.CreateUser(aspUser.UserName, aspUser.UserName, aspMembership.Email);
|
---|
368 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
369 | membershipUser.Comment = aspMembership.Comment;
|
---|
370 | Membership.UpdateUser(membershipUser);
|
---|
371 |
|
---|
372 | Guid userId = (Guid)membershipUser.ProviderUserKey;
|
---|
373 | accessUser.Id = userId;
|
---|
374 |
|
---|
375 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
376 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
377 | context.SubmitChanges();
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
382 | var newAspUser = context.aspnet_Users.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
383 | var newAspMembership = context.aspnet_Memberships.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
384 | return Convert.ToDto(accessUser, newAspUser, newAspMembership);
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | public void DeleteUser(DT.User user) {
|
---|
389 | if (user.Id != null && user.Id != Guid.Empty) {
|
---|
390 | //delete asp.net user
|
---|
391 | Membership.DeleteUser(user.UserName);
|
---|
392 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
393 | var query = context.UserGroupBases.OfType<DA.User>().Where(x => x.Id == user.Id);
|
---|
394 | if (query.Count() > 0) {
|
---|
395 |
|
---|
396 | //delete affiliation first
|
---|
397 | var queryMapping = context.UserGroupUserGroups.Where(x => x.UserGroupId == user.Id);
|
---|
398 | if (queryMapping.Count() > 0) {
|
---|
399 | context.UserGroupUserGroups.DeleteAllOnSubmit(queryMapping);
|
---|
400 | }
|
---|
401 |
|
---|
402 | //delete user from access db
|
---|
403 | context.UserGroupBases.DeleteOnSubmit(query.First());
|
---|
404 | context.SubmitChanges();
|
---|
405 | }
|
---|
406 | }
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | public void UpdateUser(DT.User user) {
|
---|
411 | AddUser(user);
|
---|
412 | }
|
---|
413 |
|
---|
414 | public void AddUserToRole(DT.Role role, DT.User user) {
|
---|
415 | //TODO: usernames and rolenames have to be unique!
|
---|
416 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
417 | if (msUser != null) {
|
---|
418 | Roles.AddUserToRole(msUser.UserName, role.Name);
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | public void RemoveUserFromRole(DT.Role role, DT.User user) {
|
---|
423 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
424 | if (msUser != null) {
|
---|
425 | Roles.RemoveUserFromRole(msUser.UserName, role.Name);
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | public bool ResetPassword(DT.User user, string oldPassword, string newPassword) {
|
---|
430 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
431 | if (msUser != null) {
|
---|
432 | return msUser.ChangePassword(oldPassword, newPassword);
|
---|
433 | }
|
---|
434 | return false;
|
---|
435 | }
|
---|
436 | #endregion
|
---|
437 |
|
---|
438 | #region UserGroup
|
---|
439 | public IEnumerable<DT.UserGroup> GetAllUserGroups() {
|
---|
440 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
441 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
442 | select Convert.ToDto(u);
|
---|
443 | return query.ToList();
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | public IEnumerable<DT.UserGroup> GetUserGroups(IEnumerable<Guid> ids) {
|
---|
448 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
449 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
450 | where ids.Contains(u.Id)
|
---|
451 | select Convert.ToDto(u);
|
---|
452 | return query.ToList();
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | public Guid AddUserGroup(DT.UserGroup group) {
|
---|
457 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
458 | //because id is not automatically set because of user, we have to do it here manually for group
|
---|
459 | group.Id = Guid.NewGuid();
|
---|
460 | context.UserGroupBases.InsertOnSubmit(Convert.ToEntity(group));
|
---|
461 | context.SubmitChanges();
|
---|
462 | return group.Id;
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | public void UpdateUserGroup(DT.UserGroup group) {
|
---|
467 | AddUserGroup(group);
|
---|
468 | }
|
---|
469 |
|
---|
470 | public void DeleteUserGroup(DT.UserGroup group) {
|
---|
471 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
472 | context.UserGroupBases.DeleteOnSubmit(Convert.ToEntity(group));
|
---|
473 | context.SubmitChanges();
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | public void AddUserGroupBaseToGroup(DT.UserGroupBase resource, DT.UserGroup group) {
|
---|
478 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
479 | DA.UserGroupUserGroup ugug = new DA.UserGroupUserGroup();
|
---|
480 | ugug.UserGroupId = resource.Id;
|
---|
481 | ugug.UserGroupUserGroupId = group.Id;
|
---|
482 | context.UserGroupUserGroups.InsertOnSubmit(ugug);
|
---|
483 | context.SubmitChanges();
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup userGroup) {
|
---|
488 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
489 | var query = from u in context.UserGroupUserGroups
|
---|
490 | where u.UserGroupId == resource.Id && u.UserGroupUserGroupId == userGroup.Id
|
---|
491 | select u;
|
---|
492 |
|
---|
493 | if (query.Count() == 1) {
|
---|
494 | context.UserGroupUserGroups.DeleteOnSubmit(query.First());
|
---|
495 | context.SubmitChanges();
|
---|
496 | }
|
---|
497 | }
|
---|
498 | }
|
---|
499 |
|
---|
500 | public IEnumerable<DT.UserGroupBase> GetUsersAndGroups() {
|
---|
501 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
502 | var query = from u in context.UserGroupBases
|
---|
503 | select Convert.ToDto(u);
|
---|
504 | return query.ToList();
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | public IEnumerable<DT.UserGroupMapping> GetUserGroupMapping() {
|
---|
509 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
510 | var query = from u in context.UserGroupUserGroups
|
---|
511 | select Convert.ToDto(u);
|
---|
512 | return query.ToList();
|
---|
513 | }
|
---|
514 | }
|
---|
515 | #endregion
|
---|
516 |
|
---|
517 | #region Roles
|
---|
518 | public IEnumerable<DT.Role> GetRoles() {
|
---|
519 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
520 | var query = from u in context.aspnet_Roles
|
---|
521 | select Convert.ToDto(u);
|
---|
522 | return query.ToList();
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | public DT.Role AddRole(DT.Role role) {
|
---|
527 | Roles.CreateRole(role.Name);
|
---|
528 | return role;
|
---|
529 | }
|
---|
530 |
|
---|
531 | public void DeleteRole(DT.Role role) {
|
---|
532 | Roles.DeleteRole(role.Name);
|
---|
533 | }
|
---|
534 |
|
---|
535 | public IEnumerable<DT.Role> GetUserRoles(DT.User user) {
|
---|
536 | var roles = Roles.GetRolesForUser(user.UserName);
|
---|
537 | return roles.Select(x => new DT.Role() { Name = x });
|
---|
538 | }
|
---|
539 |
|
---|
540 | public void AddRoleToGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
541 | Guid[] userIds;
|
---|
542 | string[] aspUsers;
|
---|
543 |
|
---|
544 | using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
|
---|
545 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
546 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
547 | select u.UserGroupId).ToArray();
|
---|
548 | }
|
---|
549 |
|
---|
550 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
551 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
552 | where userIds.Contains(u.UserId)
|
---|
553 | select u.UserName).ToArray();
|
---|
554 | }
|
---|
555 |
|
---|
556 | Roles.AddUsersToRole(aspUsers, role.Name);
|
---|
557 |
|
---|
558 | }
|
---|
559 |
|
---|
560 | public void RemoveRoleFromGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
561 | Guid[] userIds;
|
---|
562 | string[] aspUsers;
|
---|
563 |
|
---|
564 | using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
|
---|
565 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
566 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
567 | select u.UserGroupId).ToArray();
|
---|
568 | }
|
---|
569 |
|
---|
570 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
571 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
572 | where userIds.Contains(u.UserId)
|
---|
573 | select u.UserName).ToArray();
|
---|
574 | }
|
---|
575 |
|
---|
576 | Roles.RemoveUsersFromRole(aspUsers.ToArray(), role.Name);
|
---|
577 | }
|
---|
578 | #endregion
|
---|
579 |
|
---|
580 | #region Error Reporting
|
---|
581 | public void ReportError(DT.ClientError error) {
|
---|
582 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
583 | context.ClientErrors.InsertOnSubmit(Convert.ToEntity(error));
|
---|
584 | context.SubmitChanges();
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | public IEnumerable<DT.ClientError> GetClientErrors() {
|
---|
589 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
590 | var query = from c in context.ClientErrors
|
---|
591 | select Convert.ToDto(c);
|
---|
592 | return query.ToList();
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | public IEnumerable<DT.ClientError> GetLastClientErrors(DateTime startDate) {
|
---|
597 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
598 | var query = from c in context.ClientErrors
|
---|
599 | where c.Timestamp >= startDate
|
---|
600 | select Convert.ToDto(c);
|
---|
601 | return query.ToList();
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | public void DeleteError(DT.ClientError error) {
|
---|
606 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
607 | var query = context.ClientErrors.Where(x => x.Id == error.Id);
|
---|
608 | if (query.Count() > 0) {
|
---|
609 | context.ClientErrors.DeleteOnSubmit(query.First());
|
---|
610 | context.SubmitChanges();
|
---|
611 | }
|
---|
612 | }
|
---|
613 | }
|
---|
614 | #endregion
|
---|
615 | }
|
---|
616 | }
|
---|