[8040] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 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.Web.Security;
|
---|
| 26 | using DA = HeuristicLab.Services.Access.DataAccess;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Services.Access {
|
---|
| 29 | public class UserManager : IUserManager {
|
---|
| 30 | public MembershipUser CurrentUser {
|
---|
[9261] | 31 | get { return TryAndRepeat(() => { return Membership.GetUser(); }); }
|
---|
[8040] | 32 | }
|
---|
| 33 |
|
---|
| 34 | public Guid CurrentUserId {
|
---|
| 35 | get { return (Guid)CurrentUser.ProviderUserKey; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public MembershipUser GetUserByName(string username) {
|
---|
| 39 | return Membership.GetUser(username);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public MembershipUser GetUserById(Guid userId) {
|
---|
| 43 | return Membership.GetUser(userId);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public bool VerifyUser(Guid userId, List<Guid> allowedUserGroups) {
|
---|
| 47 | List<DA.UserGroupUserGroup> userGroupBases;
|
---|
| 48 | List<DA.UserGroup> groups;
|
---|
| 49 | Dictionary<Guid, Guid> ugMapping = new Dictionary<Guid, Guid>();
|
---|
| 50 |
|
---|
| 51 | if (allowedUserGroups.Contains(userId)) return true;
|
---|
| 52 |
|
---|
| 53 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 54 | userGroupBases = context.UserGroupUserGroups.ToList();
|
---|
| 55 | groups = context.UserGroupBases.OfType<DA.UserGroup>().ToList();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | foreach (var ugug in userGroupBases) {
|
---|
| 59 | ugMapping[ugug.UserGroupId] = ugug.UserGroupUserGroupId;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | foreach (Guid guid in allowedUserGroups) {
|
---|
| 63 | if (CheckInGroupHierarchy(userId, guid, ugMapping, groups)) return true;
|
---|
| 64 | }
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private bool CheckInGroupHierarchy(Guid userId, Guid group, Dictionary<Guid, Guid> ugMapping, List<DA.UserGroup> groups) {
|
---|
| 69 | //check all subgroups
|
---|
| 70 | var childs = ugMapping.Where(x => x.Value == group).Select(x => x.Key);
|
---|
| 71 | var childGroups = childs.Where(x => groups.Where(y => y.Id == x).Count() > 0).ToList();
|
---|
| 72 | //also check if user is in group
|
---|
| 73 | childGroups.Add(group);
|
---|
| 74 |
|
---|
| 75 | foreach (Guid id in childGroups) {
|
---|
| 76 | if (ugMapping.Where(x => x.Value == id).Select(x => x.Key).Contains(userId)) {
|
---|
| 77 | return true;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | return false;
|
---|
| 81 | }
|
---|
[9261] | 82 |
|
---|
| 83 | private static T TryAndRepeat<T>(Func<T> action) {
|
---|
| 84 | int repetitions = 5;
|
---|
| 85 | while (true) {
|
---|
| 86 | try { return action(); }
|
---|
| 87 | catch (Exception e) {
|
---|
| 88 | if (repetitions == 0) throw e;
|
---|
| 89 | repetitions--;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[8040] | 93 | }
|
---|
| 94 | }
|
---|