예제 #1
0
        public async Task UpdateCloudProviderService(Guid organizationId, Guid organizationCPSId, OrganizationCPSPutRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to update settings in this organization.");

                return;
            }

            OrganizationCPS organizationCPS = organization.GetCloudProviderServiceById(organizationCPSId);

            if (organizationCPS == null)
            {
                await _domainManagerService.AddConflict($"The cloud provider service with id {organizationCPSId} does not exists.");

                return;
            }

            bool validCredentials = await _cpsCredentialService(resource.Type).ValidateCredentials(resource.AccessId, resource.AccessName, resource.AccessSecret, resource.AccessAppId,
                                                                                                   resource.AccessAppSecret, resource.AccessDirectory, resource.AccessRegion);

            if (!validCredentials)
            {
                await _domainManagerService.AddConflict($"The credentials are not valid or there are some permissions problems");

                return;
            }

            user.UpdateCloudProviderService(organizationId, organizationCPSId,
                                            _dataProtectorService.Protect(resource.AccessId),
                                            _dataProtectorService.Protect(resource.AccessName),
                                            _dataProtectorService.Protect(resource.AccessSecret),
                                            _dataProtectorService.Protect(resource.AccessAppId),
                                            _dataProtectorService.Protect(resource.AccessAppSecret),
                                            _dataProtectorService.Protect(resource.AccessDirectory),
                                            _dataProtectorService.Protect(resource.AccessRegion));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
