Managing Permissions

Setting permissions in ADO is even easier than it is in DAO. The Catalog object's Users and Groups collections manage permissions:

GroupOrUser.SetPermissions ObjectName, ObjectType, Action, Rights [, Inherit] [, ObjectTypeID]

The ObjectTypeID parameter is an optional variant value that specifies the GUID for a provider object type that isn't defined by the OLE DB specification. This parameter is only used if you set ObjectType to adPermObjProviderSpecific. You should refer to the provider documentation for the specific GUID to use. The remaining parameter values for the SetPermisions method are listed in Appendix J. But, for now, let's take a look at how it works.

Dim cat As New ADOX.Catalog Dim lngPermit As Long

'Create the Catalog

Set cat.ActiveConnection = CurrentProject.Connection For this test, we want to remember the original permissions for the Customers table.

'Get the original permissions, so we can 'restore them when we've finished lngPermit = cat.Users("Doris Crockford").GetPermissions( _ "Customers", adPermObjTable)

Debug.Print "The original permissions were: " & CStr(lngPerm)

OK, so now we know what permissions Doris had. Let's play with her permissions for a while.

'Revoke all permissions to the Customers table cat.Users("Doris Crockford").SetPermissions _

"Customers", adPermObjTable, adAccessRevoke, adRightFull

Debug.Print "Permissions revoked. They are now: " & _

CStr(cat.Users("Doris Crockford").GetPermissions( _ "Customers", adPermObjTable))

'Now grant the Admin user full rights on the Customers table cat.Users("Doris Crockford").SetPermissions _

"Customers", adPermObjTable, adAccessSet, adRightFull

Debug.Print "Full permissions granted. They are now: " & _ CStr(cat.Users("Doris Crockford").GetPermissions( _ "Customers", adPermObjTable))

OK, she's had enough. She wants to get back to work, so let's restore her permissions for the table.

'Finally, restore the original permissions cat.Users("Doris Crockford").SetPermissions _

"Customers", adPermObjTable, adAccessSet, lngPermit

Debug.Print "Permissions restored: " & _

CStr(cat.Users("Doris Crockford").GetPermissions( _ "Customers", adPermObjTable))

'Clean up

Set cat.ActiveConnection = Nothing Set cat = Nothing

Note: We can do exactly the same thing to an entire group, using exactly the same code, but replacing cat.Users with cat.Groups. Now isn't that cool?

0 0

Post a comment