public static IEnumerable<SelectListItem> ToSelectListItems(this IEnumerable<DeviceProfile> deviceProfiles, DeviceProfile SelectedDeviceProfile = null)
        {
            var selectedId = 1;

            if (SelectedDeviceProfile != null)
                selectedId = SelectedDeviceProfile.Id;

            return deviceProfiles.ToSelectListItems(selectedId);
        }
        public static bool TryGetManagedGroup(DeviceProfile DeviceProfile, out DeviceProfileAssignedUsersManagedGroup ManagedGroup)
        {
            ADManagedGroup managedGroup;
            string key = GetKey(DeviceProfile);

            if (ActiveDirectory.Context.ManagedGroups.TryGetValue(key, out managedGroup))
            {
                ManagedGroup = (DeviceProfileAssignedUsersManagedGroup)managedGroup;
                return true;
            }
            else
            {
                ManagedGroup = null;
                return false;
            }
        }
        private void UpdateProvisionADAccount(DeviceProfile deviceProfile, string ProvisionADAccount)
        {
            bool bValue;
            if (bool.TryParse(ProvisionADAccount, out bValue))
            {
                deviceProfile.ProvisionADAccount = bValue;

                Database.SaveChanges();
                return;
            }
            throw new Exception("Invalid Boolean Value");
        }
 public static string GetDescription(DeviceProfile DeviceProfile)
 {
     return string.Format(DescriptionFormat, DeviceProfile.Name);
 }
 public static string GetCategoryDescription(DeviceProfile DeviceProfile)
 {
     return CategoryDescriptionFormat;
 }
 private DeviceProfileAssignedUsersManagedGroup(string Key, ADManagedGroupConfiguration Configuration, DeviceProfile DeviceProfile)
     : base(Key, Configuration)
 {
     this.DeviceProfileId = DeviceProfile.Id;
     this.DeviceProfileName = DeviceProfile.Name;
 }
 public static string GetKey(DeviceProfile DeviceProfile)
 {
     return string.Format(KeyFormat, DeviceProfile.Id);
 }
        private void UpdateAllowUntrustedReimageJobEnrolment(DeviceProfile deviceProfile, string AllowUntrustedReimageJobEnrolment)
        {
            bool bValue;
            if (bool.TryParse(AllowUntrustedReimageJobEnrolment, out bValue))
            {
                deviceProfile.AllowUntrustedReimageJobEnrolment = bValue;

                Database.SaveChanges();
                return;
            }
            throw new Exception("Invalid Boolean Value");
        }
        private ScheduledTaskStatus UpdateAssignedUsersLinkedGroup(DeviceProfile DeviceProfile, string AssignedUsersLinkedGroup)
        {
            var configJson = ADManagedGroup.ValidConfigurationToJson(DeviceProfileAssignedUsersManagedGroup.GetKey(DeviceProfile), AssignedUsersLinkedGroup, null);

            if (DeviceProfile.AssignedUsersLinkedGroup != configJson)
            {
                DeviceProfile.AssignedUsersLinkedGroup = configJson;
                Database.SaveChanges();

                var managedGroup = DeviceProfileAssignedUsersManagedGroup.Initialize(DeviceProfile);
                if (managedGroup != null) // Sync Group
                    return ADManagedGroupsSyncTask.ScheduleSync(managedGroup);
            }

            return null;
        }
        public static DeviceProfileAssignedUsersManagedGroup Initialize(DeviceProfile DeviceProfile)
        {
            if (DeviceProfile.Id > 0)
            {
                var key = GetKey(DeviceProfile);

                if (!string.IsNullOrEmpty(DeviceProfile.AssignedUsersLinkedGroup))
                {
                    var config = ADManagedGroup.ConfigurationFromJson(DeviceProfile.AssignedUsersLinkedGroup);

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

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

                        return group;
                    }
                }

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

            return null;
        }
 private void UpdateShortName(DeviceProfile deviceProfile, string ShortName)
 {
     if (string.IsNullOrWhiteSpace(ShortName))
         throw new Exception("Profile short name cannot be empty");
     else
         deviceProfile.ShortName = ShortName;
     Database.SaveChanges();
 }
        private void UpdateEnforceComputerNameConvention(DeviceProfile deviceProfile, string EnforceComputerNameConvention)
        {
            bool bValue;
            if (bool.TryParse(EnforceComputerNameConvention, out bValue))
            {
                deviceProfile.EnforceComputerNameConvention = bValue;

                Database.SaveChanges();
                return;
            }
            throw new Exception("Invalid Boolean Value");
        }
        private void UpdateEnforceOrganisationalUnit(DeviceProfile deviceProfile, string EnforceOrganisationalUnit)
        {
            bool bValue;
            if (bool.TryParse(EnforceOrganisationalUnit, out bValue))
            {
                deviceProfile.EnforceOrganisationalUnit = bValue;

                Database.SaveChanges();
                return;
            }
            throw new Exception("Invalid Boolean Value");
        }
        private void UpdateDefaultOrganisationAddress(DeviceProfile deviceProfile, string DefaultOrganisationAddress)
        {
            if (string.IsNullOrEmpty(DefaultOrganisationAddress))
            {
                deviceProfile.DefaultOrganisationAddress = null;
            }
            else
            {
                // Validate
                int daoId;
                if (int.TryParse(DefaultOrganisationAddress, out daoId))
                {
                    var oa = Database.DiscoConfiguration.OrganisationAddresses.GetAddress(daoId);
                    if (oa != null)
                    {
                        deviceProfile.DefaultOrganisationAddress = oa.Id;
                    }
                    else
                    {
                        throw new Exception("Invalid Default Organisation Address Id (No such Id)");
                    }
                }
                else
                {
                    throw new Exception("Invalid Default Organisation Address Id (Not Integer)");
                }
            }


            Database.SaveChanges();
        }
        private void UpdateComputerNameTemplate(DeviceProfile deviceProfile, string ComputerNameTemplate)
        {
            Authorization.Require(Claims.Config.DeviceProfile.ConfigureComputerNameTemplate);

            if (string.IsNullOrWhiteSpace(ComputerNameTemplate))
                throw new Exception("ComputerNameTemplate is Required");

            deviceProfile.ComputerNameTemplate = ComputerNameTemplate;

            Database.SaveChanges();

            deviceProfile.ComputerNameInvalidateCache();
        }
        private void UpdateOrganisationalUnit(DeviceProfile deviceProfile, string OrganisationalUnit)
        {
            if (string.IsNullOrWhiteSpace(OrganisationalUnit))
                OrganisationalUnit = ActiveDirectory.Context.PrimaryDomain.DefaultComputerContainer;

            if (OrganisationalUnit != deviceProfile.OrganisationalUnit)
            {
                deviceProfile.OrganisationalUnit = OrganisationalUnit;
                Database.SaveChanges();
            }
        }
        private void UpdateWirelessProfileProviders(DeviceProfile deviceProfile, string WirelessProfileProviderIds)
        {
            if (string.IsNullOrWhiteSpace(WirelessProfileProviderIds))
            {
                deviceProfile.WirelessProfileProviders = null;
            }
            else
            {
                // Validate
                var validatedProviders = new List<PluginFeatureManifest>();
                foreach (var wirelessProfileProviderId in WirelessProfileProviderIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var featureManifest = Plugins.GetPluginFeature(wirelessProfileProviderId, typeof(WirelessProfileProviderFeature));
                    if (featureManifest == null)
                    {
                        throw new Exception(string.Format("Invalid Wireless Profile Provider Plugin Id: [{0}]", wirelessProfileProviderId));
                    }
                    else
                    {
                        validatedProviders.Add(featureManifest);
                    }
                }

                if (validatedProviders.Count > 0)
                {
                    deviceProfile.WirelessProfileProviders = string.Join(",", validatedProviders.Select(p => p.Id));
                }
                else
                {
                    deviceProfile.WirelessProfileProviders = null;
                }
            }

            Database.SaveChanges();
        }
 private void UpdateDistributionType(DeviceProfile deviceProfile, string DistributionType)
 {
     int iDt;
     if (int.TryParse(DistributionType, out iDt))
     {
         deviceProfile.DistributionType = (DeviceProfile.DistributionTypes)iDt;
         Database.SaveChanges();
         return;
     }
     throw new Exception("Invalid Distribution Type Number");
 }
        private void UpdateAssignedUserLocalAdmin(DeviceProfile deviceProfile, string AssignedUserLocalAdmin)
        {
            bool bValue;
            if (bool.TryParse(AssignedUserLocalAdmin, out bValue))
            {
                deviceProfile.AssignedUserLocalAdmin = bValue;

                Database.SaveChanges();
                return;
            }
            throw new Exception("Invalid Boolean Value");
        }
 private void UpdateDescription(DeviceProfile deviceProfile, string Description)
 {
     if (string.IsNullOrWhiteSpace(Description))
         deviceProfile.Description = null;
     else
         deviceProfile.Description = Description;
     Database.SaveChanges();
 }