예제 #2
0
        public async Task DeleteOrganizationProjectServiceTemplate(Guid organizationId, Guid projectServiceTemplateId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create project service templates in this organization.");

                return;
            }

            ProjectServiceTemplate existingTemplate = organization.GetProjectServiceTemplateById(projectServiceTemplateId);

            if (existingTemplate == null)
            {
                await _domainManagerService.AddNotFound($"The project service templated with id {projectServiceTemplateId} does not exists.");

                return;
            }

            var isBeingUsed = organization.IsTemplateBeingUsed(projectServiceTemplateId);

            if (isBeingUsed)
            {
                await _domainManagerService.AddConflict($"The template with id {projectServiceTemplateId} is being used by some services. You cannot delete it.");

                return;
            }

            //delete template
            existingTemplate.Delete(loggedUserId);

            //delete relationship
            foreach (var item in existingTemplate.Organizations)
            {
                item.Delete(loggedUserId);
            }

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
        public async Task RemoveUser(Guid organizationId, Guid projectId, string userId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            Project project = user.FindProjectById(projectId);

            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInProject(projectId);

            if (role != PipelineRole.ProjectAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to invite users in this project.");

                return;
            }

            ProjectUser projectUser = project.GetProjectUserById(userId);

            if (projectUser == null)
            {
                await _domainManagerService.AddNotFound($"The user with id {userId} does not exists in the project.");

                return;
            }

            if (loggedUserId.Equals(userId, StringComparison.InvariantCultureIgnoreCase))
            {
                await _domainManagerService.AddConflict($"You cannot delete yourself from the project.");

                return;
            }

            projectUser.Delete(loggedUserId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
예제 #4
0
        public void GrantUserAccess(string userId, PipelineRole role)
        {
            if (this.Users == null)
            {
                this.Users = new List <ProjectUser>();
            }

            var userProject = ProjectUser.Factory.Create(this.ProjectId, userId, role, userId);

            this.Users.Add(userProject);
        }
        public void GrantUserAccess(string userId, PipelineRole role)
        {
            if (this.Users == null)
            {
                this.Users = new List <OrganizationUser>();
            }

            var userOrganization = OrganizationUser.Factory.Create(this.OrganizationId, userId, role, userId);

            this.Users.Add(userOrganization);
        }
        public async Task CancelInvitation(Guid organizationId, Guid projectId, Guid invitationId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            Project project = user.FindProjectById(projectId);

            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInProject(projectId);

            if (role != PipelineRole.ProjectAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to invite users in this project.");

                return;
            }

            ProjectUserInvitation projectUserInvitation = project.GetUserInvitation(invitationId);

            if (projectUserInvitation == null)
            {
                await _domainManagerService.AddNotFound($"The invitation with id {invitationId} does not exists.");

                return;
            }

            if (projectUserInvitation.InvitationStatus != UserInvitationStatus.Pending)
            {
                await _domainManagerService.AddConflict($"The invitation with id {invitationId} cannot be cancel. Only invitations in status pending can be canceled.");

                return;
            }

            projectUserInvitation.Cancel();

            await _userRepository.SaveChanges();
        }
        public async Task DeleteConfigurationManagementService(Guid organizationId, Guid organizationCMSId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to delete settings in this organization.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(organizationCMSId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddConflict($"The configuration management service with id {organizationCMSId} does not exists.");

                return;
            }

            List <Project> relatedProjects = organization.GetProjectsByCMSId(organizationCMS.OrganizationCMSId);

            if (relatedProjects.Any())
            {
                await _domainManagerService.AddConflict($"There are projects already configured with the configuration management service {organizationCMS.Name}.");

                return;
            }

            user.DeleteConfigurationManagementService(organizationId, organizationCMSId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
        public async Task UpdateConfigurationManagementService(Guid organizationId, Guid organizationCMSId, OrganizationCMSPutRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to update settings in this organization.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(organizationCMSId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddConflict($"The cloud provider service with id {organizationCMSId} does not exists.");

                return;
            }

            user.UpdateConfigurationManagementService(organizationId,
                                                      organizationCMSId,
                                                      _dataProtectorService.Protect(resource.AccessId),
                                                      _dataProtectorService.Protect(resource.AccessSecret));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
예제 #9
0
        public async Task UpdateOrganizationProjectServiceTemplate(Guid organizationId, Guid projectServiceTemplateId, OrganizationProjectServiceTemplatePutRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create project service templates in this organization.");

                return;
            }

            ProjectServiceTemplate existingTemplate = organization.GetProjectServiceTemplateById(projectServiceTemplateId);

            if (existingTemplate == null)
            {
                await _domainManagerService.AddNotFound($"The project service templated with id {projectServiceTemplateId} does not exists.");

                return;
            }

            existingTemplate.Name        = resource.Name;
            existingTemplate.Description = resource.Description;
            existingTemplate.Logo        = resource.Logo;

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
            public static ProjectUser Create(Guid projectId, string userId, PipelineRole role, string createdBy)
            {
                var entity = new ProjectUser()
                {
                    ProjectUserId = Guid.NewGuid(),
                    ProjectId     = projectId,
                    UserId        = userId,
                    CreatedBy     = createdBy,
                    Status        = EntityStatus.Active,
                    Role          = role
                };

                var validationResult = new DataValidatorManager <ProjectUser>().Build().Validate(entity);

                if (!validationResult.IsValid)
                {
                    throw new ApplicationException(validationResult.Errors);
                }

                return(entity);
            }
예제 #11
0
        public async Task <OrganizationCPSGetRp> GetOrganizationCloudProviderServiceById(Guid organizationId, Guid organizationCPSId)
        {
            var loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return(null);
            }

            OrganizationCPS cloudProvider = organization.GetCloudProviderServiceById(organizationCPSId);

            if (cloudProvider == null)
            {
                return(null);
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            OrganizationCPSGetRp organizationCPSRp = new OrganizationCPSGetRp()
            {
                OrganizationCPSId = cloudProvider.OrganizationCPSId,
                Name            = cloudProvider.Name,
                Type            = cloudProvider.Type,
                AccessId        = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessId) : DomainConstants.Obfuscator.Default,
                AccessName      = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessName) : DomainConstants.Obfuscator.Default,
                AccessSecret    = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessSecret) : DomainConstants.Obfuscator.Default,
                AccessRegion    = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessRegion) : DomainConstants.Obfuscator.Default,
                AccessAppId     = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessAppId) : DomainConstants.Obfuscator.Default,
                AccessAppSecret = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessAppSecret) : DomainConstants.Obfuscator.Default,
                AccessDirectory = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(cloudProvider.AccessDirectory) : DomainConstants.Obfuscator.Default
            };

            return(organizationCPSRp);
        }
