private static List<string> FilterAllowedTypes(AuthorizationToken Authorization) { if (!Authorization.HasAll(Claims.Job.Types.ShowHMisc, Claims.Job.Types.ShowHNWar, Claims.Job.Types.ShowHWar, Claims.Job.Types.ShowSApp, Claims.Job.Types.ShowSImg, Claims.Job.Types.ShowSOS, Claims.Job.Types.ShowUMgmt)) { // Must Filter List<string> allowedTypes = new List<string>(6); if (Authorization.Has(Claims.Job.Types.ShowHMisc)) allowedTypes.Add(JobType.JobTypeIds.HMisc); if (Authorization.Has(Claims.Job.Types.ShowHNWar)) allowedTypes.Add(JobType.JobTypeIds.HNWar); if (Authorization.Has(Claims.Job.Types.ShowHWar)) allowedTypes.Add(JobType.JobTypeIds.HWar); if (Authorization.Has(Claims.Job.Types.ShowSApp)) allowedTypes.Add(JobType.JobTypeIds.SApp); if (Authorization.Has(Claims.Job.Types.ShowSImg)) allowedTypes.Add(JobType.JobTypeIds.SImg); if (Authorization.Has(Claims.Job.Types.ShowSOS)) allowedTypes.Add(JobType.JobTypeIds.SOS); if (Authorization.Has(Claims.Job.Types.ShowUMgmt)) allowedTypes.Add(JobType.JobTypeIds.UMgmt); return allowedTypes; } return null; }
public static IQueryable<Job> FilterPermissions(this JobTableModel model, IQueryable<Job> Jobs, AuthorizationToken Authorization) { var allowedTypes = FilterAllowedTypes(Authorization); if (allowedTypes != null) { return Jobs.Where(j => allowedTypes.Contains(j.JobTypeId)); } return Jobs; }
public static JobTableModel MyJobsTable(AuthorizationToken AuthToken) { var openJobs = ManagedJobList.openJobs.Items.PermissionsFiltered(AuthToken).Cast<JobTableStatusItemModel>(); IEnumerable<Tuple<JobTableStatusItemModel, byte, byte, DateTime?>> allJobs = null; if (AuthToken.Has(Claims.Job.Lists.MyJobsOrphaned)) { allJobs = AwaitingTechnicianActionFilter( openJobs.Where(i => i.ActiveJobQueues == null || i.ActiveJobQueues.Count() == 0) ).Select(i => new Tuple<JobTableStatusItemModel, byte, byte, DateTime?>(i, (byte)JobQueuePriority.Normal, (byte)JobQueuePriority.Normal, null)); } var usersQueues = JobQueueService.UsersQueues(AuthToken).ToDictionary(q => q.JobQueue.Id); var queueJobs = openJobs .Where(i => i.ActiveJobQueues != null && i.ActiveJobQueues.Any(jqj => usersQueues.ContainsKey(jqj.QueueId))) .Select(i => new Tuple<JobTableStatusItemModel, byte, byte, DateTime?>( i, (byte)i.ActiveJobQueues.Where(q => usersQueues.ContainsKey(q.QueueId)).Max(q => (int)usersQueues[q.QueueId].JobQueue.Priority), (byte)i.ActiveJobQueues.Where(q => usersQueues.ContainsKey(q.QueueId)).Max(q => (int)q.Priority), i.ActiveJobQueues.Where(q => usersQueues.ContainsKey(q.QueueId) && q.SLAExpiresDate.HasValue).Min(q => q.SLAExpiresDate) )); if (allJobs != null) allJobs = allJobs.Concat(queueJobs).ToList(); else allJobs = queueJobs.ToList(); var allJobsSorted = allJobs .OrderByDescending(i => i.Item2).ThenByDescending(i => i.Item3).ThenBy(i => i.Item4).ThenBy(i => i.Item1.OpenedDate).Select(q => q.Item1); return new JobTableModel() { ShowStatus = true, Items = allJobsSorted.ToList() }; }
public static IEnumerable<JobTableItemModel> PermissionsFiltered(this IEnumerable<JobTableItemModel> Items, AuthorizationToken Authorization) { if (Items != null && Items.Count() > 0) { var allowedTypes = FilterAllowedTypes(Authorization); if (allowedTypes != null) { return Items.Where(j => allowedTypes.Contains(j.JobTypeId)).ToList(); } } return Items; }
public static IEnumerable<JobTableStatusQueueItemModel> UsersQueueItems(this IEnumerable<JobTableStatusQueueItemModel> queueItems, AuthorizationToken Authorization) { var usersQueues = Jobs.JobQueues.JobQueueService.UsersQueues(Authorization).ToDictionary(q => q.JobQueue.Id); return queueItems.Where(qi => usersQueues.ContainsKey(qi.QueueId)); }
public void UpdateModel(DiscoDataContext Database, AuthorizationToken Authorization) { if (this.JobTypes == null) this.JobTypes = Database.JobTypes.Include("JobSubTypes.JobQueues").FilterCreatableTypePermissions(Authorization).ToList(); if (!string.IsNullOrEmpty(DeviceSerialNumber)) { this.Device = Database.Devices.Include("DeviceModel").Where(d => d.SerialNumber == DeviceSerialNumber).FirstOrDefault(); if (this.Device == null) { throw new ArgumentException("Invalid Device Serial Number Specified", "DeviceSerialNumber"); } if (string.IsNullOrEmpty(this.UserId) && !string.IsNullOrEmpty(this.Device.AssignedUserId)) { this.UserId = this.Device.AssignedUserId; } if (string.IsNullOrEmpty(this.Type)) this.Type = this.JobTypes.Any(jt => jt.Id == Disco.Models.Repository.JobType.JobTypeIds.HWar) ? Disco.Models.Repository.JobType.JobTypeIds.HWar : this.JobTypes.First().Id; if (string.IsNullOrEmpty(this.UserId)) { // No User - Remove User Types foreach (var jobType in JobTypes.ToArray()) { switch (jobType.Id) { case Disco.Models.Repository.JobType.JobTypeIds.UMgmt: JobTypes.Remove(jobType); break; default: break; } } } } else { // No Device - Remove Hardware Types foreach (var jobType in JobTypes.ToArray()) { switch (jobType.Id) { case Disco.Models.Repository.JobType.JobTypeIds.HMisc: case Disco.Models.Repository.JobType.JobTypeIds.HNWar: case Disco.Models.Repository.JobType.JobTypeIds.HWar: case Disco.Models.Repository.JobType.JobTypeIds.SImg: JobTypes.Remove(jobType); break; default: break; } } // Set Default Job Type for Users if (string.IsNullOrEmpty(this.Type)) this.Type = this.JobTypes.Any(jt => jt.Id == Disco.Models.Repository.JobType.JobTypeIds.SApp) ? Disco.Models.Repository.JobType.JobTypeIds.SApp : this.JobTypes.First().Id; } if (!string.IsNullOrEmpty(UserId)) { this.User = Database.Users.Find(UserId); if (this.User == null) { throw new ArgumentException("Invalid User Id Specified", "UserId"); } if (string.IsNullOrEmpty(this.Type)) this.Type = Disco.Models.Repository.JobType.JobTypeIds.SApp; } if (this.User == null && this.Device == null) { throw new InvalidOperationException("A Job must reference a Device and/or a User"); } }
public static IQueryable<JobType> FilterCreatableTypePermissions(this IQueryable<JobType> JobTypes, AuthorizationToken Authorization) { var allowedTypes = FilterCreatableTypePermissions(Authorization); if (allowedTypes != null) { return JobTypes.Where(jt => allowedTypes.Contains(jt.Id)); } return JobTypes; }
public static ReadOnlyCollection<JobQueueToken> UsersQueues(AuthorizationToken UserAuthorization) { string cacheKey = string.Format(_cacheHttpRequestKey, UserAuthorization.User.UserId); ReadOnlyCollection<JobQueueToken> tokens = null; // Check for ASP.NET if (HttpContext.Current != null) { tokens = (ReadOnlyCollection<JobQueueToken>)HttpContext.Current.Items[_cacheHttpRequestKey]; } if (tokens == null) { var subjectIds = (new string[] { UserAuthorization.User.UserId }).Concat(UserAuthorization.GroupMembership); tokens = _cache.GetQueuesForSubject(subjectIds); HttpContext.Current.Items[_cacheHttpRequestKey] = tokens; } return tokens; }