CLR Stored Procedures in Sql Server Analysis Services (SSAS)

I've been doing some R&D work on a new integrated security model for our database/analysis services lately, with one of the main requirements being a system where security changes made to the Sql server database will automatically propogate through to the SSAS environment.

To illustrate this point, I employ the use of a departmental structure. For instance, if in the database, a user gets shifted to a different department, he/she should have those changes automatically replicated when they log into the SSAS server and view cube data. Ie: they should be restricted to view departments and their children that they belong to.

The thing about SSAS is that it sits alongside Sql Server, rather than a subset; and since it's seen as a seperate entity that can be made up of a number of databases, subsequently don't inherit any of their rights. Roles can be set up within SSAS, which we can use to mirror the security structure of the Sql Server. As long as windows authentication is used in Sql Server, a flexible model can then be created to manage both servers.

Coming back to the departmental structure, how can a role be created that restricts the user to viewing his departments and it's children? This can be achieved through by creating a Department dimension within SSAS, and creating a new role that returns an allowable member set via a CLR Stored Procedure that connects to the original SQL Data source.

Creating a CLR Stored proc for SSAS is pretty simple. All you need to do is create a new class library, and within a new class do something like:

public static Set GetAllowedDepartments()

{

    SetBuilder sBuilder = new SetBuilder();

 

    using (SqlConnection conn = new SqlConnection(@"data source=<data source>;initial catalog=<orig database>;integrated security=sspi;"))

    {

        conn.Open();

        SqlCommand comm = new SqlCommand("select departmentId from vwDepartment", conn);

        SqlDataReader reader = comm.ExecuteReader();

 

        TupleBuilder tBuilder = new TupleBuilder();

 

        while (reader.Read())

        {

            int departmentId = reader.GetInt32(0);

 

            Member m = new Expression(

                string.Format(

                    "[Department].[Hierarchy].&[{0}]",

                    departmentId)

                ).CalculateMdxObject(null).ToMember();

 

            tBuilder.Add(m);

 

        }

 

        sBuilder.Add(tBuilder.ToTuple());

 

        reader.Close();

        conn.Close();

    }

 

    return sBuilder.ToSet();

 

}

Two things to note:

  1. you MUST connect to the sql server using SSPI, as when it connects to the database, you want it connecting as the current user who's on SSAS
  2. the vwDepartment view filters all rows based on the current logged in user, and hence only returns departments based on those that the user has access to

Compile that into a dll, and on the SSAS side of things, go to the assemblies folder and:

  1. New Assembly
  2. File name -> browse to the dll
  3. Include debug info if you want to attache the debugger later on
  4. Permissions -> external access (as it accesses an external source)
  5. Impersonation -> use credentials of the current user

You should now have access to the stored proc within any of your MDX scripts. What is needed is to restrict department rows to the a certain user role. Open up the role (or create a new one) for the one you want to restrict and:

  1. Dimension Data
  2. Dimension -> Department
  3. Attribute -> Hierarchy
  4. Allowed Member Set -> <Your Assembly Name>.GetAllowedDepartments()

Save that and try browsing the cube as that role. If you drop the department dimension in, you should see that only the departments the user has access to are visible.

This model also relys on the fact that members of the Department dimension rely on a single ID attribute, named with the department name; as well as kerberos authentication being properly set up.

I'm hoping that this sort of model will deliver the flexibility and centralization needed to cut out the maintenance and effort needed by both developers and administrators of the system. So far, it's looking pretty good.

posted @ Thursday, December 20, 2007 1:12 PM

Print

Comments on this entry:

# re: CLR Stored Procedures in Sql Server Analysis Services (SSAS)

Left by Ed at 4/18/2009 1:49 AM
Gravatar
This is interesting, thanks for posting. I'm thinking about using a CLR on the SQL relation db server that can run DMX queries on the SSAS server. Do you have a suggestion for that? Can I use temp tables in SSAS to hold an array of params I pass to the CLR?

Thank you,

Ed

# re: CLR Stored Procedures in Sql Server Analysis Services (SSAS)

Left by Roy at 4/19/2009 2:24 PM
Gravatar
Thanks for providing the solution for Security model. We are having an issue using this model. We have a large amount of data (around 700,000) returns from the SQL and we need to build the tuple for MDX. Could you please suggest how we can proceed with large amount of data with this security model? I appreciate your quick reply.

# re: CLR Stored Procedures in Sql Server Analysis Services (SSAS)

Left by Andrew at 4/19/2009 2:44 PM
Gravatar
Hi Roy,

Sounds like you might be going about it in the wrong way. You don't want to build a tuple of 700,000 members - it'll bring SSAS to its knees.

Instead of thinking row by row what data is accessible, which is what you're doing by the sound of it, work out the rules or the handful of parent nodes/grouping you can apply so that you're only dealing with a small subset of upper-level nodes which will let the rights inherit down.

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678