[8040] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
[12962] | 46 | public string GetUserNameById(Guid userId) {
|
---|
| 47 | var user = GetUserById(userId);
|
---|
| 48 | if (user != null) {
|
---|
| 49 | return user.UserName;
|
---|
| 50 | } else {
|
---|
| 51 | return userId.ToString();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[8040] | 56 | public bool VerifyUser(Guid userId, List<Guid> allowedUserGroups) {
|
---|
| 57 | List<DA.UserGroupUserGroup> userGroupBases;
|
---|
| 58 | List<DA.UserGroup> groups;
|
---|
| 59 | Dictionary<Guid, Guid> ugMapping = new Dictionary<Guid, Guid>();
|
---|
| 60 |
|
---|
| 61 | if (allowedUserGroups.Contains(userId)) return true;
|
---|
| 62 |
|
---|
| 63 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 64 | userGroupBases = context.UserGroupUserGroups.ToList();
|
---|
| 65 | groups = context.UserGroupBases.OfType<DA.UserGroup>().ToList();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | foreach (var ugug in userGroupBases) {
|
---|
| 69 | ugMapping[ugug.UserGroupId] = ugug.UserGroupUserGroupId;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | foreach (Guid guid in allowedUserGroups) {
|
---|
| 73 | if (CheckInGroupHierarchy(userId, guid, ugMapping, groups)) return true;
|
---|
| 74 | }
|
---|
| 75 | return false;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | private bool CheckInGroupHierarchy(Guid userId, Guid group, Dictionary<Guid, Guid> ugMapping, List<DA.UserGroup> groups) {
|
---|
| 79 | //check all subgroups
|
---|
| 80 | var childs = ugMapping.Where(x => x.Value == group).Select(x => x.Key);
|
---|
| 81 | var childGroups = childs.Where(x => groups.Where(y => y.Id == x).Count() > 0).ToList();
|
---|
| 82 | //also check if user is in group
|
---|
| 83 | childGroups.Add(group);
|
---|
| 84 |
|
---|
| 85 | foreach (Guid id in childGroups) {
|
---|
| 86 | if (ugMapping.Where(x => x.Value == id).Select(x => x.Key).Contains(userId)) {
|
---|
| 87 | return true;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | return false;
|
---|
| 91 | }
|
---|
[9261] | 92 |
|
---|
| 93 | private static T TryAndRepeat<T>(Func<T> action) {
|
---|
| 94 | int repetitions = 5;
|
---|
| 95 | while (true) {
|
---|
| 96 | try { return action(); }
|
---|
| 97 | catch (Exception e) {
|
---|
| 98 | if (repetitions == 0) throw e;
|
---|
| 99 | repetitions--;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[8040] | 103 | }
|
---|
| 104 | }
|
---|