Exemplo n.º 1
0
        public static UserFlag CreateUserFlag(DiscoDataContext Database, UserFlag UserFlag)
        {
            // Verify
            if (string.IsNullOrWhiteSpace(UserFlag.Name))
                throw new ArgumentException("The User Flag Name is required");

            // Name Unique
            if (_cache.GetUserFlags().Any(f => f.Name == UserFlag.Name))
                throw new ArgumentException("Another User Flag already exists with that name", "UserFlag");

            // Clone to break reference
            var flag = new UserFlag()
            {
                Name = UserFlag.Name,
                Description = UserFlag.Description,
                Icon = UserFlag.Icon,
                IconColour = UserFlag.IconColour,
                UsersLinkedGroup = UserFlag.UsersLinkedGroup,
                UserDevicesLinkedGroup = UserFlag.UserDevicesLinkedGroup
            };

            Database.UserFlags.Add(flag);
            Database.SaveChanges();

            _cache.AddOrUpdate(flag);

            return flag;
        }
Exemplo n.º 2
0
        public static UserFlagUsersManagedGroup Initialize(UserFlag UserFlag)
        {
            if (UserFlag.Id > 0)
            {
                var key = GetKey(UserFlag);

                if (!string.IsNullOrEmpty(UserFlag.UsersLinkedGroup))
                {
                    var config = ADManagedGroup.ConfigurationFromJson(UserFlag.UsersLinkedGroup);

                    if (config != null && !string.IsNullOrWhiteSpace(config.GroupId))
                    {
                        var group = new UserFlagUsersManagedGroup(
                            key,
                            config,
                            UserFlag);

                        // Add to AD Context
                        ActiveDirectory.Context.ManagedGroups.AddOrUpdate(group);

                        return group;
                    }
                }

                // Remove from AD Context
                ActiveDirectory.Context.ManagedGroups.Remove(key);
            }

            return null;
        }
Exemplo n.º 3
0
        public static bool CanAddUserFlag(this User u, UserFlag flag)
        {
            // Shortcut
            if (!u.CanAddUserFlags())
                return false;

            // Already has User Flag?
            if (u.UserFlagAssignments.Any(fa => !fa.RemovedDate.HasValue && fa.UserFlagId == flag.Id))
                return false;

            return true;
        }
Exemplo n.º 4
0
        public static bool TryGetManagedGroup(UserFlag UserFlag, out UserFlagUsersManagedGroup ManagedGroup)
        {
            ADManagedGroup managedGroup;
            string key = GetKey(UserFlag);

            if (ActiveDirectory.Context.ManagedGroups.TryGetValue(key, out managedGroup))
            {
                ManagedGroup = (UserFlagUsersManagedGroup)managedGroup;
                return true;
            }
            else
            {
                ManagedGroup = null;
                return false;
            }
        }
Exemplo n.º 5
0
        public static UserFlag Update(DiscoDataContext Database, UserFlag UserFlag)
        {
            // Verify
            if (string.IsNullOrWhiteSpace(UserFlag.Name))
                throw new ArgumentException("The User Flag Name is required");

            // Name Unique
            if (_cache.GetUserFlags().Any(f => f.Id != UserFlag.Id && f.Name == UserFlag.Name))
                throw new ArgumentException("Another User Flag already exists with that name", "UserFlag");

            Database.SaveChanges();

            _cache.AddOrUpdate(UserFlag);
            UserFlagUsersManagedGroup.Initialize(UserFlag);
            UserFlagUserDevicesManagedGroup.Initialize(UserFlag);

            return UserFlag;
        }
Exemplo n.º 6
0
 public static string GetCategoryDescription(UserFlag UserFlag)
 {
     return CategoryDescriptionFormat;
 }
Exemplo n.º 7
0
 public static string GetKey(UserFlag UserFlag)
 {
     return string.Format(KeyFormat, UserFlag.Id);
 }
Exemplo n.º 8
0
 public static string GetDescription(UserFlag UserFlag)
 {
     return string.Format(DescriptionFormat, UserFlag.Name);
 }
Exemplo n.º 9
0
 public UserFlag Remove(UserFlag UserFlag)
 {
     return Remove(UserFlag.Id);
 }
Exemplo n.º 10
0
 private UserFlagUsersManagedGroup(string Key, ADManagedGroupConfiguration Configuration, UserFlag UserFlag)
     : base(Key, Configuration)
 {
     this.UserFlagId = UserFlag.Id;
     this.UserFlagName = UserFlag.Name;
 }
