Exemplo n.º 1
0
        public IEnumerable<ApiKeyModel> GetApplicationApiKeys(string applicationName)
        {
            var data = new List<ApiKeyData>();
            List<ApiKeyModel> result = new List<ApiKeyModel>();

            if (!Auth.AllowReadApiKeys(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.ApiKey, AuthorizationLevel.Read, applicationName, Auth.CurrentIdentity.Id);
            }
            var registerdKeys = GetApplicationKeysData(applicationName);

            foreach (var item in registerdKeys)
            {
                ApiKeyModel model = new ApiKeyModel();

                model.Active = item.Active;
                model.AdminKey = item.AdminKey;
                model.ApplicationName = item.Application.Name;
                model.Key = item.ApiKey;
                model.LastUsed = item.LastUsed;

                model.Access = new List<DirectoryAccessModel>();

                foreach (var a in item.Access)
                {
                    if (string.Equals(a.Directory.Application.Name, applicationName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        DirectoryAccessModel access = new DirectoryAccessModel();
                        access.Create = a.AllowCreate;
                        access.Write = a.AllowWrite;
                        access.Delete = a.AllowDelete;
                        access.Directory = a.Directory.Name;
                        model.Access.Add(access);
                    }
                }

                result.Add(model);
            }

            return result;
        }
Exemplo n.º 2
0
        public ApiKeyModel GetApiKey(string applicationName, string name)
        {
            if (!Auth.AllowReadApiKeys(applicationName))
            {
                throw new SettingsAuthorizationException(AuthorizationScope.ApiKey, AuthorizationLevel.Read, applicationName, Auth.CurrentIdentity.Id);
            }

            var data = GetKeyData(applicationName, name);

            if (data == null)
            {
                throw new SettingsNotFoundException("ApiKey");
            }

            var model = new ApiKeyModel
            {
                Active = data.Active,
                AdminKey = data.AdminKey,
                Key = data.ApiKey,
                ApplicationName = data.Application.Name,
                Name = data.Name
            };

            model.Access = new List<DirectoryAccessModel>();
            foreach (var item in data.Access)
            {
                model.Access.Add(new DirectoryAccessModel
                {
                    Directory = item.Directory.Name,
                    Create = item.AllowCreate,
                    Delete = item.AllowDelete,
                    Write = item.AllowWrite
                });
            }

            return model;
        }