Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Bekker Andersen 12 posts 91 karma points c-trib
    Aug 20, 2020 @ 13:41
    Christian Bekker Andersen
    0

    Custom MemberGroupService only working under "Members" but not in "Public access"

    I've implemented my owm membership/role provider and also my own IMemberGroupService by registering it in a composer like so:

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class MembersComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.RegisterUnique<IMemberGroupService, DEMemberGroupService>();
        }
    }
    

    And a my test implementation of that service:

    public class DEMemberGroupService : IMemberGroupService
    {
        public IEnumerable<IMemberGroup> GetAll()
        {
            var memberGroups = new List<MemberGroup>();
            memberGroups.Add(new MemberGroup
            {
                Name = "DE Medlem",
                Id = 0
            });
    
            return memberGroups;
        }
    
        public IMemberGroup GetById(int id)
        {
            return GetAll().FirstOrDefault(x => x.Id == id);
        }
    
        public IEnumerable<IMemberGroup> GetByIds(IEnumerable<int> ids)
        {
            return ids.Select(GetById);
        }
    
        public IMemberGroup GetByName(string name)
        {
            return GetAll().FirstOrDefault(x => x.Name == name);
        }
    
        public void Save(IMemberGroup memberGroup, bool raiseEvents = true)
        {
            throw new NotImplementedException();
        }
    
        public void Delete(IMemberGroup memberGroup)
        {
            throw new NotImplementedException();
        }
    }
    

    Now when i go to "Members -> Member Groups" i see my "DE Medlem" group just fine.

    But when i go to set "Public access" on content and choose to set "Group based", i get:

    You need to create a member group before you can use group based authentication
    

    Also the response from /umbraco/backoffice/UmbracoApi/MemberGroup/GetAllGroups looks a bit funny, but might be unrelated.

    )]}',
    []
    

    Any ideas?

  • Christian Bekker Andersen 12 posts 91 karma points c-trib
    Aug 20, 2020 @ 13:53
    Christian Bekker Andersen
    100

    After a bit of searching in source, i found that Umbraco does the following before returning groups: https://github.com/umbraco/Umbraco-CMS/blob/1eb0c93e053781cc48f801a878dee3609d92bc71/src/Umbraco.Web/Editors/MemberGroupController.cs#L121

    public IEnumerable<MemberGroupDisplay> GetAllGroups()
        {
            if (_provider.IsUmbracoMembershipProvider())
            {
                return Services.MemberGroupService.GetAll()
                    .Select(Mapper.Map<IMemberGroup, MemberGroupDisplay>);
            }
    
            return Enumerable.Empty<MemberGroupDisplay>();
        }
    

    Where IsUmbracoMembershipProvider checks wether your custom provider is "UmbracoMembershipProviderBase". And since my custom membership provider was just inheriting "MembershipProvider", it didn't work.

    So inheriting UmbracoMembershipProviderBase instead, made everything work as expected.

Please Sign in or register to post replies

Write your reply to:

Draft