You are viewing limited content. For full access, please sign in.

Question

Question

LFDS SDK and Groups

asked on December 22, 2020

I have a situation where I have to convert several hundred users in LFDS over to SAML accounts. The export and import tools work great except for one issue, groups. There are a lot of groups and most users are in a combination of them. As the system grew the users were added progressively and so setting the groups manually was not an issue. However, now we're doing it all in one go under a tight deadline, and so I really need a programmatic way to do it.

The LFDS SDK seems like the way to go here, but the code samples, at least on my reading, don't have exactly what I'm looking for, unfortunately. Not complaining or anything, just explaining why I'm posting here. Does someone have code they could post that would demonstrate two things: 1. Iterating through each user and reading each group that they are in; and 2. Iterating through each user and assigning them to more than one group?

I know that's a big ask. If someone can only provide a partial solution, I'll take whatever I can get. I'm just in a serious time crunch here and I'm casting about for whatever might provide me with the most efficient path to a solution.

0 0

Answer

SELECTED ANSWER
replied on December 22, 2020

This sample shows how to get group membership, and add a user to a group:

 

public static void LFDSMembershipSample()
{
    Server lmoServ = Server.Connect(lfdsServer, false);
    Database lmoDB = lmoServ.GetDatabaseByRealmName(lfdsDatabase);

    lmoDB.Login(new SessionTokenParameters() { });

    // Get all LFDS users
    List<User> allLFDSUsers = lmoDB.GetAllUsersEx(0, (int)UserFlags.ApplicationAccount);

    // Get all LFDS groups
    List<Group> allLFDSGroups = lmoDB.GetAllGroups();

    // Cache the mapping from SID to trustee
    Dictionary<string, ITrustee> sidCache = new Dictionary<string, ITrustee>();
    allLFDSUsers.ForEach(u => sidCache.Add(u.SIDString, u));
    allLFDSGroups.ForEach(g => sidCache.Add(g.SIDString, g));

    // Get all members for each group
    Dictionary<string, List<ITrustee>> allGroupMembers = new Dictionary<string, List<ITrustee>>();
    Dictionary<string, List<Group>> allUserGroups = new Dictionary<string, List<Group>>();
    foreach (Group group in allLFDSGroups)
    {
        var groupMembers = group.GetMembers();

        // allGroupMembers is groupSID -> <List of accounts in that group>
        allGroupMembers.Add(group.SIDString, groupMembers.Select(kvp => sidCache.ContainsKey(kvp.Key) ? sidCache[kvp.Key] : null).ToList());

        // allUserGroups is userSID -> <List of groups this user belongs to>
        foreach (var trusteeSID in groupMembers.Keys)
        {
            if (!allUserGroups.ContainsKey(trusteeSID))
                allUserGroups[trusteeSID] = new List<Group>();

            allUserGroups[trusteeSID].Add(group);
        }
    }

    // Report the groups that each user belongs to
    foreach (User user in allLFDSUsers)
    {
        Console.WriteLine($"{user.Name} belongs to :");

        if (!allUserGroups.ContainsKey(user.SIDString))
        {
            Console.WriteLine("(none)");
            continue;
        }

        foreach (var group in allUserGroups[user.SIDString])
        {
            Console.WriteLine($"{group.Name}");
        }

        // NOTE: Alternatively, you could call user.GetLFDSGroupMembership
    }

    // Assign a user to a group
    Group sampleGroup = allLFDSGroups.FirstOrDefault();
    User sampleUser = allLFDSUsers.FirstOrDefault();

    if (sampleGroup != null && sampleUser != null)
    {
        Dictionary<string, string> currentMembers = sampleGroup.GetMembers();
        List<string> newMembers = currentMembers.Keys.ToList();

        Console.WriteLine($"Adding {sampleUser.Name} to {sampleGroup.Name}");
        newMembers.Add(sampleUser.SIDString);

        sampleGroup.SetMembers(newMembers);
    }
}

 

2 0
replied on January 26, 2022

Hi Robert,

 

Sorry for the delayed response to this post, but is there a way to get a group from its name? I am using this which works but it feels very overcomplicated.

 

Laserfiche.LicenseManager.LMO.Group targetGroup = allLFDSGroups.Where(g => g.Name.Equals(groupName)).FirstOrDefault();

0 0
replied on January 26, 2022

Unless you have the id or sid of the group, I think that's pretty much the only way.

I just double-checked my process and ended up doing the following:

        private Group GetGroupByName(string name)
        {
            foreach (var group in Database.GetAllGroups())
            {
                if (group.Name == name)
                {
                    return group;
                }
            }

            return null;
        }

 

2 0

Replies

replied on December 23, 2020

Thank you. This is very helpful and I very much appreciate it.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.