Imports System.DirectoryServices
Imports AppCommon
Imports AppVariable

Public Class AppAuth
    '--------------------------------------------------------------------------------------------------
    'メソッド名：Auth_LDAP
    '機能説明:LDAPサーバへの認証結果を取得
    '引数:
    '       LDAP_ServerName As String       LDAPサーバ名またはIPアドレス
    '       LDAP_Port As String             LDAPポート番号
    '       Search_Scope As String          ユーザの検索範囲(例:ou=People,dc=my-domain,dc=com)
    '       DomainName As String          　ユーザが所属するドメイン名(例:ou=People,dc=my-domain,dc=com)
    '       UserId As String                ユーザID
    '       UserPasswd As String            ユーザパスワード
    '
    '返り値: Boolean型
    '   ture    成功
    '   false   失敗
    '--------------------------------------------------------------------------------------------------
    Shared Function Auth_LDAP(ByVal LDAP_ServerName As String, _
                                 ByVal LDAP_Port As String, _
                                 ByVal Search_Scope As String, _
                                 ByVal Domain_Name As String, _
                                 ByVal UserId As String, _
                                 ByVal UserPasswd As String) As Boolean
        Dim drEntry As DirectoryEntry
        Dim sResult As SearchResult

        Try
            drEntry = New DirectoryEntry("LDAP://" & _
                                        LDAP_ServerName & ":" & _
                                        LDAP_Port & "/" & _
                                        Search_Scope, _
                                        "uid=" & UserId & "," & _
                                        Domain_Name, _
                                        UserPasswd, _
                                        AuthenticationTypes.FastBind)

            Dim drSearch As New DirectorySearcher()
            drSearch.ServerTimeLimit = New System.TimeSpan(0, 0, 1000)
            drSearch.ClientTimeout = New System.TimeSpan(0, 0, 1000)
            drSearch.SearchRoot = drEntry

            drSearch.SearchScope = SearchScope.Subtree
            sResult = drSearch.FindOne

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    '--------------------------------------------------------------------------------------------------
    'メソッド名：Auth_Portal
    '機能説明:ポータルサイトへのアクセスが許可されているか基幹システムへ問い合わせて結果を取得
    '引数:
    '       PORTALID As String          PORTALID
    '       PASSWORD As String          パスワード
    '
    '返り値: Boolean型
    '   true   許可する
    '   false   許可しない
    '
    '2014.4.30 引数にGRADUATIONを追加。卒業生も有効とするか、無効とするかを選択可能とする/牧
    '--------------------------------------------------------------------------------------------------
    Shared Function Auth_Portal(ByVal PORTALID As String, _
                            ByVal PASSWORD As String, _
                            ByVal GRADUATION As String) As Boolean

        Using spm As New StoredProcedureManager("PT_認証_LOGIN_OUTPUT")
            spm.AddPrmIn("@PORTALID", Data.SqlDbType.VarChar, 200, PORTALID)
            spm.AddPrmIn("@PASSWORD", Data.SqlDbType.VarChar, 200, PASSWORD)
            spm.AddPrmOut("@RET_PORTALID", Data.SqlDbType.VarChar, 200)
            spm.AddPrmOut("@RET_PASSWORD", Data.SqlDbType.VarChar, 200)
            spm.AddPrmOut("@USERNAME", Data.SqlDbType.NVarChar, 200)
            spm.AddPrmOut("@USERCD", Data.SqlDbType.VarChar, 20)
            spm.AddPrmIn("@GRADUATION", Data.SqlDbType.VarChar, 200, GRADUATION)
            spm.AddPrmOut("@PORTAL区分", Data.SqlDbType.VarChar, 20)
            '2024/02/26 M.Okabe ADD Start
            spm.AddPrmOut("@学園CD", Data.SqlDbType.VarChar, 20)
            spm.AddPrmOut("@市区町村CD", Data.SqlDbType.VarChar, 20)
            spm.AddPrmOut("@市区町村名", Data.SqlDbType.VarChar, 100)
            '2024/02/26 M.Okabe ADD End
            spm.AddPrmOut("@利用者区分", Data.SqlDbType.VarChar, 20)
            spm.Exec()

            If PORTALID.Equals(spm.GetValue("@RET_PORTALID")) And PASSWORD.Equals(spm.GetValue("@RET_PASSWORD")) Then '大文字小文字も判定
                HttpContext.Current.Session(S_COM_PortalID) = PORTALID
                HttpContext.Current.Session(S_COM_User_Name) = spm.GetValue("@USERNAME")
                HttpContext.Current.Session(S_COM_UserCD) = spm.GetValue("@USERCD")
                HttpContext.Current.Session(S_COM_PortalKbn) = spm.GetValue("@PORTAL区分")
                '2024/02/26 M.Okabe ADD Start
                Dim user As LoginUser = New LoginUser
                user.PORTALID = PORTALID
                user.PORTAL区分 = spm.GetValue("@PORTAL区分")
                user.USERCD = spm.GetValue("@USERCD")
                user.USERNAME = Nz(spm.GetValue("@USERNAME"))
                user.学園CD = Nz(spm.GetValue("@学園CD"))
                user.市区町村CD = Nz(spm.GetValue("@市区町村CD"))
                user.市区町村名 = Nz(spm.GetValue("@市区町村名"))
                user.利用者区分 = Nz(spm.GetValue("@利用者区分"))
                HttpContext.Current.Session(S_COM_LoginUser) = user
                '2024/02/26 M.Okabe ADD End

                'アプリケーションパスをクッキーに保存
                HttpContext.Current.Response.Cookies(C_COM_App_Path).Value = HttpContext.Current.Request.ApplicationPath & "/"
                HttpContext.Current.Response.Cookies(C_COM_App_Path).Path = HttpContext.Current.Request.ApplicationPath & "/"
                HttpContext.Current.Response.Cookies(C_COM_App_Path).Expires = DateTime.Now.AddDays(Cookie_Expires)

                Return True
            Else
                Return False
            End If
        End Using
    End Function

    '--------------------------------------------------------------------------------------------------
    'メソッド名：Auth_Content
    '機能説明:CONTENTへのアクセスが許可されているかを判定し結果を取得
    '引数:
    '       PORTALID As String              PORTALID
    '       CONTENT_PATH As String          コンテンツのファイルパス
    '
    '返り値: Boolean型
    '   true   許可する
    '   false   許可しない
    '--------------------------------------------------------------------------------------------------
    Shared Function Auth_Content(ByVal PORTALID As String, _
                                    ByVal CONTENT_PATH As String, _
                                    ByVal UC1_PATH As String) As Boolean
        Using spm As New StoredProcedureManager("PT_認証_CONTENT_OUTPUT")
            spm.AddPrmIn("@PORTALID", Data.SqlDbType.VarChar, 200, PORTALID)
            spm.AddPrmIn("@CONTENT_PATH", Data.SqlDbType.VarChar, 500, CONTENT_PATH)
            spm.AddPrmIn("@UC1_PATH", Data.SqlDbType.VarChar, 500, UC1_PATH)
            spm.AddPrmOut("@ERR", Data.SqlDbType.Bit)
            spm.Exec()

            If spm.GetValue("@ERR") = 0 Then
                Return True
            Else
                Return False
            End If
        End Using
    End Function

End Class
