예제 #1
0
        public async Task <IActionResult> CreateEmbeddedProject(EmbeddedProjectResource embedResource)
        {
            if (embedResource == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "the embed resource is not valid.",
                    Detail   = "The embed resource is null.",
                    Instance = "48C4A6DD-30AD-434F-BE98-694AA9F80140"
                };
                return(BadRequest(problem));
            }
            EmbeddedProject embeddedProject = mapper.Map <EmbeddedProjectResource, EmbeddedProject>(embedResource);

            Project project = await projectService.FindAsync(embedResource.ProjectId);

            if (project == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Project does not exist.",
                    Detail   = "There is no project with this project ID.",
                    Instance = "644FE34C-FC98-4BE9-8BB7-D0773409F636"
                };
                return(BadRequest(problem));
            }

            string identity = HttpContext.User.GetIdentityId(HttpContext);
            User   user     = await userService.GetUserByIdentityIdAsync(identity);

            bool isAllowed = userService.UserHasScope(identity, nameof(Defaults.Scopes.EmbedWrite));

            if (!(project.UserId == user.Id || isAllowed))
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "User is not allowed to create an embed project.",
                    Detail   = "The user does not own the project and does not have enough privileges to add an embed project.",
                    Instance = "D6E83BEC-D9FA-4C86-9FA7-7D74DE0F5B23"
                };
                return(Unauthorized(problem));
            }

            //Ensure we have a non existing Guid
            Guid guid;

            while (true)
            {
                guid = Guid.NewGuid();
                if (!await embedService.IsNonExistingGuidAsync(guid))
                {
                    continue;
                }
                break;
            }
            embeddedProject.Guid = guid;
            embeddedProject.User = user;

            try
            {
                embedService.Add(embeddedProject);
                embedService.Save();
                return(Created(nameof(CreateEmbeddedProject), mapper.Map <EmbeddedProject, EmbeddedProjectResourceResult>(embeddedProject)));
            } catch (DbUpdateException e)
            {
                Log.Logger.Error(e, "Database exception");

                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Could not create the Embedded project.",
                    Detail   = "The database failed to save the embed project.",
                    Instance = "D481A8DD-B507-4AC5-A2CB-16EBEF758097"
                };
                return(BadRequest(problem));
            }
        }