Exemplo n.º 1
0
        public async Task <User> AddAsync(string orgId, User user)
        {
            if (!(await new UserValidator().ValidateAsync(user)).IsValid)
            {
                throw new ArgumentException($"The user information is invalid");
            }

            if (!await organisationRepository.ExistsByAsync(o => o.Id == orgId))
            {
                throw new ArgumentException($"The organisation {orgId} not exists");
            }

            if (await this.repository.ExistsByAsync(u => u.Username == user.Username && u.OrganisationId == orgId))
            {
                throw new NameAlreadyUsedException(user.Username);
            }

            var userEntity = user.ToEntity();

            userEntity.OrganisationId = orgId;
            var passkeys = user.Password.Encrypt();

            userEntity.PasswordHash      = Convert.ToBase64String(passkeys.PasswordHash);
            userEntity.PasswordSalt      = Convert.ToBase64String(passkeys.PasswordSalt);
            userEntity.OrganisationRoles = user.Roles;
            userEntity = await repository.AddAsync(userEntity);

            return(userEntity.ToContract());
        }
Exemplo n.º 2
0
        public async Task <Team> GetByTeamNameAsync(string organisationId, string teamId)
        {
            if (!await organisationRepository.ExistsByAsync(o => o.Id == organisationId))
            {
                throw new ArgumentException($"The organisation {organisationId} not exists");
            }

            var team = await repository.GetByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId);

            return(team?.ToContract());
        }
Exemplo n.º 3
0
        public async Task <Organisation> RegisterAsync(OrganisationRegistration organisationContract)
        {
            if (await repository.ExistsByAsync(o => o.Id == organisationContract.Id))
            {
                throw new NameAlreadyUsedException(organisationContract.Name);
            }
            var organisationEntity = organisationContract.ToEntity();

            organisationEntity = await repository.AddAsync(organisationEntity);


            var user = GeneratePasswordAndAttachAdminRole(organisationContract.AdminUser, organisationContract.Id);

            user.CreatedAt = DateTime.Now;
            user           = await userRepository.AddAsync(user);

            var administrationTeam = new Domain.Entities.Team
            {
                Name           = "org-admin",
                TeamCode       = "org-admin",
                OrganisationId = organisationContract.Id,
                CreatedAt      = DateTime.Now
            };

            administrationTeam.Users.Add(new Domain.Entities.TeamUser(user));

            administrationTeam = await teamRepository.AddAsync(administrationTeam);

            user.TeamsPermissions.Add(new Domain.Entities.TeamPermission
            {
                TeamCode = administrationTeam.TeamCode,
                IsOwner  = true
            });
            user = await userRepository.UpdateAsync(user);

            return(organisationEntity.ToContract());
        }
Exemplo n.º 4
0
        public async Task DeleteByIdAsync(string organisationId, string teamId, string appId)
        {
            if (!await organisationRepository.ExistsByAsync(o => o.Id == organisationId))
            {
                throw new ArgumentException($"The organisation {organisationId} not exists");
            }

            var teamEntity = await this.teamRepository.GetByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId);

            if (teamEntity == null)
            {
                throw new ArgumentException($"The team {teamId} from organisation {organisationId} not exists");
            }

            teamEntity.Applications.RemoveAll(a => a.Id == appId);
            await this.versionHistoryRepository.DeleteAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appId);

            await this.teamRepository.UpdateAsync(teamEntity);

            await this.repository.DeleteAsync(app => appId.Equals(app.Id) && app.TeamCode == teamId && app.OrganisationId == organisationId);
        }