using BS.Shared.Core; using System.Collections.Generic; using System.Linq; namespace BS.Shared.DataContracts { public partial class UserDC { public override bool Equals(object obj) { if (obj is UserDC) { var y = (UserDC)obj; if (this.UserOid == null && y.UserOid == null) { return this.GetHashCode() == y.GetHashCode(); } if (this.UserOid != null && y.UserOid != null) { return this.UserOid == y.UserOid; } } return false; } public List GetGrantedRights() { return (this.UserGroups != null) ? this.UserGroups.Where(g => g.Rights != null).SelectMany(g => g.Rights).Distinct().ToList() : new List(); } public override int GetHashCode() { return this.UserOid.HasValue ? this.GetType().Name.GetHashCode() ^ this.UserOid.GetHashCode() : base.GetHashCode(); } public SettingsDC GetSettingValue(SettingsType pType) { if (this.Settings != null) { return this.Settings.SingleOrDefault(s => s.Type == pType); } return null; } public string GetSettingValue(SettingsType pType, string key) { SettingsDC dc = this.GetSettingValue(pType); if (dc != null && dc.Value.IndexOf(";") == -1 && dc.Value.IndexOf("=") == -1) { return dc.Value; } else { string[] keyValuePairs = dc.Value.Split(';'); foreach (string pair in keyValuePairs) { string[] keyValuePair = pair.Split('='); if (keyValuePair.Length == 2) { if (keyValuePair[0] == key) { return keyValuePair[1]; } } } } return null; } public bool HasRight(UserRightType right) { return this.GetGrantedRights().Contains(right); } /// /// Prüft, ob der ApplicationUser mindestens ein Recht aus der übergebenen UserRightType-Liste hat /// /// Liste der Rechte, von denen der ApplicationUser mindestens eins haben muss /// Ob der ApplicationUser eins der Rechte hat. public virtual bool CheckForAtLeastOneRight(List rightsToCheck) { foreach (var userGroup in UserGroups) { foreach (var rightRelation in userGroup.Rights) { if (rightsToCheck.Contains(rightRelation)) { return true; } } } return false; } public void SetSettingValue(SettingsType pType, string key, string val) { SettingsDC dc = this.GetSettingValue(pType); if (dc == null) { SettingsDC newDc = new SettingsDC(); newDc.Type = pType; newDc.Value = key + "=" + val; this.Settings.Add(newDc); } else { string[] keyValuePairs = dc.Value.Split(';'); string newValue = string.Empty; foreach (string pair in keyValuePairs) { if (pair.IndexOf(key + "=") != 0) { if (newValue.Length > 0) { newValue += ";"; } newValue += pair; } } if (newValue.Length > 0) { newValue += ";"; } newValue += key + "=" + val; dc.Value = newValue; } } public override string ToString() { return this.Employee.ToString(); } } }