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