예제 #12
0
        public async Task <OrganizationCPSListRp> GetOrganizationCloudProviderServices(Guid organizationId)
        {
            var loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return(null);
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            OrganizationCPSListRp list = new OrganizationCPSListRp();

            List <OrganizationCPS> cloudProviderServices = organization.GetCloudProviderServices();

            if (cloudProviderServices != null)
            {
                list.Items = cloudProviderServices.Select(x => new OrganizationCPSListItemRp()
                {
                    OrganizationCPSId = x.OrganizationCPSId,
                    Name            = x.Name,
                    Type            = x.Type,
                    AccessId        = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessId) : DomainConstants.Obfuscator.Default,
                    AccessName      = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessName) : DomainConstants.Obfuscator.Default,
                    AccessSecret    = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessSecret) : DomainConstants.Obfuscator.Default,
                    AccessRegion    = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessRegion) : DomainConstants.Obfuscator.Default,
                    AccessAppId     = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessAppId) : DomainConstants.Obfuscator.Default,
                    AccessAppSecret = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessAppSecret) : DomainConstants.Obfuscator.Default,
                    AccessDirectory = role == PipelineRole.OrganizationAdmin ? _dataProtectorService.Unprotect(x.AccessDirectory) : DomainConstants.Obfuscator.Default
                }).ToList();
            }

            return(list);
        }
        public async Task UpdateOrganization(Guid organizationId, OrganizationPutRp resource)
        {
            string ownerUserId  = _identityService.GetOwnerId();
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation name {resource.Name} with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to perform updates in this organization.");

                return;
            }

            Organization existingOrganization = user.FindOrganizationByName(resource.Name);

            if (existingOrganization != null && existingOrganization.OrganizationId != organizationId)
            {
                await _domainManagerService.AddConflict($"The organzation name {resource.Name} has already been taken.");

                return;
            }

            user.UpdateOrganization(organizationId, resource.Name, resource.Description, resource.WebSiteUrl);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
        public async Task UpdateProjectBasicInformation(Guid organizationId, Guid projectId, ProjectPutRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            Project project = user.FindProjectById(projectId);

            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInProject(projectId);

            if (role != PipelineRole.ProjectAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to perform updates in this project.");

                return;
            }

            user.UpdateProject(organizationId, projectId, resource.Name, resource.Description);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
예제 #15
0
        public async Task InviteUser(Guid organizationId, OrganizationUserInvitationPostRp resource)
        {
            string loggedUserId   = _identityService.GetUserId();
            string loggedUserName = _identityService.GetUserName();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to invite users in this organization.");

                return;
            }

            User invitedUser = await _userRepository.GetUserByEmail(resource.UserEmail);

            if (invitedUser != null)
            {
                OrganizationUser organizationUser = organization.GetOrganizationUser(resource.UserEmail);
                if (organizationUser != null)
                {
                    await _domainManagerService.AddConflict($"The user with email {resource.UserEmail} already exists.");

                    return;
                }
            }

            OrganizationUserInvitation organizationUserInvitation = organization.GetOrganizationUserInvitation(resource.UserEmail);

            if (organizationUserInvitation != null)
            {
                await _domainManagerService.AddConflict($"The user with email {resource.UserEmail} was already invited.");

                return;
            }

            OrganizationUserInvitation newUserInvitation = OrganizationUserInvitation.Factory.Create(organizationId, invitedUser == null ? null : invitedUser.Id, resource.UserEmail, resource.Role, loggedUserId);

            organization.AddUserInvitation(newUserInvitation);

            await _userRepository.SaveChanges();

            var @event = new OrganizationUserInvitedEvent(_correlationId)
            {
                OrganizationUserInvitationId = newUserInvitation.OrganizationUserInvitationId,
                InvitationType    = newUserInvitation.InvitationType,
                RequestorFullName = loggedUserName,
                Role         = newUserInvitation.Role,
                UserEmail    = newUserInvitation.UserEmail,
                UserFullName = invitedUser == null ? null : invitedUser.GetFullName()
            };

            await _eventBusService.Publish(queueName : "OrganizationUserInvitedEvent", @event : @event);
        }
        public async Task DeleteProject(Guid organizationId, Guid projectId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            Project project = user.FindProjectById(projectId, false);

            if (project == null)
            {
                await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInProject(projectId);

            if (role != PipelineRole.ProjectAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to delete this project.");

                return;
            }

            if (project.Status != EntityStatus.Active)
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to be modified/deleted.");

                return;
            }

            var preparingServices = project.GetPreparingServices();

            if (preparingServices.Any())
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} has pipes in status Preparing. All services must be in status Active to delete the project");

                return;
            }

            var preparingFeatures = project.GetPreparingFeatures();

            if (preparingFeatures.Any())
            {
                await _domainManagerService.AddConflict($"The project with id {projectId} has features in status Preparing. All features must be in status Active to delete the project");

                return;
            }

            user.DeleteProject(organizationId, projectId);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            //send event to delete project in CMS
            var projectDeletedEvent = new ProjectDeletedEvent(_correlationId, project.IsImported)
            {
                OrganizationExternalId = project.OrganizationExternalId,
                ProjectExternalId      = project.ProjectExternalId,
                ProjectVSTSFakeId      = project.ProjectVSTSFakeId,
                CMSType         = project.OrganizationCMS.Type,
                CMSAccountId    = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountId),
                CMSAccountName  = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName),
                CMSAccessId     = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessId),
                CMSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret),
                CMSAccessToken  = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessToken)
            };

            await _eventBusService.Publish(queueName : "ProjectDeletedEvent", @event : projectDeletedEvent);

            //send event to delete cloud services in CPS - Services
            var services = await _projectRepository.GetProjectServices(organizationId, projectId);

            var environments = await _projectRepository.GetProjectEnvironments(organizationId, projectId);

            foreach (var service in services)
            {
                var projectServiceDeletedEvent = new ProjectServiceDeletedEvent(_correlationId)
                {
                    OrganizationExternalId                = project.OrganizationExternalId,
                    OrganizationName                      = organization.Name,
                    ProjectName                           = project.Name,
                    ServiceName                           = service.Name,
                    ProjectVSTSFakeName                   = project.ProjectVSTSFakeName,
                    ProjectExternalId                     = project.ProjectExternalId,
                    ProjectServiceExternalId              = service.ProjectServiceExternalId,
                    CommitStageId                         = service.CommitStageId,
                    ReleaseStageId                        = service.ReleaseStageId,
                    CommitServiceHookId                   = service.CommitServiceHookId,
                    ReleaseServiceHookId                  = service.ReleaseServiceHookId,
                    CodeServiceHookId                     = service.CodeServiceHookId,
                    ReleaseStartedServiceHookId           = service.ReleaseStartedServiceHookId,
                    ReleasePendingApprovalServiceHookId   = service.ReleasePendingApprovalServiceHookId,
                    ReleaseCompletedApprovalServiceHookId = service.ReleaseCompletedApprovalServiceHookId,
                    Environments                          = environments.Select(x => x.Name).ToList(),
                    CMSType            = project.OrganizationCMS.Type,
                    CMSAccountId       = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountId),
                    CMSAccountName     = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName),
                    CMSAccessId        = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessId),
                    CMSAccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret),
                    CMSAccessToken     = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessToken),
                    CPSType            = project.OrganizationCPS.Type,
                    CPSAccessId        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId),
                    CPSAccessName      = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName),
                    CPSAccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret),
                    CPSAccessRegion    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion),
                    CPSAccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId),
                    CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret),
                    CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory),
                    SourceEvent        = Domain.Models.Enums.SourceEvent.Project
                };

                await _eventBusService.Publish(queueName : "ProjectServiceDeletedEvent", @event : projectServiceDeletedEvent);
            }

            //send event to delete cloud services in CPS - Features
            var features = await _projectRepository.GetProjectFeatures(organizationId, projectId);

            foreach (var feature in features)
            {
                List <ProjectFeatureServiceDeletedEvent> projectFeatureServiceDeletedEventList = new List <ProjectFeatureServiceDeletedEvent>();

                var featureServices = await _projectFeatureRepository.GetProjectFeatureServices(organizationId, projectId, feature.ProjectFeatureId);

                foreach (var item in featureServices)
                {
                    projectFeatureServiceDeletedEventList.Add(new ProjectFeatureServiceDeletedEvent(_correlationId)
                    {
                        ServiceId                             = item.ProjectServiceId,
                        ServiceExternalId                     = item.ProjectService.ProjectServiceExternalId,
                        ServiceExternalUrl                    = item.ProjectService.ProjectServiceExternalUrl,
                        ServiceName                           = item.ProjectService.Name,
                        ServiceTemplateUrl                    = item.ProjectService.ProjectServiceTemplate.Url,
                        CommitStageId                         = item.CommitStageId,
                        ReleaseStageId                        = item.ReleaseStageId,
                        CommitServiceHookId                   = item.CommitServiceHookId,
                        ReleaseServiceHookId                  = item.ReleaseServiceHookId,
                        CodeServiceHookId                     = item.CodeServiceHookId,
                        ReleaseStartedServiceHookId           = item.ReleaseStartedServiceHookId,
                        ReleasePendingApprovalServiceHookId   = item.ReleasePendingApprovalServiceHookId,
                        ReleaseCompletedApprovalServiceHookId = item.ReleaseCompletedApprovalServiceHookId
                    });
                }

                var projectFeatureDeletedEvent = new ProjectFeatureDeletedEvent(_correlationId)
                {
                    OrganizationId            = organization.OrganizationId,
                    OrganizationName          = organization.Name,
                    ProjectId                 = project.ProjectId,
                    Services                  = projectFeatureServiceDeletedEventList,
                    ProjectExternalId         = project.ProjectExternalId,
                    ProjectExternalEndpointId = project.ProjectExternalEndpointId,
                    ProjectVSTSFakeName       = project.ProjectVSTSFakeName,
                    ProjectName               = project.Name,
                    FeatureId                 = feature.ProjectFeatureId,
                    FeatureName               = feature.Name,
                    CMSType            = project.OrganizationCMS.Type,
                    CMSAccountId       = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountId),
                    CMSAccountName     = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName),
                    CMSAccessId        = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessId),
                    CMSAccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret),
                    CMSAccessToken     = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessToken),
                    CPSType            = project.OrganizationCPS.Type,
                    CPSAccessId        = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId),
                    CPSAccessName      = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName),
                    CPSAccessSecret    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret),
                    CPSAccessRegion    = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion),
                    CPSAccessAppId     = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId),
                    CPSAccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret),
                    CPSAccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory),
                    SourceEvent        = Domain.Models.Enums.SourceEvent.Project
                };

                await _eventBusService.Publish(queueName : "ProjectFeatureDeletedEvent", @event : projectFeatureDeletedEvent);
            }
        }
