Permissions & Authorization for Users #24

Initial Release; Includes Database and MVC refactoring
This commit is contained in:
Gary Sharp
2013-10-10 19:13:16 +11:00
parent 172ce5524a
commit a099d68915
458 changed files with 40221 additions and 12130 deletions
+25 -12
View File
@@ -9,6 +9,7 @@ using System.Text;
using System.Threading.Tasks;
using Disco.BI.Extensions;
using System.Reactive.Linq;
using Disco.Services.Users;
namespace Disco.BI.JobBI
{
@@ -20,10 +21,22 @@ namespace Disco.BI.JobBI
private IDisposable unsubscribeToken;
private object updateLock = new object();
public ManagedJobList Initialize(DiscoDataContext dbContext)
public override List<JobTableItemModel> Items
{
get
{
return base.Items.PermissionsFiltered(UserService.CurrentAuthorization);
}
set
{
throw new InvalidOperationException("Items cannot be manually set in a Managed Job List");
}
}
public ManagedJobList Initialize(DiscoDataContext Database)
{
// Initially fill table
this.Items = this.SortFunction(this.DetermineItems(dbContext, this.FilterFunction(dbContext.Jobs))).ToList();
base.Items = this.SortFunction(this.DetermineItems(Database, this.FilterFunction(Database.Jobs))).ToList();
// Subscribe for Changes
// - Job (or Job Meta) Changes
@@ -65,19 +78,19 @@ namespace Disco.BI.JobBI
if (e.EntityType == typeof(DeviceProfile))
{
int deviceProfileId = ((DeviceProfile)e.Entity).Id;
existingItems = this.Items.Where(i => i.DeviceProfileId == deviceProfileId).ToArray();
existingItems = base.Items.Where(i => i.DeviceProfileId == deviceProfileId).ToArray();
}
else
if (e.EntityType == typeof(DeviceModel))
{
int deviceModelId = ((DeviceModel)e.Entity).Id;
existingItems = this.Items.Where(i => i.DeviceModelId == deviceModelId).ToArray();
existingItems = base.Items.Where(i => i.DeviceModelId == deviceModelId).ToArray();
}
else
if (e.EntityType == typeof(Device))
{
string deviceSerialNumber = ((Device)e.Entity).SerialNumber;
existingItems = this.Items.Where(i => i.DeviceSerialNumber == deviceSerialNumber).ToArray();
existingItems = base.Items.Where(i => i.DeviceSerialNumber == deviceSerialNumber).ToArray();
}
else
return; // Subscription should never reach
@@ -93,20 +106,20 @@ namespace Disco.BI.JobBI
if (jobIds.Count == 0)
return;
else
UpdateJobs(e.dbContext, jobIds, existingItems);
UpdateJobs(e.Database, jobIds, existingItems);
}
private void UpdateJobs(DiscoDataContext dbContext, List<int> jobIds, JobTableItemModel[] existingItems = null)
private void UpdateJobs(DiscoDataContext Database, List<int> jobIds, JobTableItemModel[] existingItems = null)
{
lock (updateLock)
{
// Check for existing items, if not handed them
if (existingItems == null)
existingItems = this.Items.Where(i => jobIds.Contains(i.Id)).ToArray();
existingItems = base.Items.Where(i => jobIds.Contains(i.Id)).ToArray();
var updatedItems = this.DetermineItems(Database, this.FilterFunction(Database.Jobs.Where(j => jobIds.Contains(j.Id))));
var updatedItems = this.DetermineItems(dbContext, this.FilterFunction(dbContext.Jobs.Where(j => jobIds.Contains(j.Id))));
var refreshedList = this.Items.ToList();
var refreshedList = base.Items.ToList();
// Remove Existing
if (existingItems.Length > 0)
@@ -119,7 +132,7 @@ namespace Disco.BI.JobBI
refreshedList.Add(updatedItem);
// Reorder
this.Items = this.SortFunction(refreshedList).ToList();
base.Items = this.SortFunction(refreshedList).ToList();
}
}
+8 -8
View File
@@ -11,7 +11,7 @@ namespace Disco.BI.JobBI
{
public static class Searching
{
public static JobTableModel Search(DiscoDataContext dbContext, string Term, int? LimitCount = null, bool IncludeJobStatus = true, bool SearchDetails = false)
public static JobTableModel Search(DiscoDataContext Database, string Term, int? LimitCount = null, bool IncludeJobStatus = true, bool SearchDetails = false)
{
int termInt = default(int);
@@ -22,7 +22,7 @@ namespace Disco.BI.JobBI
// Term is a Number (int)
if (SearchDetails)
{
query = BuildJobTableModel(dbContext).Where(j =>
query = BuildJobTableModel(Database).Where(j =>
j.Id == termInt ||
j.DeviceHeldLocation.Contains(Term) ||
j.Device.SerialNumber.Contains(Term) ||
@@ -36,7 +36,7 @@ namespace Disco.BI.JobBI
}
else
{
query = BuildJobTableModel(dbContext).Where(j =>
query = BuildJobTableModel(Database).Where(j =>
j.Id == termInt ||
j.DeviceHeldLocation.Contains(Term) ||
j.Device.SerialNumber.Contains(Term) ||
@@ -51,7 +51,7 @@ namespace Disco.BI.JobBI
{
if (SearchDetails)
{
query = BuildJobTableModel(dbContext).Where(j =>
query = BuildJobTableModel(Database).Where(j =>
j.DeviceHeldLocation.Contains(Term) ||
j.Device.SerialNumber.Contains(Term) ||
j.Device.AssetNumber.Contains(Term) ||
@@ -64,7 +64,7 @@ namespace Disco.BI.JobBI
}
else
{
query = BuildJobTableModel(dbContext).Where(j =>
query = BuildJobTableModel(Database).Where(j =>
j.DeviceHeldLocation.Contains(Term) ||
j.Device.SerialNumber.Contains(Term) ||
j.Device.AssetNumber.Contains(Term) ||
@@ -79,14 +79,14 @@ namespace Disco.BI.JobBI
query = query.Take(LimitCount.Value);
JobTableModel model = new JobTableModel() { ShowStatus = IncludeJobStatus };
model.Fill(dbContext, query);
model.Fill(Database, query);
return model;
}
public static IQueryable<Job> BuildJobTableModel(DiscoDataContext dbContext)
public static IQueryable<Job> BuildJobTableModel(DiscoDataContext Database)
{
return dbContext.Jobs.Include("JobType").Include("Device").Include("User").Include("OpenedTechUser");
return Database.Jobs.Include("JobType").Include("Device").Include("User").Include("OpenedTechUser");
}
}
@@ -25,7 +25,7 @@ namespace Disco.BI.JobBI.Statistics
public override bool SingleInstanceTask { get { return true; } }
public override bool CancelInitiallySupported { get { return false; } }
public override void InitalizeScheduledTask(DiscoDataContext dbContext)
public override void InitalizeScheduledTask(DiscoDataContext Database)
{
// Trigger Daily @ 12:29am
TriggerBuilder triggerBuilder = TriggerBuilder.Create().WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(0, 29));
@@ -34,40 +34,13 @@ namespace Disco.BI.JobBI.Statistics
}
protected override void ExecuteTask()
{
using (var dbContext = new DiscoDataContext())
using (var database = new DiscoDataContext())
{
UpdateDataHistory(dbContext, true);
UpdateDataHistory(database, true);
}
}
//public void InitalizeScheduledTask(DiscoDataContext dbContext, IScheduler Scheduler)
//{
// // Run @ 12:29am
// IJobDetail jobDetail = new JobDetailImpl("JobStatisticsDailyOpenedClosed", typeof(DailyOpenedClosed));
// ITrigger trigger = TriggerBuilder.Create().
// WithIdentity("JobStatisticsDailyOpenedClosedTrigger").
// StartNow().
// WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(0, 29)).
// Build();
// Scheduler.ScheduleJob(jobDetail, trigger);
//}
//public void Execute(IJobExecutionContext context)
//{
// try
// {
// using (var dbContext = new DiscoDataContext())
// {
// UpdateDataHistory(dbContext, true);
// }
// }
// catch (Exception ex)
// {
// Logging.SystemLog.LogException("Disco.BI.JobBI.Statistics.DailyOpenedClosed", ex);
// }
//}
private static void UpdateDataHistory(DiscoDataContext dbContext, bool Refresh = false)
private static void UpdateDataHistory(DiscoDataContext Database, bool Refresh = false)
{
DateTime historyEnd = DateTime.Now.Date;
@@ -98,7 +71,7 @@ namespace Disco.BI.JobBI.Statistics
// Cache Data
while (processDate <= historyEnd)
{
resultData.Add(Data(dbContext, processDate));
resultData.Add(Data(Database, processDate));
processDate = processDate.AddDays(1);
}
_data = resultData;
@@ -162,14 +135,14 @@ namespace Disco.BI.JobBI.Statistics
}
}
private static DailyOpenedClosedItem Data(DiscoDataContext dbContext, DateTime ProcessDate)
private static DailyOpenedClosedItem Data(DiscoDataContext Database, DateTime ProcessDate)
{
DateTime processDateStart = ProcessDate;
DateTime processDateEnd = ProcessDate.AddDays(1);
int totalJobs = dbContext.Jobs.Where(j => j.OpenedDate < processDateEnd && (!j.ClosedDate.HasValue || j.ClosedDate > processDateEnd)).Count();
int openedJobs = dbContext.Jobs.Where(j => j.OpenedDate > processDateStart && j.OpenedDate < processDateEnd).Count();
int closedJobs = dbContext.Jobs.Where(j => j.ClosedDate > processDateStart && j.ClosedDate < processDateEnd).Count();
int totalJobs = Database.Jobs.Where(j => j.OpenedDate < processDateEnd && (!j.ClosedDate.HasValue || j.ClosedDate > processDateEnd)).Count();
int openedJobs = Database.Jobs.Where(j => j.OpenedDate > processDateStart && j.OpenedDate < processDateEnd).Count();
int closedJobs = Database.Jobs.Where(j => j.ClosedDate > processDateStart && j.ClosedDate < processDateEnd).Count();
return new DailyOpenedClosedItem()
{
@@ -180,11 +153,11 @@ namespace Disco.BI.JobBI.Statistics
};
}
public static List<DailyOpenedClosedItem> Data(DiscoDataContext dbContext, bool FilterUnimportantWeekends = false)
public static List<DailyOpenedClosedItem> Data(DiscoDataContext Database, bool FilterUnimportantWeekends = false)
{
List<DailyOpenedClosedItem> resultData;
UpdateDataHistory(dbContext);
UpdateDataHistory(Database);
if (FilterUnimportantWeekends)
resultData = _data.Where(i => (i.Timestamp.DayOfWeek != DayOfWeek.Saturday && i.Timestamp.DayOfWeek != DayOfWeek.Sunday) ||
@@ -192,9 +165,6 @@ namespace Disco.BI.JobBI.Statistics
else
resultData = _data.ToList();
// Removed - Live Updated via Repository Monitor; See: RepositoryEvent_JobChange
//resultData.Add(Data(dbContext, DateTime.Today));
return resultData;
}
}
+6 -6
View File
@@ -10,7 +10,7 @@ namespace Disco.BI.JobBI
{
public static class Utilities
{
public static Job Create(DiscoDataContext dbContext, Device device, User user, JobType type, List<JobSubType> subTypes, User initialTech)
public static Job Create(DiscoDataContext Database, Device device, User user, JobType type, List<JobSubType> subTypes, User initialTech)
{
Job j = new Job()
{
@@ -38,19 +38,19 @@ namespace Disco.BI.JobBI
List<JobSubType> jobSubTypes = subTypes.ToList();
j.JobSubTypes = jobSubTypes;
dbContext.Jobs.Add(j);
Database.Jobs.Add(j);
switch (type.Id)
{
case JobType.JobTypeIds.HWar:
dbContext.JobMetaWarranties.Add(new JobMetaWarranty() { Job = j });
Database.JobMetaWarranties.Add(new JobMetaWarranty() { Job = j });
break;
case JobType.JobTypeIds.HNWar:
dbContext.JobMetaNonWarranties.Add(new JobMetaNonWarranty() { Job = j });
Database.JobMetaNonWarranties.Add(new JobMetaNonWarranty() { Job = j });
if (device != null)
{
// Add Job Components
var components = dbContext.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId);
var components = Database.DeviceComponents.Include("JobSubTypes").Where(c => !c.DeviceModelId.HasValue || c.DeviceModelId == j.Device.DeviceModelId);
var addedComponents = new List<DeviceComponent>();
foreach (var c in components)
{
@@ -76,7 +76,7 @@ namespace Disco.BI.JobBI
}
}
foreach (var c in addedComponents)
dbContext.JobComponents.Add(new JobComponent()
Database.JobComponents.Add(new JobComponent()
{
Job = j,
TechUserId = initialTech.Id,