Copied to clipboard

Flag this post as spam?

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


  • Umbraco_Tim 45 posts 20 karma points
    May 08, 2009 @ 18:07
    Umbraco_Tim
    0

    Getting Members Groups From Member ID in XSLT

    Hi,

    I have a huge project here.

    [Part 1]

    The area I am stuck on and can't find any documentation on is how to retrieve what groups a user belongs too.

    [Part 2]

    When you use Role Based permissions.

    Is there a way of iterating through the Node tree and seeing what user groups are set to what node?

  • Casey Neehouse 1339 posts 483 karma points MVP 2x admin
    May 08, 2009 @ 23:16
    Casey Neehouse
    0

    I am not sure on how to access this through the membership provider api, but can offer up an xslt extension that will provide a series of member calls.

    Some of these are good, others are not so good. Pardon the use of string builder rather than building the xml document directly - something I wrote a long time ago.

    GetMembersByGroupName
    GetMembersByGroupId
    GetMembersAll
    GetMemberGroupsAll
    GetMemberGroupsForMember

    [code]
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.XPath;
    using System.Xml;
    using umbraco.cms.businesslogic.member;
    using umbraco;
    using Microsoft.ApplicationBlocks.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Net;
    using System.Web.Caching;
    using System.IO;
    using System.Data;

    namespace bctllc.xsltext
    {
    class members
    {
    #region Member XML Methods
    public static XPathNodeIterator GetMembersByGroupName(string groupName)
    {
    try
    {
    return GetMembersByGroupId(MemberGroup.GetByName(groupName).Id);
    }
    catch (Exception ex)
    {
    XmlDocument xd = new XmlDocument();
    xd.LoadXml("");
    return xd.CreateNavigator().Select("/response");
    }
    }
    public static XPathNodeIterator GetMembersByGroupId(int id)
    {
    XmlDocument xd = new XmlDocument();

    try
    {
    DataSet ds = SqlHelper.ExecuteDataset(
    new SqlConnection(GlobalSettings.DbDSN),
    CommandType.Text,
    @"
    Select ccx.xml
    from cmsContentXml ccx
    inner join cmsMember cm
    on ccx.nodeId = cm.nodeId
    inner join cmsMember2MemberGroup cm2mg
    on cm.nodeId = cm2mg.Member
    Where cm2mg.MemberGroup = @group
    ", new SqlParameter("@group", id.ToString()));

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("");

    xd.LoadXml(sb.ToString());

    return xd.CreateNavigator().Select("/root");
    }
    catch (Exception ex)
    {
    xd.LoadXml("");
    return xd.CreateNavigator().Select("/response");
    }
    }
    public static XPathNodeIterator GetMembersAll()
    {
    XmlDocument xd = new XmlDocument();

    try
    {
    DataSet ds = SqlHelper.ExecuteDataset(
    new SqlConnection(GlobalSettings.DbDSN),
    CommandType.Text,
    @"
    Select ccx.xml from cmsContentXml ccx inner join cmsMember cm on ccx.nodeId = cm.nodeId
    ");

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("");

    xd.LoadXml(sb.ToString());

    return xd.CreateNavigator().Select("/root");
    }
    catch (Exception ex)
    {
    xd.LoadXml("");
    return xd.CreateNavigator().Select("/response");
    }
    }
    public static XPathNodeIterator GetMemberGroupsAll()
    {
    MemberGroup[] mga = MemberGroup.GetAll;
    XmlDocument d = new XmlDocument();
    XmlElement g = d.CreateElement("groups");

    foreach (MemberGroup mg in mga)
    {
    XmlElement e = d.CreateElement("group");
    XmlAttribute a = d.CreateAttribute("id");
    a.Value = mg.Id.ToString();
    e.Attributes.Append(a);
    e.InnerText = mg.Text;
    g.AppendChild(e);
    }
    d.AppendChild(g);
    return d.CreateNavigator().Select("/groups");
    }
    public static XPathNodeIterator GetMemberGroupsForMember(int memberId)
    {
    Member m = new Member(memberId);
    XmlDocument d = new XmlDocument();
    XmlElement g = d.CreateElement("groups");

    foreach (int i in m.Groups.Keys)
    {
    MemberGroup mg = new MemberGroup(i);
    XmlElement e = d.CreateElement("group");
    XmlAttribute a = d.CreateAttribute("id");
    a.Value = mg.Id.ToString();
    e.Attributes.Append(a);
    e.InnerText = mg.Text;
    g.AppendChild(e);
    }
    d.AppendChild(g);
    return d.CreateNavigator().Select("/groups");
    }
    #endregion

    }
    }

    [/code]

  • Bill 81 posts 83 karma points
    May 11, 2009 @ 15:02
    Bill
    0

    thanx! i was soooo in need of this!

  • Jonas 49 posts 21 karma points
    May 22, 2009 @ 11:55
    Jonas
    0

    Thanks Casey !

    Once again I was stuck when I tried to parse the xml-data, but I figured it out, here's how I got info of the groups:

    [code]

    [/code]

    (First compile the .cs to a class library and save the .dll to the bin-directory and add the extension in the xsltExtensions.config, more about that : http://umbraco.tv/documentation/books/multilingual-11-sites/5)-use-the-dictionary-with-an-xslt-extension)

Please Sign in or register to post replies

Write your reply to:

Draft