imports system
imports system.data
imports system.configuration
public class ttuser
 '****************************************************************************
 '
 ' ttuser class
 '
 ' the ttuser class represents 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 username as string, byval name as string, byval role as string)
 _userid = userid
 _username = username
 _displayname = name
 _role = role
 end sub 'new
 public property displayname() as string
 get
 return _displayname
 end get
 set(byval value as string)
 _displayname = value
 end set
 end property
 public 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 set
 end property
 public property name() as string
 get
 return _displayname
 end get
 set(byval value as string)
 _displayname = value
 end set
 end property
 public property password() as string
 get
 return _password
 end get
 set(byval value as string)
 _password = value
 end set
 end property
 public property role() as string
 get
 return _role
 end get
 set(byval value as string)
 _role = value
 end set
 end property
 public property rolename() as string
 get
 return _rolename
 end get
 set(byval value as string)
 _rolename = value
 end set
 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 get
 set(byval value as string)
 _username = value
 end set
 end property
 '*********************************************************************
 '
 ' getallusers static method
 ' retrieves a list of all users.
 '
 '*********************************************************************
 public shared function getallusers(byval userid as integer) as userscollection
 return getusers(userid, ttuser.userroleadministrator)
 end function 'getallusers
 '*********************************************************************
 '
 ' getusers static method
 ' retrieves a list of users based on the specified userid and role.
 ' the list returned is restricted by role. for instance, users with
 ' the role of administrator can see all users, while users with the
 ' role of consultant can only see themselves.
 '
 '*********************************************************************
 public shared function getusers(byval userid as integer, byval role as string) as userscollection
 dim firstname as string = string.empty
 dim lastname as string = string.empty
 dim ds as dataset = sqlhelper.executedataset(configurationsettings.appsettings(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 'getdisplayname
 public 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
 '*********************************************************************
 '
 ' listmanagers static method
 ' 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
 '*********************************************************************
 '
 ' remove static method
 ' removes a user from database
 '
 '*********************************************************************
 public shared sub remove(byval userid as integer)
 sqlhelper.executenonquery(configurationsettings.appsettings(global.cfgkeyconnstring), "tt_deleteuser", userid)
 end sub 'remove
 '*********************************************************************
 '
 ' load method
 ' retrieve user information from the data access layer
 ' returns true if user information is loaded successfully, 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(dr("userid"))
 _username = dr("username").tostring()
 _role = dr("roleid").tostring()
 _password = iif(dr("password") is dbnull.value, "", dr("password"))
 _displayname = getdisplayname(_username, _firstname, _lastname)
 return true
 end function 'load
 '*********************************************************************
 '
 ' save method
 ' add or update user information in the database depending on the tt_userid.
 ' returns true if saved successfully, 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
 '*********************************************************************
 '
 ' save method
 ' add or update user information in the database depending on the ttuserid.
 ' returns true if saved 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 'save
 private 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 'insert
 private 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 < convert.toint32(sqlhelper.executescalar(configurationsettings.appsettings(global.cfgkeyconnstring), "tt_updateuser", _userid, _username, _password, _displayname, convert.toint32(_role)))
 end function 'update
 '*********************************************************************
 '
 ' usersdb.login() method 
 '
 ' the login method validates a email/password pair against credentials
 ' stored in the users database. if the 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 string
 dim username as string
 username = cstr(sqlhelper.executescalar(configurationsettings.appsettings(global.cfgkeyconnstring), "tt_userlogin", email, password))
 if not username is nothing or username is "" then
 return username
 else
 return string.empty
 end if
 end function
end class