示例#1
0
        public void SaveSettings(SettingStore store, IEnumerable <SettingModel> settings)
        {
            var currentSettings = GetSettingsFromStore(store);

            using (TransactionScope scope = TransactionScopeFactory.CreateReaduncommited())
            {
                foreach (var item in settings)
                {
                    if (item == null || string.IsNullOrWhiteSpace(item.Key))
                    {
                        throw new SettingsStoreException(Constants.ERROR_SETTING_NO_KEY);
                    }

                    var existingOrNew = currentSettings.SingleOrDefault(s => s.Equals(item));

                    if (existingOrNew != null)
                    {
                        if (Auth.AllowriteSetting(store.ApplicationName, store.DirectoryName))
                        {
                            existingOrNew.SettingInfo     = item.Info;
                            existingOrNew.SettingTypeInfo = item.TypeInfo;
                            existingOrNew.SettingValue    = item.Value;
                            existingOrNew.Modified        = DateTime.UtcNow;
                        }
                        else
                        {
                            throw new SettingsAuthorizationException(AuthorizationScope.Setting, AuthorizationLevel.Write, store.DirectoryName, Auth.CurrentIdentity.Id);
                        }
                    }
                    else
                    {
                        if (Auth.AllowCreateSetting(store.ApplicationName, store.DirectoryName))
                        {
                            if (!NameValidator.ValidateKey(item.Key))
                            {
                                throw new SettingsStoreException(Constants.ERROR_SETTING_INVALID_KEY);
                            }
                            existingOrNew                 = CreateDataForStore(store);
                            existingOrNew.SettingKey      = item.Key.Trim().Replace("  ", " ");
                            existingOrNew.SettingValue    = item.Value;
                            existingOrNew.SettingInfo     = item.Info;
                            existingOrNew.SettingTypeInfo = item.TypeInfo;
                            existingOrNew.Created         = DateTime.Now;
                            existingOrNew.ObjecId         = item.ObjectId;
                            Store.Context.Settings.Add(existingOrNew);
                        }
                        else
                        {
                            throw new SettingsAuthorizationException(AuthorizationScope.Setting, AuthorizationLevel.Create, store.DirectoryName, Auth.CurrentIdentity.Id);
                        }
                    }
                }

                Store.Save();
                scope.Complete();
            }
        }
        public void CreateVersion(string applicationName, int version)
        {
            if (!Auth.AllowCreateVersion(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.Version, AuthorizationLevel.Create, applicationName, Auth.CurrentIdentity.Id);
            }

            var application = GetApplicationsData(applicationName).SingleOrDefault();

            if (application == null)
            {
                throw new SettingsNotFoundException(Constants.ERROR_APPLICATION_UNKNOWN);
            }

            var data = GetVersionData(application).SingleOrDefault(v => v.Version == version);

            if (data != null)
            {
                throw new SettingsDuplicateException(Constants.ERROR_VERION_ALREADY_EXISTS);
            }

            data = new VersionData();
            data.ApplicationId = application.Id;
            data.Created       = DateTime.UtcNow;
            data.Version       = version;
            application.Versions.Add(data);
            Store.Save();
        }
示例#3
0
        public ApiKeyModel CreateApiKey(string applicationName, SaveApiKeyModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("No Data");
            }

            if (!Auth.AllowEditApiKeys(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.ApiKey, AuthorizationLevel.Create, applicationName, Auth.CurrentIdentity.Id);
            }

            if (string.IsNullOrWhiteSpace(model.Name))
            {
                throw new SettingsStoreException("Key has no Name");
            }

            var application = GetApplicationData(applicationName);

            if (application == null)
            {
                throw new SettingsNotFoundException(applicationName);
            }

            var existingKey = GetKeyData(applicationName, model.Name);

            if (existingKey != null)
            {
                throw new SettingsDuplicateException("Key with name already exist");
            }

            var apiKeyData = new ApiKeyData();

            using (TransactionScope scope = TransactionScopeFactory.CreateReaduncommited())
            {
                apiKeyData.ApiKey        = ApiKeyGenerator.Create();
                apiKeyData.ApplicationId = application.Id;
                apiKeyData.Active        = true;
                apiKeyData.AdminKey      = model.AdminKey;
                apiKeyData.Created       = DateTime.Now;
                apiKeyData.Name          = model.Name;
                Store.Context.ApiKeys.Add(apiKeyData);
                Store.Save();

                if (model.Access != null && model.Access.Count > 0)
                {
                    foreach (var item in model.Access)
                    {
                        var directiry = application.Directories.SingleOrDefault(d => d.Name == item.Directory);

                        if (directiry == null)
                        {
                            throw new SettingsNotFoundException(item.Directory);
                        }

                        DirectoryAccessData access = new DirectoryAccessData();

                        access.DirectoryId = directiry.Id;
                        access.ApiKeyId    = apiKeyData.Id;
                        access.AllowWrite  = item.Write;
                        access.AllowDelete = item.Delete;
                        access.AllowCreate = item.Create;

                        apiKeyData.Access.Add(access);
                    }

                    Store.Save();
                }

                scope.Complete();
            }

            return(GetApiKey(applicationName, apiKeyData.Name));
        }