/// <summary> /// Make a member joined activity object from the more generic database activity object /// </summary> /// <param name="activity"></param> /// <param name="memberService"></param> /// <returns></returns> private MemberJoinedActivity GenerateMemberJoinedActivity(Activity activity, MemberService memberService) { var dataPairs = ActivityBase.UnpackData(activity); if (!dataPairs.ContainsKey(AppConstants.KeyUserId)) { // Log the problem then skip AppHelpers.LogError($"A member joined activity record with id '{activity.Id}' has no user id in its data."); return(null); } var userId = dataPairs[AppConstants.KeyUserId]; var user = memberService.Get(Convert.ToInt32(userId)); if (user == null) { // Log the problem then skip AppHelpers.LogError($"A member joined activity record with id '{activity.Id}' has a user id '{userId}' that is not found in the user table."); return(null); } return(new MemberJoinedActivity(activity, user)); }
public IEnumerable <ActivityBase> GetAll(int howMany, BadgeService badgeService, MemberService memberService) { var activities = ContextPerRequest.Db.Activity.AsNoTracking().Take(howMany); var specificActivities = ConvertToSpecificActivities(activities, badgeService, memberService); return(specificActivities); }
public PagedList <ActivityBase> SearchPagedGroupedActivities(string search, int pageIndex, int pageSize, BadgeService badgeService, MemberService memberService) { // Read the database for all activities and convert each to a more specialised activity type var totalCount = ContextPerRequest.Db.Activity.AsNoTracking().Count(x => x.Type.ToUpper().Contains(search.ToUpper())); // Get the topics using an efficient var results = ContextPerRequest.Db.Activity.AsNoTracking() .Where(x => x.Type.ToUpper().Contains(search.ToUpper())) .OrderByDescending(x => x.Timestamp) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToList() .OrderByDescending(x => x.Timestamp) .ThenByDescending(x => x.Timestamp.TimeOfDay); var activities = new PagedList <Activity>(results, pageIndex, pageSize, totalCount); return(ConvertToSpecificActivities(activities, pageIndex, pageSize, badgeService, memberService)); }
/// <summary> /// Gets a paged list of badges /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="badgeService"></param> /// <param name="memberService"></param> /// <returns></returns> public PagedList <ActivityBase> GetPagedGroupedActivities(int pageIndex, int pageSize, BadgeService badgeService, MemberService memberService) { // Read the database for all activities and convert each to a more specialised activity type var totalCount = ContextPerRequest.Db.Activity.AsNoTracking().Count(); var results = ContextPerRequest.Db.Activity .AsNoTracking() .OrderByDescending(x => x.Timestamp) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToList() .OrderByDescending(x => x.Timestamp) .ThenByDescending(x => x.Timestamp.TimeOfDay); var activities = new PagedList <Activity>(results, pageIndex, pageSize, totalCount); var specificActivities = ConvertToSpecificActivities(activities, pageIndex, pageSize, badgeService, memberService); return(specificActivities); }
/// <summary> /// Converts a paged list of generic activities into a list of more specific activity instances /// </summary> /// <param name="activities"></param> /// <param name="badgeService"></param> /// <param name="memberService"></param> /// <returns></returns> private IEnumerable <ActivityBase> ConvertToSpecificActivities(IEnumerable <Activity> activities, BadgeService badgeService, MemberService memberService) { var listedResults = new List <ActivityBase>(); foreach (var activity in activities) { if (activity.Type == ActivityType.BadgeAwarded.ToString()) { var badgeActivity = GenerateBadgeActivity(activity, badgeService, memberService); if (badgeActivity != null) { listedResults.Add(badgeActivity); } } else if (activity.Type == ActivityType.MemberJoined.ToString()) { var memberJoinedActivity = GenerateMemberJoinedActivity(activity, memberService); if (memberJoinedActivity != null) { listedResults.Add(memberJoinedActivity); } } else if (activity.Type == ActivityType.ProfileUpdated.ToString()) { var profileUpdatedActivity = GenerateProfileUpdatedActivity(activity, memberService); if (profileUpdatedActivity != null) { listedResults.Add(profileUpdatedActivity); } } } return(listedResults); }
/// <summary> /// Converts a paged list of generic activities into a paged list of more specific activity instances /// </summary> /// <param name="activities">Paged list of activities where each member may be a specific activity instance e.g. a profile updated activity</param> /// <param name="pageIndex"> </param> /// <param name="pageSize"> </param> /// <param name="badgeService"></param> /// <param name="memberService"></param> /// <returns></returns> private PagedList <ActivityBase> ConvertToSpecificActivities(PagedList <Activity> activities, int pageIndex, int pageSize, BadgeService badgeService, MemberService memberService) { var listedResults = ConvertToSpecificActivities(activities, badgeService, memberService); return(new PagedList <ActivityBase>(listedResults, pageIndex, pageSize, activities.Count)); }
public List <Category> GetAllowedCategories(IMemberGroup role, string actionType, PermissionService permissionService, MemberService memberService, CategoryPermissionService categoryPermissionService) { var cacheKey = string.Concat("GetAllowedCategoriesCode-", role.Id, "-", actionType); return((List <Category>)ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem(cacheKey, () => { var filteredCats = new List <Category>(); var allCats = GetAll(); foreach (var category in allCats) { var permissionSet = permissionService.GetPermissions(category, role, memberService, categoryPermissionService); if (!permissionSet[actionType].IsTicked) { // TODO Only add it category is NOT locked filteredCats.Add(category); } } return filteredCats; })); }
/// <summary> /// Make a badge activity object from the more generic database activity object /// </summary> /// <param name="activity"></param> /// <param name="badgeService"></param> /// <returns></returns> private BadgeActivity GenerateBadgeActivity(Activity activity, BadgeService badgeService, MemberService memberService) { // Get the corresponding badge var dataPairs = ActivityBase.UnpackData(activity); if (!dataPairs.ContainsKey(AppConstants.KeyBadgeId)) { // Log the problem then skip AppHelpers.LogError($"A badge activity record with id '{activity.Id}' has no badge id in its data."); return(null); } var badgeId = dataPairs[AppConstants.KeyBadgeId]; var badge = badgeService.Get(new Guid(badgeId)); if (badge == null) { // Log the problem then skip AppHelpers.LogError($"A badge activity record with id '{activity.Id}' has a badge id '{badgeId}' that is not found in the badge table."); return(null); } var userId = dataPairs[AppConstants.KeyUserId]; var user = memberService.Get(Convert.ToInt32(userId)); if (user == null) { // Log the problem then skip AppHelpers.LogError($"A badge activity record with id '{activity.Id}' has a user id '{userId}' that is not found in the user table."); return(null); } return(new BadgeActivity(activity, badge, user)); }
public List <Category> GetAllowedCategories(IMemberGroup role, PermissionService permissionService, MemberService memberService, CategoryPermissionService categoryPermissionService) { return(GetAllowedCategories(role, AppConstants.PermissionDenyAccess, permissionService, memberService, categoryPermissionService)); }
/// <summary> /// The allowed Categories of a member /// </summary> /// <param name="memberGroup"></param> /// <param name="permissionService"></param> /// <param name="memberService"></param> /// <param name="categoryPermissionService"></param> /// <returns></returns> public List <Category> AllowedCreateCategories(IMemberGroup memberGroup, PermissionService permissionService, MemberService memberService, CategoryPermissionService categoryPermissionService) { var allowedAccessCategories = GetAllowedCategories(memberGroup, permissionService, memberService, categoryPermissionService); var allowedCreateTopicCategories = GetAllowedCategories(memberGroup, AppConstants.PermissionCreateTopics, permissionService, memberService, categoryPermissionService); var allowedCreateTopicCategoryIds = allowedCreateTopicCategories.Select(x => x.Id); if (allowedAccessCategories.Any()) { allowedAccessCategories.RemoveAll(x => allowedCreateTopicCategoryIds.Contains(x.Id)); allowedAccessCategories.RemoveAll(x => memberGroup.Name != AppConstants.AdminRoleName && x.LockCategory); } return(allowedAccessCategories); }
/// <summary> /// Add a new post /// </summary> /// <param name="postContent"> </param> /// <param name="topic"> </param> /// <param name="user"></param> /// <param name="permissionService"></param> /// <param name="categoryPermissionService"></param> /// <param name="memberPointsService"></param> /// <param name="permissions"> </param> /// <param name="memberService"></param> /// <returns>True if post added</returns> public Post AddNewPost(string postContent, Topic topic, Member user, PermissionService permissionService, MemberService memberService, CategoryPermissionService categoryPermissionService, MemberPointsService memberPointsService, out PermissionSet permissions) { // Get the permissions for the category that this topic is in permissions = permissionService.GetPermissions(topic.Category, user.Groups.FirstOrDefault(), memberService, categoryPermissionService); // Check this users role has permission to create a post if (permissions[AppConstants.PermissionDenyAccess].IsTicked || permissions[AppConstants.PermissionReadOnly].IsTicked) { // Throw exception so Ajax caller picks it up throw new ApplicationException(AppHelpers.Lang("Errors.NoPermission")); } // Has permission so create the post var newPost = new Post { PostContent = postContent, Member = user, MemberId = user.Id, Topic = topic, IpAddress = AppHelpers.GetUsersIpAddress(), DateCreated = DateTime.UtcNow, DateEdited = DateTime.UtcNow }; newPost = SanitizePost(newPost); var category = topic.Category; if (category.ModerateAllPostsInThisCategory == true) { newPost.Pending = true; } // create the post Add(newPost); // Update the users points score and post count for posting memberPointsService.Add(new MemberPoints { Points = Dialogue.Settings().PointsAddedPerNewPost, MemberId = user.Id, RelatedPostId = newPost.Id }); // add the last post to the topic topic.LastPost = newPost; // Add post to members count memberService.AddPostCount(user); return(newPost); }
/// <summary> /// Delete a post /// </summary> /// <param name="unitOfWork"></param> /// <param name="post"></param> /// <param name="memberService"></param> /// <param name="memberPointsService"></param> /// <param name="topicNotificationService"></param> /// <returns> True if parent was deleted too</returns> public bool Delete(UnitOfWork unitOfWork, Post post, MemberService memberService, MemberPointsService memberPointsService, TopicNotificationService topicNotificationService) { // Get the topic var topic = post.Topic; // The member who created this post var postMember = memberService.Get(post.MemberId); var topicDeleted = false; // See if we need to delete the topic or not if (post.IsTopicStarter) { topic.LastPost = null; // Delete all posts if (topic.Posts != null) { var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); var memberIds = postsToDelete.Select(x => x.MemberId).Distinct().ToList(); foreach (var postFromTopic in postsToDelete) { post.Files.Clear(); DeleteIndividualPost(topic, postFromTopic, memberPointsService, false); unitOfWork.SaveChanges(); } // Sync the members post count. For all members who had a post deleted. var members = memberService.GetAllById(memberIds); memberService.SyncMembersPostCount(members); } if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { topicNotificationService.Delete(topicNotification); } } topic.Posts?.Clear(); topic.TopicNotifications?.Clear(); topic.Category = null; topic.LastPost = null; ContextPerRequest.Db.Topic.Remove(topic); // Set to true topicDeleted = true; } else { DeleteIndividualPost(topic, post, memberPointsService); memberService.SyncMembersPostCount(new List <Member> { postMember }); } return(topicDeleted); }
/// <summary> /// Returns permission set based on category and role /// </summary> /// <param name="category"></param> /// <param name="memberGroup"></param> /// <param name="memberService"></param> /// <param name="categoryPermissionService"></param> /// <returns></returns> public PermissionSet GetPermissions(Category category, IMemberGroup memberGroup, MemberService memberService, CategoryPermissionService categoryPermissionService) { if (memberGroup == null) { // This can only happen if the user has deleted a group, and not reassigned them // so in this occasion we just set them to a guest until the admin assigns them a new group memberGroup = memberService.GetGroupByName(AppConstants.GuestRoleName); } // Pass the role in to see select which permissions to apply // Going to cache this per request, just to help with performance var objectContextKey = string.Concat("GetPermissions", "-", category.Id, "-", memberGroup.Id); if (!HttpContext.Current.Items.Contains(objectContextKey)) { switch (memberGroup.Name) { case AppConstants.AdminRoleName: _permissions = GetAdminPermissions(category, memberGroup); break; case AppConstants.GuestRoleName: _permissions = GetGuestPermissions(category, memberGroup, categoryPermissionService); break; default: _permissions = GetOtherPermissions(category, memberGroup, categoryPermissionService); break; } HttpContext.Current.Items.Add(objectContextKey, _permissions); } return(HttpContext.Current.Items[objectContextKey] as PermissionSet); }