Pergunta

Within the Kentico Administration area, there is a facility to group 'Roles' together using the 'Membership' Feature. I have built a 'Custom Role Provider' that allows me to access the Kentico database through the API, which is all working and correct.

The API allows me to access the roles of a user which is fine, but I ideally I would like to access the 'Memberships' instead and the 'Roles' that are associated to it through the API. When I am authenticating users access to a page/link etc I only have to call the group name and then search through the mappings to find the associated 'Role', then grant or deny access.

Is there any way in the API to do this, I would I need to create a custom SQL string and execute it against the database to extract the data that I need?

Foi útil?

Solução

There is no out-of-box functionality to do that. You will have to utilize offered info objects and their providers. I have created short sample code to help you understand the concept.

    // Get user by name
    UserInfo user = UserInfoProvider.GetUserInfo("testUser");

    // Get user-role bindings by user's identifier
    InfoDataSet<UserRoleInfo> userRoles = UserRoleInfoProvider.GetUserRoles("UserID=" + user.UserID, null, -1, null);
    Response.Write("User: " + user.UserName + "<br /><br />");

    // Enumerate through user-role binding
    foreach (UserRoleInfo userRoleInfo in userRoles)
    {
        // Get role information based on role identifier
        RoleInfo role = RoleInfoProvider.GetRoleInfo(userRoleInfo.RoleID);
        Response.Write("User role: " + role.DisplayName + "<br />");

        // Get role-membership bindings using where condition
        InfoDataSet<MembershipRoleInfo> membershipRoles = MembershipRoleInfoProvider.GetMembershipRoles("RoleID=" + role.RoleID, null, -1, null);

        // Enumerate through role-membership bindings
        foreach (MembershipRoleInfo membershipRoleInfo in membershipRoles)
        {
            // Get membership info using identifier
            MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo(membershipRoleInfo.MembershipID);
            Response.Write("Role membership: " + membership.MembershipDisplayName + "<br />");
        }

        Response.Write("<br />");
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top