Users
List of examples:
Users
Creating a new user
// Creates a new user object
UserInfo newUser = new UserInfo();
// Sets the user properties
newUser.FullName = "New user";
newUser.UserName = "NewUser";
newUser.Email = "new.user@domain.com";
newUser.PreferredCultureCode = "en-us";
// Sets the user's privilege level to 'Editor'
newUser.SiteIndependentPrivilegeLevel = UserPrivilegeLevelEnum.Editor;
// Saves the user to the database
UserInfo.Provider.Set(newUser);
Updating an existing user
// Gets the user
UserInfo updateUser = UserInfo.Provider.Get("NewUser");
if (updateUser != null)
{
// Updates the user's properties
updateUser.FullName = updateUser.FullName.ToLowerCSafe();
// Saves the changes to the database
UserInfo.Provider.Set(updateUser);
}
Updating multiple users
// Gets all users whose username starts with 'NewUser'
var users = UserInfo.Provider.Get().WhereStartsWith("UserName", "NewUser");
// Loops through individual users
foreach (UserInfo modifyUser in users)
{
// Updates the user properties
modifyUser.FullName = modifyUser.FullName.ToUpper();
// Saves the changes to the database
UserInfo.Provider.Set(modifyUser);
}
Working with custom user fields
// Gets the user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Attempts to retrieve a value from a custom text field named 'CustomField'
string value = user.GetValue("CustomField", "");
// Sets a modified value for the custom field
user.SetValue("CustomField", value + "_customSuffix");
// Saves the changes to the database
UserInfo.Provider.Set(user);
}
Deleting a user
// Gets the user
UserInfo deleteUser = UserInfo.Provider.Get("NewUser");
if (deleteUser != null)
{
// Deletes the user
UserInfo.Provider.Delete(deleteUser);
}
User-site relationships
Getting all sites to which a user is assigned
// Gets the user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Gets the sites to which the user is assigned
var userSiteIDs = UserSiteInfo.Provider.Get().Column("SiteID").WhereEquals("UserID", user.UserID);
var sites = SiteInfo.Provider.Get().WhereIn("SiteID", userSiteIDs);
// Loops through the sites
foreach (SiteInfo site in sites)
{
// Process the site
}
}
Assigning a user to a site
// Gets the user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Adds the user to the site
UserInfoProvider.AddUserToSite(user.UserName, SiteContext.CurrentSiteName);
}
Removing a user from a site
// Gets the user
UserInfo removeUser = UserInfo.Provider.Get("NewUser");
if (removeUser != null)
{
// Removes the user from the site
UserInfoProvider.RemoveUserFromSite(removeUser.UserName, SiteContext.CurrentSiteName);
}
User authentication
Authenticating user credentials
UserInfo user = null;
// Attempts to authenticate user credentials (username and password) against the current site
user = AuthenticationHelper.AuthenticateUser("username", "password", SiteContext.CurrentSiteName);
if (user != null)
{
// Authentication was successful
}
Signing in a user via forms authentication
UserInfo user = null;
// Attempts to authenticate user credentials (username and password) against the current site
user = AuthenticationHelper.AuthenticateUser("username", "password", SiteContext.CurrentSiteName);
if (user != null)
{
// Sets the forms authentication cookie
System.Web.Security.FormsAuthentication.SetAuthCookie(user.UserName, false);
// Redirects (or refreshes) the page to apply the authentication cookie
URLHelper.Redirect(RequestContext.CurrentURL);
}
User authorization
Checking the user privilege level
// Gets the user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Checks whether the user has the Editor privilege level or higher
if (user.CheckPrivilegeLevel(UserPrivilegeLevelEnum.Editor, SiteContext.CurrentSiteName))
{
// Perform an action (the user has the required privilege level)
}
}
Checking permissions for a module
// Gets the user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Checks whether the user has the Read permission for the Content module
if (UserInfoProvider.IsAuthorizedPerResource("CMS.Content", "Read", SiteContext.CurrentSiteName, user))
{
// Perform an action (the user has the required module permission)
}
}
Checking permissions for a page type or custom table
// Gets the user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Checks whether the user has the Read permission for the custom page type
if (UserInfoProvider.IsAuthorizedPerClass("Custom.Article", "Read", SiteContext.CurrentSiteName, user))
{
// Perform an action (the user is authorized to read Custom.Article page types)
}
}
Checking permissions for specific pages (ACLs)
// Gets a page
TreeNode page = new DocumentQuery<TreeNode>()
.Path("/Example", PathTypeEnum.Single)
.OnSite("MySite")
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
// If the page does not exist return
if (page == null)
{
return;
}
// Gets a user
UserInfo user = UserInfo.Provider.Get("NewUser");
if (user != null)
{
// Checks whether the user is authorized to read the page
if (user.IsAuthorizedPerTreeNode(page, NodePermissionsEnum.Read) == AuthorizationResultEnum.Allowed)
{
// Perform an action (the user is allowed to read the page)
}
}
User macro signature identities
Creating a macro signature identity
// Creates a new identity object
MacroIdentityInfo newMacroIdentity = new MacroIdentityInfo();
// Sets the identity name
newMacroIdentity.MacroIdentityName = "CustomIdentity";
// Gets a user and assigns them as the identity's effective user
UserInfo effectiveUser = UserInfo.Provider.Get("administrator");
newMacroIdentity.MacroIdentityEffectiveUserID = effectiveUser.UserID;
// Saves the identity to the database
MacroIdentityInfo.Provider.Set(newMacroIdentity);
Assigning a signature identity to a user
// Gets a user
UserInfo user = UserInfo.Provider.Get("NewUser");
// Gets a macro signature identity
MacroIdentityInfo macroIdentity = MacroIdentityInfo.Provider.Get("CustomIdentity");
// Assigns the macro signature identity to the user
UserMacroIdentityHelper.SetMacroIdentity(user, macroIdentity.MacroIdentityID);