[8040] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
[12932] | 46 | public string GetUserNameById(Guid userId) {
|
---|
| 47 | var user = GetUserById(userId);
|
---|
[15969] | 48 | if (user != null && !string.IsNullOrWhiteSpace(user.UserName)) {
|
---|
[12932] | 49 | return user.UserName;
|
---|
| 50 | } else {
|
---|
| 51 | return userId.ToString();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[15500] | 56 | public IEnumerable<Guid> GetUserGroupIdsOfUser(Guid userId) {
|
---|
| 57 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 58 | var groupIds = from g in context.UserGroupUserGroups
|
---|
| 59 | where g.UserGroupId == userId
|
---|
| 60 | select g.UserGroupUserGroupId;
|
---|
| 61 |
|
---|
| 62 | var query = from g in context.UserGroupBases.OfType<DA.UserGroup>()
|
---|
| 63 | where groupIds.Contains(g.Id)
|
---|
| 64 | select g.Id;
|
---|
| 65 |
|
---|
| 66 | return query.ToList();
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[8040] | 70 | public bool VerifyUser(Guid userId, List<Guid> allowedUserGroups) {
|
---|
| 71 | List<DA.UserGroupUserGroup> userGroupBases;
|
---|
| 72 | List<DA.UserGroup> groups;
|
---|
| 73 | Dictionary<Guid, Guid> ugMapping = new Dictionary<Guid, Guid>();
|
---|
| 74 |
|
---|
| 75 | if (allowedUserGroups.Contains(userId)) return true;
|
---|
| 76 |
|
---|
| 77 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 78 | userGroupBases = context.UserGroupUserGroups.ToList();
|
---|
| 79 | groups = context.UserGroupBases.OfType<DA.UserGroup>().ToList();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | foreach (var ugug in userGroupBases) {
|
---|
| 83 | ugMapping[ugug.UserGroupId] = ugug.UserGroupUserGroupId;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | foreach (Guid guid in allowedUserGroups) {
|
---|
| 87 | if (CheckInGroupHierarchy(userId, guid, ugMapping, groups)) return true;
|
---|
| 88 | }
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[15577] | 92 | public IEnumerable<DataTransfer.UserGroupMapping> GetUserGroupMapping() {
|
---|
| 93 | using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
|
---|
| 94 | var query = from u in context.UserGroupUserGroups
|
---|
| 95 | select Convert.ToDto(u);
|
---|
| 96 | return query.ToList();
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[8040] | 100 | private bool CheckInGroupHierarchy(Guid userId, Guid group, Dictionary<Guid, Guid> ugMapping, List<DA.UserGroup> groups) {
|
---|
| 101 | //check all subgroups
|
---|
| 102 | var childs = ugMapping.Where(x => x.Value == group).Select(x => x.Key);
|
---|
| 103 | var childGroups = childs.Where(x => groups.Where(y => y.Id == x).Count() > 0).ToList();
|
---|
| 104 | //also check if user is in group
|
---|
| 105 | childGroups.Add(group);
|
---|
| 106 |
|
---|
| 107 | foreach (Guid id in childGroups) {
|
---|
| 108 | if (ugMapping.Where(x => x.Value == id).Select(x => x.Key).Contains(userId)) {
|
---|
| 109 | return true;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | return false;
|
---|
| 113 | }
|
---|
[9261] | 114 |
|
---|
| 115 | private static T TryAndRepeat<T>(Func<T> action) {
|
---|
| 116 | int repetitions = 5;
|
---|
| 117 | while (true) {
|
---|
| 118 | try { return action(); }
|
---|
| 119 | catch (Exception e) {
|
---|
| 120 | if (repetitions == 0) throw e;
|
---|
| 121 | repetitions--;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
[8040] | 125 | }
|
---|
| 126 | }
|
---|