Beispiel #1
0
        public async Task <IActionResult> Add([FromBody] NewResource model)
        {
            var resource = await _svc.Add(model);

            Audit(AuditId.ResourceState);
            return(Json(resource));
        }
Beispiel #2
0
        public async Task <Resource> Add(NewResource model)
        {
            if (model.Type != ResourceType.Api && !_profile.IsPrivileged)
            {
                throw new InvalidOperationException();
            }

            var entity = Mapper.Map <Data.Resource>(model);

            entity.Name        = model.Name ?? $"new-api-{_profile.Name.ToKebabCase()}-{new Random().Next().ToString("x")}";
            entity.DisplayName = model.DisplayName ?? entity.Name;

            if (String.IsNullOrWhiteSpace(entity.Scopes))
            {
                entity.Scopes = entity.Name;
            }

            entity.Enabled = _profile.IsPrivileged;

            if (entity.Type == ResourceType.Api && !_profile.IsPrivileged)
            {
                entity.Managers.Add(new Data.ResourceManager {
                    SubjectId = _profile.Id, Name = _profile.Name
                });
            }

            try
            {
                await _store.Add(entity);
            }
            catch (Exception ex)
            {
                if (ex.GetBaseException().Message.ToLower().Contains("unique"))
                {
                    throw new ResourceNameNotUniqueException();
                }
                else
                {
                    throw new ResourceUpdateException();
                }
            }

            return(Mapper.Map <Resource>(entity));
        }
Beispiel #3
0
        public async virtual Task <IActionResult> NewAsync(string facets = "")
        {
            if (!User.IsAuthorized(ResourceType))
            {
                return(Unauthorized());
            }

            var resourceModel = new ResourceModel <TResource>()
            {
                Resource = new TResource(),
                Facets   = facets
            };

            var newResourceEvent = new NewResource <TResource>(resourceModel);
            var context          = await _mediator.Send(newResourceEvent);

            if (context != null)
            {
                resourceModel = context;
            }

            return(PartialView("_Editor", resourceModel));
        }