private async Task SetForumLastPost(PhpbbForums forum, PhpbbPosts post, AuthenticatedUser author, bool hardReset = false) { if (hardReset || forum.ForumLastPostTime < post.PostTime) { forum.ForumLastPostId = post.PostId; forum.ForumLastPostSubject = post.PostSubject; forum.ForumLastPostTime = post.PostTime; forum.ForumLastPosterColour = author.UserColor !; forum.ForumLastPosterId = post.PosterId; forum.ForumLastPosterName = author.UserId == Constants.ANONYMOUS_USER_ID ? post.PostUsername : author.Username !; var conn = _context.GetDbConnection(); await conn.ExecuteAsync( @"UPDATE phpbb_forums SET forum_last_post_id = @ForumLastPostId, forum_last_post_subject = @ForumLastPostSubject, forum_last_post_time = @ForumLastPostTime, forum_last_poster_colour = @ForumLastPosterColour, forum_last_poster_id = @ForumLastPosterId, forum_last_poster_name = @ForumLastPosterName WHERE forum_id = @ForumId", forum ); } }
public async Task <(string Message, bool?IsSuccess)> ManageForumsAsync(UpsertForumDto dto, int adminUserId, bool isRoot) { var lang = GetLanguage(); try { if (isRoot) { await ReorderChildren(0); await _context.SaveChangesAsync(); return(string.Format(LanguageProvider.Admin[lang, "FORUM_UPDATED_SUCCESSFULLY_FORMAT"], _config.GetObject <string>("ForumName")), true); } var actual = await _context.PhpbbForums.FirstOrDefaultAsync(f => f.ForumId == dto.ForumId); var isNewForum = false; if (string.IsNullOrWhiteSpace(dto.ForumName)) { return(LanguageProvider.Admin[lang, "INVALID_FORUM_NAME"], false); } if ((dto.ForumId ?? 0) > 0 && actual == null) { return(string.Format(LanguageProvider.Admin[lang, "FORUM_DOESNT_EXIST_FORMAT"], dto.ForumId), false); } else if ((dto.ForumId ?? 0) == 0) { actual = new PhpbbForums(); isNewForum = true; } actual !.ForumName = dto.ForumName; actual.ForumDesc = dto.ForumDesc ?? string.Empty; if (dto.HasPassword.HasValue && !dto.HasPassword.Value) { actual.ForumPassword = string.Empty; } if (!string.IsNullOrWhiteSpace(dto.ForumPassword)) { actual.ForumPassword = Crypter.Phpass.Crypt(dto.ForumPassword, Crypter.Phpass.GenerateSalt()); } actual.ParentId = dto.ParentId ?? actual.ParentId; actual.ForumType = dto.ForumType ?? actual.ForumType; actual.ForumRules = HttpUtility.HtmlEncode(dto.ForumRules ?? actual.ForumRules ?? string.Empty); actual.ForumRulesLink = HttpUtility.HtmlEncode(dto.ForumRulesLink ?? actual.ForumRules ?? string.Empty); if (isNewForum) { var result = _context.PhpbbForums.Add(actual); result.Entity.ForumId = 0; await _context.SaveChangesAsync(); actual = result.Entity; } else { await _context.SaveChangesAsync(); } await ReorderChildren(actual.ForumId); var rolesForAclEntity = new Dictionary <AclEntityType, HashSet <(int entityId, int roleId)> > { { AclEntityType.User, translatePermissions(dto.UserForumPermissions) }, { AclEntityType.Group, translatePermissions(dto.GroupForumPermissions) } }; foreach (var idx in dto.UserPermissionToRemove ?? new List <int>()) { var(entityId, roleId) = translatePermission(dto.UserForumPermissions?[idx]); _context.PhpbbAclUsers.Remove(await _context.PhpbbAclUsers.FirstAsync(x => x.UserId == entityId && x.AuthRoleId == roleId && x.ForumId == actual.ForumId)); rolesForAclEntity[AclEntityType.User].Remove((entityId, roleId)); } foreach (var idx in dto.GroupPermissionToRemove ?? new List <int>()) { var(entityId, roleId) = translatePermission(dto.GroupForumPermissions?[idx]); _context.PhpbbAclGroups.Remove(await _context.PhpbbAclGroups.FirstAsync(x => x.GroupId == entityId && x.AuthRoleId == roleId && x.ForumId == actual.ForumId)); rolesForAclEntity[AclEntityType.Group].Remove((entityId, roleId)); } await _context.SaveChangesAsync(); var tasks = new List <Task>(); foreach (var byType in rolesForAclEntity) { foreach (var(entityId, roleId) in byType.Value) { var prefix = byType.Key == AclEntityType.Group ? "group" : "user"; var table = $"phpbb_acl_{prefix}s"; var entityIdColumn = $"{prefix}_id"; tasks.Add(ManagePermissions(table, entityIdColumn, actual.ForumId, entityId, roleId)); } } await Task.WhenAll(tasks); await _operationLogService.LogAdminForumAction(isNewForum?AdminForumActions.Add : AdminForumActions.Update, adminUserId, actual); return(string.Format(LanguageProvider.Admin[lang, "FORUM_UPDATED_SUCCESSFULLY_FORMAT"], actual.ForumName), true); }