예제 #17
0
        public async Task CreateCloudProviderService(Guid organizationId, OrganizationCPSPostRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create settings in this organization.");

                return;
            }

            //OrganizationCPS organizationCPS = organization.GetCloudProviderServiceByType(resource.Type);
            //if (organizationCPS != null)
            //{
            //    await _domainManagerService.AddConflict($"The cloud provider service with type {resource.Type} already exists.");
            //    return;
            //}

            OrganizationCPS existingCSP = organization.GetCloudProviderServiceByName(resource.Name);

            if (existingCSP != null)
            {
                await _domainManagerService.AddConflict($"The cloud provider service {resource.Name} has already been taken.");

                return;
            }

            bool validCredentials = await _cpsCredentialService(resource.Type).ValidateCredentials(resource.AccessId, resource.AccessName, resource.AccessSecret, resource.AccessAppId,
                                                                                                   resource.AccessAppSecret, resource.AccessDirectory, resource.AccessRegion);

            if (!validCredentials)
            {
                if (resource.Type == CloudProviderService.AWS)
                {
                    await _domainManagerService.AddConflict($"The credentials are not valid");
                }
                else
                {
                    await _domainManagerService.AddConflict($"The credentials are not valid or the client does not have enough privileges");
                }
                return;
            }

            user.AddCloudProviderService(organizationId,
                                         resource.Name,
                                         resource.Type,
                                         _dataProtectorService.Protect(resource.AccessId),
                                         _dataProtectorService.Protect(resource.AccessName),
                                         _dataProtectorService.Protect(resource.AccessSecret),
                                         _dataProtectorService.Protect(resource.AccessAppId),
                                         _dataProtectorService.Protect(resource.AccessAppSecret),
                                         _dataProtectorService.Protect(resource.AccessDirectory),
                                         _dataProtectorService.Protect(resource.AccessRegion));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
