예제 #1
0
        public List <ModeratorWrapped> Wrap(List <Moderator> moderators, User currentUser = null)
        {
            var items = moderators.Select(x => new ModeratorWrapped(x)).ToList();

            var users            = _membershipService.GetUsersByIds(moderators.Select(x => x.UserId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subs             = _subDao.GetSubsByIds(moderators.Select(x => x.SubId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subModeratorInfo = currentUser != null?subs.Keys.ToDictionary(x => x, x => _moderationDao.GetModeratorInfoForUserInSub(currentUser.Id, x)) : new Dictionary <Guid, Moderator>();

            foreach (var item in items)
            {
                item.Sub  = subs[item.Moderator.SubId];
                item.User = users[item.Moderator.UserId];

                if (currentUser == null)
                {
                    item.CanRemove            = false;
                    item.CanChangePermissions = false;
                }
                else
                {
                    if (item.User.Id == currentUser.Id)
                    {
                        // we can remove ourself
                        item.CanRemove = true;
                        // but we cant change our permissions
                        item.CanChangePermissions = false;
                    }
                    else if (currentUser.IsAdmin)
                    {
                        // admins can always remove people anc hange permissions
                        item.CanRemove            = true;
                        item.CanChangePermissions = true;
                    }
                    else
                    {
                        var currentModeratorInfo = subModeratorInfo.ContainsKey(item.Sub.Id)
                            ? subModeratorInfo[item.Sub.Id]
                            : null;

                        if (currentModeratorInfo != null)
                        {
                            // the user is a moderator of some kind in the sub
                            // remember a mod can never remove or change permissions of a mod who has been one longer
                            if (currentModeratorInfo.AddedOn > item.Moderator.AddedOn)
                            {
                                // the current user was added as a moderator later than this current interated one.
                                // seniority!
                                item.CanRemove            = false;
                                item.CanChangePermissions = false;
                            }
                            else
                            {
                                // only users with "all" permission can change/remove another mod
                                if (currentModeratorInfo.Permissions.HasPermission(ModeratorPermissions.All))
                                {
                                    item.CanRemove            = true;
                                    item.CanChangePermissions = true;
                                }
                                else
                                {
                                    item.CanRemove            = false;
                                    item.CanChangePermissions = false;
                                }
                            }
                        }
                        else
                        {
                            // the user is not a moderator in this sub
                            item.CanRemove            = false;
                            item.CanChangePermissions = false;
                        }
                    }
                }
            }

            return(items);
        }
예제 #2
0
        public List <ModeratorInviteWrapped> Wrap(List <ModeratorInvite> invites, User currentUser = null)
        {
            var items = invites.Select(x => new ModeratorInviteWrapped(x)).ToList();

            var users            = _membershipDao.GetUsersByIds(invites.Select(x => x.UserId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subs             = _subDao.GetSubsByIds(invites.Select(x => x.SubId).Distinct().ToList()).ToDictionary(x => x.Id, x => x);
            var subModeratorInfo = currentUser != null?subs.Keys.ToDictionary(x => x, x => _moderationDao.GetModeratorInfoForUserInSub(currentUser.Id, x)) : new Dictionary <Guid, Moderator>();

            foreach (var item in items)
            {
                item.Sub  = subs[item.ModeratorInvite.SubId];
                item.User = users[item.ModeratorInvite.UserId];

                if (currentUser == null)
                {
                    item.CanRemove = false;
                }
                else
                {
                    if (item.User.Id == currentUser.Id)
                    {
                        // we can remove ourself
                        item.CanRemove = true;
                        // but we can't change our permissions
                        item.CanChangePermissions = false;
                    }
                    else if (currentUser.IsAdmin)
                    {
                        // admins can always remove people and change permissions
                        item.CanRemove            = true;
                        item.CanChangePermissions = true;
                    }
                    else
                    {
                        var currentModeratorInfo = subModeratorInfo.ContainsKey(item.Sub.Id)
                            ? subModeratorInfo[item.Sub.Id]
                            : null;

                        if (currentModeratorInfo != null)
                        {
                            // only users with "all" permission can change invite permissions
                            if (currentModeratorInfo.Permissions.HasPermission(ModeratorPermissions.All))
                            {
                                item.CanRemove            = true;
                                item.CanChangePermissions = true;
                            }
                            else
                            {
                                item.CanRemove            = false;
                                item.CanChangePermissions = false;
                            }
                        }
                        else
                        {
                            // the user is not a moderator in this sub
                            item.CanRemove            = false;
                            item.CanChangePermissions = false;
                        }
                    }
                }
            }

            return(items);
        }