internal UmbracoMembershipUser GetUmbracoUser(IGroupUnit<ISecurityStore> uow, HiveId id, bool userIsOnline)
        {
            return AppContext.FrameworkContext.ScopedCache.GetOrCreateTyped<UmbracoMembershipUser>(GetUmbracoUserCacheKeyForId(id), () =>
            {
                // TODO: Enable type of extension method GetEntityByRelationType to be passed all the way to the provider
                // so that it can use the typemappers collection to map back to a User

                // APN: I changed SingleOrDefault to FirstOrDefault to guard against YSODs if somehow a duplicate user gets into the store [31/Jan]
                //var userEntity = uow.Repositories
                //    .GetEntityByRelationType<UmbracoMembershipUser>(FixedRelationTypes.DefaultRelationType, _virtualRootId)
                //    .SingleOrDefault(x => x.EntitySchema.Alias == MembershipUserSchema.SchemaAlias && x.Username == username);

                // Get a list of all member relations
                var memberRelations = uow.Repositories.GetChildRelations(VirtualRootId, FixedRelationTypes.DefaultRelationType)
                    .Select(x => x.DestinationId)
                    .ToList();

                // Get a list of all users / members with a matching id
                var userEntities =
                    uow.Repositories.Where(
                        x => x.EntitySchema.Alias == MembershipUserSchema.SchemaAlias
                            && x.Id == id).ToList();

                // Get the first matching user with a member relation
                var userEntity = userEntities.FirstOrDefault(x => memberRelations.Any(y => y.Value == x.Id.Value));

                if (userEntity == null) return null;

                var user = new UmbracoMembershipUser();
                user.SetupFromEntity(userEntity);

                //if (userIsOnline)
                //{
                //    user.LastActivityDate = DateTime.UtcNow;

                //    uow.Repositories.AddOrUpdate(user);
                //    uow.Complete();
                //}

                return user;
            });
        }
        internal UmbracoMembershipUser GetUmbracoUser(IGroupUnit<ISecurityStore> uow, string username, bool userIsOnline)
        {
            return AppContext.FrameworkContext.ScopedCache.GetOrCreateTyped<UmbracoMembershipUser>(GetUmbracoUserCacheKeyForUsername(username), () =>
            {
                // Use FirstOrDefault in case somehow a duplicate user got into the system
                var matchingUsers = uow.Repositories
                    .WithParentIds(FixedRelationTypes.DefaultRelationType, VirtualRootId)
                    .Where(
                        x =>
                        x.EntitySchema.Alias == MembershipUserSchema.SchemaAlias &&
                        x.Attribute<string>(MembershipUserSchema.UsernameAlias) == username)
                    .OrderByDescending(x => x.UtcCreated)
                    .FirstOrDefault();

                if (matchingUsers == null) return null;

                var user = new UmbracoMembershipUser();
                user.SetupFromEntity(matchingUsers);

                //if (userIsOnline)
                //{
                //    user.LastActivityDate = DateTime.UtcNow;

                //    uow.Repositories.AddOrUpdate(user);
                //    uow.Complete();
                //}

                return user;
            });
        }