📄 users.cs
字号:
using System;
using System.Text.RegularExpressions;
using AspNetForums.Components;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace AspNetForums {
// *********************************************************************
// Users
//
/// <summary>
/// This class encapsulates all data operations for managing forum users.
/// </summary>
// ***********************************************************************/
public class Users {
// *********************************************************************
// GetUserInfo
//
/// <summary>
/// Return information about a particular user.
/// </summary>
/// <param name="username">The user whose information you are interested in.</param>
/// <param name="updateIsOnline">Updates user's online datetime stamp.</param>
/// <returns>Instance of User with details about a given forum user.</returns>
/// <remarks>
/// If the specified user is not found, a UserNotFoundException exception is thrown. Feel
/// free to call this multiple times during the request as the value is stored in Context once
/// read from the data source.
/// </remarks>
// ***********************************************************************/
public static User GetUserInfo(String username, bool updateIsOnline) {
string userKey = "UserInfo-" + username;
// Create Instance of the IWebForumsDataProviderBase
IWebForumsDataProviderBase dp = DataProvider.Instance();
// Let's not go to the database each time we need the user's info
if (HttpContext.Current.Items[userKey] == null) {
User user = dp.GetUserInfo(username, updateIsOnline);
// Hang on to the data for this request only
HttpContext.Current.Items[userKey] = user;
return user;
}
return (User) HttpContext.Current.Items[userKey];
}
// *********************************************************************
// ChangePasswordForLoggedOnUser
//
/// <summary>
/// Changes the password for the currently logged on user.
/// </summary>
/// <param name="password">User's current password.</param>
/// <param name="newPassword">User's new password.</param>
/// <returns>Indicates whether or not the password change succeeded</returns>
// ***********************************************************************/
public static bool ChangePasswordForLoggedOnUser(string password, string newPassword) {
User user;
// Create Instance of the IWebForumsDataProviderBase
IWebForumsDataProviderBase dp = DataProvider.Instance();
// Get the current user
user = Users.GetUserInfo(HttpContext.Current.User.Identity.Name, false);
// Check to ensure the passwords match
if (password != user.Password)
return false;
else
dp.ChangePasswordForLoggedOnUser(user.Username, newPassword);
user.Password = newPassword;
// Email the user their password
Emails.SendEmail(user.Username, EmailTypeEnum.ChangedPassword);
return true;
}
// *********************************************************************
// WhoIsOnline
//
/// <summary>
/// Returns a user collection of all the user's online. Lookup is only
/// performed every 30 seconds.
/// </summary>
/// <param name="pastMinutes">How many minutes in time we should go back to return users.</param>
/// <returns>A collection of user.</returns>
///
// ********************************************************************/
public static UserCollection WhoIsOnline(int pastMinutes) {
UserCollection users;
// Read from the cache if available
if (HttpContext.Current.Cache["WhoIsOnline"] == null) {
// Create Instance of the IWebForumsDataProviderBase
IWebForumsDataProviderBase dp = DataProvider.Instance();
// Get the users
users = dp.WhoIsOnline(pastMinutes);
// Add to the Cache
HttpContext.Current.Cache.Insert("WhoIsOnline", users, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero);
}
return (UserCollection) HttpContext.Current.Cache["WhoIsOnline"];
}
// *********************************************************************
// GetUsernameByEmail
//
/// <summary>
/// Returns a username given a user's email address.
/// </summary>
/// <param name="emailAddress">Email address to look up username by.</param>
/// <returns>Username</returns>
///
// ********************************************************************/
public static string GetUsernameByEmail(string emailAddress) {
// Create Instance of the IWebForumsDataProviderBase
IWebForumsDataProviderBase dp = DataProvider.Instance();
return dp.GetUsernameByEmail(emailAddress);
}
// *********************************************************************
// FindUsersByName
//
/// <summary>
/// Returns a user collection of users that match the string provided
/// </summary>
/// <param name="emailAddress">String to match on.</param>
/// <returns>Username</returns>
///
// ********************************************************************/
public static UserCollection FindUsersByName(int pageIndex, int pageSize, string usernameToMatch) {
// Create Instance of the IWebForumsDataProviderBase
IWebForumsDataProviderBase dp = DataProvider.Instance();
return dp.FindUsersByName(pageIndex, pageSize, usernameToMatch);
}
// *********************************************************************
// AdjustForTimezone
//
/// <summary>
/// Adjusts a date/time for a user's particular timezone offset.
/// </summary>
/// <param name="dtAdjust">The time to adjust.</param>
/// <param name="user">The user viewing the time.</param>
/// <returns>A datetime adjusted for the user's timezone offset.</returns>
///
// ********************************************************************/
public static DateTime AdjustForTimezone(DateTime dtAdjust, User user) {
return dtAdjust.AddHours(user.Timezone - Globals.DBTimezone);
}
// *********************************************************************
// AdjustForTimezone
//
/// <summary>
/// Adjusts a date/time for a specified timezone offset.
/// </summary>
/// <param name="dtAdjust">The time to adjust.</param>
/// <param name="TimezoneOffset">The timezone offset to adjust the date/time to (0 = GMT)</param>
/// <returns>A string containing the date and time, adjusted for the specified timezone offset</returns>
///
// ********************************************************************/
public static String AdjustForTimezone(DateTime dtAdjust, int TimezoneOffset) {
return dtAdjust.AddHours(TimezoneOffset - Globals.DBTimezone).ToString();
}
// *********************************************************************
// AdjustForTimezone
//
/// <summary>
/// Adjusts a date/time for a user's particular timezone offset applying a particular formatting.
/// </summary>
/// <param name="dtAdjust">The time to adjust.</param>
/// <param name="user">The user viewing the time.</param>
/// <param name="format">A string representing the desired date/time format.</param>
/// <returns>A string containing the date and time, adjusted for the user's timezone offset.</returns>
///
// ********************************************************************/
public static String AdjustForTimezone(DateTime dtAdjust, User user, String format) {
return dtAdjust.AddHours(user.Timezone - Globals.DBTimezone).ToString(format);
}
// *********************************************************************
// AdjustForTimezone
//
/// <summary>
/// Adjusts a date/time for a specified timezone offset applying a particular formatting.
/// </summary>
/// <param name="dtAdjust">The time to adjust.</param>
/// <param name="TimezoneOffset">The timezone offset to adjust the date/time to (0 = GMT)</param>
/// <param name="format">A string representing the desired date/time format.</param>
/// <returns>A string containing the date and time, adjusted for the specified timezone offset</returns>
///
// ********************************************************************/
public static String AdjustForTimezone(DateTime dtAdjust, int TimezoneOffset, String format) {
return dtAdjust.AddHours(TimezoneOffset - Globals.DBTimezone).ToString(format);
}
// *********************************************************************
// GetAllUsers
//
/// <summary>
/// Returns all the users currently in the system.
/// </summary>
/// <param name="pageIndex">Page position in which to return user's for, e.g. position of result set</param>
/// <param name="pageSize">Size of a given page, e.g. size of result set.</param>
/// <param name="sortBy">How the returned user's are to be sorted.</param>
/// <param name="sortOrder">Direction in which to sort</param>
/// <returns>A collection of user.</returns>
///
// ********************************************************************/
public static UserCollection GetAllUsers(int pageIndex, int pageSize, SortUsersBy sortBy, int sortOrder, string usernameBeginsWith) {
// Create Instance of the IWebForumsDataProviderBase
IWebForumsDataProviderBase dp = DataProvider.Instance();
return dp.GetAllUsers(pageIndex, pageSize, sortBy, sortOrder, usernameBeginsWith);
}
// *********************************************************************
// TrackAnonymousUsers
//
/// <summary>
/// Used to keep track of the number of anonymous users on the system
/// </summary>
/// <returns>A collection of user.</returns>
///
// ********************************************************************/
public static void TrackAnonymousUsers() {
string userId;
HttpCookie cookie;
string cookieName = "AspNetForumsAnonymousUser";
// Check if the Tracking cookie exists
cookie = HttpContext.Current.Request.Cookies[cookieName];
// Anonymous users are tracking in 15 minute intervals
if (null == cookie) {
// Create Instance of the IWebForumsDataProviderBase
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -