Files
BeWoPlaner/Shared/DataContracts/ClientPartials/UserDC.cs
2025-06-11 22:56:25 +02:00

142 lines
4.3 KiB
C#

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<UserRightType> GetGrantedRights()
{
return (this.UserGroups != null) ? this.UserGroups.Where(g => g.Rights != null).SelectMany(g => g.Rights).Distinct().ToList() : new List<UserRightType>();
}
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);
}
/// <summary>
/// Prüft, ob der ApplicationUser mindestens ein Recht aus der übergebenen UserRightType-Liste hat
/// </summary>
/// <param name="rightsToCheck">Liste der Rechte, von denen der ApplicationUser mindestens eins haben muss</param>
/// <returns>Ob der ApplicationUser eins der Rechte hat.</returns>
public virtual bool CheckForAtLeastOneRight(List<UserRightType> 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();
}
}
}