示例#1
0
        public async Task <string> CreateAsync(AddOrEditAccountDto dto)
        {
            if (await CheckDuplicationClientIdAsync(dto.UserName) != null)
            {
                throw new BadRequestException($"This user already exists.");
            }

            var client = await _context.Clients.SingleOrDefaultAsync(x => x.ClientId.Equals(dto.ClientId));

            if (client == null)
            {
                throw new BadRequestException("Invalid client");
            }

            var entity = dto.ToEntity();

            entity.PasswordHash = _passwordHasher.HashPassword(entity, dto.Password);

            _context.Users.Add(entity);

            _context.UserClients.Add(new UserClient {
                UserId = entity.Id, ClientId = client.Id
            });

            await _context.SaveChangesAsync();

            return(entity.Id);
        }
示例#2
0
        public async Task <string> CreateOrEditAsync(AddOrEditAccountDto dto)
        {
            var client = await _context.Clients.SingleOrDefaultAsync(x => x.ClientId.Equals(dto.ClientId));

            if (client == null)
            {
                throw new BadRequestException("Invalid client");
            }

            var entity = await CheckDuplicationClientIdAsync(dto.UserName);

            if (entity == null)
            {
                entity = dto.ToEntity();

                if (!string.IsNullOrEmpty(dto.Password))
                {
                    entity.PasswordHash = _passwordHasher.HashPassword(entity, dto.Password);
                }

                _context.Users.Add(entity);

                _context.UserClients.Add(new UserClient {
                    UserId = entity.Id, ClientId = client.Id
                });
            }
            else
            {
                var userClients = await _context.UserClients
                                  .SingleOrDefaultAsync(x => x.UserId.Equals(entity.Id) && x.ClientId.Equals(client.Id));

                if (userClients == null)
                {
                    _context.UserClients.Add(new UserClient {
                        UserId = entity.Id, ClientId = client.Id
                    });
                }
            }

            await _context.SaveChangesAsync();

            return(entity.Id);
        }
示例#3
0
 public static User ToEntity(this AddOrEditAccountDto dto, User entity)
 {
     return(Mapper.Map(dto, entity));
 }
示例#4
0
 public static User ToEntity(this AddOrEditAccountDto dto)
 {
     return(Mapper.Map <User>(dto));
 }
示例#5
0
 public async Task <IActionResult> SyncPassword([FromBody] AddOrEditAccountDto dto)
 {
     return(Ok(await _accountService.CreateOrEditAsync(dto)));
 }