/// <inheritdoc /> public async Task CreateDevice(UserDevice device) { GuardDevice(device); _dbContext.UserDevices.Add(device); await _dbContext.SaveChangesAsync(); var user = device.User; if (user == null) { user = await _dbContext.Users.SingleOrDefaultAsync(x => x.Id == device.UserId); } var @event = new DeviceCreatedEvent(DeviceInfo.FromUserDevice(device), SingleUserInfo.FromUser(user)); await _eventService.Publish(@event); }
public async Task <IActionResult> Post([FromBody] RegistrationViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userIdentity = mapper.Map <AppUser>(model); var result = await userManager.CreateAsync(userIdentity, model.Password); if (!result.Succeeded) { foreach (var e in result.Errors) { ModelState.TryAddModelError(e.Code, e.Description); } return(new BadRequestObjectResult(ModelState)); } await context.OtherUsers.AddAsync(new OtherUser { IdentityId = userIdentity.Id, Location = model.Location }); await context.SaveChangesAsync(); return(new OkResult()); }
public async Task <IActionResult> AddClaims([FromBody] IEnumerable <CreateClaimRequest> claims) { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound()); } var systemClaims = await _configurationDbContext .ClaimTypes .Where(x => claims.Select(x => x.Type).Contains(x.Name)) .ToListAsync(); var userAllowedClaims = systemClaims.Where(x => x.UserEditable).Select(x => x.Name).ToList(); var isSystemClient = User.IsSystemClient(); if (isSystemClient && systemClaims.Count != claims.Count()) { var notAllowedClaims = claims.Select(x => x.Type).Except(systemClaims.Select(x => x.Name)); ModelState.AddModelError(nameof(claims), $"The following claims are not allowed to add by the client: '{string.Join(", ", notAllowedClaims)}'."); return(BadRequest(new ValidationProblemDetails(ModelState))); } if (!isSystemClient && userAllowedClaims.Count != claims.Count()) { var notAllowedClaims = claims.Select(x => x.Type).Except(userAllowedClaims); ModelState.AddModelError(nameof(claims), $"The following claims are not allowed to add: '{string.Join(", ", notAllowedClaims)}'."); return(BadRequest(new ValidationProblemDetails(ModelState))); } var claimsToAdd = claims.Select(x => new IdentityUserClaim <string> { UserId = user.Id, ClaimType = x.Type, ClaimValue = x.Value }) .ToArray(); _dbContext.UserClaims.AddRange(claimsToAdd); await _dbContext.SaveChangesAsync(); return(Ok(claimsToAdd.Select(x => new ClaimInfo { Id = x.Id, Type = x.ClaimType, Value = x.ClaimValue }))); }
public async Task <IActionResult> AddUserClaim([FromRoute] string userId, [FromBody] CreateClaimRequest request) { var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(x => x.Id == userId); if (user == null) { return(NotFound()); } var claimToAdd = new IdentityUserClaim <string> { UserId = userId, ClaimType = request.Type, ClaimValue = request.Value }; _dbContext.UserClaims.Add(claimToAdd); await _dbContext.SaveChangesAsync(); return(CreatedAtAction(nameof(GetUserClaim), Name, new { userId, claimId = claimToAdd.Id }, new ClaimInfo { Id = claimToAdd.Id, Type = claimToAdd.ClaimType, Value = claimToAdd.ClaimValue })); }