1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Web.Security;
|
---|
27 | using DA = HeuristicLab.Services.Access.DataAccess;
|
---|
28 | using DT = HeuristicLab.Services.Access.DataTransfer;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Services.Access {
|
---|
31 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
|
---|
32 | public class AccessService : IAccessService {
|
---|
33 | private IUserManager userManager;
|
---|
34 | private IUserManager UserManager {
|
---|
35 | get {
|
---|
36 | if (userManager == null) userManager = new UserManager();
|
---|
37 | return userManager;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | #region Client Members
|
---|
42 | public bool ClientExists(Guid id) {
|
---|
43 | if (id != Guid.Empty) {
|
---|
44 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
45 | return (context.Resources.Where(x => x.Id == id).Count() != 0);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | return false;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public DT.Client GetClient(Guid id) {
|
---|
52 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
53 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
54 | where c.Id == id
|
---|
55 | select c;
|
---|
56 |
|
---|
57 | return Convert.ToDto(query.FirstOrDefault());
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public IEnumerable<DT.Client> GetClients(IEnumerable<Guid> ids) {
|
---|
62 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
63 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
64 | where ids.Contains(c.Id)
|
---|
65 | select Convert.ToDto(c);
|
---|
66 | return query.ToList();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public IEnumerable<DT.Client> GetAllClients() {
|
---|
71 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
72 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
73 | select Convert.ToDto(c);
|
---|
74 | return query.ToList();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public Guid AddClient(DT.Client client) {
|
---|
79 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
80 | DA.Client entity = Convert.ToEntity(client);
|
---|
81 | context.Resources.InsertOnSubmit(entity);
|
---|
82 | context.SubmitChanges();
|
---|
83 | return entity.Id;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void UpdateClient(DT.Client client) {
|
---|
88 | AddClient(client);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void DeleteClient(DT.Client client) {
|
---|
92 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
93 | //load client because we could get a detached object
|
---|
94 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
95 | where c.Id == client.Id
|
---|
96 | select c;
|
---|
97 | if (query.Count() > 0) {
|
---|
98 |
|
---|
99 | //delete affiliation first
|
---|
100 | var queryMapping = context.ResourceResourceGroups.Where(x => x.ResourceId == client.Id);
|
---|
101 | if (queryMapping.Count() > 0) {
|
---|
102 | context.ResourceResourceGroups.DeleteAllOnSubmit(queryMapping);
|
---|
103 | }
|
---|
104 |
|
---|
105 | context.Resources.DeleteOnSubmit(query.First());
|
---|
106 | context.SubmitChanges();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region ClientGroup
|
---|
113 | public IEnumerable<DT.ClientGroup> GetAllClientGroups() {
|
---|
114 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
115 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
116 | select Convert.ToDto(c);
|
---|
117 | return query.ToList();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public IEnumerable<DT.ClientGroup> GetClientGroups(IEnumerable<Guid> ids) {
|
---|
122 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
123 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
124 | where ids.Contains(c.Id)
|
---|
125 | select Convert.ToDto(c);
|
---|
126 | return query.ToList();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public Guid AddClientGroup(DT.ClientGroup group) {
|
---|
131 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
132 | var entity = Convert.ToEntity(group);
|
---|
133 | context.Resources.InsertOnSubmit(entity);
|
---|
134 | context.SubmitChanges();
|
---|
135 | return entity.Id;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void UpdateClientGroup(DT.ClientGroup group) {
|
---|
140 | AddClientGroup(group);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void DeleteClientGroup(DT.ClientGroup clientGroup) {
|
---|
144 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
145 | //load clientGroup because we could get a detached object
|
---|
146 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
147 | where c.Id == clientGroup.Id
|
---|
148 | select c;
|
---|
149 | if (query.Count() > 0) {
|
---|
150 | context.Resources.DeleteOnSubmit(query.First());
|
---|
151 | context.SubmitChanges();
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void AddResourceToGroup(DT.Resource resource, DT.ClientGroup group) {
|
---|
157 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
158 | DA.ResourceResourceGroup rrg = new DA.ResourceResourceGroup() {
|
---|
159 | ResourceId = resource.Id,
|
---|
160 | ResourceGroupId = group.Id
|
---|
161 | };
|
---|
162 |
|
---|
163 | context.ResourceResourceGroups.InsertOnSubmit(rrg);
|
---|
164 | context.SubmitChanges();
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void RemoveResourceFromGroup(DT.Resource resource, DT.ClientGroup group) {
|
---|
169 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
170 | var query = context.ResourceResourceGroups.Where(x => x.ResourceId == resource.Id && x.ResourceGroupId == group.Id);
|
---|
171 | if (query.Count() > 0) {
|
---|
172 | context.ResourceResourceGroups.DeleteOnSubmit(query.First());
|
---|
173 | context.SubmitChanges();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 |
|
---|
179 | #region ClientGroupMapping
|
---|
180 | public IEnumerable<DT.ClientGroupMapping> GetClientGroupMapping() {
|
---|
181 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
182 | var query = from c in context.GetTable<DA.ResourceResourceGroup>()
|
---|
183 | select Convert.ToDto(c);
|
---|
184 | return query.ToList();
|
---|
185 | }
|
---|
186 | }
|
---|
187 | #endregion
|
---|
188 |
|
---|
189 | #region Resource
|
---|
190 | public IEnumerable<DT.Resource> GetResources() {
|
---|
191 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
192 | var query = from r in context.Resources
|
---|
193 | select Convert.ToDto(r);
|
---|
194 | return query.ToList();
|
---|
195 | }
|
---|
196 | }
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | #region ClientLog
|
---|
200 | public DT.ClientLog GetLastClientLog(Guid clientId) {
|
---|
201 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
202 | var query = from r in context.ClientLogs
|
---|
203 | where r.ResourceId == clientId
|
---|
204 | select r;
|
---|
205 | return Convert.ToDto(query.OrderBy(x => x.Timestamp).LastOrDefault());
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | public IEnumerable<DT.ClientLog> GetClientLogs(Guid clientId) {
|
---|
210 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
211 | var query = from r in context.ClientLogs
|
---|
212 | where r.ResourceId == clientId
|
---|
213 | select Convert.ToDto(r);
|
---|
214 | return query.ToList();
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | public IEnumerable<DT.ClientLog> GetClientLogsSince(DateTime startDate) {
|
---|
219 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
220 | var query = from r in context.ClientLogs
|
---|
221 | where r.Timestamp >= startDate
|
---|
222 | select Convert.ToDto(r);
|
---|
223 | return query.ToList();
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void AddClientLog(DT.ClientLog log) {
|
---|
228 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
229 | context.ClientLogs.InsertOnSubmit(Convert.ToEntity(log));
|
---|
230 | context.SubmitChanges();
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | public void DeleteClientLog(DT.ClientLog log) {
|
---|
235 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
236 | context.ClientLogs.DeleteOnSubmit(Convert.ToEntity(log));
|
---|
237 | context.SubmitChanges();
|
---|
238 | }
|
---|
239 | }
|
---|
240 | #endregion
|
---|
241 |
|
---|
242 | #region User
|
---|
243 | private DT.User BuildUserDto(Guid userId) {
|
---|
244 | DA.aspnet_User aspUser = null;
|
---|
245 | DA.aspnet_Membership aspMembership = null;
|
---|
246 | DA.User accessUser = null;
|
---|
247 |
|
---|
248 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
249 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
250 | where u.Id == userId
|
---|
251 | select u;
|
---|
252 | if (query.Count() == 1) {
|
---|
253 | accessUser = query.First();
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
258 | var userQuery = from u in context.aspnet_Users
|
---|
259 | where u.UserId == userId
|
---|
260 | select u;
|
---|
261 | if (userQuery.Count() == 1) {
|
---|
262 | aspUser = userQuery.First();
|
---|
263 | }
|
---|
264 |
|
---|
265 | var memQuery = from u in context.aspnet_Memberships
|
---|
266 | where u.UserId == userId
|
---|
267 | select u;
|
---|
268 | if (memQuery.Count() == 1) {
|
---|
269 | aspMembership = memQuery.First();
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (aspUser == null || aspMembership == null || accessUser == null) {
|
---|
274 | //TODO: error handling
|
---|
275 | return null;
|
---|
276 | } else {
|
---|
277 | return Convert.ToDto(accessUser, aspUser, aspMembership);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | public DT.User Login() {
|
---|
282 | Guid userId = UserManager.CurrentUserId;
|
---|
283 | return BuildUserDto(userId);
|
---|
284 | }
|
---|
285 |
|
---|
286 | public IEnumerable<DT.User> GetAllUsers() {
|
---|
287 | List<Guid> accessUserGuids = null;
|
---|
288 |
|
---|
289 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
290 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
291 | select u.Id;
|
---|
292 | accessUserGuids = query.ToList();
|
---|
293 | }
|
---|
294 |
|
---|
295 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
296 | }
|
---|
297 |
|
---|
298 | public IEnumerable<DT.User> GetUsers(IEnumerable<Guid> ids) {
|
---|
299 | List<Guid> accessUserGuids = null;
|
---|
300 |
|
---|
301 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
302 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
303 | where ids.Contains(u.Id)
|
---|
304 | select u.Id;
|
---|
305 | accessUserGuids = query.ToList();
|
---|
306 | }
|
---|
307 |
|
---|
308 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
309 | }
|
---|
310 |
|
---|
311 | public DT.User AddUser(DT.User user) {
|
---|
312 | DA.User accessUser;
|
---|
313 | DA.aspnet_User aspUser;
|
---|
314 | DA.aspnet_Membership aspMembership;
|
---|
315 | bool userExistsInASP;
|
---|
316 |
|
---|
317 | Convert.ToEntity(user, out accessUser, out aspUser, out aspMembership, out userExistsInASP);
|
---|
318 |
|
---|
319 | if (userExistsInASP) {
|
---|
320 | if (accessUser.Id == null || accessUser.Id == Guid.Empty) {
|
---|
321 | accessUser.Id = aspMembership.UserId;
|
---|
322 | }
|
---|
323 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
324 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
325 | context.SubmitChanges();
|
---|
326 | }
|
---|
327 | MembershipUser membershipUser = Membership.GetUser((object)accessUser.Id);
|
---|
328 | if (membershipUser != null) {
|
---|
329 | membershipUser.Email = aspMembership.Email;
|
---|
330 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
331 | membershipUser.Comment = aspMembership.Comment;
|
---|
332 | Membership.UpdateUser(membershipUser);
|
---|
333 | }
|
---|
334 | } else {
|
---|
335 | MembershipUser membershipUser = Membership.CreateUser(aspUser.UserName, aspUser.UserName, aspMembership.Email);
|
---|
336 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
337 | membershipUser.Comment = aspMembership.Comment;
|
---|
338 | Membership.UpdateUser(membershipUser);
|
---|
339 |
|
---|
340 | Guid userId = (Guid)membershipUser.ProviderUserKey;
|
---|
341 | accessUser.Id = userId;
|
---|
342 |
|
---|
343 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
344 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
345 | context.SubmitChanges();
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
350 | var newAspUser = context.aspnet_Users.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
351 | var newAspMembership = context.aspnet_Memberships.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
352 | return Convert.ToDto(accessUser, newAspUser, newAspMembership);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | public void DeleteUser(DT.User user) {
|
---|
357 | if (user.Id != null && user.Id != Guid.Empty) {
|
---|
358 | //delete asp.net user
|
---|
359 | Membership.DeleteUser(user.UserName);
|
---|
360 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
361 | var query = context.UserGroupBases.OfType<DA.User>().Where(x => x.Id == user.Id);
|
---|
362 | if (query.Count() > 0) {
|
---|
363 |
|
---|
364 | //delete affiliation first
|
---|
365 | var queryMapping = context.UserGroupUserGroups.Where(x => x.UserGroupId == user.Id);
|
---|
366 | if (queryMapping.Count() > 0) {
|
---|
367 | context.UserGroupUserGroups.DeleteAllOnSubmit(queryMapping);
|
---|
368 | }
|
---|
369 |
|
---|
370 | //delete user from access db
|
---|
371 | context.UserGroupBases.DeleteOnSubmit(query.First());
|
---|
372 | context.SubmitChanges();
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | public void UpdateUser(DT.User user) {
|
---|
379 | AddUser(user);
|
---|
380 | }
|
---|
381 |
|
---|
382 | public void AddUserToRole(DT.Role role, DT.User user) {
|
---|
383 | //TODO: usernames and rolenames have to be unique!
|
---|
384 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
385 | if (msUser != null) {
|
---|
386 | Roles.AddUserToRole(msUser.UserName, role.Name);
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | public void RemoveUserFromRole(DT.Role role, DT.User user) {
|
---|
391 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
392 | if (msUser != null) {
|
---|
393 | Roles.RemoveUserFromRole(msUser.UserName, role.Name);
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | public bool ResetPassword(DT.User user, string oldPassword, string newPassword) {
|
---|
398 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
399 | if (msUser != null) {
|
---|
400 | return msUser.ChangePassword(oldPassword, newPassword);
|
---|
401 | }
|
---|
402 | return false;
|
---|
403 | }
|
---|
404 | #endregion
|
---|
405 |
|
---|
406 | #region UserGroup
|
---|
407 | public IEnumerable<DT.UserGroup> GetAllUserGroups() {
|
---|
408 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
409 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
410 | select Convert.ToDto(u);
|
---|
411 | return query.ToList();
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 | public IEnumerable<DT.UserGroup> GetUserGroups(IEnumerable<Guid> ids) {
|
---|
416 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
417 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
418 | where ids.Contains(u.Id)
|
---|
419 | select Convert.ToDto(u);
|
---|
420 | return query.ToList();
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | public Guid AddUserGroup(DT.UserGroup group) {
|
---|
425 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
426 | //because id is not automatically set because of user, we have to do it here manually for group
|
---|
427 | group.Id = Guid.NewGuid();
|
---|
428 | context.UserGroupBases.InsertOnSubmit(Convert.ToEntity(group));
|
---|
429 | context.SubmitChanges();
|
---|
430 | return group.Id;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | public void UpdateUserGroup(DT.UserGroup group) {
|
---|
435 | AddUserGroup(group);
|
---|
436 | }
|
---|
437 |
|
---|
438 | public void DeleteUserGroup(DT.UserGroup group) {
|
---|
439 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
440 | context.UserGroupBases.DeleteOnSubmit(Convert.ToEntity(group));
|
---|
441 | context.SubmitChanges();
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | public void AddUserGroupBaseToGroup(DT.UserGroupBase resource, DT.UserGroup group) {
|
---|
446 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
447 | DA.UserGroupUserGroup ugug = new DA.UserGroupUserGroup();
|
---|
448 | ugug.UserGroupId = resource.Id;
|
---|
449 | ugug.UserGroupUserGroupId = group.Id;
|
---|
450 | context.UserGroupUserGroups.InsertOnSubmit(ugug);
|
---|
451 | context.SubmitChanges();
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup userGroup) {
|
---|
456 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
457 | var query = from u in context.UserGroupUserGroups
|
---|
458 | where u.UserGroupId == resource.Id && u.UserGroupUserGroupId == userGroup.Id
|
---|
459 | select u;
|
---|
460 |
|
---|
461 | if (query.Count() == 1) {
|
---|
462 | context.UserGroupUserGroups.DeleteOnSubmit(query.First());
|
---|
463 | context.SubmitChanges();
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | public IEnumerable<DT.UserGroupBase> GetUsersAndGroups() {
|
---|
469 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
470 | var query = from u in context.UserGroupBases
|
---|
471 | select Convert.ToDto(u);
|
---|
472 | return query.ToList();
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | public IEnumerable<DT.UserGroupMapping> GetUserGroupMapping() {
|
---|
477 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
478 | var query = from u in context.UserGroupUserGroups
|
---|
479 | select Convert.ToDto(u);
|
---|
480 | return query.ToList();
|
---|
481 | }
|
---|
482 | }
|
---|
483 | #endregion
|
---|
484 |
|
---|
485 | #region Roles
|
---|
486 | public IEnumerable<DT.Role> GetRoles() {
|
---|
487 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
488 | var query = from u in context.aspnet_Roles
|
---|
489 | select Convert.ToDto(u);
|
---|
490 | return query.ToList();
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | public DT.Role AddRole(DT.Role role) {
|
---|
495 | Roles.CreateRole(role.Name);
|
---|
496 | return role;
|
---|
497 | }
|
---|
498 |
|
---|
499 | public void DeleteRole(DT.Role role) {
|
---|
500 | Roles.DeleteRole(role.Name);
|
---|
501 | }
|
---|
502 |
|
---|
503 | public IEnumerable<DT.Role> GetUserRoles(DT.User user) {
|
---|
504 | var roles = Roles.GetRolesForUser(user.UserName);
|
---|
505 | return roles.Select(x => new DT.Role() { Name = x });
|
---|
506 | }
|
---|
507 |
|
---|
508 | public void AddRoleToGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
509 | Guid[] userIds;
|
---|
510 | string[] aspUsers;
|
---|
511 |
|
---|
512 | using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
|
---|
513 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
514 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
515 | select u.UserGroupId).ToArray();
|
---|
516 | }
|
---|
517 |
|
---|
518 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
519 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
520 | where userIds.Contains(u.UserId)
|
---|
521 | select u.UserName).ToArray();
|
---|
522 | }
|
---|
523 |
|
---|
524 | Roles.AddUsersToRole(aspUsers, role.Name);
|
---|
525 |
|
---|
526 | }
|
---|
527 |
|
---|
528 | public void RemoveRoleFromGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
529 | Guid[] userIds;
|
---|
530 | string[] aspUsers;
|
---|
531 |
|
---|
532 | using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
|
---|
533 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
534 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
535 | select u.UserGroupId).ToArray();
|
---|
536 | }
|
---|
537 |
|
---|
538 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
539 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
540 | where userIds.Contains(u.UserId)
|
---|
541 | select u.UserName).ToArray();
|
---|
542 | }
|
---|
543 |
|
---|
544 | Roles.RemoveUsersFromRole(aspUsers.ToArray(), role.Name);
|
---|
545 | }
|
---|
546 | #endregion
|
---|
547 |
|
---|
548 | #region Error Reporting
|
---|
549 | public void ReportError(DT.ClientError error) {
|
---|
550 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
551 | context.ClientErrors.InsertOnSubmit(Convert.ToEntity(error));
|
---|
552 | context.SubmitChanges();
|
---|
553 | }
|
---|
554 | }
|
---|
555 |
|
---|
556 | public IEnumerable<DT.ClientError> GetClientErrors() {
|
---|
557 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
558 | var query = from c in context.ClientErrors
|
---|
559 | select Convert.ToDto(c);
|
---|
560 | return query.ToList();
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 | public IEnumerable<DT.ClientError> GetLastClientErrors(DateTime startDate) {
|
---|
565 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
566 | var query = from c in context.ClientErrors
|
---|
567 | where c.Timestamp >= startDate
|
---|
568 | select Convert.ToDto(c);
|
---|
569 | return query.ToList();
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | public void DeleteError(DT.ClientError error) {
|
---|
574 | using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
|
---|
575 | var query = context.ClientErrors.Where(x => x.Id == error.Id);
|
---|
576 | if (query.Count() > 0) {
|
---|
577 | context.ClientErrors.DeleteOnSubmit(query.First());
|
---|
578 | context.SubmitChanges();
|
---|
579 | }
|
---|
580 | }
|
---|
581 | }
|
---|
582 | #endregion
|
---|
583 | }
|
---|
584 | }
|
---|