public static User ToUser(AdministratorReqModel model, string pwd, string secret) { return(new User { Email = model.Email.ToLower(), Name = model.Name, PasswordHash = HashUtility.CreatePasswordHash(pwd, secret), IsTemporaryPassword = true, LastUpdatedOn = DateTime.UtcNow, IsEmailVerified = true, VerificationToken = Guid.NewGuid().ToString(), VerificationTokenExpiry = DateTime.UtcNow.AddHours(24) }); }
public async Task <ActionResult> Create([FromRoute] long locationId, [FromBody] AdministratorReqModel model) { if (!ModelState.IsValid) { return(BadRequest(new ErrorModel { Message = string.Join(",", ModelState.Values.SelectMany(v => v.Errors.Select(x => x.ErrorMessage))) })); } if (await _authService.IsEmailExists(model.Email)) { var res = await _adminService.UpdatedAdmin(locationId, _authHelpers.GetCurrentUserId().Value, model); return(StatusCode(res.GetStatusCode(), res.Result)); } else { var res = await _adminService.CreateAdmin(locationId, _authHelpers.GetCurrentUserId().Value, model); return(StatusCode(res.GetStatusCode(), res.Result)); } }
public async Task <ServiceResponseResult> CreateAdmin(long locationId, long userId, AdministratorReqModel model) { Logger.WriteInformation("Creating admin data."); var tempPwd = Guid.NewGuid().ToString(); var user = UserMapper.ToUser(model, tempPwd, _appSettings.Secret); user = user.UpdateLastUpdatedBy(userId); _context.Add(user); await _context.SaveChangesAsync(); var userLocation = new UserLocation { CreatedBy = userId, CreatedOn = DateTime.UtcNow, LocationId = locationId, LastUpdatedBy = userId, LastUpdatedOn = DateTime.UtcNow, UserId = user.Id, State = (int)AdministratorState.Invited }; _context.Add(userLocation); await _context.SaveChangesAsync(); var response = new AdministratorResult(user, userLocation); if (model.Permissions != null) { var userPermission = UserPermissionMapper.ToUserPermission(model.Permissions); userPermission = userPermission.UpdateLastUpdatedBy(userId) .UpdateLastUpdatedOn(DateTime.UtcNow) .UpdateUserLocationId(userLocation.Id); _context.Add(userPermission); await _context.SaveChangesAsync(); response.AddPermission(userPermission); } var key = await _context.KeyHolder.FirstOrDefaultAsync(x => x.KeySerialNumber == model.KeySerialNumber); if (key != null) { var userKeyMapping = new UserKeyMapping { AppliedOn = DateTime.UtcNow, KeySerialNumber = key.KeySerialNumber, LocationId = locationId, UserId = user.Id }; userLocation.UpdateIsToolKitEnabled(true); _context.Update(userLocation); _context.Add <UserKeyMapping>(userKeyMapping); await _context.SaveChangesAsync(); response.AddToolkit(key); } var emailData = await _emailService.ConstructResetPassword(tempPwd); await _emailSender.SendMailViaSmtpClientAsync(new string[] { user.Email }, new string[] { }, new string[] { }, emailData); Logger.WriteInformation("Creating admin data completed."); return(new ServiceResponseResult { Result = response, StatusCode = System.Net.HttpStatusCode.OK }); }
public async Task <ServiceResponseResult> UpdatedAdmin(long locationId, long userId, AdministratorReqModel model) { Logger.WriteInformation("Updating admin data."); var user = await _context.User.FirstOrDefaultAsync(x => x.Email == model.Email); var userLoc = await _context.UserLocation.FirstOrDefaultAsync(x => x.UserId == user.Id && x.LocationId == locationId); if (userLoc == null) { return(new ServiceResponseResult { Result = new { Message = $"User does not exists in this location {locationId}" }, StatusCode = System.Net.HttpStatusCode.NotFound }); } user = user.UpdateName(model.Name) .UpdateLastUpdatedBy(userId) .UpdateLastUpdatedOn(DateTime.UtcNow); _context.Update <User>(user); var userPermission = await _context.UserPermission.FirstOrDefaultAsync(x => x.UserLocation.LocationId == locationId && x.UserLocation.UserId == user.Id); if (userPermission != null) { return(new ServiceResponseResult { Result = new { Message = "User permission for location already added" }, StatusCode = System.Net.HttpStatusCode.BadRequest }); } await _context.SaveChangesAsync(); var response = new AdministratorResult(user, userLoc); if (model.Permissions != null) { userPermission = UserPermissionMapper.ToUserPermission(model.Permissions); userPermission = userPermission.UpdateLastUpdatedBy(userId) .UpdateLastUpdatedOn(DateTime.UtcNow) .UpdateUserLocationId(userLoc.Id); _context.Add <UserPermission>(userPermission); await _context.SaveChangesAsync(); response.AddPermission(userPermission); } var key = await _context.KeyHolder.FirstOrDefaultAsync(x => x.KeySerialNumber == model.KeySerialNumber); if (key != null) { var userKeyMapping = new UserKeyMapping { AppliedOn = DateTime.UtcNow, KeySerialNumber = key.KeySerialNumber, LocationId = locationId, UserId = user.Id }; userLoc.UpdateIsToolKitEnabled(true); _context.Update(userLoc); _context.Add <UserKeyMapping>(userKeyMapping); await _context.SaveChangesAsync(); response.AddToolkit(key); } Logger.WriteInformation("Updating admin activity data completed."); return(new ServiceResponseResult { Result = response, StatusCode = System.Net.HttpStatusCode.OK }); }