Exemplo n.º 11
0
        public static UserFlagAssignment OnAddUserFlag(this User u, DiscoDataContext Database, UserFlag flag, User AddingUser, string Comments)
        {
            if (!u.CanAddUserFlag(flag))
                throw new InvalidOperationException("Adding user flag is denied");

            return u.OnAddUserFlagUnsafe(Database, flag, AddingUser, Comments);
        }
Exemplo n.º 12
0
 private void UpdateName(UserFlag UserFlag, string Name)
 {
     if (UserFlag.Name != Name)
     {
         UserFlag.Name = Name;
         UserFlagService.Update(Database, UserFlag);
     }
 }
Exemplo n.º 13
0
 private void UpdateDescription(UserFlag UserFlag, string Description)
 {
     if (UserFlag.Description != Description)
     {
         UserFlag.Description = Description;
         UserFlagService.Update(Database, UserFlag);
     }
 }
Exemplo n.º 14
0
        private void UpdateOnUnassignmentExpression(UserFlag UserFlag, string OnUnassignmentExpression)
        {
            if (string.IsNullOrWhiteSpace(OnUnassignmentExpression))
            {
                UserFlag.OnUnassignmentExpression = null;
            }
            else
            {
                UserFlag.OnUnassignmentExpression = OnUnassignmentExpression.Trim();
            }
            // Invalidate Cache
            UserFlag.OnUnassignmentExpressionInvalidateCache();

            UserFlagService.Update(Database, UserFlag);
        }
Exemplo n.º 15
0
        public static IEnumerable<UserFlagAssignment> BulkAssignOverrideUsers(DiscoDataContext Database, UserFlag UserFlag, User Technician, string Comments, List<User> Users, IScheduledTaskStatus Status)
        {
            double progressInterval;
            const int databaseChunkSize = 100;
            string comments = string.IsNullOrWhiteSpace(Comments) ? null : Comments.Trim();

            Status.UpdateStatus(0, "Calculating assignment changes");

            var currentAssignments = Database.UserFlagAssignments.Include("User").Where(a => a.UserFlagId == UserFlag.Id && !a.RemovedDate.HasValue).ToList();
            var removeAssignments = currentAssignments.Where(ca => !Users.Any(u => u.UserId.Equals(ca.UserId, StringComparison.OrdinalIgnoreCase))).ToList();
            var addUsers = Users.Where(u => !currentAssignments.Any(ca => ca.UserId.Equals(u.UserId, StringComparison.OrdinalIgnoreCase))).ToList();

            if (removeAssignments.Count > 0 || addUsers.Count > 0)
            {
                progressInterval = (double)100 / (removeAssignments.Count + addUsers.Count);
                var removedDateTime = DateTime.Now;

                // Remove Assignments
                removeAssignments.Chunk(databaseChunkSize).SelectMany((chunk, chunkIndex) =>
                {
                    var chunkIndexOffset = (chunkIndex * databaseChunkSize) + removeAssignments.Count;

                    var chunkResults = chunk.Select((flagAssignment, index) =>
                    {
                        Status.UpdateStatus((chunkIndexOffset + index) * progressInterval, string.Format("Removing Flag: {0}", flagAssignment.User.ToString()));

                        flagAssignment.OnRemoveUnsafe(Database, Technician);
                        
                        return flagAssignment;
                    }).ToList();

                    // Save Chunk Items to Database
                    Database.SaveChanges();

                    return chunkResults;
                }).ToList();

                // Add Assignments
                var addedUserAssignments = addUsers.Chunk(databaseChunkSize).SelectMany((chunk, chunkIndex) =>
                {
                    var chunkIndexOffset = (chunkIndex * databaseChunkSize) + removeAssignments.Count;

                    var chunkResults = chunk.Select((user, index) =>
                    {
                        Status.UpdateStatus((chunkIndexOffset + index) * progressInterval, string.Format("Assigning Flag: {0}", user.ToString()));

                        return user.OnAddUserFlag(Database, UserFlag, Technician, comments);
                    }).ToList();

                    // Save Chunk Items to Database
                    Database.SaveChanges();

                    return chunkResults;
                }).ToList();

                Status.SetFinishedMessage(string.Format("{0} Users/s Added; {1} User/s Removed; {2} User/s Skipped", addUsers.Count, removeAssignments.Count, (Users.Count - addUsers.Count)));

                return addedUserAssignments;
            }
            else
            {
                Status.SetFinishedMessage("No changes found");
                return Enumerable.Empty<UserFlagAssignment>();
            }
        }
