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
UserInfoProvider.SetUserInfo(newUser);


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

Deleting a user




// Gets the user
UserInfo deleteUser = UserInfoProvider.GetUserInfo("NewUser");

if (deleteUser != null)
{
    // Deletes the user
    UserInfoProvider.DeleteUser(deleteUser);
}


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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
}


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

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


> Back to list of examples

Kicking an online user




// Gets the user 
UserInfo kickedUser = UserInfoProvider.GetUserInfo("NewUser");

if (kickedUser != null)
{
    // Kicks the user
    SessionManager.KickUser(kickedUser.UserID);
}


> Back to list of examples