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