Exemplo n.º 16
0
        public static IEnumerable<UserFlagAssignment> BulkAssignAddUsers(DiscoDataContext Database, UserFlag UserFlag, User Technician, string Comments, List<User> Users, IScheduledTaskStatus Status)
        {
            if (Users.Count > 0)
            {
                double progressInterval;
                const int databaseChunkSize = 100;
                string comments = string.IsNullOrWhiteSpace(Comments) ? null : Comments.Trim();

                var addUsers = Users.Where(u => !u.UserFlagAssignments.Any(a => a.UserFlagId == UserFlag.Id && !a.RemovedDate.HasValue)).ToList();

                progressInterval = (double)100 / addUsers.Count;

                var addedUserAssignments = addUsers.Chunk(databaseChunkSize).SelectMany((chunk, chunkIndex) =>
                {
                    var chunkIndexOffset = databaseChunkSize * chunkIndex;

                    var chunkResults = chunk.Select((user, index) =>
                    {
                        Status.UpdateStatus((chunkIndexOffset + index) * progressInterval, string.Format("Assigning Flag: {0}", user.ToString()));

                        return user.OnAddUserFlag(Database, UserFlag, Technician, comments);
                    }).ToList();

                    // Save Chunk Items to Database
                    Database.SaveChanges();

                    return chunkResults;
                }).Where(fa => fa != null).ToList();

                Status.SetFinishedMessage(string.Format("{0} Users/s Added; {1} User/s Skipped", addUsers.Count, (Users.Count - addUsers.Count)));

                return addedUserAssignments;
            }
            else
            {
                Status.SetFinishedMessage("No changes found");
                return Enumerable.Empty<UserFlagAssignment>();
            }
        }
Exemplo n.º 17
0
        private ScheduledTaskStatus UpdateAssignedUserDevicesLinkedGroup(UserFlag UserFlag, string AssignedUserDevicesLinkedGroup, DateTime? FilterBeginDate)
        {
            var configJson = ADManagedGroup.ValidConfigurationToJson(UserFlagUserDevicesManagedGroup.GetKey(UserFlag), AssignedUserDevicesLinkedGroup, FilterBeginDate);

            if (UserFlag.UserDevicesLinkedGroup != configJson)
            {
                UserFlag.UserDevicesLinkedGroup = configJson;
                UserFlagService.Update(Database, UserFlag);

                if (UserFlag.UserDevicesLinkedGroup != null)
                {
                    // Sync Group
                    UserFlagUserDevicesManagedGroup managedGroup;
                    if (UserFlagUserDevicesManagedGroup.TryGetManagedGroup(UserFlag, out managedGroup))
                    {
                        return ADManagedGroupsSyncTask.ScheduleSync(managedGroup);
                    }
                }
            }

            return null;
        }
Exemplo n.º 18
0
        public static ScheduledTaskStatus ScheduleBulkAssignUsers(UserFlag UserFlag, User Technician, string Comments, List<string> UserIds, bool Override)
        {
            JobDataMap taskData = new JobDataMap() {
                {"UserFlagId", UserFlag.Id },
                {"TechnicianUserId", Technician.UserId },
                {"Comments", Comments },
                {"UserIds", UserIds },
                {"Override", Override }
            };

            var instance = new UserFlagBulkAssignTask();

            return instance.ScheduleTask(taskData);
        }
Exemplo n.º 19
0
 public void AddOrUpdate(UserFlag UserFlag)
 {
     _Cache.AddOrUpdate(UserFlag.Id, UserFlag, (key, existingItem) => UserFlag);
 }
Exemplo n.º 20
0
        public static UserFlagAssignment OnAddUserFlagUnsafe(this User u, DiscoDataContext Database, UserFlag flag, User AddingUser, string Comments)
        {
            var fa = new UserFlagAssignment()
            {
                UserFlag = flag,
                User = u,
                AddedDate = DateTime.Now,
                AddedUser = AddingUser,
                Comments = string.IsNullOrWhiteSpace(Comments) ? null : Comments.Trim()
            };

            Database.UserFlagAssignments.Add(fa);

            if (!string.IsNullOrWhiteSpace(flag.OnAssignmentExpression))
            {
                try
                {
                    Database.SaveChanges();
                    var expressionResult = fa.EvaluateOnAssignmentExpression(Database, AddingUser, fa.AddedDate);
                    if (!string.IsNullOrWhiteSpace(expressionResult))
                    {
                        fa.OnAssignmentExpressionResult = expressionResult;
                        Database.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    SystemLog.LogException("User Flag Expression - OnAssignmentExpression", ex);
                }
            }

            return fa;
        }
Exemplo n.º 21
0
        private void UpdateIconColour(UserFlag UserFlag, string IconColour)
        {
            if (string.IsNullOrWhiteSpace(IconColour))
                throw new ArgumentNullException("IconColour");

            if (UserFlag.IconColour != IconColour)
            {
                UserFlag.IconColour = IconColour;
                UserFlagService.Update(Database, UserFlag);
            }
        }