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); } |