Quantcast
Channel: わんくま同盟
Viewing all articles
Browse latest Browse all 994

ドメインユーザ用のクラス

$
0
0

ドメインユーザを表すクラス DomainUser クラスは DomainObject クラスを継承し IUser インターフェイスを実装します。

Webアプリでデータソースとして使えるようにします。

C#のコードはのちほど。まずは VBのコード。

 

<DataObject()>

Public NotInheritable ClassDomainUser

  InheritsDomainObject

  ImplementsIUser

 

#Region" プライベートフィールド "

  Private ReadOnly _name As String

#End Region

 

#Region" フレンドコンストラクタ "

  'DirectoryEntry を指定して DomainUser クラスの新しいインスタンスを初期化します。

  Friend Sub New(entry AsDirectoryEntry)

    MyBase.New(entry)

    _name = entry.Properties.Item("sAMAccountName").Value.ToString()

  End Sub

#End Region

 

#Region" パブリックプロパティ "

  '電子メールを取得または設定します。

  Public Property EmailAddress As String

    Get

      If MyBase.IsDisposed Then

        Throw NewObjectDisposedException(Me.GetType().Name)

      End If

      Return DirectCast(MyBase.Entry.Properties.Item("mail").Value, String)

    End Get

    Set(value As String)

      If MyBase.IsDisposed Then

        Throw NewObjectDisposedException(Me.GetType().Name)

      End If

      MyBase.Entry.Properties.Item("mail").Value = value

    End Set

  End Property

'***** 長くなるので以降プロパティの Dispse チェックを省略(DisplayName と Name プロパティは元々なし)

  'アカウントが無効かどうかを取得または設定します。

  Public Property Disabled As Boolean ImplementsIUser.Disabled

    Get

      Return DirectCast(MyBase.Entry.NativeObject, IADsUser).AccountDisabled

    End Get

    Set(value As Boolean)

      DirectCast(MyBase.Entry.NativeObject, IADsUser).AccountDisabled = value

    End Set

  End Property

 

  '表示用の名前を取得します。

  Public ReadOnly Property DisplayName As String

    Get

      Return MyBase.Name

    End Get

  End Property

 

  '名を取得または設定します。

  Public Property FirstName As String

    Get

      Return DirectCast(MyBase.Entry.Properties.Item("givenName").Value, String)

    End Get

    Set(value As String)

      MyBase.Entry.Properties.Item("givenName").Value = value

    End Set

  End Property

 

  '表示名を取得または設定します。

  Public Property FullName As String Implements IUser.FullName

    Get

      Return DirectCast(MyBase.Entry.Properties.Item("displayName").Value, String)

    End Get

    Set(value As String)

      MyBase.Entry.Properties.Item("displayName").Value = value

    End Set

  End Property

 

  '姓を取得または設定します。

  Public Property LastName As String

    Get

      Return DirectCast(MyBase.Entry.Properties.Item("sn").Value, String)

    End Get

    Set(value As String)

      MyBase.Entry.Properties.Item("sn").Value = value

    End Set

  End Property

 

  'アカウント名を取得します。

  Public Overrides ReadOnly Property Name As String

    Get

      Return _name

    End Get

  End Property

 

  'Entry の ADSI User オブジェクトを取得します。

  Public ReadOnly Property Native AsIADsUserImplementsIUser.Native

    Get

      Return DirectCast(MyBase.Entry.NativeObject, IADsUser)

    End Get

  End Property

 

  '事業所を取得または設定します。

  Public Property OfficeName As String

    Get

      Return DirectCast(MyBase.Entry.Properties.Item("physicalDeliveryOfficeName").Value, String)

    End Get

    Set(value As String)

      MyBase.Entry.Properties.Item("physicalDeliveryOfficeName").Value = value

    End Set

  End Property

 

  'プライマリグループ ID を取得します。

  Public ReadOnly Property PrimaryGroupId As Integer

    Get

      Return Convert.ToInt32(MyBase.Entry.Properties.Item("primaryGroupID").Value)

    End Get

  End Property

 

  '電話番号を取得または設定します。

  Public Property TelephoneNumber As String

    Get

      Return DirectCast(MyBase.Entry.Properties.Item("telephoneNumber").Value, String)

    End Get

    Set(value As String)

      MyBase.Entry.Properties.Item("telephoneNumber").Value = value

    End Set

  End Property

#End Region

 

#Region" パブリックメソッド "

  '指定したアカウント名のユーザを検索します。このメソッドはデータバインド用です。

  <DataObjectMethod(DataObjectMethodType.Select)>

  Public Shared Function FindByName(name As String) AsDomainUser

    Return DirectCast(DirectoryAccess.FindDirectoryObject(name, CategoryType.User), DomainUser)

  End Function

 

  '指定したユーザの所属するグループを取得します。このメソッドはデータバインド用です。

  <DataObjectMethod(DataObjectMethodType.Select)>

  Public Shared Function GetBelongGroups(userName As String) AsReadOnlyCollection(Of String)

    Dim user = FindByName(userName)    'ユーザを検索

    Return DirectoryAccess.GetBelongGroups(user)    'ユーザの所属するグループを取得

  End Function

 

  'ユーザの一覧を取得します。このメソッドはデータバインド用です。

  <DataObjectMethod(DataObjectMethodType.Select)>

  Public Shared Function GetUsers() AsIList(OfDomainUser)

    Return DirectoryAccess.GetUsers(OfDomainUser)()    'ユーザを取得

  End Function

 

  '表示用の名前を返します。

  Public Overrides Function ToString() As String

    Return Me.DisplayName

  End Function

#End Region

 

継承元の Name プロパティは表示用の名前を取得するよう name 属性の値を返すようになってるんですが、ユーザの場合はアカウント名を返したいので、sAMAccountName 属性の値を返すようオーバーライドしてます。

表示用の名前は別途 DisplayName プロパティ(Webアプリ用)と オーバーライドした ToString メソッド(Windowsアプリ用)で取得します。

ユーザアカウントを作る時ににちゃんと氏名を指定していれば、それが表示される方が見た目に判りやすいと思います。

 

データバインド用のメソッドは Webアプリで必要になります。内部で DirectoryAccess クラスのメソッドを呼んでますが、これについては別途書きます。


Viewing all articles
Browse latest Browse all 994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>