/// <summary> /// Saves the assign permission. /// </summary> /// <param name="permissionIds">The permission ids.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public UserPermissionResponse SaveAssignPermission(UserRolesPermissionDTO permissionIds, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); TblUserPermissions userPermissions = null; foreach (var item in permissionIds.RolePermissionIds) { var newPermission = item.PermissionIds.ToList(); var existingPerm = _context.TblUserPermissions.Where(t => t.UserId == permissionIds.UserId && t.UserorRole == "User" && t.RoleId == item.RoleId).ToList(); //Delete which are not in current permissions-- var delPermission = existingPerm.Where(m => !item.PermissionIds.Contains((int)m.PermissionId)).ToList(); foreach (var perm in delPermission) { _context.Remove(perm); existingPerm.Remove(perm); } var includedPermission = existingPerm.Where(m => item.PermissionIds.Contains((int)m.PermissionId)).ToList(); foreach (var incPerm in includedPermission) { newPermission.Remove((int)incPerm.PermissionId); } //Add new record foreach (var permissionId in newPermission) { userPermissions = new TblUserPermissions(); userPermissions.UserId = permissionIds.UserId; userPermissions.PermissionId = permissionId; userPermissions.RoleId = item.RoleId; userPermissions.UserorRole = "User"; // userPermissions.CreatedBy = CreatedBy; userPermissions.CreatedDate = DateTime.Now; userPermissions.Status = true; _context.TblUserPermissions.Add(userPermissions); } } _context.SaveChanges(); return(new UserPermissionResponse { Status = BusinessStatus.Created, Id = userPermissions?.UserPermissionsId.ToString(), ResponseMessage = $"Assigned Permissions successfully!!" }); }
/// <summary> /// Assigns the permission. /// </summary> /// <param name="permissionIds">The permission ids.</param> /// <param name="apiContext">The API context.</param> /// <returns></returns> public UserPermissionResponse AssignPermission(UserPermissionDTO permissionIds, ApiContext apiContext) { _context = (AVOUMContext)DbManager.GetContext(apiContext.ProductType, apiContext.ServerType); UserPermissionsDTO userPermissions = null; for (int i = 0; i < permissionIds.PermissionIds.Length; i++) { userPermissions = new UserPermissionsDTO(); userPermissions.UserId = permissionIds.UserId; userPermissions.PermissionId = Convert.ToInt16(permissionIds.PermissionIds[i]); userPermissions.UserorRole = "User"; // userPermissions.CreatedBy = CreatedBy; userPermissions.CreatedDate = DateTime.Now; userPermissions.Status = true; var _usersPer = _mapper.Map <TblUserPermissions>(userPermissions); _context.TblUserPermissions.Add(_usersPer); } _context.SaveChanges(); //return userPermissions; return(new UserPermissionResponse { Status = BusinessStatus.Created, perm = userPermissions, ResponseMessage = $"Assigned {userPermissions.PermissionId} Permissions successfully!!" }); }