Imports system
Imports system.data
Imports system.configuration
Public Class TTUser
'********************************************************** ***********************************
'
'TTUSER CLASS
'
'The Ttuser Class Repensets a Time Tracker User, Including Their Unique
'userid and username. Custom Role Information Retrieved from The Database
'Is Also Stored In The Ttuser Class.
'
'********************************************************** ***********************************
Public Const UserRoleNone As String = "0" Public Const UserRoleAdministrator As String = "1" Public Const UserRoleProjectManager As String = "2" Public Const UserRoleConsultant As String = "3" Public Const UserRoleAdminPMgr As String = UserRoleAdministrator "," UserRoleProjectManager Public Const UserRolepmgrconsultant As String = UserRoleProjectManager "," Userroleconsultant
Private _displayName As String = String.Empty Private _firstName As String = String.Empty Private _lastName As String = String.Empty Private _password As String = String.Empty Private _role As String = UserRoleNone Private _roleName As String Private _userID As Integer Private _userName As String
Public Sub New () End Sub 'NEW
Public Sub New (Byval UserName As String) _USERNAME = Username End Sub 'New
Public Sub New (Byval Userid As Integer, Byval Name As String, BYVAL NAME AS STRING, BYVAL ROLE AS STRING "_USERID = UserId _Username = username _displayName = name _role = role end subth
Public Property DisplayName () As String Get Return _displayName End Get Set (ByVal Value As String) _displayName = Value End Set End PropertyPublic Property FirstName () As String Get Return _firstName End Get Set (ByVal Value As String) _firstName = Value End Set End Property
Public property lastname () as string get return _lastname End get set (byval value as string) _lastname = value end vendy
Public property name () as string get return _displayname end set (byval value as string) _displayName = value end vend @ dPlayName = Value End set
Public property password () AS String Get Return _Password End Get Set (Byval Value As String) _password = Value End End Property
Public property role () as string get return _role End get set (Byval value as string) _role = value end vend =
Public property rolename () AS STRING GET RETURN _ROLENAME END GET SET (BYVAL VALUE As String) _roleName = Value End End Property
Public property userid () AS integer get return _Userid End get set (byval value as integer) _USERID = VALUE END SET End Property
Public property username () AS String get return _username end set (byval value as string) _USERNAME = Value End End Property
'********************************************************** ******************* 'GetAllUsers Static Method' RETRIEVES A LIST OF All Uses. '' *************** *********************************************************** **** Public Shared Function GetAllUsers (Byval Userid AS Integer) AS UserScollection Return GetUsers (UserId, TTUSERROLLEADMINISTRATOR) End Function 'getAllUsers
'********************************************************** ******************* '' GetUsers Static Method 'Retrieves a list of users based on the specified userid and role.' The list returned is restrict by role. For instance Users with 'The Role of Administrator Can See All Users, While Users with the' Role of Consultant Can Only See Temselves' ******************************** ******************************************************
Public Shared Function GetUsers (Byval Role As String) AS UserScollection Dim Firstname As String = String.empty Dim Lastname As String = String.empty
DIM DS AS DATASET = SQLHELPER.EXECUTEDASet (Global.cfgKeyConnstring), "TT_LISTUSERS", UserId, Convert.Toint32 (Role) DIM Users As New UserScollection
'Separate Data INTO A Collection of Users. DIM R AS DATAROW for Each R in ds.tables (0) .ROWS DIM USR AS NEW TTUSER USR.USERNAME = R ("UserName"). Tostring () usr.role = r "Roleid"). Tostring () usr.rolename = r ("RoleName"). Tostring () usr.userid = convert.Toint32 (R ("UserID")) USR.Name = getDisplayName (usr.username, firstname, Lastname Usr.firstname = firstname usr.lastname = lastname users.add (usr) Next r return users end function 'getUsers' ********************************* ****************************************************** 'GetDisplayName Static Method 'Gets the user's first and last name from the specified ttuser account source, which is' set in Web.confg. '' *********************** ***************************************************
Public Shared Function GetDisplayName (ByVal userName As String, ByRef firstName As String, ByRef lastName As String) As String Dim displayName As String = String.Empty Dim dbName As String = String.Empty
'The DirectoryHelper Class Will Attempt To Get The User's First' and last name from the specified account source. DirectoryHelper.Finduser (username, firstname, lastname)
'If the first and last name could not be retrieved, return the TTUserName. If firstName.Length> 0 Or lastName.Length> 0 Then displayName = firstName "" lastName Else dbName = GetDisplayNameFromDB (userName) If Not dbName Is String. Empty Then displayName = dbName Else displayName = userName End If End If Return displayName End Function 'GetDisplayNamePublic Shared Function GetDisplayNameFromDB (ByVal userName As String) As String Dim displayName As String = String.Empty displayName = CStr (SqlHelper.ExecuteScalar (ConfigurationSettings.AppSettings ( Global.cfgKeyConnstring, "tt_getuserdisplayname", username) Return DisplayName End Function
'********************************************************** ******************** The RETRIEVES A LIST OF Users with the role of project manager. '*************** *********************************************************** **********
Public Shared Function ListManagers () AS UserScollection Dim Firstname As String = String.empty Dim Lastname As String = String.empty
Dim ds As DataSet = SqlHelper.ExecuteDataset (ConfigurationSettings.AppSettings (Global.CfgKeyConnString), CommandType.StoredProcedure, "TT_ListManagers") Dim managersArray As New UsersCollection
'Separate Data INTO A List of Collections. Dim R AS DataRow for Each R IN DS.TABLES (0) .ROWS DIM USR AS NEW TTUSER USR.USERNAME = R ("UserName"). Tostring () usr.role = r "RoleID"). ToString () usr.UserID = Convert.ToInt32 (r ( "UserID")) usr.Name = GetDisplayName (usr.UserName, firstName, lastName) usr.FirstName = firstName usr.LastName = lastName managersArray.Add (USR) Next R Return ManagerSArray End Function 'ListManagers' ***************************************** ****************************** The 'Remove Static Method' Removes a User from Database '' ****** *********************************************************** ************
Public Shared Sub Remove (Byval Userid AS Integer) SQLHELPER.EXECUTENONQUERY (GLOBAL.CFGKEYCONNSTINGS (Global.cfgKeyConnstring), "TT_DELETEUSER", Userid) End Sub 'Remove
'********************************************************** *************************** The Data Access Layer 'Returns True If User Information Is Loaded Success, False Otherwise.' ** *********************************************************** ******************
Public Function Load () As Boolean 'Get the user's information from the database Dim ds As DataSet = SqlHelper.ExecuteDataset (ConfigurationSettings.AppSettings (Global.CfgKeyConnString), "TT_GetUserByUserName", _userName)
If DS.TABLES (0) .Rows.count <1 Then Return False End IF DIM DR AS DATAROW = DS.TABLES (0) .ROWS (0) _USERID = Convert.Toint32 ("UserID")) _UserName = Dr ("Username"). Tostring () _role = DR ("RoleID"). Tostring () _password = IIF (DR ("Password") is dbn dbnull.value, "" ")) _displayName = getDisplayName _USERNAME, _FIRSTNAME, _LASTNAME) RETURN TRUE END FUNCTION 'LOAD
'********************************************************** ************************** The TTHE DATABASE DEPENDITEER INFORMATION IN THE DATABASE Depending On The Tt_Userid. 'Returns True if Saved Success, False OtherWise.' ' *********************************************************** *******************
Public overloads function save () as boolean Dim isuserfound as boolean = false Dim isuseractivemanager as boolean = true return save (false, isuserfound, isuseractiveManager) End Function 'SAVE
'********************************************************** ************************** The TRUSERID SUCCESSFULLY, FALSE OTHERWISE. '' '' '' *********************************************************** *******************
Public Overloads Function Save (ByVal checkUsername As Boolean, ByRef isUserFound As Boolean, ByRef isUserActiveManager As Boolean) As Boolean 'Determines whether object needs update or to be inserted. If _userID = 0 Then Return Insert (checkUsername, isUserFound) Else If _userID> 0 Then Return Update (isUserActiveManager) Else _userID = 0 Return False End If End If End Function 'SavePrivate Function Insert (ByVal checkUsername As Boolean, ByRef isUserFound As Boolean) As Boolean Dim firstName As String = String.Empty Dim lastName As String = String. EMPTY isuserfound = false
If ConfigurationSettings.AppSettings (Global.CfgKeyUserAcctSource) <> "None" Then 'Check to see if the user is in the NT SAM or Active Directory before inserting them' into the Time Tracker database. If a first or last name is returned, the user exists and 'can be inserted into the Time Tracker database. If checkUsername Then ttuser.GetDisplayName (_userName, firstName, lastName) isUserFound = firstName <> String.Empty Or lastName <> String.Empty End If Else checkUsername = False isUserFound = True END IF
If checkUsername And isUserFound Or Not checkUsername Then _userID = Convert.ToInt32 (SqlHelper.ExecuteScalar (ConfigurationSettings.AppSettings (Global.CfgKeyConnString), "TT_AddUser", _userName, _password, _displayName, Convert.ToInt32 (_role))) isUserFound = True End If Return _userID> 0 End Function 'InsertPrivate Function Update (ByRef isUserActiveManger As Boolean) As Boolean' 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 If _role = UserRoleConsultant Then If Convert.ToInt32 (SqlHelper.ExecuteScalar (ConfigurationSettings.AppSettings (Global.CfgKeyConnString), "TT_GetManagerProjectCount", _userID))> 0 Then isUserActiveManger = True Return False Else isUserActiveManger = False End If End If Return 0 '********************************************************** ******************* '' '' '' '' '' '' '' '' '' '''D PAIR AGAINST CREDENTIALS 'Stored In The Users Database. If the user Email / Password Pair Is Valid, 'The Method Returns User's Name.' 'Other Relevant Sources:' UserLogin Stored Procedure '' *********************** ************************************************************ Public Function Login (ByVal email As String, ByVal password As String) As StringDim userName As String userName = CStr (SqlHelper.ExecuteScalar (ConfigurationSettings.AppSettings (Global.CfgKeyConnString), "TT_UserLogin", email, password)) IF not username is nothing or usrname is "" The return usrname else returnction.empty endiff END FUNCTION END CLASS