Пример #1
0
 public ClientViewModel(RoleUserAssignedClient extendedClient, string currentRoleUserEmail)
 {
     this.Id         = extendedClient.Client.Id;
     this.ClientName = extendedClient.Client.ClientName;
     this.ClientId   = extendedClient.Client.ClientId;
     this.Secrets    = extendedClient.Client.ClientSecrets
                       .Select(x => new ClientSecretViewModel(x))
                       .ToList();
     this.AbleToAddSecret = Secrets.Count() < 2 &&
                            extendedClient.OrganisationRoleUserAssignedClients.Any(x => x.Organisation.RoleUsers
                                                                                   .Any(y => y.Role == RoleType.Developer));
 }
        public async Task <OperationResult> LinkRoleUserToClientAsync(
            int clientDbId,
            string roleUserEmail,
            List <Guid> selectedOrganisationIds,
            List <string> selectedOrganisationScopes,
            List <string> authorisedOrgIds)
        {
            var extendedClientEntity = new RoleUserAssignedClient
            {
                ClientRecordId = clientDbId,
                CreatedByEmail = roleUserEmail,
            };

            _organisationContext.RoleUserAssignedClients.Add(extendedClientEntity);

            var organisations = await _organisationContext.Organisations
                                .Include(x => x.Scopes)
                                .Where(x => authorisedOrgIds.Any(y => y.Equals(x.ExternalId)) &&
                                       selectedOrganisationIds.Any(y => y == x.Id) &&
                                       x.Scopes.Any(y => selectedOrganisationScopes
                                                    .Any(z => z == y.Name)))
                                .ToListAsync();

            if (!organisations.Any())
            {
                _logger.LogError($"Not authorised to create links between Role user with Email: {roleUserEmail} and Client with Id: {clientDbId}");
                return(OperationResult.Error(OperationErrors.Unauthorised));
            }

            _organisationContext.OrganisationRoleUserAssignedClients.AddRange(organisations.Select(x =>
                                                                                                   new OrganisationRoleUserAssignedClient()
            {
                RoleUserAssignedClient = extendedClientEntity,
                Organisation           = x
            }));

            var organisationScopes = organisations.SelectMany(x => x.Scopes)
                                     .Where(scope => selectedOrganisationScopes.Any(selectedScope => selectedScope.Equals(scope.Name)))
                                     .ToList();

            _organisationContext.OrganisationScopeRoleUserAssignedClients
            .AddRange(organisationScopes.Select(x => new OrganisationScopeRoleUserAssignedClient
            {
                OrganisationScope      = x,
                RoleUserAssignedClient = extendedClientEntity
            }));

            await _organisationContext.SaveChangesAsync();

            return(OperationResult.Success());
        }