public virtual IActionResult UsersIdRolesPost([FromRoute] int id, [FromBody] HetUserRole item) { bool exists = _context.HetUser.Any(x => x.UserId == id); // not found if (!exists) { return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } // check the role id bool roleExists = _context.HetRole.Any(x => x.RoleId == item.RoleId); // record not found if (!roleExists) { return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } // check the user exists HetUser user = UserHelper.GetRecord(id, _context); if (user == null) { return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } // check if the user has this role - then add if (user.HetUserRole.All(x => x.RoleId != item.RoleId)) { // create a new UserRole record HetUserRole userRole = new HetUserRole { RoleId = item.RoleId, UserId = id, EffectiveDate = item.EffectiveDate, ExpiryDate = item.ExpiryDate }; _context.HetUserRole.Add(userRole); _context.SaveChanges(); } // return updated roles user = UserHelper.GetRecord(id, _context); // return user roles return(new ObjectResult(new HetsResponse(user.HetUserRole))); }
public virtual IActionResult UsersIdRolesPut([FromRoute] int id, [FromBody] HetUserRole[] items) { bool exists = _context.HetUser.Any(x => x.UserId == id); // not found if (!exists || items == null) { return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } // get record HetUser user = UserHelper.GetRecord(id, _context); if (user.HetUserRole == null) { return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } // iterate the roles and update effective date foreach (HetUserRole item in items) { // check the role id bool roleExists = _context.HetUserRole.Any(x => x.RoleId == item.RoleId && x.UserId == id); if (roleExists) { // check if we need to modify the effective date HetUserRole role = _context.HetUserRole.First(x => x.RoleId == item.RoleId && x.UserId == id); if (role.ExpiryDate != item.ExpiryDate) { role.ExpiryDate = item.ExpiryDate; _context.SaveChanges(); } } } // return updated roles user = UserHelper.GetRecord(id, _context); // return user roles return(new ObjectResult(new HetsResponse(user.HetUserRole))); }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="user"></param> /// <param name="systemId"></param> /// <param name="smUserId"></param> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="maxUserIndex"></param> private static void CopyToInstance(DbAppContext dbContext, ImportModels.UserHets oldObject, ref HetUser user, string systemId, string smUserId, string firstName, string lastName, ref int maxUserIndex) { try { // file contains multiple records per user (1 for each district they can access) // we are currently importing 1 only -> where Default_Service_Area = Y if (oldObject.Default_Service_Area != "Y") { return; } // check if this user already exists in the db bool userExists = dbContext.HetUser.Any(x => x.SmUserId == smUserId); if (userExists) { user = dbContext.HetUser.First(x => x.SmUserId == smUserId); // ******************************************************************* // check if this is a different district // ******************************************************************* int serviceAreaId; try { serviceAreaId = int.Parse(oldObject.Service_Area_Id); } catch { // not mapped correctly throw new ArgumentException(string.Format("User Error - Invalid Service Area (userId: {0}", user.SmUserId)); } HetServiceArea serviceArea = dbContext.HetServiceArea.AsNoTracking() .FirstOrDefault(x => x.MinistryServiceAreaId == serviceAreaId); if (serviceArea == null) { // not mapped correctly throw new ArgumentException(string.Format("User Error - Invalid Service Area (userId: {0}", user.SmUserId)); } int tempUserId = user.UserId; int?tempDistrictId = serviceArea.DistrictId; if (tempDistrictId != null) { HetUserDistrict userDistrict = dbContext.HetUserDistrict.AsNoTracking() .FirstOrDefault(x => x.User.UserId == tempUserId && x.District.DistrictId == tempDistrictId); // *********************************************** // create user district record // *********************************************** if (userDistrict == null) { userDistrict = new HetUserDistrict { UserId = tempUserId, DistrictId = tempDistrictId, AppCreateTimestamp = DateTime.UtcNow, AppCreateUserid = systemId, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.HetUserDistrict.Add(userDistrict); dbContext.SaveChangesForImport(); } } } else { user = new HetUser { UserId = ++maxUserIndex, Active = true, SmUserId = smUserId, SmAuthorizationDirectory = "IDIR" }; if (!string.IsNullOrEmpty(firstName)) { user.GivenName = firstName; } if (!string.IsNullOrEmpty(lastName)) { user.Surname = lastName; } // ******************************************************************* // create initials // ******************************************************************* string temp = ""; if (!string.IsNullOrEmpty(lastName) && lastName.Length > 0) { temp = lastName.Substring(0, 1).ToUpper(); } if (!string.IsNullOrEmpty(firstName) && firstName.Length > 0) { temp = temp + firstName.Substring(0, 1).ToUpper(); } if (!string.IsNullOrEmpty(temp)) { user.Initials = temp; } // ******************************************************************* // map user to the correct service area // ******************************************************************* int serviceAreaId; try { serviceAreaId = int.Parse(oldObject.Service_Area_Id); } catch { // not mapped correctly throw new ArgumentException(string.Format("User Error - Invalid Service Area (userId: {0}", user.SmUserId)); } HetServiceArea serviceArea = dbContext.HetServiceArea.AsNoTracking() .FirstOrDefault(x => x.MinistryServiceAreaId == serviceAreaId); if (serviceArea == null) { // not mapped correctly throw new ArgumentException(string.Format("User Error - Invalid Service Area (userId: {0}", user.SmUserId)); } user.DistrictId = serviceArea.DistrictId; // ******************************************************************* // set the user's role // ** all new users will be added with basic access only // ******************************************************************* HetUserRole userRole = new HetUserRole(); HetRole role = dbContext.HetRole.FirstOrDefault(x => x.Name == "HETS District User"); int roleId = -1; if (role != null) { roleId = role.RoleId; } // *********************************************** // create user // *********************************************** user.AppCreateTimestamp = DateTime.UtcNow; user.AppCreateUserid = systemId; user.AppLastUpdateUserid = systemId; user.AppLastUpdateTimestamp = DateTime.UtcNow; userRole.Role = dbContext.HetRole.First(x => x.RoleId == roleId); userRole.EffectiveDate = DateTime.UtcNow.AddDays(-1); userRole.AppCreateTimestamp = DateTime.UtcNow; userRole.AppCreateUserid = systemId; userRole.AppLastUpdateUserid = systemId; userRole.AppLastUpdateTimestamp = DateTime.UtcNow; user.HetUserRole = new List <HetUserRole> { userRole }; dbContext.HetUser.Add(user); dbContext.SaveChangesForImport(); // *********************************************** // create user district record // *********************************************** HetUserDistrict userDistrict = new HetUserDistrict { UserId = user.UserId, DistrictId = user.DistrictId, IsPrimary = true, AppCreateTimestamp = DateTime.UtcNow, AppCreateUserid = systemId, AppLastUpdateUserid = systemId, AppLastUpdateTimestamp = DateTime.UtcNow }; dbContext.HetUserDistrict.Add(userDistrict); dbContext.SaveChangesForImport(); } } catch (Exception ex) { Debug.WriteLine("***Error*** - Employee Id: " + oldObject.Popt_Id); Debug.WriteLine("***Error*** - Master User Index: " + maxUserIndex); Debug.WriteLine(ex.Message); throw; } }
public virtual IActionResult UsersCurrentGet() { _logger.LogDebug("Get Current User"); // get the current user id string businessGuid = _context.SmBusinessGuid; string userId = _context.SmUserId; _logger.LogDebug("User Id: {0}", userId); _logger.LogDebug("Business Guid: {0}", businessGuid); // not found - return an HTTP 401 error response if (string.IsNullOrEmpty(userId)) { return(StatusCode(401)); } User user = new User(); if (string.IsNullOrEmpty(businessGuid)) { HetUser currentUser = _context.HetUser .Include(x => x.District) .Include(x => x.HetUserRole) .ThenInclude(y => y.Role) .ThenInclude(z => z.HetRolePermission) .ThenInclude(z => z.Permission) .First(x => x.SmUserId == userId); // remove inactive roles for (int i = currentUser.HetUserRole.Count - 1; i >= 0; i--) { if (currentUser.HetUserRole.ElementAt(i).EffectiveDate > DateTime.UtcNow || (currentUser.HetUserRole.ElementAt(i).ExpiryDate != null && currentUser.HetUserRole.ElementAt(i).ExpiryDate < DateTime.UtcNow)) { currentUser.HetUserRole.Remove(currentUser.HetUserRole.ElementAt(i)); } } user.Id = currentUser.UserId; user.SmUserId = currentUser.SmUserId; user.GivenName = currentUser.GivenName; user.Surname = currentUser.Surname; user.DisplayName = currentUser.GivenName + " " + currentUser.Surname; user.UserGuid = currentUser.Guid; user.BusinessUser = false; user.District = currentUser.District; user.HetUserDistrict = currentUser.HetUserDistrict; user.HetUserRole = currentUser.HetUserRole; user.SmAuthorizationDirectory = currentUser.SmAuthorizationDirectory; // set environment user.Environment = "Development"; if (_env.IsProduction()) { user.Environment = "Production"; } else if (_env.IsStaging()) { user.Environment = "Test"; } else if (_env.IsEnvironment("Training")) { user.Environment = "Training"; } else if (_env.IsEnvironment("UAT")) { user.Environment = "UAT"; } } else { HetBusinessUser tmpUser = _context.HetBusinessUser.AsNoTracking() .Include(x => x.HetBusinessUserRole) .ThenInclude(y => y.Role) .ThenInclude(z => z.HetRolePermission) .ThenInclude(z => z.Permission) .FirstOrDefault(x => x.BceidUserId.Equals(userId, StringComparison.InvariantCultureIgnoreCase)); if (tmpUser != null) { // get business HetBusiness business = _context.HetBusiness.AsNoTracking() .First(x => x.BusinessId == tmpUser.BusinessId); user.Id = tmpUser.BusinessUserId; user.SmUserId = tmpUser.BceidUserId; user.GivenName = tmpUser.BceidFirstName; user.Surname = tmpUser.BceidLastName; user.DisplayName = tmpUser.BceidDisplayName; user.UserGuid = tmpUser.BceidGuid; user.BusinessUser = true; user.BusinessId = tmpUser.BusinessId; user.BusinessGuid = business.BceidBusinessGuid; user.SmAuthorizationDirectory = "BCeID"; int id = 0; foreach (HetBusinessUserRole role in tmpUser.HetBusinessUserRole) { id++; HetUserRole userRole = new HetUserRole { UserRoleId = id, UserId = role.BusinessUserId, RoleId = role.RoleId, Role = role.Role }; if (user.HetUserRole == null) { user.HetUserRole = new List <HetUserRole>(); } user.HetUserRole.Add(userRole); } } } return(new ObjectResult(new HetsResponse(user))); }