[8040] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11185] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8040] | 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 = AccessServiceLocator.Instance.UserManager;
|
---|
| 39 | return userManager;
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | private IRoleVerifier roleVerifier;
|
---|
| 44 | private IRoleVerifier RoleVerifier {
|
---|
| 45 | get {
|
---|
| 46 | if (roleVerifier == null) roleVerifier = AccessServiceLocator.Instance.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.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 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.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 63 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
| 64 | where c.Id == id
|
---|
| 65 | select c;
|
---|
| 66 | if (query.Count() > 0) {
|
---|
| 67 | return Convert.ToDto(query.FirstOrDefault());
|
---|
| 68 | } else {
|
---|
| 69 | return null;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public IEnumerable<DT.Client> GetClients(IEnumerable<Guid> ids) {
|
---|
| 75 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 76 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
| 77 | where ids.Contains(c.Id)
|
---|
| 78 | select Convert.ToDto(c);
|
---|
| 79 | return query.ToList();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public IEnumerable<DT.Client> GetAllClients() {
|
---|
| 84 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 85 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
| 86 | select Convert.ToDto(c);
|
---|
| 87 | return query.ToList();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void AddClient(DT.Client client) {
|
---|
| 92 | string country = string.Empty;
|
---|
| 93 |
|
---|
| 94 | OperationContext opContext = OperationContext.Current;
|
---|
| 95 |
|
---|
| 96 | if (opContext != null) {
|
---|
| 97 | MessageProperties properties = opContext.IncomingMessageProperties;
|
---|
| 98 | RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
|
---|
| 99 | string ipAdr = endpoint.Address;
|
---|
| 100 | country = GeoIPLookupService.Instance.GetCountryName(ipAdr);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 104 | DA.Client entity = Convert.ToEntity(client);
|
---|
| 105 |
|
---|
| 106 | if (country != string.Empty) {
|
---|
| 107 | var query = from c in context.GetTable<DA.Country>()
|
---|
| 108 | where c.Name == country
|
---|
| 109 | select c;
|
---|
| 110 | if (query.Count() > 0) {
|
---|
| 111 | entity.CountryId = query.First().Id;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (entity.OperatingSystem != null) {
|
---|
| 116 | string osversion = entity.OperatingSystem.Name;
|
---|
| 117 | var query = from os in context.GetTable<DA.OperatingSystem>()
|
---|
| 118 | where os.Name == osversion
|
---|
| 119 | select os;
|
---|
| 120 | if (query.Count() > 0) {
|
---|
| 121 | entity.OperatingSystem = query.First();
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | if (entity.ClientType != null) {
|
---|
| 126 | string cType = entity.ClientType.Name;
|
---|
| 127 | var query = from t in context.GetTable<DA.ClientType>()
|
---|
| 128 | where t.Name == cType
|
---|
| 129 | select t;
|
---|
| 130 | if (query.Count() > 0) {
|
---|
| 131 | entity.ClientType = query.First();
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | context.Resources.InsertOnSubmit(entity);
|
---|
| 136 | context.SubmitChanges();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | public void UpdateClient(DT.Client client) {
|
---|
| 141 | string country = string.Empty;
|
---|
| 142 |
|
---|
| 143 | OperationContext opContext = OperationContext.Current;
|
---|
| 144 |
|
---|
| 145 | if (opContext != null) {
|
---|
| 146 | MessageProperties properties = opContext.IncomingMessageProperties;
|
---|
| 147 | RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
|
---|
| 148 | string ipAdr = endpoint.Address;
|
---|
| 149 | country = GeoIPLookupService.Instance.GetCountryName(ipAdr);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 153 | var query = from c in context.Resources.OfType<DA.Client>()
|
---|
| 154 | where c.Id == client.Id
|
---|
| 155 | select c;
|
---|
| 156 |
|
---|
| 157 | if (query.Count() > 0) {
|
---|
| 158 | var entity = query.First();
|
---|
| 159 |
|
---|
| 160 | if (country != string.Empty) {
|
---|
| 161 | var countryQuery = from c in context.GetTable<DA.Country>()
|
---|
| 162 | where c.Name == country
|
---|
| 163 | select c;
|
---|
| 164 | if (countryQuery.Count() > 0) {
|
---|
| 165 | entity.CountryId = countryQuery.First().Id;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | entity.Name = client.Name;
|
---|
| 170 | entity.Description = client.Description;
|
---|
| 171 | entity.HeuristicLabVersion = client.HeuristicLabVersion;
|
---|
| 172 | entity.Timestamp = DateTime.Now;
|
---|
| 173 |
|
---|
| 174 | context.SubmitChanges();
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | public void DeleteClient(DT.Client client) {
|
---|
| 180 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 181 |
|
---|
| 182 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 183 | //load client because we could get a detached object
|
---|
| 184 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
|
---|
| 185 | where c.Id == client.Id
|
---|
| 186 | select c;
|
---|
| 187 | if (query.Count() > 0) {
|
---|
| 188 |
|
---|
| 189 | //delete affiliation first
|
---|
| 190 | var queryMapping = context.ResourceResourceGroups.Where(x => x.ResourceId == client.Id);
|
---|
| 191 | if (queryMapping.Count() > 0) {
|
---|
| 192 | context.ResourceResourceGroups.DeleteAllOnSubmit(queryMapping);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | context.Resources.DeleteOnSubmit(query.First());
|
---|
| 196 | context.SubmitChanges();
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | #endregion
|
---|
| 201 |
|
---|
| 202 | #region ClientGroup
|
---|
| 203 | public IEnumerable<DT.ClientGroup> GetAllClientGroups() {
|
---|
| 204 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 205 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
| 206 | select Convert.ToDto(c);
|
---|
| 207 | return query.ToList();
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | public IEnumerable<DT.ClientGroup> GetClientGroups(IEnumerable<Guid> ids) {
|
---|
| 212 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 213 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
| 214 | where ids.Contains(c.Id)
|
---|
| 215 | select Convert.ToDto(c);
|
---|
| 216 | return query.ToList();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | public Guid AddClientGroup(DT.ClientGroup group) {
|
---|
| 221 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 222 | if (group.Id == Guid.Empty)
|
---|
| 223 | group.Id = Guid.NewGuid();
|
---|
| 224 |
|
---|
| 225 | var entity = Convert.ToEntity(group);
|
---|
| 226 | context.Resources.InsertOnSubmit(entity);
|
---|
| 227 | context.SubmitChanges();
|
---|
| 228 | return entity.Id;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | public void UpdateClientGroup(DT.ClientGroup clientGroup) {
|
---|
| 233 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 234 | var query = from g in context.Resources.OfType<DA.ClientGroup>()
|
---|
| 235 | where g.Id == clientGroup.Id
|
---|
| 236 | select g;
|
---|
| 237 |
|
---|
| 238 | if (query.Count() > 0) {
|
---|
| 239 | var entity = query.First();
|
---|
| 240 | entity.Name = clientGroup.Name;
|
---|
| 241 | entity.Description = clientGroup.Description;
|
---|
| 242 | context.SubmitChanges();
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | public void DeleteClientGroup(DT.ClientGroup clientGroup) {
|
---|
| 248 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 249 |
|
---|
| 250 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 251 | //load clientGroup because we could get a detached object
|
---|
| 252 | var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
|
---|
| 253 | where c.Id == clientGroup.Id
|
---|
| 254 | select c;
|
---|
| 255 | if (query.Count() > 0) {
|
---|
| 256 | context.Resources.DeleteOnSubmit(query.First());
|
---|
| 257 | context.SubmitChanges();
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | public void AddResourceToGroup(DT.Resource resource, DT.ClientGroup group) {
|
---|
| 263 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 264 | DA.ResourceResourceGroup rrg = new DA.ResourceResourceGroup() {
|
---|
| 265 | ResourceId = resource.Id,
|
---|
| 266 | ResourceGroupId = group.Id
|
---|
| 267 | };
|
---|
| 268 |
|
---|
| 269 | context.ResourceResourceGroups.InsertOnSubmit(rrg);
|
---|
| 270 | context.SubmitChanges();
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | public void RemoveResourceFromGroup(DT.Resource resource, DT.ClientGroup group) {
|
---|
| 275 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 276 | var query = context.ResourceResourceGroups.Where(x => x.ResourceId == resource.Id && x.ResourceGroupId == group.Id);
|
---|
| 277 | if (query.Count() > 0) {
|
---|
| 278 | context.ResourceResourceGroups.DeleteOnSubmit(query.First());
|
---|
| 279 | context.SubmitChanges();
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | #endregion
|
---|
| 284 |
|
---|
| 285 | #region ClientGroupMapping
|
---|
| 286 | public IEnumerable<DT.ClientGroupMapping> GetClientGroupMapping() {
|
---|
| 287 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 288 | var query = from c in context.GetTable<DA.ResourceResourceGroup>()
|
---|
| 289 | select Convert.ToDto(c);
|
---|
| 290 | return query.ToList();
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | #endregion
|
---|
| 294 |
|
---|
| 295 | #region Resource
|
---|
| 296 | public IEnumerable<DT.Resource> GetResources() {
|
---|
| 297 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 298 | var query = from r in context.Resources
|
---|
| 299 | select Convert.ToDto(r);
|
---|
| 300 | return query.ToList();
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | #endregion
|
---|
| 304 |
|
---|
| 305 | #region ClientLog
|
---|
| 306 | public DT.ClientLog GetLastClientLog(Guid clientId) {
|
---|
| 307 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 308 | var query = from r in context.ClientLogs
|
---|
| 309 | where r.ResourceId == clientId
|
---|
| 310 | select r;
|
---|
| 311 | return Convert.ToDto(query.OrderBy(x => x.Timestamp).LastOrDefault());
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | public IEnumerable<DT.ClientLog> GetClientLogs(Guid clientId) {
|
---|
| 316 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 317 | var query = from r in context.ClientLogs
|
---|
| 318 | where r.ResourceId == clientId
|
---|
| 319 | select Convert.ToDto(r);
|
---|
| 320 | return query.ToList();
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | public IEnumerable<DT.ClientLog> GetClientLogsSince(DateTime startDate) {
|
---|
| 325 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 326 | var query = from r in context.ClientLogs
|
---|
| 327 | where r.Timestamp >= startDate
|
---|
| 328 | select Convert.ToDto(r);
|
---|
| 329 | return query.ToList();
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | public void AddClientLog(DT.ClientLog log) {
|
---|
| 334 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 335 | context.ClientLogs.InsertOnSubmit(Convert.ToEntity(log));
|
---|
| 336 | context.SubmitChanges();
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | public void DeleteClientLog(DT.ClientLog log) {
|
---|
| 341 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 342 |
|
---|
| 343 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 344 | context.ClientLogs.DeleteOnSubmit(Convert.ToEntity(log));
|
---|
| 345 | context.SubmitChanges();
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 | #endregion
|
---|
| 349 |
|
---|
| 350 | #region User
|
---|
| 351 | private DT.User BuildUserDto(Guid userId) {
|
---|
| 352 | DA.aspnet_User aspUser = null;
|
---|
| 353 | DA.aspnet_Membership aspMembership = null;
|
---|
| 354 | DA.User accessUser = null;
|
---|
| 355 |
|
---|
| 356 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 357 | var userQuery = from u in context.aspnet_Users
|
---|
| 358 | where u.UserId == userId
|
---|
| 359 | select u;
|
---|
| 360 | if (userQuery.Count() == 1) {
|
---|
| 361 | aspUser = userQuery.First();
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | var memQuery = from u in context.aspnet_Memberships
|
---|
| 365 | where u.UserId == userId
|
---|
| 366 | select u;
|
---|
| 367 | if (memQuery.Count() == 1) {
|
---|
| 368 | aspMembership = memQuery.First();
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | if (aspUser != null || aspMembership != null) {
|
---|
| 373 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 374 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
| 375 | where u.Id == userId
|
---|
| 376 | select u;
|
---|
| 377 | if (query.Count() == 1) {
|
---|
| 378 | accessUser = query.First();
|
---|
| 379 | } else {
|
---|
| 380 | //if the user is not in the access db add it (this makes it easy to upgrade with an existing asp.net authentication db)
|
---|
| 381 | DA.User user = new DA.User();
|
---|
| 382 | user.Id = userId;
|
---|
| 383 | user.FullName = "Not set";
|
---|
| 384 | context.UserGroupBases.InsertOnSubmit(user);
|
---|
| 385 | context.SubmitChanges();
|
---|
| 386 | accessUser = user;
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | if (aspUser == null || aspMembership == null || accessUser == null) {
|
---|
| 392 | throw new Exception("User with id " + userId + " not found.");
|
---|
| 393 | } else {
|
---|
| 394 | return Convert.ToDto(accessUser, aspUser, aspMembership);
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | private DT.LightweightUser BuildLightweightUserDto(Guid userId) {
|
---|
| 399 | DA.aspnet_User aspUser = null;
|
---|
| 400 | DA.aspnet_Membership aspMembership = null;
|
---|
| 401 | DA.User accessUser = null;
|
---|
| 402 | List<DA.aspnet_Role> roles = new List<DA.aspnet_Role>();
|
---|
| 403 | List<DA.UserGroup> groups = new List<DA.UserGroup>();
|
---|
| 404 |
|
---|
| 405 |
|
---|
| 406 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 407 | var userQuery = from u in context.aspnet_Users
|
---|
| 408 | where u.UserId == userId
|
---|
| 409 | select u;
|
---|
| 410 |
|
---|
| 411 | var memQuery = from u in context.aspnet_Memberships
|
---|
| 412 | where u.UserId == userId
|
---|
| 413 | select u;
|
---|
| 414 | if (memQuery.Count() == 1) {
|
---|
| 415 | aspMembership = memQuery.First();
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | if (userQuery.Count() == 1) {
|
---|
| 419 | aspUser = userQuery.First();
|
---|
| 420 | roles = (from ur in context.aspnet_UsersInRoles
|
---|
| 421 | where ur.UserId == aspUser.UserId
|
---|
| 422 | join r in context.aspnet_Roles on ur.RoleId equals r.RoleId
|
---|
| 423 | select r).ToList();
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | if (aspUser != null || aspMembership != null) {
|
---|
| 428 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 429 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
| 430 | where u.Id == userId
|
---|
| 431 | select u;
|
---|
| 432 | if (query.Count() == 1) {
|
---|
| 433 | accessUser = query.First();
|
---|
| 434 | groups = (from ug in context.UserGroupUserGroups
|
---|
| 435 | where ug.UserGroupId == accessUser.Id
|
---|
| 436 | join g in context.UserGroupBases.OfType<DA.UserGroup>() on ug.UserGroupUserGroupId equals g.Id
|
---|
| 437 | select g).ToList();
|
---|
| 438 | } else {
|
---|
| 439 | //if the user is not in the access db add it (this makes it easy to upgrade with an existing asp.net authentication db)
|
---|
| 440 | DA.User user = new DA.User();
|
---|
| 441 | user.Id = userId;
|
---|
| 442 | user.FullName = "Not set";
|
---|
| 443 | context.UserGroupBases.InsertOnSubmit(user);
|
---|
| 444 | context.SubmitChanges();
|
---|
| 445 | accessUser = user;
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | if (aspUser == null || accessUser == null || aspMembership == null) {
|
---|
| 451 | throw new Exception("User with id " + userId + " not found.");
|
---|
| 452 | } else {
|
---|
| 453 | return Convert.ToDto(accessUser, aspUser, aspMembership, roles, groups);
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | public DT.LightweightUser Login() {
|
---|
| 458 | Guid userId = UserManager.CurrentUserId;
|
---|
| 459 | return BuildLightweightUserDto(userId);
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | public void UpdateLightweightUser(DT.LightweightUser user) {
|
---|
| 463 | DT.User u = BuildUserDto(user.Id);
|
---|
| 464 |
|
---|
| 465 | u.Email = user.EMail;
|
---|
| 466 | u.FullName = user.FullName;
|
---|
| 467 |
|
---|
| 468 | UpdateUser(u);
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | public IEnumerable<DT.UserGroup> GetGroupsOfCurrentUser() {
|
---|
| 472 | Guid userId = UserManager.CurrentUserId;
|
---|
| 473 |
|
---|
| 474 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 475 | var query = from g in context.UserGroupUserGroups
|
---|
| 476 | from ug in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 477 | where g.UserGroupId == userId && g.UserGroupUserGroupId == ug.Id
|
---|
| 478 | select Convert.ToDto(ug);
|
---|
| 479 | return query.ToList();
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | public IEnumerable<DT.Role> GetRolesOfCurrentUser() {
|
---|
| 484 | Guid userId = UserManager.CurrentUserId;
|
---|
| 485 |
|
---|
| 486 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 487 | var query = from ur in context.aspnet_UsersInRoles
|
---|
| 488 | from r in context.aspnet_Roles
|
---|
| 489 | where ur.UserId == userId && ur.RoleId == r.RoleId
|
---|
| 490 | select Convert.ToDto(r);
|
---|
| 491 | return query.ToList();
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 |
|
---|
| 496 | public IEnumerable<DT.LightweightUser> GetAllLightweightUsers() {
|
---|
| 497 | List<Guid> accessUserGuids = null;
|
---|
| 498 |
|
---|
| 499 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 500 | var query = from u in context.aspnet_Users
|
---|
| 501 | select u.UserId;
|
---|
| 502 | accessUserGuids = query.ToList();
|
---|
| 503 | }
|
---|
| 504 | return accessUserGuids.Select(x => BuildLightweightUserDto(x));
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | public IEnumerable<DT.User> GetAllUsers() {
|
---|
| 508 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 509 |
|
---|
| 510 | List<Guid> accessUserGuids = null;
|
---|
| 511 |
|
---|
| 512 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 513 | var query = from u in context.aspnet_Users
|
---|
| 514 | select u.UserId;
|
---|
| 515 | accessUserGuids = query.ToList();
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | public IEnumerable<DT.User> GetUsers(IEnumerable<Guid> ids) {
|
---|
| 522 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 523 |
|
---|
| 524 | List<Guid> accessUserGuids = null;
|
---|
| 525 |
|
---|
| 526 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 527 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
| 528 | where ids.Contains(u.Id)
|
---|
| 529 | select u.Id;
|
---|
| 530 | accessUserGuids = query.ToList();
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | if (accessUserGuids.Count() != ids.Count()) {
|
---|
| 534 | throw new Exception("Couldn't find one or more users for the given user ids.");
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | return accessUserGuids.Select(x => BuildUserDto(x));
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | public IEnumerable<DT.LightweightUser> GetLightweightUsers(IEnumerable<Guid> ids) {
|
---|
| 541 | List<Guid> accessUserGuids = null;
|
---|
| 542 |
|
---|
| 543 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 544 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
| 545 | where ids.Contains(u.Id)
|
---|
| 546 | select u.Id;
|
---|
| 547 | accessUserGuids = query.ToList();
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | if (accessUserGuids.Count() != ids.Count()) {
|
---|
| 551 | throw new Exception("Couldn't find one or more users for the given user ids.");
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | return accessUserGuids.Select(x => BuildLightweightUserDto(x));
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | public DT.User AddUser(DT.User user) {
|
---|
| 558 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 559 |
|
---|
| 560 | DA.User accessUser;
|
---|
| 561 | DA.aspnet_User aspUser;
|
---|
| 562 | DA.aspnet_Membership aspMembership;
|
---|
| 563 | bool userExistsInASP;
|
---|
| 564 |
|
---|
| 565 | Convert.ToEntity(user, out accessUser, out aspUser, out aspMembership, out userExistsInASP);
|
---|
| 566 |
|
---|
| 567 | if (userExistsInASP) {
|
---|
| 568 | if (accessUser.Id == null || accessUser.Id == Guid.Empty) {
|
---|
| 569 | accessUser.Id = aspMembership.UserId;
|
---|
| 570 | }
|
---|
| 571 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 572 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
| 573 | context.SubmitChanges();
|
---|
| 574 | }
|
---|
| 575 | MembershipUser membershipUser = Membership.GetUser((object)accessUser.Id);
|
---|
| 576 | if (membershipUser != null) {
|
---|
| 577 | membershipUser.Email = aspMembership.Email;
|
---|
| 578 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
| 579 | membershipUser.Comment = aspMembership.Comment;
|
---|
| 580 | Membership.UpdateUser(membershipUser);
|
---|
| 581 | }
|
---|
| 582 | } else {
|
---|
| 583 | MembershipUser membershipUser = Membership.CreateUser(aspUser.UserName, aspUser.UserName, aspMembership.Email);
|
---|
| 584 | membershipUser.IsApproved = aspMembership.IsApproved;
|
---|
| 585 | membershipUser.Comment = aspMembership.Comment;
|
---|
| 586 | Membership.UpdateUser(membershipUser);
|
---|
| 587 |
|
---|
| 588 | Guid userId = (Guid)membershipUser.ProviderUserKey;
|
---|
| 589 | accessUser.Id = userId;
|
---|
| 590 |
|
---|
| 591 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 592 | context.UserGroupBases.InsertOnSubmit(accessUser);
|
---|
| 593 | context.SubmitChanges();
|
---|
| 594 | }
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 598 | var newAspUser = context.aspnet_Users.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
| 599 | var newAspMembership = context.aspnet_Memberships.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
|
---|
| 600 | return Convert.ToDto(accessUser, newAspUser, newAspMembership);
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | public void DeleteUser(DT.User user) {
|
---|
| 605 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 606 |
|
---|
| 607 | if (user.Id != null && user.Id != Guid.Empty) {
|
---|
| 608 | //delete asp.net user
|
---|
| 609 | Membership.DeleteUser(user.UserName);
|
---|
| 610 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 611 | var query = context.UserGroupBases.OfType<DA.User>().Where(x => x.Id == user.Id);
|
---|
| 612 | if (query.Count() > 0) {
|
---|
| 613 |
|
---|
| 614 | //delete affiliation first
|
---|
| 615 | var queryMapping = context.UserGroupUserGroups.Where(x => x.UserGroupId == user.Id);
|
---|
| 616 | if (queryMapping.Count() > 0) {
|
---|
| 617 | context.UserGroupUserGroups.DeleteAllOnSubmit(queryMapping);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | //delete user from access db
|
---|
| 621 | context.UserGroupBases.DeleteOnSubmit(query.First());
|
---|
| 622 | context.SubmitChanges();
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | public void UpdateUser(DT.User user) {
|
---|
| 629 | if (user.Id != UserManager.CurrentUserId) {
|
---|
| 630 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | MembershipUser membershipUser = Membership.GetUser((object)user.Id);
|
---|
| 634 | if (membershipUser != null) {
|
---|
| 635 | membershipUser.Email = user.Email;
|
---|
| 636 | membershipUser.IsApproved = user.IsApproved;
|
---|
| 637 | membershipUser.Comment = user.Comment;
|
---|
| 638 | Membership.UpdateUser(membershipUser);
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 642 | var query = from u in context.UserGroupBases.OfType<DA.User>()
|
---|
| 643 | where u.Id == user.Id
|
---|
| 644 | select u;
|
---|
| 645 | if (query.Count() > 0) {
|
---|
| 646 | DA.User u = query.First();
|
---|
| 647 | u.FullName = user.FullName;
|
---|
| 648 | context.SubmitChanges();
|
---|
| 649 | }
|
---|
| 650 | }
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | public void AddUserToRole(DT.Role role, DT.User user) {
|
---|
| 654 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 655 |
|
---|
| 656 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
| 657 | if (msUser != null) {
|
---|
| 658 | Roles.AddUserToRole(msUser.UserName, role.Name);
|
---|
| 659 | }
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | public void RemoveUserFromRole(DT.Role role, DT.User user) {
|
---|
| 663 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 664 |
|
---|
| 665 | MembershipUser msUser = Membership.GetUser((object)user.Id);
|
---|
| 666 | if (msUser != null) {
|
---|
| 667 | Roles.RemoveUserFromRole(msUser.UserName, role.Name);
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | public bool ChangePassword(Guid userId, string oldPassword, string newPassword) {
|
---|
| 672 | MembershipUser msUser = Membership.GetUser(userId);
|
---|
| 673 | if (msUser != null) {
|
---|
| 674 | return msUser.ChangePassword(oldPassword, newPassword);
|
---|
| 675 | }
|
---|
| 676 | return false;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | public string ResetPassword(Guid userId) {
|
---|
| 680 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 681 |
|
---|
| 682 | MembershipUser msUser = Membership.GetUser(userId);
|
---|
| 683 | if (msUser != null) {
|
---|
| 684 | return msUser.ResetPassword();
|
---|
| 685 | } else {
|
---|
| 686 | throw new Exception("Password reset failed.");
|
---|
| 687 | }
|
---|
| 688 | }
|
---|
| 689 | #endregion
|
---|
| 690 |
|
---|
| 691 | #region UserGroup
|
---|
| 692 | public IEnumerable<DT.UserGroup> GetAllUserGroups() {
|
---|
| 693 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 694 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 695 | select Convert.ToDto(u);
|
---|
| 696 | return query.ToList();
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | public IEnumerable<DT.UserGroup> GetUserGroupsOfUser(Guid userId) {
|
---|
| 701 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 702 | var groupIds = from g in context.UserGroupUserGroups
|
---|
| 703 | where g.UserGroupId == userId
|
---|
| 704 | select g.UserGroupUserGroupId;
|
---|
| 705 |
|
---|
| 706 | var query = from g in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 707 | where groupIds.Contains(g.Id)
|
---|
| 708 | select Convert.ToDto(g);
|
---|
| 709 |
|
---|
| 710 | return query.ToList();
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | public IEnumerable<DT.UserGroup> GetUserGroups(IEnumerable<Guid> ids) {
|
---|
| 715 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 716 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 717 | where ids.Contains(u.Id)
|
---|
| 718 | select Convert.ToDto(u);
|
---|
| 719 | return query.ToList();
|
---|
| 720 | }
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | public Guid AddUserGroup(DT.UserGroup group) {
|
---|
| 724 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 725 | //because id is not automatically set because of user, we have to do it here manually for group
|
---|
| 726 | group.Id = Guid.NewGuid();
|
---|
| 727 |
|
---|
| 728 | context.UserGroupBases.InsertOnSubmit(Convert.ToEntity(group));
|
---|
| 729 | context.SubmitChanges();
|
---|
| 730 | return group.Id;
|
---|
| 731 | }
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | public void UpdateUserGroup(DT.UserGroup group) {
|
---|
| 735 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 736 | DA.UserGroup entity = context.UserGroupBases.OfType<DA.UserGroup>().FirstOrDefault(x => x.Id == group.Id);
|
---|
| 737 | Convert.ToEntity(group, entity);
|
---|
| 738 | context.SubmitChanges();
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | public void DeleteUserGroup(DT.UserGroup group) {
|
---|
| 743 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 744 | var g = group; //linq does not like vars called group
|
---|
| 745 |
|
---|
[8144] | 746 | if (g.Id != null && g.Id != Guid.Empty) {
|
---|
| 747 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 748 | var query = from ug in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 749 | where ug.Id == g.Id
|
---|
| 750 | select ug;
|
---|
| 751 | if (query.Count() > 0) {
|
---|
| 752 | context.UserGroupBases.DeleteOnSubmit(query.First());
|
---|
| 753 | context.SubmitChanges();
|
---|
| 754 | } else {
|
---|
| 755 | throw new Exception("UserGroup with id " + g.Id + " does not exist.");
|
---|
| 756 | }
|
---|
[8040] | 757 | }
|
---|
| 758 | }
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | public void AddUserGroupBaseToGroup(DT.UserGroupBase resource, DT.UserGroup group) {
|
---|
| 762 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 763 | DA.UserGroupUserGroup ugug = new DA.UserGroupUserGroup();
|
---|
| 764 | ugug.UserGroupId = resource.Id;
|
---|
| 765 | ugug.UserGroupUserGroupId = group.Id;
|
---|
| 766 | context.UserGroupUserGroups.InsertOnSubmit(ugug);
|
---|
| 767 | context.SubmitChanges();
|
---|
| 768 | }
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup userGroup) {
|
---|
| 772 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 773 | var query = from u in context.UserGroupUserGroups
|
---|
| 774 | where u.UserGroupId == resource.Id && u.UserGroupUserGroupId == userGroup.Id
|
---|
| 775 | select u;
|
---|
| 776 |
|
---|
| 777 | if (query.Count() == 1) {
|
---|
| 778 | context.UserGroupUserGroups.DeleteOnSubmit(query.First());
|
---|
| 779 | context.SubmitChanges();
|
---|
| 780 | }
|
---|
| 781 | }
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | public IEnumerable<DT.UserGroupBase> GetUsersAndGroups() {
|
---|
| 785 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 786 | var query = from u in context.UserGroupBases
|
---|
| 787 | select Convert.ToDto(u);
|
---|
| 788 | return query.ToList();
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | public IEnumerable<DT.UserGroupMapping> GetUserGroupMapping() {
|
---|
| 793 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 794 | var query = from u in context.UserGroupUserGroups
|
---|
| 795 | select Convert.ToDto(u);
|
---|
| 796 | return query.ToList();
|
---|
| 797 | }
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | public IEnumerable<Guid> GetUserGroupIdsOfGroup(Guid groupId) {
|
---|
| 801 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 802 | var query = from u in context.UserGroupUserGroups
|
---|
| 803 | where u.UserGroupUserGroupId == groupId
|
---|
| 804 | select u.UserGroupId;
|
---|
| 805 | return query.ToList();
|
---|
| 806 | }
|
---|
| 807 | }
|
---|
| 808 | #endregion
|
---|
| 809 |
|
---|
| 810 | #region UserGroupBase
|
---|
| 811 | public IEnumerable<DT.UserGroupBase> GetAllLeightweightUsersAndGroups() {
|
---|
| 812 | List<DT.UserGroup> userGroups = new List<DT.UserGroup>();
|
---|
| 813 | List<DT.UserGroupBase> result = new List<DT.UserGroupBase>();
|
---|
| 814 |
|
---|
| 815 | List<Guid> accessUserGuids = null;
|
---|
| 816 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 817 | var query = from u in context.aspnet_Users
|
---|
| 818 | select u.UserId;
|
---|
| 819 | accessUserGuids = query.ToList();
|
---|
| 820 | }
|
---|
| 821 | var lightweightUsers = accessUserGuids.Select(x => BuildLightweightUserDto(x));
|
---|
| 822 |
|
---|
| 823 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 824 | var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 825 | select Convert.ToDto(u);
|
---|
| 826 | userGroups = query.ToList();
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 | result.AddRange(lightweightUsers);
|
---|
| 830 | result.AddRange(userGroups);
|
---|
| 831 |
|
---|
| 832 | return result;
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | public IEnumerable<DT.UserGroupBase> GetLeightweightUsersAndGroups(IEnumerable<Guid> ids) {
|
---|
| 836 | List<DA.UserGroupBase> dbUserGroupsBases = new List<DA.UserGroupBase>();
|
---|
| 837 | List<DT.UserGroupBase> result = new List<DT.UserGroupBase>();
|
---|
| 838 |
|
---|
| 839 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 840 | var query = from u in context.UserGroupBases
|
---|
| 841 | where ids.Contains(u.Id)
|
---|
| 842 | select u;
|
---|
| 843 | dbUserGroupsBases = query.ToList();
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | foreach (var ugb in dbUserGroupsBases) {
|
---|
| 847 | if (ugb.GetType() == typeof(DA.User)) {
|
---|
| 848 | var user = BuildLightweightUserDto(ugb.Id);
|
---|
| 849 | result.Add(user);
|
---|
| 850 | } else if (ugb.GetType() == typeof(DA.UserGroup)) {
|
---|
| 851 | var group = Convert.ToDto(ugb as DA.UserGroup);
|
---|
| 852 | result.Add(group);
|
---|
| 853 | }
|
---|
| 854 | }
|
---|
| 855 | return result;
|
---|
| 856 | }
|
---|
| 857 | #endregion
|
---|
| 858 |
|
---|
| 859 | #region Roles
|
---|
| 860 | public IEnumerable<DT.Role> GetRoles() {
|
---|
| 861 | using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 862 | var query = from u in context.aspnet_Roles
|
---|
| 863 | select Convert.ToDto(u);
|
---|
| 864 | return query.ToList();
|
---|
| 865 | }
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | public DT.Role AddRole(DT.Role role) {
|
---|
| 869 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 870 |
|
---|
| 871 | Roles.CreateRole(role.Name);
|
---|
| 872 | return role;
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | public void DeleteRole(DT.Role role) {
|
---|
| 876 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 877 |
|
---|
| 878 | Roles.DeleteRole(role.Name);
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 | public IEnumerable<DT.Role> GetUserRoles(DT.User user) {
|
---|
| 882 | var roles = Roles.GetRolesForUser(user.UserName);
|
---|
| 883 | return roles.Select(x => new DT.Role() { Name = x });
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | public void AddRoleToGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
| 887 | Guid[] userIds;
|
---|
| 888 | string[] aspUsers;
|
---|
| 889 |
|
---|
| 890 | using (DA.AccessServiceDataContext accessContext = new DA.AccessServiceDataContext()) {
|
---|
| 891 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
| 892 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
| 893 | select u.UserGroupId).ToArray();
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 897 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
| 898 | where userIds.Contains(u.UserId)
|
---|
| 899 | select u.UserName).ToArray();
|
---|
| 900 | }
|
---|
| 901 |
|
---|
| 902 | Roles.AddUsersToRole(aspUsers, role.Name);
|
---|
| 903 |
|
---|
| 904 | }
|
---|
| 905 |
|
---|
| 906 | public void RemoveRoleFromGroup(DT.UserGroup userGroup, DT.Role role) {
|
---|
| 907 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 908 |
|
---|
| 909 | Guid[] userIds;
|
---|
| 910 | string[] aspUsers;
|
---|
| 911 |
|
---|
| 912 | using (DA.AccessServiceDataContext accessContext = new DA.AccessServiceDataContext()) {
|
---|
| 913 | userIds = (from u in accessContext.UserGroupUserGroups
|
---|
| 914 | where u.UserGroupUserGroupId == userGroup.Id
|
---|
| 915 | select u.UserGroupId).ToArray();
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 | using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
|
---|
| 919 | aspUsers = (from u in aspContext.aspnet_Users
|
---|
| 920 | where userIds.Contains(u.UserId)
|
---|
| 921 | select u.UserName).ToArray();
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | Roles.RemoveUsersFromRole(aspUsers.ToArray(), role.Name);
|
---|
| 925 | }
|
---|
| 926 | #endregion
|
---|
| 927 |
|
---|
| 928 | #region Error Reporting
|
---|
| 929 | public void ReportError(DT.ClientError error) {
|
---|
| 930 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 931 | context.ClientErrors.InsertOnSubmit(Convert.ToEntity(error));
|
---|
| 932 | context.SubmitChanges();
|
---|
| 933 | }
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | public IEnumerable<DT.ClientError> GetClientErrors() {
|
---|
| 937 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 938 |
|
---|
| 939 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 940 | var query = from c in context.ClientErrors
|
---|
| 941 | select Convert.ToDto(c);
|
---|
| 942 | return query.ToList();
|
---|
| 943 | }
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | public IEnumerable<DT.ClientError> GetLastClientErrors(DateTime startDate) {
|
---|
| 947 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 948 |
|
---|
| 949 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 950 | var query = from c in context.ClientErrors
|
---|
| 951 | where c.Timestamp >= startDate
|
---|
| 952 | select Convert.ToDto(c);
|
---|
| 953 | return query.ToList();
|
---|
| 954 | }
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 | public void DeleteError(DT.ClientError error) {
|
---|
| 958 | RoleVerifier.AuthenticateForAllRoles(AccessServiceRoles.Administrator);
|
---|
| 959 |
|
---|
| 960 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 961 | var query = context.ClientErrors.Where(x => x.Id == error.Id);
|
---|
| 962 | if (query.Count() > 0) {
|
---|
| 963 | context.ClientErrors.DeleteOnSubmit(query.First());
|
---|
| 964 | context.SubmitChanges();
|
---|
| 965 | }
|
---|
| 966 | }
|
---|
| 967 | }
|
---|
| 968 | #endregion
|
---|
| 969 | }
|
---|
| 970 | }
|
---|