Esempio n. 1
0
        // Note (arnec): GetSeatRecommendations cannot be unit tested because it calls into PermissionsBL.GetRoles(), which calls into
        // DekiContext
        public IEnumerable <UserBE> GetSeatRecommendations(XDoc license, uint?offset, uint?limit, out uint totalCount, out uint queryCount)
        {
            if (!IsSeatLicensingEnabled(license))
            {
                throw new MindTouchLicenseSeatLicensingNotInUseException();
            }
            var pointsByUserId = new Dictionary <ulong, ushort>();

            // return all enabled users
            var usersById = _dataSession.Users_GetBySeat(false).ToDictionary(u => u.ID, true);

            // return all groups (and their member users)
            var groups = _dataSession.Groups_GetByQuery(null, null, SortDirection.UNDEFINED, GroupsSortField.UNDEFINED, null, null, out totalCount, out queryCount);

            ulong unseatedMask = GetUnseatedUserMask(license);

            // get all roles and filter by those that have revoked operations
            var roles = from r in PermissionsBL.GetRoles()
                        where (r.PermissionFlags & unseatedMask) != r.PermissionFlags
                        select r.ID;

            // add up points for membership to group that has a selected role
            foreach (var group in groups)
            {
                if (ArrayUtil.IsNullOrEmpty(group.UserIdsList))
                {
                    continue;
                }
                if (roles.Contains(group.RoleId))
                {
                    // group has one of the preselected roles.
                    foreach (uint userId in group.UserIdsList)
                    {
                        if (pointsByUserId.ContainsKey(userId))
                        {
                            pointsByUserId[userId] += POINTS_FOR_GROUP_MEMBERSHIP;
                        }
                        else
                        {
                            pointsByUserId[userId] = POINTS_FOR_GROUP_MEMBERSHIP;
                        }
                    }
                }
            }

            // points from having a revoked role directly
            foreach (UserBE user in usersById.Values)
            {
                if (roles.Contains(user.RoleId))
                {
                    if (pointsByUserId.ContainsKey(user.ID))
                    {
                        pointsByUserId[user.ID] += POINTS_FOR_USER_ROLE;
                    }
                    else
                    {
                        pointsByUserId[user.ID] = POINTS_FOR_USER_ROLE;
                    }
                }
            }

            // filter out users without points; sort by points; map back to user objects
            var ret = (from up in pointsByUserId
                       where usersById.ContainsKey((uint)up.Key) && up.Value >= POINTS_FOR_RECOMMENDATION
                       orderby up.Value descending, usersById[(uint)up.Key].Name
                       select usersById[(uint)up.Key])
                      .ToArray();

            queryCount = totalCount = (uint)ret.Count();

            // apply offset and limit and recalculate querycount
            if (offset != null || limit != null)
            {
                ret        = ret.Skip((int)(offset ?? 0)).Take((int)(limit ?? Int32.MaxValue)).ToArray();
                queryCount = (uint)ret.Count();
            }
            return(ret);
        }