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

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples

Deleting a user



// Gets the user
UserInfo deleteUser = UserInfo.Provider.Get("NewUser");

if (deleteUser != null)
{
    // Deletes the user
    UserInfo.Provider.Delete(deleteUser);
}

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples

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

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

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples

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

> Back to list of examples