Esempio n. 1
0
        /// <summary>
        ///     Gets the active software license.
        /// </summary>
        /// <returns>
        ///     The most applicable software license.
        /// </returns>
        public LocalLicenseModel GetCurrent()
        {
            if (_license == null)
            {
                _license = this.Provider.Security.License.FetchAll().OrderBy(license =>
                                                                             license.Status == LicenseStatus.OK ? 1 :
                                                                             license.Status == LicenseStatus.Invalid ? 2 :
                                                                             license.Status == LicenseStatus.Expired ? 3 :
                                                                             license.Status == LicenseStatus.Inactive ? 4 :
                                                                             license.Status == LicenseStatus.Revoked ? 5 : 6
                                                                             ).FirstOrDefault();
            }

            return(_license);
        }
        /// <summary>
        ///     Constructs a hashable object of the specified license.
        /// </summary>
        private static Dictionary <String, Object> ConstructDefinition(LocalLicenseModel model)
        {
            // Get the database-persisted non-selectable modules, plus the instance-persisted selectable modules.
            List <LicenseModules> modules = new List <LicenseModules>();

            modules.AddRange(model.Provider.Security.LicenseModule.FetchAllByLicenseId(model.Id).Select(licmod => licmod.Module).Where(mod => !IsSelectableModule(mod)));
            modules.AddRange(model.Modules.Where(mod => mod.Enabled).Select(mod => mod.Module));

            Dictionary <String, Object> definition = new Dictionary <String, Object>();

            definition.Add("Active", model.Active.ToString("yyyy-MM-dd"));
            definition.Add("Expiry", model.Expiry == null ? null : model.Expiry.Value.ToString("yyyy-MM-dd"));
            definition.Add("Limits", model.Limits.Select(LocalLicenseModel.ConstructDefinition));
            definition.Add("Modules", modules.Select(module => module.ConvertTo <Guid>()));
            return(definition);
        }
        /// <summary>
        ///     Computes an SHA-512 cryptographic hash from the protected data held on the license.
        /// </summary>
        /// <returns>
        ///     An SHA-512 cryptographic hash which can be used to verify or sign the license.
        /// </returns>
        private Byte[] ComputeHash()
        {
            Byte[] hash;

            // Serialize the license information into a JSON object.
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Object objectDefinition         = LocalLicenseModel.ConstructDefinition(this);
            String stringDefinition         = serializer.Serialize(objectDefinition);

            Byte[] binaryDefinition = Encoding.ASCII.GetBytes(stringDefinition);

            // Compute a SHA-512 hash from the JSON object
            using (SHA512 hashAlgorithm = SHA512.Create())
                hash = hashAlgorithm.ComputeHash(binaryDefinition);

            return(hash);
        }
        /// <summary>
        ///     Executes after this <see cref="LocalLicenseModel"/> has been saved and is used to save the modules that have been selected/deselected.
        /// </summary>
        protected override void ResolveDependent()
        {
            base.ResolveDependent();
            if (_modules != null)
            {
                // Delete any modules that were enabled in the database but are no longer enabled in this License instance.
                LocalLicenseModuleModel[] dbModules = this.Provider.Security.LicenseModule.FetchAllByLicenseId(this.Id).Where(licmod => LocalLicenseModel.IsSelectableModule(licmod.Module) && !this.Modules.Any(mod => mod.Module == licmod.Module && mod.Enabled)).ToArray();
                dbModules.Execute(licmod => licmod.Delete());

                // Create new database modules for modules which are now enabled
                LicenseModules[] enabledModules = this.Modules.Where(mod => LocalLicenseModel.IsSelectableModule(mod.Module) && mod.Enabled && !dbModules.Any(licmod => mod.Module == licmod.Module)).Select(mod => mod.Module).ToArray();
                foreach (LicenseModules module in enabledModules)
                {
                    LocalLicenseModuleModel newEntry = this.Provider.Security.LicenseModule.Create();
                    newEntry.License = this.Id;
                    newEntry.Module  = module;
                    newEntry.Save();
                }
            }
        }
Esempio n. 5
0
 /// <summary>
 ///     Clears the cached license from memory so that the updated license can be reloaded and recached.
 /// </summary>
 internal void Reset()
 {
     _license = null;
 }