Search code examples
c#apikentico

Kentico UserInfoProvider.IsAuthorizedPerClass not working as expected


I'm attempting to check if a given user has access to a specific Custom Table. Based on the example listed on the kentico documentation to check permissions for a custom table, I have setup a similar call, using my custom table class name and userinfo, but the call to "UserInfoProvider.IsAuthorizedPerClass" always return false:

private bool CheckCustomTableReadPermission(UserInfo user = null)
{
    // Gets the user object
    //UserInfo user = UserInfoProvider.GetUserInfo("CMSEditor");
    //UserInfo user = UserInfoProvider.GetUserInfo("someothervalidusername");
    //UserInfo user = CurrentUser;

    //normally outside of this function
    UserInfo CurrentUser = MembershipContext.AuthenticatedUser;
    string CustomTableClassName = "Namespc.TblName";

    if (user == null)
    {
        user = CurrentUser;
    }

    if (user != null)
    {
        // Checks whether the user has the Read permission for the CMS.MenuItem page type
        if (UserInfoProvider.IsAuthorizedPerClass(CustomTableClassName, "Read", SiteContext.CurrentSiteName, user))
        {
            // Perform an action according to the result
            return true;
        }
    }

    return false;
}

Can anyone also mention what the valid permission name strings are, other than "Read"? (e.g.: "Modify"? "Delete"? "Insert"?)

Does UserInfoProvider.IsAuthorizedPerClass resolve all memberships of the given user, or does it only check if the user is explicitly added to the Custom Table?

Any suggestions? We're using Kentico v8.2.25

Thanks!

Victor


Solution

  • What about doing it the same way as it's done in

    CMS\CMSModules\CustomTables\Tools\CustomTable_Data_EditItem.aspx.cs

    which is:

    DataClassInfo dci = DataClassInfoProvider.GetDataClassInfo(customTableId);
    dci.CheckPermissions(PermissionsEnum.Read, SiteContext.CurrentSiteName, MembershipContext.AuthenticatedUser)
    

    And the possible permissions are located in CMS.DataEngine.PermissionsEnum. (Read, Modify, Create, Delete, Destroy)