예제 #18
0
            public static OrganizationUserInvitation Create(Guid organizationId, string userId, string userEmail, PipelineRole role, string createdBy)
            {
                var entity = new OrganizationUserInvitation()
                {
                    OrganizationId   = organizationId,
                    UserId           = userId,
                    UserEmail        = userEmail,
                    InvitationType   = string.IsNullOrEmpty(userId) ? UserInvitationType.ExternalUser : UserInvitationType.InternalUser,
                    Role             = role,
                    InvitationStatus = UserInvitationStatus.Pending,
                    CreatedBy        = createdBy,
                    Status           = EntityStatus.Active
                };

                var validationResult = new DataValidatorManager <OrganizationUserInvitation>().Build().Validate(entity);

                if (!validationResult.IsValid)
                {
                    throw new ApplicationException(validationResult.Errors);
                }

                return(entity);
            }
        public async Task CreateConfigurationManagementService(Guid organizationId, OrganizationCMSPostRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create settings in this organization.");

                return;
            }

            //OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceByType(resource.Type);
            //if (organizationCMS != null && organizationCMS.ConnectionType == resource.ConnectionType)
            //{
            //    await _domainManagerService.AddConflict($"The configuration management service with type {resource.Type} already exists.");
            //    return;
            //}

            OrganizationCMS existingCMP = organization.GetConfigurationManagementServiceByName(resource.Name);

            if (existingCMP != null)
            {
                await _domainManagerService.AddConflict($"The configuration management service {resource.Name} has already been taken.");

                return;
            }

            //existing same connection in other account
            OrganizationCMS existingInOtherOrganization = await _organizationCMSRepository.FindOrganizationCMSByTypeAndAccountName(resource.Type, resource.AccountName);

            if (existingInOtherOrganization != null)
            {
                await _domainManagerService.AddConflict($"The configuration management service {resource.Type}/{resource.AccountName} has already been taken in other organization.");

                return;
            }

            user.AddConfigurationManagementService(organizationId,
                                                   resource.Name,
                                                   resource.Type,
                                                   resource.ConnectionType,
                                                   _dataProtectorService.Protect(resource.AccountId),
                                                   _dataProtectorService.Protect(resource.AccountName),
                                                   _dataProtectorService.Protect(resource.AccessId),
                                                   _dataProtectorService.Protect(resource.AccessSecret),
                                                   _dataProtectorService.Protect(resource.AccessToken));

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }
예제 #20
0
        public async Task CreateOrganizationProjectServiceTemplate(Guid organizationId, OrganizationProjectServiceTemplatePostRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create project service templates in this organization.");

                return;
            }

            OrganizationProjectServiceTemplate existingTemplate = organization.GetProjectServiceTemplateByName(resource.Name);

            if (existingTemplate != null)
            {
                await _domainManagerService.AddConflict($"The project service templates {resource.Name} has already been taken.");

                return;
            }

            ProjectServiceTemplate sourceProjectServiceTemplate = await _projectServiceTemplateRepository.GetProjectServiceTemplateInternalById(resource.SourceProjectServiceTemplateId);

            if (existingTemplate != null)
            {
                await _domainManagerService.AddConflict($"The source project service templates {resource.Name} does not exists.");

                return;
            }

            ProgrammingLanguage programmingLanguage = await _programmingLanguageRepository.GetProgrammingLanguageById(resource.ProgrammingLanguageId);

            if (programmingLanguage == null)
            {
                await _domainManagerService.AddNotFound($"The programming language with id {resource.ProgrammingLanguageId} does not exists.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(resource.ConnectionId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddNotFound($"The connection with id {resource.ConnectionId} does not exists.");

                return;
            }

            if (organizationCMS.ConnectionType != Domain.Models.Enums.CMSConnectionType.TemplateLevel)
            {
                await _domainManagerService.AddConflict($"The connection with id {resource.ConnectionId} is not for templates.");

                return;
            }

            /*Create repository : BEGIN*/

            CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(organizationCMS.Type).GetToken(
                _dataProtectorService.Unprotect(organizationCMS.AccountId),
                _dataProtectorService.Unprotect(organizationCMS.AccountName),
                _dataProtectorService.Unprotect(organizationCMS.AccessSecret),
                _dataProtectorService.Unprotect(organizationCMS.AccessToken));

            var teamId    = string.Empty;
            var projectId = string.Empty;

            if (resource.RepositoryCMSType == ConfigurationManagementService.Bitbucket)
            {
                teamId    = resource.TeamId;
                projectId = resource.ProjectExternalId;
            }
            else
            {
                teamId    = resource.ProjectExternalId;
                projectId = resource.ProjectExternalId;
            }

            CMSServiceAvailabilityResultModel cmsServiceAvailability =
                await _cmsService(organizationCMS.Type).ValidateServiceAvailability(cmsAuthCredential, teamId, projectId, resource.ProjectExternalName, resource.Name);

            if (!cmsServiceAvailability.Success)
            {
                await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsServiceAvailability.GetReasonForNoSuccess()}");

                return;
            }

            //SaveChanges in CMS
            CMSServiceCreateModel       serviceCreateModel = CMSServiceCreateModel.Factory.Create(teamId, projectId, resource.ProjectExternalName, resource.Name, true);
            CMSServiceCreateResultModel cmsServiceCreate   = await _cmsService(organizationCMS.Type).CreateService(cmsAuthCredential, serviceCreateModel);

            if (!cmsServiceCreate.Success)
            {
                await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsServiceCreate.GetReasonForNoSuccess()}");

                return;
            }

            /*Create repository : END*/
            var template = user.AddProjectTemplateService(organizationId, resource.Name,
                                                          sourceProjectServiceTemplate.ServiceCMSType,
                                                          sourceProjectServiceTemplate.ServiceCPSType,
                                                          resource.Description,
                                                          cmsServiceCreate.ServiceExternalUrl,
                                                          resource.Logo,
                                                          resource.PipeType,
                                                          Domain.Models.Enums.TemplateType.Standard,
                                                          Domain.Models.Enums.TemplateAccess.Organization,
                                                          true, resource.ProgrammingLanguageId, resource.Framework, resource.RepositoryCMSType,
                                                          organizationCMS.AccessId,
                                                          organizationCMS.AccessSecret,
                                                          organizationCMS.AccessToken,
                                                          sourceProjectServiceTemplate.Parameters);

            _projectServiceTemplateRepository.Add(template);
            await _projectServiceTemplateRepository.SaveChanges();

            _userRepository.Update(user);
            await _userRepository.SaveChanges();

            var @event = new ProjectServiceTemplateCreatedEvent(_correlationId)
            {
                OrganizationId           = organization.OrganizationId,
                ProjectServiceTemplateId = template.ProjectServiceTemplateId,
                SourceTemplateUrl        = sourceProjectServiceTemplate.Url,
                TemplateAccess           = template.TemplateAccess,
                NeedCredentials          = template.NeedCredentials,
                RepositoryCMSType        = template.Credential.CMSType,
                RepositoryAccessId       = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessId) : string.Empty,
                RepositoryAccessSecret   = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessSecret) : string.Empty,
                RepositoryAccessToken    = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessToken) : string.Empty,
                RepositoryUrl            = template.Url
            };

            await _eventBusService.Publish(queueName : "ProjectServiceTemplateCreatedEvent", @event : @event);
        }