Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Xml
Imports System.Xml.Serialization


Public Class AdminPage
    Inherits Page

    Dim _roles As String()
    Dim _roleList As List(Of String)
    dim userid as int32
    

    Protected ReadOnly Property Roles() As String()
        Get
            Return _roles
        End Get
    End Property

    Public ReadOnly Property CurrentUserID() As Int32
        Get
            Return userid
        End Get
    End Property

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        Dim info As Object = Session("userinfo")
        If info Is Nothing Then
            Dim authTicket As Object = Request.Cookies("userToken")

'           since the ticket is not memory resident, check to see if it exists as a client cookie
'           if not, then the user was never authenticated so redirect.  Otherwise read it in and use it
'           to find the user row.  Store both to session after this
            If authTicket Is Nothing Then
                Dim redirecturl As Uri = Request.Url
                Response.Redirect(String.Format(getUrl() + "/login.aspx?redirecturl={0}", redirecturl.AbsolutePath))
            Else
                Dim ticketData As String = Request.Cookies("userToken").Value
                Dim _info As CMSUserToken = CMSUserToken.LoadUserFromTicket(ticketData)
                _roles = _info._roles
                _roleList = New List(Of String)(_roles)
                userid = _info._userid
                Session("userinfo") = _info

'               find the DAL user row and place that in the session
                Dim usersTA As DALTableAdapters.UsersTableAdapter = New DALTableAdapters.UsersTableAdapter
                Dim users As DAL.UsersDataTable = usersTA.GetData()
                Dim user As DAL.UsersRow = users.FindByUserID(_info._userid)
                Session("user") = user
            End If
        Else
            Dim _info As CMSUserToken = CType(info, CMSUserToken)
            _roles = _info._roles
            _roleList = New List(Of String)(_roles)
            userid = _info._userid
        End If
    End Sub

    Protected Function IsInRole(ByVal role As String) As Boolean
        Return _roleList.Contains(role)
    End Function
    
    public readonly property UserData as DAL.UsersRow 
        Get
            Dim user As DAL.UsersRow = CType(Session("user"), DAL.UsersRow)
            Return user
        End Get
    End Property

    Protected Sub SignOut()
        Session.Remove("roles")
        Session.RemoveAll()
    End Sub

    Public Function getUrl() As String
        Dim url As String
        '#####################################################
        'KEYWORDS. Issue: 2362
        'NAQIB 10/9/08    
        '#####################################################

        Dim machine As String = Environment.MachineName.ToLower.ToString()
        Select Case machine
            Case "kstlintcdce0514"  'naqib's machine
                url = "http://localhost/BL.REMOTINGHOST"
            Case Else
                url = ""
        End Select

        Return url
    End Function
End Class
