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