Managing roles and permissions

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The following sample code shows how you can create a new role and add it to the current site:

 

[C#]

 

using CMS.SiteProvider;

using CMS.CMSHelper;

 

...

 

        // Create empty role object

        RoleInfo ri = new RoleInfo();

 

        // Set properties

        ri.DisplayName = "Custom role";

        ri.RoleName = "CustomRole";

        ri.SiteID = CMSContext.CurrentSiteID;

 

        // Save role object to database

        RoleInfoProvider.SetRoleInfo(ri);

 

The following sample code shows how you can add a user to a role:

 

[C#]

 

using CMS.SiteProvider;

using CMS.CMSHelper;

 

...

 

        // Get role object

        RoleInfo ri = RoleInfoProvider.GetRoleInfo("CustomRole"CMSContext.CurrentSiteID);

 

        // Get user object

        UserInfo ui = UserInfoProvider.GetUserInfo("Username");

 

        // If both exist

        if ((ri != null) && (ui != null))

        {

            // Create new user role object

            UserRoleInfo uri = new UserRoleInfo();

                

            // Set properties

            uri.UserID = ui.UserID;

            uri.RoleID = ri.RoleID;

 

            // Save to database

            UserRoleInfoProvider.SetUserRoleInfo(uri);

        }

 

The following sample code shows how you can add a permission to a role:

 

[C#]

 

using CMS.SiteProvider;

using CMS.CMSHelper;

 

...

 

        // Get role object

        RoleInfo ri = RoleInfoProvider.GetRoleInfo("CustomRole",CMSContext.CurrentSiteID);

 

        // Get permission object

        PermissionNameInfo pni = PermissionNameInfoProvider.GetPermissionNameInfo("Read""CMS.Blog""");

 

        // If both exist

        if ((ri != null) && (pni != null))

        {

            // Create new role permission object

            RolePermissionInfo rpi = new RolePermissionInfo();

                

            // Set properties

            rpi.RoleID = ri.RoleID;

            rpi.PermissionID = pni.PermissionId;

 

            // Save to database

            RolePermissionInfoProvider.SetRolePermissionInfo(rpi);

        }

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?api_membership_managing_roles_and_permissions.htm