Example #1
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();
            }
        }
Example #2
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);
        }
        public ApplicationModel CreateApplication(string applicationName, string applicationDescription, string directoryName, string directoryDescription)
        {
            if (!Auth.AllowCreateApplication(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.Application, AuthorizationLevel.Create, applicationName, Auth.CurrentIdentity.Id);
            }

            if (string.IsNullOrWhiteSpace(applicationName))
            {
                throw new SettingsStoreException(Constants.ERROR_APPLICATION_NO_NAME);
            }

            var application = Store.Context.Applications.FirstOrDefault(app => app.Name == applicationName);

            if (application != null)
            {
                throw new SettingsStoreException(Constants.ERROR_APPLICATION_ALREADY_EXISTS);
            }

            if (!NameValidator.ValidateName(applicationName))
            {
                throw new SettingsNotFoundException(Constants.ERROR_APPLICATION_NAME_INVALID);
            }

            application = new ApplicationData();
            DirectoryData cust_directory = null;
            DirectoryData def_directory = null;

            using (TransactionScope scope = new TransactionScope())
            {
                application.Name = applicationName;

                if (string.IsNullOrWhiteSpace(applicationDescription))
                {
                    applicationDescription = Constants.DEAULT_APPLICATION_DESCRIPTION;
                }

                //Create application
                application.Description = applicationDescription.Trim().Replace("  ", " ");
                application.Created = DateTime.UtcNow;

                Store.Context.Applications.Add(application);
                Store.Context.SaveChanges();

                //Create version 1
                VersionData version = new VersionData { Version = 1, Created = DateTime.UtcNow, ApplicationId = application.Id };
                Store.Context.Versions.Add(version);
                Store.Context.SaveChanges();

                //Create application default directory
                def_directory = new DirectoryData();
                def_directory.Name = Constants.DEAULT_DIRECTORY_NAME;
                def_directory.Description = Constants.DEAULT_DIRECTORY_DESCRIPTION;
                def_directory.ApplicationId = application.Id;
                def_directory.Created = DateTime.UtcNow;
                Store.Context.Directories.Add(def_directory);

                //Create custom first directory, if provided.
                if (!string.IsNullOrWhiteSpace(directoryName))
                {
                    cust_directory = new DirectoryData();
                    cust_directory.Name = directoryName.Trim();
                    if (directoryDescription != null)
                        cust_directory.Description = directoryDescription.Trim();
                    cust_directory.ApplicationId = application.Id;
                    cust_directory.Created = DateTime.UtcNow;
                    Store.Context.Directories.Add(cust_directory);
                }

                Store.Context.SaveChanges();

                //Create default api key for applicaiton, a trigger maintains access for the master apikey to the application directories.
                ApiKeyData apiKey = new ApiKeyData { ApiKey = ApiKeyGenerator.Create(), Name = application.Name, Application = application, Active = true, AdminKey = true, Created = DateTime.UtcNow };

                Store.Context.ApiKeys.Add(apiKey);
                Store.Context.SaveChanges();

                //Set access right to default directory.
                DirectoryAccessData access = new DirectoryAccessData();
                access.AllowWrite = true;
                access.AllowDelete = true;
                access.AllowCreate = true;
                access.ApiKey = apiKey;
                access.Directory = def_directory;
                Store.Context.Access.Add(access);

                //Set access right to custom directory.
                if (cust_directory != null)
                {
                    access = new DirectoryAccessData();
                    access.AllowWrite = true;
                    access.AllowDelete = true;
                    access.AllowCreate = true;
                    access.ApiKey = apiKey;
                    access.Directory = cust_directory;
                    Store.Context.Access.Add(access);
                }

                Store.Save();
                scope.Complete();
            }

            Auth.Invalidate();

            Store.Context.Entry<ApplicationData>(application).Reload();

            //reload the enities the reflect the master key access created by the trigger.
            if (cust_directory != null)
                Store.Context.Entry<DirectoryData>(cust_directory).Collection("Access").Load();

            if (def_directory != null)
                Store.Context.Entry<DirectoryData>(def_directory).Collection("Access").Load();

            return GetApplication(applicationName);
        }