GIT: perform LF normalization
This commit is contained in:
@@ -1,61 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Models.BI.Search;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
|
||||
namespace Disco.BI.UserBI
|
||||
{
|
||||
public static class Searching
|
||||
{
|
||||
|
||||
public static List<User> SearchUpstream(string Term)
|
||||
{
|
||||
return Interop.ActiveDirectory.ActiveDirectory.SearchUsers(Term).Select(adU => adU.ToRepositoryUser()).ToList();
|
||||
}
|
||||
|
||||
private static List<UserSearchResultItem> Search_SelectUserSearchResultItems(IQueryable<User> Query, int? LimitCount = null)
|
||||
{
|
||||
if (LimitCount.HasValue)
|
||||
Query = Query.Take(LimitCount.Value);
|
||||
|
||||
return Query.Select(u => new UserSearchResultItem()
|
||||
{
|
||||
Id = u.Id,
|
||||
Surname = u.Surname,
|
||||
GivenName = u.GivenName,
|
||||
DisplayName = u.DisplayName,
|
||||
AssignedDevicesCount = u.DeviceUserAssignments.Where(dua => !dua.UnassignedDate.HasValue).Count(),
|
||||
JobCount = u.Jobs.Count()
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public static List<UserSearchResultItem> Search(DiscoDataContext dbContext, string Term, int? LimitCount = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Term) || Term.Length < 2)
|
||||
throw new ArgumentException("Search Term must contain at least two characters", "Term");
|
||||
|
||||
// Search Active Directory & Import Relevant Users
|
||||
var adImportedUsers = Interop.ActiveDirectory.ActiveDirectory.SearchUsers(Term).Select(adU => adU.ToRepositoryUser());
|
||||
foreach (var adU in adImportedUsers)
|
||||
{
|
||||
var existingUser = dbContext.Users.Find(adU.Id);
|
||||
if (existingUser != null)
|
||||
existingUser.UpdateSelf(adU);
|
||||
else
|
||||
dbContext.Users.Add(adU);
|
||||
dbContext.SaveChanges();
|
||||
UserCache.InvalidateValue(adU.Id);
|
||||
}
|
||||
|
||||
return Search_SelectUserSearchResultItems(dbContext.Users.Where(u =>
|
||||
u.Id.Contains(Term) ||
|
||||
u.Surname.Contains(Term) ||
|
||||
u.GivenName.Contains(Term) ||
|
||||
u.DisplayName.Contains(Term)
|
||||
), LimitCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Models.BI.Search;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
|
||||
namespace Disco.BI.UserBI
|
||||
{
|
||||
public static class Searching
|
||||
{
|
||||
|
||||
public static List<User> SearchUpstream(string Term)
|
||||
{
|
||||
return Interop.ActiveDirectory.ActiveDirectory.SearchUsers(Term).Select(adU => adU.ToRepositoryUser()).ToList();
|
||||
}
|
||||
|
||||
private static List<UserSearchResultItem> Search_SelectUserSearchResultItems(IQueryable<User> Query, int? LimitCount = null)
|
||||
{
|
||||
if (LimitCount.HasValue)
|
||||
Query = Query.Take(LimitCount.Value);
|
||||
|
||||
return Query.Select(u => new UserSearchResultItem()
|
||||
{
|
||||
Id = u.Id,
|
||||
Surname = u.Surname,
|
||||
GivenName = u.GivenName,
|
||||
DisplayName = u.DisplayName,
|
||||
AssignedDevicesCount = u.DeviceUserAssignments.Where(dua => !dua.UnassignedDate.HasValue).Count(),
|
||||
JobCount = u.Jobs.Count()
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public static List<UserSearchResultItem> Search(DiscoDataContext dbContext, string Term, int? LimitCount = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Term) || Term.Length < 2)
|
||||
throw new ArgumentException("Search Term must contain at least two characters", "Term");
|
||||
|
||||
// Search Active Directory & Import Relevant Users
|
||||
var adImportedUsers = Interop.ActiveDirectory.ActiveDirectory.SearchUsers(Term).Select(adU => adU.ToRepositoryUser());
|
||||
foreach (var adU in adImportedUsers)
|
||||
{
|
||||
var existingUser = dbContext.Users.Find(adU.Id);
|
||||
if (existingUser != null)
|
||||
existingUser.UpdateSelf(adU);
|
||||
else
|
||||
dbContext.Users.Add(adU);
|
||||
dbContext.SaveChanges();
|
||||
UserCache.InvalidateValue(adU.Id);
|
||||
}
|
||||
|
||||
return Search_SelectUserSearchResultItems(dbContext.Users.Where(u =>
|
||||
u.Id.Contains(Term) ||
|
||||
u.Surname.Contains(Term) ||
|
||||
u.GivenName.Contains(Term) ||
|
||||
u.DisplayName.Contains(Term)
|
||||
), LimitCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+193
-193
@@ -1,193 +1,193 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.Concurrent;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using System.Web;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Disco.Services.Tasks;
|
||||
|
||||
namespace Disco.BI.UserBI
|
||||
{
|
||||
public class UserCache : ScheduledTask
|
||||
{
|
||||
private static ConcurrentDictionary<string, Tuple<User, DateTime>> _Cache = new ConcurrentDictionary<string, Tuple<User, DateTime>>();
|
||||
private const long CacheTimeoutTicks = 6000000000; // 10 Minutes
|
||||
private const string CacheHttpRequestKey = "Disco_CurrentUser";
|
||||
|
||||
public static User CurrentUser
|
||||
{
|
||||
get
|
||||
{
|
||||
string username = null;
|
||||
User user;
|
||||
|
||||
// Check for ASP.NET
|
||||
if (HttpContext.Current != null)
|
||||
{
|
||||
if (HttpContext.Current.Request.IsAuthenticated)
|
||||
{
|
||||
user = (User)HttpContext.Current.Items[CacheHttpRequestKey];
|
||||
if (user != null)
|
||||
return user;
|
||||
|
||||
username = HttpContext.Current.User.Identity.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
//throw new PlatformNotSupportedException("ASP.NET Authentication is not correctly configured");
|
||||
}
|
||||
}
|
||||
|
||||
// User default User
|
||||
if (username == null)
|
||||
{
|
||||
username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||
}
|
||||
|
||||
user = GetUser(username);
|
||||
|
||||
if (HttpContext.Current != null && HttpContext.Current.Request.IsAuthenticated)
|
||||
{
|
||||
// Cache in current request
|
||||
HttpContext.Current.Items[CacheHttpRequestKey] = user;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
public static User GetUser(string Username)
|
||||
{
|
||||
// Check Cache
|
||||
User u = TryUserCache(Username);
|
||||
|
||||
if (u == null)
|
||||
{
|
||||
// Load from Repository
|
||||
using (DiscoDataContext dbContext = new DiscoDataContext())
|
||||
{
|
||||
u = GetUser(Username, dbContext, true);
|
||||
}
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
public static User GetUser(string Username, DiscoDataContext dbContext, bool ForceRefresh = false)
|
||||
{
|
||||
User u = null;
|
||||
|
||||
// Check Cache
|
||||
if (!ForceRefresh)
|
||||
u = TryUserCache(Username);
|
||||
|
||||
if (u == null)
|
||||
{
|
||||
string username = Username.ToLower();
|
||||
u = UserBI.Utilities.LoadUser(dbContext, username);
|
||||
SetValue(username, u);
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
private static User TryUserCache(string Username)
|
||||
{
|
||||
string username = Username.ToLower();
|
||||
Tuple<User, DateTime> userRecord;
|
||||
if (_Cache.TryGetValue(username, out userRecord))
|
||||
{
|
||||
if (userRecord.Item2 > DateTime.Now)
|
||||
return userRecord.Item1;
|
||||
else
|
||||
_Cache.TryRemove(username, out userRecord);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool InvalidateValue(string Key)
|
||||
{
|
||||
Tuple<User, DateTime> userRecord;
|
||||
return _Cache.TryRemove(Key.ToLower(), out userRecord);
|
||||
}
|
||||
|
||||
private static bool SetValue(string Key, User User)
|
||||
{
|
||||
string key = Key.ToLower();
|
||||
Tuple<User, DateTime> userRecord = new Tuple<User, DateTime>(User, DateTime.Now.AddTicks(CacheTimeoutTicks));
|
||||
if (_Cache.ContainsKey(key))
|
||||
{
|
||||
Tuple<User, DateTime> oldUser;
|
||||
if (_Cache.TryGetValue(key, out oldUser))
|
||||
{
|
||||
return _Cache.TryUpdate(key, userRecord, oldUser);
|
||||
}
|
||||
}
|
||||
return _Cache.TryAdd(key, userRecord);
|
||||
}
|
||||
|
||||
private static void CleanStaleCache()
|
||||
{
|
||||
var usernames = _Cache.Keys.ToArray();
|
||||
foreach (string username in usernames)
|
||||
{
|
||||
Tuple<User, DateTime> userRecord;
|
||||
if (_Cache.TryGetValue(username, out userRecord))
|
||||
{
|
||||
if (userRecord.Item2 <= DateTime.Now)
|
||||
_Cache.TryRemove(username, out userRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//public void InitalizeScheduledTask(DiscoDataContext dbContext, IScheduler Scheduler)
|
||||
//{
|
||||
// // Run @ every 15mins
|
||||
|
||||
// // Next 15min interval
|
||||
// DateTime now = DateTime.Now;
|
||||
// int mins = (15 - (now.Minute % 15));
|
||||
// if (mins < 10)
|
||||
// mins += 15;
|
||||
// DateTimeOffset startAt = new DateTimeOffset(now).AddMinutes(mins).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
|
||||
|
||||
// IJobDetail jobDetail = new JobDetailImpl("UserCache_CleanStaleCache", typeof(UserCache));
|
||||
// ITrigger trigger = TriggerBuilder.Create().
|
||||
// WithIdentity("UserCache_CleanStaleCacheTrigger").StartAt(startAt).
|
||||
// WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(15)).
|
||||
// Build();
|
||||
// Scheduler.ScheduleJob(jobDetail, trigger);
|
||||
//}
|
||||
|
||||
public override string TaskName { get { return "User Cache - Clean Stale Cache"; } }
|
||||
|
||||
public override bool SingleInstanceTask { get { return true; } }
|
||||
public override bool CancelInitiallySupported { get { return false; } }
|
||||
public override bool LogExceptionsOnly { get { return true; } }
|
||||
|
||||
public override void InitalizeScheduledTask(DiscoDataContext dbContext)
|
||||
{
|
||||
// Run @ every 15mins
|
||||
|
||||
// Next 15min interval
|
||||
DateTime now = DateTime.Now;
|
||||
int mins = (15 - (now.Minute % 15));
|
||||
if (mins < 10)
|
||||
mins += 15;
|
||||
DateTimeOffset startAt = new DateTimeOffset(now).AddMinutes(mins).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
|
||||
|
||||
TriggerBuilder triggerBuilder = TriggerBuilder.Create().StartAt(startAt).
|
||||
WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(15));
|
||||
|
||||
this.ScheduleTask(triggerBuilder);
|
||||
}
|
||||
|
||||
protected override void ExecuteTask()
|
||||
{
|
||||
CleanStaleCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.Concurrent;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using System.Web;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Disco.Services.Tasks;
|
||||
|
||||
namespace Disco.BI.UserBI
|
||||
{
|
||||
public class UserCache : ScheduledTask
|
||||
{
|
||||
private static ConcurrentDictionary<string, Tuple<User, DateTime>> _Cache = new ConcurrentDictionary<string, Tuple<User, DateTime>>();
|
||||
private const long CacheTimeoutTicks = 6000000000; // 10 Minutes
|
||||
private const string CacheHttpRequestKey = "Disco_CurrentUser";
|
||||
|
||||
public static User CurrentUser
|
||||
{
|
||||
get
|
||||
{
|
||||
string username = null;
|
||||
User user;
|
||||
|
||||
// Check for ASP.NET
|
||||
if (HttpContext.Current != null)
|
||||
{
|
||||
if (HttpContext.Current.Request.IsAuthenticated)
|
||||
{
|
||||
user = (User)HttpContext.Current.Items[CacheHttpRequestKey];
|
||||
if (user != null)
|
||||
return user;
|
||||
|
||||
username = HttpContext.Current.User.Identity.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
//throw new PlatformNotSupportedException("ASP.NET Authentication is not correctly configured");
|
||||
}
|
||||
}
|
||||
|
||||
// User default User
|
||||
if (username == null)
|
||||
{
|
||||
username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||
}
|
||||
|
||||
user = GetUser(username);
|
||||
|
||||
if (HttpContext.Current != null && HttpContext.Current.Request.IsAuthenticated)
|
||||
{
|
||||
// Cache in current request
|
||||
HttpContext.Current.Items[CacheHttpRequestKey] = user;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
public static User GetUser(string Username)
|
||||
{
|
||||
// Check Cache
|
||||
User u = TryUserCache(Username);
|
||||
|
||||
if (u == null)
|
||||
{
|
||||
// Load from Repository
|
||||
using (DiscoDataContext dbContext = new DiscoDataContext())
|
||||
{
|
||||
u = GetUser(Username, dbContext, true);
|
||||
}
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
public static User GetUser(string Username, DiscoDataContext dbContext, bool ForceRefresh = false)
|
||||
{
|
||||
User u = null;
|
||||
|
||||
// Check Cache
|
||||
if (!ForceRefresh)
|
||||
u = TryUserCache(Username);
|
||||
|
||||
if (u == null)
|
||||
{
|
||||
string username = Username.ToLower();
|
||||
u = UserBI.Utilities.LoadUser(dbContext, username);
|
||||
SetValue(username, u);
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
private static User TryUserCache(string Username)
|
||||
{
|
||||
string username = Username.ToLower();
|
||||
Tuple<User, DateTime> userRecord;
|
||||
if (_Cache.TryGetValue(username, out userRecord))
|
||||
{
|
||||
if (userRecord.Item2 > DateTime.Now)
|
||||
return userRecord.Item1;
|
||||
else
|
||||
_Cache.TryRemove(username, out userRecord);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool InvalidateValue(string Key)
|
||||
{
|
||||
Tuple<User, DateTime> userRecord;
|
||||
return _Cache.TryRemove(Key.ToLower(), out userRecord);
|
||||
}
|
||||
|
||||
private static bool SetValue(string Key, User User)
|
||||
{
|
||||
string key = Key.ToLower();
|
||||
Tuple<User, DateTime> userRecord = new Tuple<User, DateTime>(User, DateTime.Now.AddTicks(CacheTimeoutTicks));
|
||||
if (_Cache.ContainsKey(key))
|
||||
{
|
||||
Tuple<User, DateTime> oldUser;
|
||||
if (_Cache.TryGetValue(key, out oldUser))
|
||||
{
|
||||
return _Cache.TryUpdate(key, userRecord, oldUser);
|
||||
}
|
||||
}
|
||||
return _Cache.TryAdd(key, userRecord);
|
||||
}
|
||||
|
||||
private static void CleanStaleCache()
|
||||
{
|
||||
var usernames = _Cache.Keys.ToArray();
|
||||
foreach (string username in usernames)
|
||||
{
|
||||
Tuple<User, DateTime> userRecord;
|
||||
if (_Cache.TryGetValue(username, out userRecord))
|
||||
{
|
||||
if (userRecord.Item2 <= DateTime.Now)
|
||||
_Cache.TryRemove(username, out userRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//public void InitalizeScheduledTask(DiscoDataContext dbContext, IScheduler Scheduler)
|
||||
//{
|
||||
// // Run @ every 15mins
|
||||
|
||||
// // Next 15min interval
|
||||
// DateTime now = DateTime.Now;
|
||||
// int mins = (15 - (now.Minute % 15));
|
||||
// if (mins < 10)
|
||||
// mins += 15;
|
||||
// DateTimeOffset startAt = new DateTimeOffset(now).AddMinutes(mins).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
|
||||
|
||||
// IJobDetail jobDetail = new JobDetailImpl("UserCache_CleanStaleCache", typeof(UserCache));
|
||||
// ITrigger trigger = TriggerBuilder.Create().
|
||||
// WithIdentity("UserCache_CleanStaleCacheTrigger").StartAt(startAt).
|
||||
// WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(15)).
|
||||
// Build();
|
||||
// Scheduler.ScheduleJob(jobDetail, trigger);
|
||||
//}
|
||||
|
||||
public override string TaskName { get { return "User Cache - Clean Stale Cache"; } }
|
||||
|
||||
public override bool SingleInstanceTask { get { return true; } }
|
||||
public override bool CancelInitiallySupported { get { return false; } }
|
||||
public override bool LogExceptionsOnly { get { return true; } }
|
||||
|
||||
public override void InitalizeScheduledTask(DiscoDataContext dbContext)
|
||||
{
|
||||
// Run @ every 15mins
|
||||
|
||||
// Next 15min interval
|
||||
DateTime now = DateTime.Now;
|
||||
int mins = (15 - (now.Minute % 15));
|
||||
if (mins < 10)
|
||||
mins += 15;
|
||||
DateTimeOffset startAt = new DateTimeOffset(now).AddMinutes(mins).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);
|
||||
|
||||
TriggerBuilder triggerBuilder = TriggerBuilder.Create().StartAt(startAt).
|
||||
WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(15));
|
||||
|
||||
this.ScheduleTask(triggerBuilder);
|
||||
}
|
||||
|
||||
protected override void ExecuteTask()
|
||||
{
|
||||
CleanStaleCache();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.BI.Search;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.DirectoryServices.ActiveDirectory;
|
||||
using Disco.Services.Logging;
|
||||
|
||||
namespace Disco.BI.UserBI
|
||||
{
|
||||
public static class Utilities
|
||||
{
|
||||
|
||||
public static User LoadUser(DiscoDataContext dbContext, string Username)
|
||||
{
|
||||
// Machine Account ?
|
||||
if (Username.EndsWith("$"))
|
||||
{
|
||||
return Interop.ActiveDirectory.ActiveDirectory.GetMachineAccount(Username).ToRepositoryUser();
|
||||
}
|
||||
|
||||
// User Account
|
||||
User user = null;
|
||||
try
|
||||
{
|
||||
var ADUser = Interop.ActiveDirectory.ActiveDirectory.GetUserAccount(Username);
|
||||
if (ADUser == null)
|
||||
throw new ArgumentException(string.Format("Invalid Username: '{0}'", Username), "Username");
|
||||
user = ADUser.ToRepositoryUser();
|
||||
}
|
||||
catch (COMException ex)
|
||||
{
|
||||
// If "Server is not operational" then Try Cache
|
||||
if (ex.ErrorCode != -2147016646)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
SystemLog.LogException("Primary Domain Controller Down? Disco.BI.UserBI.Utilities.LoadUser", ex);
|
||||
}
|
||||
catch (ActiveDirectoryOperationException ex)
|
||||
{
|
||||
// Try From Cache...
|
||||
SystemLog.LogException("Primary Domain Controller Down? Disco.BI.UserBI.Utilities.LoadUser", ex);
|
||||
}
|
||||
|
||||
// Update Repository
|
||||
User existingUser;
|
||||
if (user == null)
|
||||
{
|
||||
string username = Username.Contains(@"\") ? Username.Substring(Username.IndexOf(@"\") + 1) : Username;
|
||||
existingUser = dbContext.Users.Find(username);
|
||||
if (existingUser == null)
|
||||
throw new ArgumentException(string.Format("Invalid User - Not In Disco DB: '{0}'", Username), "Username");
|
||||
else
|
||||
return existingUser;
|
||||
}
|
||||
existingUser = dbContext.Users.Find(user.Id);
|
||||
if (existingUser == null)
|
||||
{
|
||||
dbContext.Users.Add(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
existingUser.UpdateSelf(user);
|
||||
user = existingUser;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.BI.Search;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.DirectoryServices.ActiveDirectory;
|
||||
using Disco.Services.Logging;
|
||||
|
||||
namespace Disco.BI.UserBI
|
||||
{
|
||||
public static class Utilities
|
||||
{
|
||||
|
||||
public static User LoadUser(DiscoDataContext dbContext, string Username)
|
||||
{
|
||||
// Machine Account ?
|
||||
if (Username.EndsWith("$"))
|
||||
{
|
||||
return Interop.ActiveDirectory.ActiveDirectory.GetMachineAccount(Username).ToRepositoryUser();
|
||||
}
|
||||
|
||||
// User Account
|
||||
User user = null;
|
||||
try
|
||||
{
|
||||
var ADUser = Interop.ActiveDirectory.ActiveDirectory.GetUserAccount(Username);
|
||||
if (ADUser == null)
|
||||
throw new ArgumentException(string.Format("Invalid Username: '{0}'", Username), "Username");
|
||||
user = ADUser.ToRepositoryUser();
|
||||
}
|
||||
catch (COMException ex)
|
||||
{
|
||||
// If "Server is not operational" then Try Cache
|
||||
if (ex.ErrorCode != -2147016646)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
SystemLog.LogException("Primary Domain Controller Down? Disco.BI.UserBI.Utilities.LoadUser", ex);
|
||||
}
|
||||
catch (ActiveDirectoryOperationException ex)
|
||||
{
|
||||
// Try From Cache...
|
||||
SystemLog.LogException("Primary Domain Controller Down? Disco.BI.UserBI.Utilities.LoadUser", ex);
|
||||
}
|
||||
|
||||
// Update Repository
|
||||
User existingUser;
|
||||
if (user == null)
|
||||
{
|
||||
string username = Username.Contains(@"\") ? Username.Substring(Username.IndexOf(@"\") + 1) : Username;
|
||||
existingUser = dbContext.Users.Find(username);
|
||||
if (existingUser == null)
|
||||
throw new ArgumentException(string.Format("Invalid User - Not In Disco DB: '{0}'", Username), "Username");
|
||||
else
|
||||
return existingUser;
|
||||
}
|
||||
existingUser = dbContext.Users.Find(user.Id);
|
||||
if (existingUser == null)
|
||||
{
|
||||
dbContext.Users.Add(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
existingUser.UpdateSelf(user);
|
||||
user = existingUser;
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user