Users
List of examples:
- Users
- User-site relationships
- User authentication
- User authorization
- Online users
- User macro signature identities
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
UserInfoProvider.SetUserInfo(newUser);
Updating an existing user
// Gets the user
UserInfo updateUser = UserInfoProvider.GetUserInfo("NewUser");
if (updateUser != null)
{
// Updates the user's properties
updateUser.FullName = updateUser.FullName.ToLowerCSafe();
// Saves the changes to the database
UserInfoProvider.SetUserInfo(updateUser);
}
Updating multiple users
// Gets all users whose username starts with 'NewUser'
var users = UserInfoProvider.GetUsers().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
UserInfoProvider.SetUserInfo(modifyUser);
}
Working with custom user fields
// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("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
UserInfoProvider.SetUserInfo(user);
}
Deleting a user
// Gets the user
UserInfo deleteUser = UserInfoProvider.GetUserInfo("NewUser");
if (deleteUser != null)
{
// Deletes the user
UserInfoProvider.DeleteUser(deleteUser);
}
User-site relationships
Getting all sites to which a user is assigned
// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("NewUser");
if (user != null)
{
// Gets the sites to which the user is assigned
var userSiteIDs = UserSiteInfoProvider.GetUserSites().Column("SiteID").WhereEquals("UserID", user.UserID);
var sites = SiteInfoProvider.GetSites().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 = UserInfoProvider.GetUserInfo("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 = UserInfoProvider.GetUserInfo("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 = UserInfoProvider.GetUserInfo("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 = UserInfoProvider.GetUserInfo("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 = UserInfoProvider.GetUserInfo("NewUser");
if (user != null)
{
// Checks whether the user has the Read permission for the CMS.MenuItem page type
if (UserInfoProvider.IsAuthorizedPerClass(SystemDocumentTypes.MenuItem, "Read", SiteContext.CurrentSiteName, user))
{
// Perform an action (the user is authorized to read CMS.MenuItem page types)
}
}
Checking permissions for specific pages (ACLs)
// Creates a TreeProvider instance
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// Gets the Example page
TreeNode page = tree.SelectNodes()
.Path("/Example")
.OnCurrentSite()
.Culture("en-us")
.FirstObject;
if (page != null)
{
// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("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)
}
}
}
Online users
Getting and updating online users
string where = "";
int topN = 10;
string orderBy = "";
string location = "";
string siteName = SiteContext.CurrentSiteName;
bool includeHidden = true;
bool includeKicked = false;
// Gets DataSet of online users
DataSet users = SessionManager.GetOnlineUsers(where, orderBy, topN, location, siteName, includeHidden, includeKicked);
if (!DataHelper.DataSourceIsEmpty(users))
{
// Loops through the online user data
foreach (DataRow userDr in users.Tables[0].Rows)
{
// Creates a user from the DataRow
UserInfo modifyUser = new UserInfo(userDr);
// Updates the user's properties
modifyUser.FullName = modifyUser.FullName.ToUpper();
// Saves the changes to the database
UserInfoProvider.SetUserInfo(modifyUser);
}
}
Checking if a user is online
bool includeHidden = true;
// Gets user and site objects
UserInfo user = UserInfoProvider.GetUserInfo("NewUser");
SiteInfo site = SiteInfoProvider.GetSiteInfo(SiteContext.CurrentSiteName);
if ((user != null) && (site != null))
{
// Checks if the user is online
bool userIsOnline = SessionManager.IsUserOnline(site.SiteName, user.UserID, includeHidden);
}
Kicking an online user
// Gets the user
UserInfo kickedUser = UserInfoProvider.GetUserInfo("NewUser");
if (kickedUser != null)
{
// Kicks the user
SessionManager.KickUser(kickedUser.UserID);
}
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 = UserInfoProvider.GetUserInfo("administrator");
newMacroIdentity.MacroIdentityEffectiveUserID = effectiveUser.UserID;
// Saves the identity to the database
MacroIdentityInfoProvider.SetMacroIdentityInfo(newMacroIdentity);
Assigning a signature identity to a user
// Gets a user
UserInfo user = UserInfoProvider.GetUserInfo("NewUser");
// Gets a macro signature identity
MacroIdentityInfo macroIdentity = MacroIdentityInfoProvider.GetMacroIdentityInfo("CustomIdentity");
// Assigns the macro signature identity to the user
UserMacroIdentityHelper.SetMacroIdentity(user, macroIdentity.MacroIdentityID);