You probably know by now that a specific permission granted or denied will potentially imply many other permissions to also be granted or denied. During the post about how to Identify All Possible Permissions using sys.fn_builtin_permission two days ago, I showed you that that very catalog function does also return information about other permissions that would imply the current one. However, that is only one step along the permissions hierarchy ladder. So, how can we get the complete list of permissions implied by a chosen one?
This kind of question calls for a recursive approach. There are many ways to implement recursion in SQL Server. Our goal here is to get the complete list of implied permissions in a result set, so a recursive CTE seems to be the best approach:
The parent_child part of the CTE converts the two possible parents of a given permission into two rows. The parent permission is mentioned first in the class_desc and permission_name columns. The two columns starting with "implied" contain the same information for the child or implied permission. I also added a relationship column that indicates if the implied permission is a step down the securable hierarchy (marked as "C" for class) or the privilege hierarchy (marked as "P" for privilege).
The next step in the CTE, any_achestor, is where the recursion happens. It joins the result of parent_chiled to itself to travers the hierarchies. It also counts the steps necessary for this and collects the relationship (different Privilege or different Class) into a path.
The result of the above query looks like this:
On SQL Server 2012 it returns 1342 rows. If the two "implied" columns are NULL for a given permission, no further implied permissions exists. Also, note that this query does not take the principal hierarchy into account at all. It only looks at securables and privileges.
To get from the list of the two directly covering permissions, returned by sys.fn_builtin_permissions, to a complete list of implied permissions, including those permissions that are implied over several hops, a recursive query against that catalog function can be used. This post demonstrated one possible implementation of such a query, using the recursive CTE functionality.