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