<html><head><link rel=stylesheet href=style.css></head><body><div class=SourcePanel style='font-size:12'><pre style='background-color:white'>
<font color= "blue">using</font> System;
<font color= "blue">using</font> System.Data;
<font color= "blue">using</font> System.Configuration;
<font color= "blue">using</font> ASPNET.StarterKit.TimeTracker.DataAccessLayer;
<font color= "blue"></font>
<font color= "blue">namespace</font> ASPNET.StarterKit.TimeTracker.BusinessLogicLayer
<font color= "blue"></font>{
<font color= "green"> //****************************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // TTUser Class</font>
<font color= "green"> //</font>
<font color= "green"> // The TTUser class represents a Time Tracker user, including their unique</font>
<font color= "green"> // userID and UserName. Custom role information retrieved from the database</font>
<font color= "green"> // is also stored in the TTUser class.</font>
<font color= "green"> //</font>
<font color= "green"> //****************************************************************************</font>
<font color= "blue"></font>
<font color= "blue"> public class</font> TTUser
<font color= "blue"> </font>{
<font color= "blue"> public const </font>string UserRoleNone = "0";
<font color= "blue"> public const </font>string UserRoleAdministrator = "1";
<font color= "blue"> public const </font>string UserRoleProjectManager = "2";
<font color= "blue"> public const </font>string UserRoleConsultant = "3";
<font color= "blue"> public const </font>string UserRoleAdminPMgr = UserRoleAdministrator + "," + UserRoleProjectManager;
<font color= "blue"> public const </font>string UserRolePMgrConsultant = UserRoleProjectManager + "," + UserRoleConsultant;
<font color= "blue"></font>
<font color= "blue"> private </font>string _displayName = string.Empty;
<font color= "blue"> private </font>string _firstName = string.Empty;
<font color= "blue"> private </font>string _lastName = string.Empty;
<font color= "blue"> private </font>string _password = String.Empty;
<font color= "blue"> private </font>string _role = UserRoleNone;
<font color= "blue"> private </font>string _roleName;
<font color= "blue"> private </font>int _userID;
<font color= "blue"> private </font>string _userName;
<font color= "blue"></font>
<font color= "blue"> public </font>TTUser()
<font color= "blue"> </font>{
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>TTUser(string UserName)
<font color= "blue"> </font>{
<font color= "blue"> </font>_userName = UserName;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>TTUser(int UserID, string UserName, string Name, string Role)
<font color= "blue"> </font>{
<font color= "blue"> </font>_userID = UserID;
<font color= "blue"> </font>_userName = UserName;
<font color= "blue"> </font>_displayName = Name;
<font color= "blue"> </font>_role = Role;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string DisplayName
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _displayName; }
<font color= "blue"> set</font> { _displayName = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string FirstName
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _firstName; }
<font color= "blue"> set</font> { _firstName = value; }
<font color= "blue"> </font>}
<font color= "blue"> public </font>string LastName
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _lastName; }
<font color= "blue"> set</font> { _lastName = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string Name
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _displayName; }
<font color= "blue"> set</font> { _displayName = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string Password
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _password; }
<font color= "blue"> set</font> { _password = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string Role
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _role; }
<font color= "blue"> set</font> { _role = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string RoleName
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _roleName; }
<font color= "blue"> set</font> { _roleName = value; }
<font color= "blue"> </font>}
<font color= "blue"> public </font>int UserID
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _userID; }
<font color= "blue"> set</font> { _userID = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>string UserName
<font color= "blue"> </font>{
<font color= "blue"> get</font> { return _userName; }
<font color= "blue"> set</font> { _userName = value; }
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // GetAllUsers Static Method</font>
<font color= "green"> // Retrieves a list of all users.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue"> public </font>static UsersCollection GetAllUsers(int userID)
<font color= "blue"> </font>{
<font color= "blue"> return</font> GetUsers(userID, TTUser.UserRoleAdministrator);
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // GetUsers Static Method</font>
<font color= "green"> // Retrieves a list of users based on the specified userID and role.</font>
<font color= "green"> // The list returned is restricted by role. For instance, users with</font>
<font color= "green"> // the role of Administrator can see all users, while users with the</font>
<font color= "green"> // role of Consultant can only see themselves.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> </font>
<font color= "blue"> public </font>static UsersCollection GetUsers(int userID, string role)
<font color= "blue"> </font>{
<font color= "blue"> </font>string firstName = string.Empty;
<font color= "blue"> </font>string lastName = string.Empty;
<font color= "blue"></font>
<font color= "blue"> </font>DataSet ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString],
<font color= "blue"> </font>"TT_ListUsers", userID, Convert.ToInt32(role));
<font color= "blue"> </font>UsersCollection users = new UsersCollection();
<font color= "blue"></font>
<font color= "green"> // Separate Data into a collection of Users.</font>
<font color= "blue"> </font>foreach(DataRow r in ds.Tables[0].Rows)
<font color= "blue"> </font>{
<font color= "blue"> </font>TTUser usr = new TTUser();
<font color= "blue"> </font>usr.UserName = r["UserName"].ToString();
<font color= "blue"> </font>usr.Role = r["RoleID"].ToString();
<font color= "blue"> </font>usr.RoleName = r["RoleName"].ToString();
<font color= "blue"> </font>usr.UserID = Convert.ToInt32(r["UserID"]);
<font color= "blue"> </font>usr.Name = GetDisplayName(usr.UserName, ref firstName, ref lastName);
<font color= "blue"> </font>usr.FirstName = firstName;
<font color= "blue"> </font>usr.LastName = lastName;
<font color= "blue"> </font>users.Add(usr);
<font color= "blue"> </font>}
<font color= "blue"> return</font> users;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // GetDisplayName static method</font>
<font color= "green"> // Gets the user's first and last name from the specified TTUser account source, which is</font>
<font color= "green"> // set in Web.confg.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue"> public </font>static string GetDisplayName(string userName, ref string firstName, ref string lastName)
<font color= "blue"> </font>{
<font color= "blue"> </font>string displayName = string.Empty;
<font color= "blue"> </font>string dbName = string.Empty;
<font color= "blue"></font>
<font color= "green"> // The DirectoryHelper class will attempt to get the user's first </font>
<font color= "green"> // and last name from the specified account source.</font>
<font color= "blue"> </font>DirectoryHelper.FindUser(userName, ref firstName, ref lastName);
<font color= "blue"></font>
<font color= "green"> // If the first and last name could not be retrieved, return the TTUserName.</font>
<font color= "blue"></font><font color= "blue"> if </font>(firstName.Length > 0 || lastName.Length > 0)<font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>displayName = firstName + " " + lastName;
<font color= "blue"> </font>}
<font color= "blue"></font><font color= "blue"> else</font><font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>dbName = GetDisplayNameFromDB(userName);
<font color= "blue"></font><font color= "blue"> if </font>(dbName != string.Empty)<font color= "blue"></font>
<font color= "blue"> </font>displayName = dbName;
<font color= "blue"></font><font color= "blue"> else</font><font color= "blue"></font>
<font color= "blue"> </font>displayName = userName;
<font color= "blue"> </font>}
<font color= "blue"> return</font> displayName;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> public </font>static string GetDisplayNameFromDB(string userName)
<font color= "blue"> </font>{
<font color= "blue"> </font>string displayName = string.Empty;
<font color= "blue"> </font>displayName = Convert.ToString(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString],
<font color= "blue"> </font>"TT_GetUserDisplayName", userName));
<font color= "blue"> return</font> displayName;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // ListManagers Static Method</font>
<font color= "green"> // Retrieves a list of users with the role of Project Manager.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> </font>
<font color= "blue"> public </font>static UsersCollection ListManagers()
<font color= "blue"> </font>{
<font color= "blue"> </font>string firstName = string.Empty;
<font color= "blue"> </font>string lastName = string.Empty;
<font color= "blue"> </font>
<font color= "blue"> </font>DataSet ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString],
<font color= "blue"> </font>CommandType.StoredProcedure, "TT_ListManagers");
<font color= "blue"> </font>UsersCollection managersArray = new UsersCollection();
<font color= "blue"></font>
<font color= "green"> // Separate Data into a list of collections.</font>
<font color= "blue"> </font>foreach(DataRow r in ds.Tables[0].Rows)
<font color= "blue"> </font>{
<font color= "blue"> </font>TTUser usr = new TTUser();
<font color= "blue"> </font>usr.UserName = r["UserName"].ToString();
<font color= "blue"> </font>usr.Role = r["RoleID"].ToString();
<font color= "blue"> </font>usr.UserID = Convert.ToInt32(r["UserID"]);
<font color= "blue"> </font>usr.Name = GetDisplayName(usr.UserName, ref firstName, ref lastName);
<font color= "blue"> </font>usr.FirstName = firstName;
<font color= "blue"> </font>usr.LastName = lastName;
<font color= "blue"> </font>managersArray.Add(usr);
<font color= "blue"> </font>}
<font color= "blue"> return</font> managersArray;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // Remove static method</font>
<font color= "green"> // Removes a user from database</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> </font>
<font color= "blue"> public </font>static void Remove (int userID)
<font color= "blue"> </font>{
<font color= "blue"> </font>SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString],
<font color= "blue"> </font>"TT_DeleteUser", userID);
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // Load method</font>
<font color= "green"> // Retrieve user information from the data access layer</font>
<font color= "green"> // returns True if user information is loaded successfully, false otherwise.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> public </font>bool Load ()
<font color= "blue"> </font>{
<font color= "green"> // Get the user's information from the database</font>
<font color= "blue"> </font>DataSet ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString],
<font color= "blue"> </font>"TT_GetUserByUserName", _userName);
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue"> if </font>(ds.Tables[0].Rows.Count < 1)<font color= "blue"></font>
<font color= "blue"> return</font> false;
<font color= "blue"></font>
<font color= "blue"> </font>DataRow dr = ds.Tables[0].Rows[0];
<font color= "blue"> </font>_userID = Convert.ToInt32(dr["UserID"]);
<font color= "blue"> </font>_userName = dr["UserName"].ToString();
<font color= "blue"> </font>_role = dr["RoleID"].ToString();
<font color= "blue"> </font>_password = dr["Password"] == DBNull.Value ? "":dr["Password"].ToString();
<font color= "blue"> </font>_displayName = GetDisplayName(_userName, ref _firstName, ref _lastName);
<font color= "blue"> </font>
<font color= "blue"> return</font> true;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // Save method</font>
<font color= "green"> // Add or update user information in the database depending on the TTUserID.</font>
<font color= "green"> // Returns True if saved successfully, false otherwise.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue"> public </font>bool Save ()
<font color= "blue"> </font>{
<font color= "blue"> </font>bool isUserFound = false;
<font color= "blue"> </font>bool isUserActiveManager = true;
<font color= "blue"> return</font> Save(false, ref isUserFound, ref isUserActiveManager);
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // Save method</font>
<font color= "green"> // Add or update user information in the database depending on the TTUserID.</font>
<font color= "green"> // Returns True if saved successfully, false otherwise.</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"></font>
<font color= "blue"> public </font>bool Save (bool checkUsername, ref bool isUserFound, ref bool isUserActiveManager)
<font color= "blue"> </font>{
<font color= "green"> // Determines whether object needs update or to be inserted.</font>
<font color= "blue"></font><font color= "blue"> if </font>(_userID == 0)<font color= "blue"></font>
<font color= "blue"> return</font> Insert(checkUsername, ref isUserFound);
<font color= "blue"></font><font color= "blue"> else if </font>(_userID > 0)<font color= "blue"></font>
<font color= "blue"> return</font> Update(ref isUserActiveManager);
<font color= "blue"></font><font color= "blue"> else</font><font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>_userID = 0;
<font color= "blue"> return</font> false;
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> private </font>bool Insert(bool checkUsername, ref bool isUserFound)
<font color= "blue"> </font>{
<font color= "blue"> </font>string firstName = string.Empty;
<font color= "blue"> </font>string lastName = string.Empty;
<font color= "blue"> </font>isUserFound = false;
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue"> if </font>(ConfigurationSettings.AppSettings[Web.Global.CfgKeyUserAcctSource] != "None")<font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "green"> // Check to see if the user is in the NT SAM or Active Directory before inserting them</font>
<font color= "green"> // into the Time Tracker database. If a first or last name is returned, the user exists and</font>
<font color= "green"> // can be inserted into the Time Tracker database.</font>
<font color= "blue"></font><font color= "blue"> if </font>(checkUsername)<font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>TTUser.GetDisplayName(_userName, ref firstName, ref lastName);
<font color= "blue"> </font>isUserFound = (firstName != string.Empty || lastName != string.Empty);
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"></font><font color= "blue"> else</font><font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>checkUsername = false;
<font color= "blue"> </font>isUserFound = true;
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue"> if </font>((checkUsername && isUserFound) || (!checkUsername))<font color= "blue"></font>
<font color= "blue"> </font>{
<font color= "blue"> </font>_userID = Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString], "TT_AddUser",
<font color= "blue"> </font>_userName, _password, _displayName, Convert.ToInt32(_role)));
<font color= "blue"> </font>isUserFound = true;
<font color= "blue"> </font>}
<font color= "blue"> return</font> (_userID > 0);
<font color= "blue"> </font>}
<font color= "blue"> </font>
<font color= "green"> //*********************************************************************</font>
<font color= "green"> //</font>
<font color= "green"> // UsersDB.Login() Method </font>
<font color= "green"> //</font>
<font color= "green"> // The Login method validates a email/password pair against credentials</font>
<font color= "green"> // stored in the users database. If the email/password pair is valid,</font>
<font color= "green"> // the method returns user's name.</font>
<font color= "green"> // </font>
<font color= "green"> // Other relevant sources:</font>
<font color= "green"> // + UserLogin Stored Procedure</font>
<font color= "green"> //</font>
<font color= "green"> //*********************************************************************</font>
<font color= "blue"> </font>
<font color= "blue"> public </font>string Login(string email, string password)
<font color= "blue"> </font>{
<font color= "blue"> </font>string userName = string.Empty;
<font color= "blue"> </font>userName = Convert.ToString(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString], "TT_UserLogin", email, password));
<font color= "blue"></font>
<font color= "blue"></font><font color= "blue"> if </font>(userName != "" || userName !=string.Empty)<font color= "blue"></font>
<font color= "blue"> return</font> userName;
<font color= "blue"></font><font color= "blue"> else</font><font color= "blue"></font>
<font color= "blue"> return</font> string.Empty;
<font color= "blue"></font>
<font color= "blue"> </font>}
<font color= "blue"></font>
<font color= "blue"> private </font>bool Update(ref bool isUserActiveManger)
<font color= "blue"> </font>{
<font color= "green"> // if new user role is a consultant, check if user is a active manager of one or more project. if so, no update is applied </font>
<font color= "blue"></font><font color= "blue"> if </font>(_role == UserRoleConsultant && <font color= "blue"></font>
<font color= "blue"> </font>(isUserActiveManger = (0 < Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString], "TT_GetManagerProjectCount", _userID)))))
<font color= "blue"> return</font> false;
<font color= "blue"> </font>
<font color= "blue"> return</font> (0 < Convert.ToInt32(SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings[Web.Global.CfgKeyConnString], "TT_UpdateUser",
<font color= "blue"> </font>_userID, _userName, _password, _displayName, Convert.ToInt32(_role))));
<font color= "blue"> </font>}
<font color= "blue"> </font>}
<font color= "blue"></font>}
</pre>