Copied to clipboard

Flag this post as spam?

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


  • Niels Steinmeier 3 posts 28 karma points
    May 19, 2022 @ 09:09
    Niels Steinmeier
    0

    Restrict users to a specific member group

    Is there any way to restrict a user to a specific member group. A scenario could be a user in Sweden should be able to create members – but only in the Swedish member group.

    Anybody else been facing the same issue?

    Thank yo

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    May 24, 2022 @ 11:30
    Marc Goodson
    1

    Hi Niels

    There isn't that granular level of permissions available via the UI (but you know that), but it should be possible to 'intercept' the request angularJS makes to get the list of Member Groups to pick, and in that interception, check which User Groups the current backoffice user is in and then filter out any Member Groups they are not allowed to pick:

    An example of an interception to do this based on a convention where User Group contains the 'name' of the Member Group you want to allow...

    angular.module('umbraco.services').config([
        '$httpProvider',
        function ($httpProvider) {
    
            $httpProvider.interceptors.push(function ($q, $injector) {
                return {
                    'response': function (response) {
                         if (response.config.url.indexOf("/umbraco/backoffice/UmbracoTrees/ApplicationTree/GetApplicationTrees?application=member&tree=memberGroups&use=dialog") === 0) {
                                // somehow we need to map 'User Group' membership to which 'Member Groups' a user can add
                                // what if UserGroups followed a convention of being called 'languageEditors_Spanish'
                                // and member groups followed the pattern of being called the same thing after the underscore eg 'Spanish'
                                // then here we could get the current user, loop through their user Groups
                                // and then remove all member groups that didn't 'match' their groups membership
                                // the user would only be able to pick 'matching' Member Groups
                                //for reference look in console to see this logged output of the request we have hijacked:
                                console.log(response);
                                // it has a data property, with children for each Member Group
                                // lets create a variable to hold 'permitted member groups' for the current user
                                var permittedMemberGroups = [];       
                                // and a variable to track if they should have their groups restricted
                                // people not in any of these special User Groups won't have any Member Group restrictions
                                var hasMemberGroupRestriction = false;              
                                //get the current user
                                var userService = $injector.get('userService');
                                userService.getCurrentUser().then(function (currentUser) {
                                if (currentUser !== null && currentUser.userGroups.length > 0) {
                                    // loop for all groups and see if any are special 'language groups' ones to determine whether any filtering should take place
                                    var i;
                                    for (i = 0; i < currentUser.userGroups.length; i++) {
                                        console.log(currentUser.userGroups[i]);
                                        var userGroupName = currentUser.userGroups[i];
                                        // check group name starts with languageEditors
                                        if (userGroupName.startsWith("languageEditors")) {
                                            hasMemberGroupRestriction = true;
                                            console.log('Is Language Group');
                                            // split language group name on _ and add language name to permittedMemberGroups array
                                            var languageName = userGroupName.split("_")[1];
                                            console.log(languageName);
                                            permittedMemberGroups.push(languageName)
                                        }
                                    }
                                }
    
                                // now we should have a list of language member groups to permit and a flag to determine whether to restrict!
                                console.log(hasMemberGroupRestriction);                            
                                if (hasMemberGroupRestriction) {
                                    // if so lets loop through the Member Group options and remove any that aren't in the list of permitted groups
                                    var i = response.data.children.length;
                                    while (i--) {
                                        var memberGroup = response.data.children[i].name;
                                        if (permittedMemberGroups.indexOf(memberGroup) === -1) {
                                            response.data.children.splice(i, 1);
                                        }
                                    }
                                }  
    });                            
                    }
    
    
                        return response;
                    }
                };
            });
    
        }]);
    

    So create a folder in app_plugins called BackofficeTweaks, add a package.manifest file, which then loads a javascript file called tweaks.js - where you paste the example above

    {
       javascript: [
           '~/App_Plugins/BackofficeTweaks/tweaks.js'
       ],
       css: [   ]
    }
    

    Create some Member Groups, eg Spanish, French, German Create some User Groups, eg languageEditorsSpanish, languageEditorsFrench, languageEditors_German

    Now create some Users, and put them in single or multiple languageEditors_ groups

    Now logged in as one of those users, if you add a Member, and click 'Add Member Group' your list of options should only be Member Groups matching the User Group convention...

    Spanish editors will only be able to add Spanish Member Group...

    ... or at least that's the gist of it!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft