Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void UpdateApiKey(string applicationName, SaveApiKeyModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("ApiKey");
            }

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

            var application = GetApplicationData(applicationName);

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

            var apiKeyData = GetKeyData(applicationName, model.Key);

            if (apiKeyData == null)
            {
                throw new SettingsNotFoundException("Key");
            }

            using (TransactionScope scope = TransactionScopeFactory.CreateReaduncommited())
            {

                apiKeyData.Active = model.Active;
                apiKeyData.AdminKey = model.AdminKey;

                if (model.Access != null)
                {
                    apiKeyData.Access.Clear();
                    Store.Save();

                    foreach (var item in model.Access)
                    {
                        var directiry = application.Directories.SingleOrDefault(d => d.Name == item.Directory);

                        if (directiry == null)
                        {
                            if (application == 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();
            }
        }