DirectoryAccess クラスに追加したグループ関連のメソッドのVBのコードです。
前回追加したコードはこちら。
'指定した ADSI オブジェクトの所属パスを取得します。
Public Shared Function GetBelongPath(native AsIADs) As String 'オーバーロードを追加
If native Is Nothing Then
Throw NewArgumentNullException("native", "native が Nothing です。")
End If
Return GetBelongPath(native.ADsPath)
End Function
'グループを取得を取得します。GroupTokens プロパティが設定されます。
Public Shared Function GetGroups(Of T As {DirectoryObject, IGroup})() AsIList(OfT)
Dim groups As NewList(OfT)()
Using root = GetRootEntry() 'ルートのDirectoryEntryを取得
If CanConnectDomain Then 'ドメインに接続できる時
Dim filter = String.Format("(objectCategory={0})", CategoryType.Group)
GroupTokens.Clear()
Using searcher As NewDirectorySearcher(root, filter)
Using results = searcher.FindAll()
For Each res AsSearchResultIn results
Dim entry = res.GetDirectoryEntry()
groups.Add(DirectCast(CreateInstance(entry), T))
AddGroupToken(entry) 'PrimaryGroupTokenを追加
Next
End Using
End Using
Else 'ドメインに接続できない時 <-- こっちはローカル
root.Children.SchemaFilter.Add(CategoryType.Group.ToString())
For Each entry AsDirectoryEntryIn root.Children
groups.Add(DirectCast(CreateInstance(entry), T))
Next
End If
End Using
Return groups
End Function
'指定した PrimaryGroupToken を持つドメイングループをプライマリグループとしているメンバの DirectoryEntry のコレクションを取得します。
Public Shared Function GetPrimaryGroupMemberEntries(primaryGroupToken As Integer) AsIList(OfDirectoryEntry)
If CanConnectDomain = False Then 'ドメインに接続できない時
Return NewList(OfDirectoryEntry)()
End If
Dim entries As NewList(OfDirectoryEntry)()
Using root = GetRootEntry() 'ルートのDirectoryEntryを取得
Dim filter = String.Format("(&(|(objectCategory={0})(objectCategory={1}))(primaryGroupID={2}))",
CategoryType.User, CategoryType.Computer, primaryGroupToken)
Using results = searcher.FindAll()
For Each res AsSearchResultIn results
entries.Add(res.GetDirectoryEntry())
Next
End Using
End Using
Return entries
End Function
'指定した LDAP パスの名前(オブジェクト名)を取得します。
Public Shared Function PathToCn(ldapPath As String) As String
If ldapPath Is Nothing Then
Throw NewArgumentNullException("ldapPath", "ldapPath が Nothing です。")
End If
Dim spos = ldapPath.IndexOf("="c) + 1
If spos = 0Then
Return ldapPath
End If
Dim epos = ldapPath.IndexOf(","c)
If epos > 0Then
Return ldapPath.Substring(spos, epos - spos)
Else
Return ldapPath.Substring(spos)
End If
End Function
'PrimaryGroupToken を追加します。
Private Shared Sub AddGroupToken(entry AsDirectoryEntry)
entry.Invoke("GetInfoEx", New Object() {"primaryGroupToken"}, 0)
Dim token = Convert.ToInt32(entry.Properties.Item("primaryGroupToken").Value)
GroupTokens.Add(token, entry.Properties.Item("cn").Value.ToString())
End Sub
DirectoryObject のインスタンスを作成する CreateInstance メソッドにグループ部分のコード(太字の部分)を追加しました。
Private Shared Function CreateInstance(entry AsDirectoryEntry) AsDirectoryObject
Dim category AsCategoryType
If[Enum].TryParse(OfCategoryType)(entry.SchemaClassName, True, category) = False Then
Throw NewArgumentException("entry の種類が CategoryType に該当しません。", "entry")
End If
Select Case category
CaseCategoryType.User
If CanConnectDomain Then 'ドメインに接続できる時
Return NewDomainUser(entry)
Else 'ドメインに接続できない時
Return NewLocalUser(entry)
End If
CaseCategoryType.Group
If CanConnectDomain Then 'ドメインに接続できる時
Return NewDomainGroup(entry)
Else 'ドメインに接続できない時
Return NewLocalGroup(entry)
End If
Case Else
Throw NewNotImplementedException()
End Select
End Function