Imports System
Imports System.Data
Imports System.Web.UI.WebControls
Imports ASPNET.StarterKit.TimeTracker.BusinessLogicLayer

Namespace ASPNET.StarterKit.TimeTracker.Web

    '*********************************************************************
    '
    ' UserDetail.aspx
    '
    ' The UserDetail.aspx page is used to add new user or make changes to
    ' existing users.
    ' In addition, user Roles are added and changed on this page.
    ' Only members of the Admin Role can access this page.
    '
    '*********************************************************************

    Public Class UserDetail
        Inherits System.Web.UI.Page
        Protected WithEvents Save As System.Web.UI.WebControls.Button
        Protected WithEvents Cancel As System.Web.UI.WebControls.Button
        Protected UserName As System.Web.UI.WebControls.TextBox
        Protected RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
        Protected Roles As System.Web.UI.WebControls.DropDownList
        Protected lblError As System.Web.UI.WebControls.Label
        Protected ErrorLabel As System.Web.UI.WebControls.Label
        Protected ErrorLabel2 As System.Web.UI.WebControls.Label
        Protected WithEvents DisplayName As System.Web.UI.WebControls.TextBox
        Protected WithEvents Password As System.Web.UI.WebControls.TextBox
        Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents ConfirmPassword As System.Web.UI.WebControls.TextBox
        Protected WithEvents RequiredFieldValidator4 As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator
        Protected WithEvents rolelbl As System.Web.UI.WebControls.Label
        Protected WithEvents displayNamelbl As System.Web.UI.WebControls.Label
        Protected WithEvents passwordlbl As System.Web.UI.WebControls.Label
        Protected WithEvents confirmPasswordlbl As System.Web.UI.WebControls.Label
        Protected WithEvents userNamelbl As System.Web.UI.WebControls.Label

        'Use TTUser component to capture user's input during edit mode
        Private Usr As New TTUser()

        '*********************************************************
        '
        ' UserDetail page of the Application
        '
        '*********************************************************

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Ensure that the visiting user has access to view the current page
            If TTSecurity.IsInRole(TTUser.UserRoleAdministrator) = False Then
                Response.Redirect("AccessDenied.aspx?Index=-1", True)
            End If

            If Not Page.IsPostBack Then
                BindInfo()

                'Hide fields for editing/adding windows authenticated users
                If (Usr.Password Is "" And Not Usr.UserID = 0) Or ((Not Context.User.Identity.AuthenticationType Is "Forms") And Usr.UserID = 0) Then
                    displayNamelbl.Visible = False
                    DisplayName.Visible = False
                    passwordlbl.Visible = False
                    Password.Visible = False
                    confirmPasswordlbl.Visible = False
                    ConfirmPassword.Visible = False
                    RequiredFieldValidator3.Visible = False
                    RequiredFieldValidator4.Visible = False
                    userNamelbl.Text = "Username (Use the format: DOMAIN or Computer Name\Username )"
                End If

            End If
        End Sub 'Page_Load

        '*********************************************************************
        '
        ' The BindInfo method retrieves the list of roles 
        ' and Load User information if user exists
        '
        '*********************************************************************

        Private Sub BindInfo()
            ' Gets UserName from querystring
            ' This value should by pass in by UserList.aspx
            Dim User As String = Request.QueryString("UserName")

            Roles.DataSource = BusinessLogicLayer.Roles.GetRoles()
            Roles.DataTextField = "Name"
            Roles.DataValueField = "RoleID"
            Roles.DataBind()

            If Not (User Is Nothing) Then
                Usr.UserName = User
                Usr.Load()
                UserName.Text = Usr.UserName
                UserName.ReadOnly = True
                Roles.Items.FindByValue(Usr.Role).Selected = True

                DisplayName.Text = Usr.Name

            Else
                UserName.ReadOnly = False
            End If
        End Sub 'BindInfo

        '*********************************************************************
        '
        ' The SaveButton_Click event handler saves the configuration of the user.
        ' It updates if the current user id is not 0 and inserts if it is 0.
        '
        '*********************************************************************

        Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
            Dim isUserFound As Boolean = False
            Dim isUserActiveManager As Boolean = False

            ' Save the user and ask the user object to make sure the username is
            ' found in the user account source (NT SAM or Active Directory)
            Usr.UserName = UserName.Text
            Usr.Load()
            Usr.Role = Roles.SelectedItem.Value
            Usr.DisplayName = TTSecurity.CleanStringRegex(DisplayName.Text)
            If Not (Password.Text Is String.Empty) Then
                Usr.Password = TTSecurity.Encrypt(Password.Text)
            End If
            Usr.Save(True, isUserFound, isUserActiveManager)

            ' If we're adding a new user to the system and the username is not found in
            ' the user account source (i.e. Active Directory or NT SAM), then we will
            ' not allow the user to be added to our Time Tracker database.  Display an error
            ' message so the user knows what happened.
            If Usr.UserID = 0 And Not isUserFound Then
                ErrorLabel.Text = "The username was not found on your system or network."
            Else
                If isUserActiveManager Then
                    ErrorLabel.Text = "User Role can not be changed, user is a manager of one or more projects."

                Else
                    Response.Redirect("UserList.aspx?index=2&adminIndex=1", False)
                End If
            End If
        End Sub 'Save_Click

        '*********************************************************************
        '
        ' The Cancel_Click event handler undo changes and redirect to UserList page
        '
        '*********************************************************************

        Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
            Response.Redirect("UserList.aspx?index=2&adminIndex=1", False)
        End Sub 'Cancel_Click

        Private Sub InitializeComponent()

        End Sub
    End Class 'UserDetail
End Namespace 'ASPNET.StarterKit.TimeTracker.Web