Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Model
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports System.IO
Imports System.Security
Imports System.Xml
Imports System.Security.Principal
Imports DevExpress.Persistent.Base.Security
Imports DevExpress.ExpressApp.Security
Imports DevExpress.ExpressApp.Updating

Public Class RolesModelSecurity
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnFrameAssigned()
        MyBase.OnFrameAssigned()

    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        '// Ideiglenes tiltás
        'Frame.GetController(Of RolesModelSecurity).aRoleActionDisable.Active.SetItemValue("", False)
        'Frame.GetController(Of RolesModelSecurity).aRoleActionEnable.Active.SetItemValue("", False)
        Frame.GetController(Of RolesModelSecurity).aRoleBOModelDisable.Active.SetItemValue("", False)
        Frame.GetController(Of RolesModelSecurity).aRoleBOModelEnable.Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If _ocur Is Nothing Then Exit Sub

        Dim _LoggedUser As User = TryCast(SecuritySystem.CurrentUser, User)
        If _LoggedUser IsNot Nothing Then
            For Each _Role As Role In _LoggedUser.Roles
                Dim _ModelActionRole_Collection As New XPCollection(Of ModelActionRole)(_ocur.Session, CriteriaOperator.Parse("Role.Oid=?", _Role.Oid))

                For Each _ModelActionRole As ModelActionRole In _ModelActionRole_Collection
                    For Each _Controller As Controller In Frame.Controllers
                        If Replace(_Controller.Name, ".Win", "") = _ModelActionRole.Action_Controller Then
                            For Each _Action As ActionBase In _Controller.Actions
                                If _Action.Id = _ModelActionRole.Action_ID Then
                                    _Action.Active.SetItemValue("SISBusinessSecurity", False)
                                End If
                            Next
                        End If
                        If Replace(_Controller.Name, ".Web", "") = _ModelActionRole.Action_Controller Then
                            For Each _Action As ActionBase In _Controller.Actions
                                If _Action.Id = _ModelActionRole.Action_ID Then
                                    _Action.Active.SetItemValue("SISBusinessSecurity", False)
                                End If
                            Next
                        End If
                    Next
                Next
            Next
        End If
    End Sub

    Private Sub aRoleActionEnable_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aRoleActionEnable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelActions))
        Dim _ModelActions As ModelActions
        Dim _Action As IModelAction
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
        If _Role IsNot Nothing Then
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("1=2"))
            _CS.EndUpdateCriteria()

            For Each _Action In Application.Model.ActionDesign.Actions
                If _Action.Controller.Name Like "SIS*" Then
                    _ModelActions = _onew.CreateObject(Of ModelActions)()
                    _ModelActions.Action_ID = _Action.Id
                    _ModelActions.Action_Caption = _Action.Caption
                    _ModelActions.Action_ImageName = _Action.ImageName
                    _ModelActions.Action_Controller = _Action.Controller.Name

                    Dim _ModelActionRole As ModelActionRole = _onew.FindObject(Of ModelActionRole)(CriteriaOperator.Parse("Action_ID=? AND Action_Controller=? AND Role=?", _
                                                                                  _ModelActions.Action_ID, _ModelActions.Action_Controller, _onew.GetObject(_Role)))
                    If _ModelActionRole IsNot Nothing Then
                        _ModelActions.Action_Enabled = False
                    Else
                        _ModelActions.Action_Enabled = True
                    End If

                    _CS.Add(_ModelActions)
                End If
            Next
        End If
        e.View = Application.CreateListView("ModelActions_ListView", _CS, False)
    End Sub
    Private Sub aRoleActionEnable_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aRoleActionEnable.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
        If _Role IsNot Nothing Then
            For Each _ModelActions As ModelActions In e.PopupWindow.View.SelectedObjects
                Dim _ModelActionRole As ModelActionRole = _ocur.FindObject(Of ModelActionRole)(CriteriaOperator.Parse("Action_ID=? AND Action_Controller=? AND Role=?", _
                                                                               _ModelActions.Action_ID, _ModelActions.Action_Controller, _Role))
                If _ModelActionRole IsNot Nothing Then
                    _ocur.Delete(_ModelActionRole)
                End If
            Next
        End If
        _ocur.CommitChanges()
    End Sub
    Private Sub aRoleActionView_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRoleActionView.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelActions))
        Dim _ModelActions As ModelActions
        Dim _Action As IModelAction
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("1=2"))
        _CS.EndUpdateCriteria()

        For Each _Action In Application.Model.ActionDesign.Actions
            If _Action.Controller.Name Like "SIS*" Then
                _ModelActions = _onew.CreateObject(Of ModelActions)()
                _ModelActions.Action_ID = _Action.Id
                _ModelActions.Action_Caption = _Action.Caption
                _ModelActions.Action_ImageName = _Action.ImageName
                _ModelActions.Action_Controller = _Action.Controller.Name

                _ModelActions.Action_SISGroup1 = TryCast(_Action, IModelActionExtender).SISGroup1
                _ModelActions.Action_SISGroup2 = TryCast(_Action, IModelActionExtender).SISGroup2

                _CS.Add(_ModelActions)
            End If
        Next

        e.ShowViewParameters.CreatedView = Application.CreateListView("ModelActions_ListView", _CS, True)
        e.ShowViewParameters.CreatedView.Caption += " (" & _Role.Name & ")"
    End Sub

    Private Sub aRoleNavigationEnable_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aRoleNavigationEnable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _MasterKey As Guid = SecuritySystem.CurrentUserId

        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _onew.Session.ExecuteNonQuery("delete from ""ModelNavigation"" where ""MasterKey""='" & _MasterKey.ToString & "'")
                _onew.CommitChanges()
            Case Else
                _onew.Session.ExecuteNonQuery("delete from ModelNavigation where MasterKey='" & _MasterKey.ToString & "'")
                _onew.CommitChanges()
        End Select

        If View.SelectedObjects.Count = 1 Then
            Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
            If _Role IsNot Nothing Then
                Dim _NavigationItemRole_Collection As New XPCollection(Of NavigationItemRole)(_onew.Session, CriteriaOperator.Parse("Role.Oid=?", _Role.Oid))
                Dim _NavigationItemRole As NavigationItemRole

                Dim _ModelNavigation As ModelNavigation

                Dim _IModelNavigationItems As IModelNavigationItems
                Dim _IModelNavigationItem As IModelNavigationItem

                Dim _NavigationItems As IModelRootNavigationItems = DirectCast(Application.Model, IModelApplicationNavigationItems).NavigationItems
                _IModelNavigationItems = TryCast(_NavigationItems.Items, IModelNavigationItems)
                For Each _IModelNavigationItem In _IModelNavigationItems
                    RecursiveAddModelNavigation(_uow, _MasterKey, Nothing, _IModelNavigationItem)
                Next
                _uow.CommitChanges()

                Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelNavigation))

                _CS.BeginUpdateCriteria()
                _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("MasterKey=?", _MasterKey))
                _CS.EndUpdateCriteria()
                _CS.Sorting.Add(New SortProperty("Index", DB.SortingDirection.Ascending))

                For Each _ModelNavigation In _CS.Collection
                    For Each _NavigationItemRole In _NavigationItemRole_Collection
                        If _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name Then
                            RecursiveDeny(_ModelNavigation)
                        End If
                    Next
                Next

                e.View = Application.CreateListView("ModelNavigation_ListView", _CS, False)
                If _Role IsNot Nothing Then
                    e.View.Caption += " (" & _Role.Name & ")"
                End If
            End If
        End If
    End Sub
    Private Sub aRoleNavigationEnable_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aRoleNavigationEnable.Execute
        Dim _ModelNavigation As ModelNavigation
        Dim _NavigationItemRole As NavigationItemRole
        Dim _ocur As IObjectSpace = View.ObjectSpace

        Dim _Role As Role

        For Each _Role In View.SelectedObjects
            For Each _ModelNavigation In e.PopupWindow.View.SelectedObjects
                _NavigationItemRole = _ocur.FindObject(Of NavigationItemRole)(CriteriaOperator.Parse("Role.Oid=? and ChoiceActionItemId=?", _Role.Oid, _ModelNavigation.Name))
                If _NavigationItemRole IsNot Nothing Then
                    _ocur.Delete(_NavigationItemRole)
                End If
            Next
        Next
        _ocur.CommitChanges()
    End Sub

    Private Sub aRoleNavigationDisable_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aRoleNavigationDisable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _MasterKey As Guid = SecuritySystem.CurrentUserId


        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _onew.Session.ExecuteNonQuery("delete from ""ModelNavigation"" where ""MasterKey""='" & _MasterKey.ToString & "'")
                _onew.CommitChanges()
            Case Else
                _onew.Session.ExecuteNonQuery("delete from ModelNavigation where MasterKey='" & _MasterKey.ToString & "'")
                _onew.CommitChanges()
        End Select
        
        If View.SelectedObjects.Count = 1 Then
            Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
            If _Role IsNot Nothing Then
                Dim _NavigationItemRole_Collection As New XPCollection(Of NavigationItemRole)(_onew.Session, CriteriaOperator.Parse("Role.Oid=?", _Role.Oid))
                Dim _NavigationItemRole As NavigationItemRole

                Dim _ModelNavigation As ModelNavigation

                Dim _IModelNavigationItems As IModelNavigationItems
                Dim _IModelNavigationItem As IModelNavigationItem

                Dim _NavigationItems As IModelRootNavigationItems = DirectCast(Application.Model, IModelApplicationNavigationItems).NavigationItems
                _IModelNavigationItems = TryCast(_NavigationItems.Items, IModelNavigationItems)
                For Each _IModelNavigationItem In _IModelNavigationItems
                    RecursiveAddModelNavigation(_uow, _MasterKey, Nothing, _IModelNavigationItem)
                Next
                _onew.CommitChanges()

                Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelNavigation))

                _CS.BeginUpdateCriteria()
                _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("MasterKey=?", _MasterKey))
                _CS.EndUpdateCriteria()
                _CS.Sorting.Add(New SortProperty("Index", DB.SortingDirection.Ascending))

                For Each _ModelNavigation In _CS.Collection
                    For Each _NavigationItemRole In _NavigationItemRole_Collection
                        If _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name Then
                            RecursiveDeny(_ModelNavigation)
                        End If
                    Next
                Next

                e.View = Application.CreateListView("ModelNavigation_ListView", _CS, False)
                If _Role IsNot Nothing Then
                    e.View.Caption += " (" & _Role.Name & ")"
                End If
            End If
        End If

    End Sub
    Private Sub aRoleNavigationDisable_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aRoleNavigationDisable.Execute
        Dim _ModelNavigation As ModelNavigation
        Dim _NavigationItemRole As NavigationItemRole
        Dim _ocur As IObjectSpace = View.ObjectSpace

        Dim _Role As Role

        For Each _Role In View.SelectedObjects
            For Each _ModelNavigation In e.PopupWindow.View.SelectedObjects
                _NavigationItemRole = _ocur.FindObject(Of NavigationItemRole)(CriteriaOperator.Parse("Role.Oid=? and ChoiceActionItemId=?", _Role.Oid, _ModelNavigation.Name))
                If _NavigationItemRole Is Nothing Then
                    _NavigationItemRole = _ocur.CreateObject(Of NavigationItemRole)()
                    _NavigationItemRole.Role = _ocur.GetObject(_Role)
                    _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name
                End If
            Next
        Next
        _ocur.CommitChanges()

    End Sub

    Private Sub aRoleNavigationView_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRoleNavigationView.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelNavigation))
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)

        Dim _NavigationItemRole_Collection As New XPCollection(Of NavigationItemRole)(_onew.Session, CriteriaOperator.Parse("Role.Oid=?", _Role.Oid))
        Dim _NavigationItemRole As NavigationItemRole

        Dim _ModelNavigation As ModelNavigation

        For Each _ModelNavigation In _CS.Collection
            For Each _NavigationItemRole In _NavigationItemRole_Collection
                If _NavigationItemRole.ChoiceActionItemId = _ModelNavigation.Name Then
                    RecursiveDeny(_ModelNavigation)
                End If
            Next
        Next

        e.ShowViewParameters.CreatedView = Application.CreateListView("ModelNavigation_ListView", _CS, True)
        e.ShowViewParameters.CreatedView.Caption += " (" & _Role.Name & ")"
    End Sub

    Private Sub aRoleBOModelEnable_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aRoleBOModelEnable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelBOModel))
        Dim _ModelBOModel As ModelBOModel
        Dim _IModelClass As IModelClass
        _CS.BeginUpdateCriteria()
        _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("1=2"))
        _CS.EndUpdateCriteria()

        For Each _IModelClass In Application.Model.BOModel
            If _IModelClass.Name Like "SIS*" Then
                _ModelBOModel = _onew.CreateObject(Of ModelBOModel)()
                _ModelBOModel.BOModel_ID = _IModelClass.Name
                _ModelBOModel.BOModel_Caption = _IModelClass.Caption
                _ModelBOModel.BOModel_ImageName = _IModelClass.ImageName
                _ModelBOModel.BOModel_SISGroup1 = TryCast(_IModelClass, IModelClassAllowSort).SISGroup1
                _ModelBOModel.BOModel_SISGroup2 = TryCast(_IModelClass, IModelClassAllowSort).SISGroup2
                _ModelBOModel.BOModel_NonPersistent = TryCast(_IModelClass, IModelClassAllowSort).NonPersistent
                _ModelBOModel.BOModel_IsPopup = TryCast(_IModelClass, IModelClassAllowSort).IsPopup
                _CS.Add(_ModelBOModel)
            End If

        Next

        e.View = Application.CreateListView("ModelBOModel_ListView", _CS, False)
    End Sub
    Private Sub aRoleBOModelEnable_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aRoleBOModelEnable.Execute

    End Sub

    Private Sub aRoleBOModelView_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRoleBOModelView.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelBOModel))
        Dim _ModelBOModel As ModelBOModel
        Dim _IModelClass As IModelClass
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
        Dim _Permission As IPermission
        Dim _XMLDocument As New XmlDocument
        Dim _XmlNodes As Xml.XmlNodeList
        Dim _XmlNode As Xml.XmlNode
        Dim _XmlNode1 As Xml.XmlNode
        Dim i As Long = 0
        _CS.BeginUpdateCriteria()
        _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("1=2"))
        _CS.EndUpdateCriteria()

        For Each _IModelClass In Application.Model.BOModel
            If _IModelClass.Name Like "SIS*" Then
                _ModelBOModel = _onew.CreateObject(Of ModelBOModel)()
                _ModelBOModel.BOModel_ID = _IModelClass.Name
                _ModelBOModel.BOModel_Caption = _IModelClass.Caption
                _ModelBOModel.BOModel_ImageName = _IModelClass.ImageName
                _ModelBOModel.BOModel_SISGroup1 = TryCast(_IModelClass, IModelClassAllowSort).SISGroup1
                _ModelBOModel.BOModel_SISGroup2 = TryCast(_IModelClass, IModelClassAllowSort).SISGroup2
                _ModelBOModel.BOModel_NonPersistent = TryCast(_IModelClass, IModelClassAllowSort).NonPersistent
                _ModelBOModel.BOModel_IsPopup = TryCast(_IModelClass, IModelClassAllowSort).IsPopup
                _CS.Add(_ModelBOModel)
            End If
        Next
        '// For Each _Permission In _Role.Permissions
        For Each _Permission In _Role.Permissions
            _XMLDocument.LoadXml(_Permission.ToXml.ToString)
            _XmlNodes = _XMLDocument.SelectNodes("IPermission")
            For Each _XmlNode In _XmlNodes
                i = 0
                For Each _XmlNode1 In _XmlNode.SelectNodes("ParticularAccessItem")
                    For Each _ModelBOModel In _CS.List
                        If _ModelBOModel.BOModel_ID = _XmlNode1.Attributes("objectType").Value.ToString Then
                            Select Case _XmlNode1.Attributes("access").Value.ToString
                                Case "Create"
                                    If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _ModelBOModel.BOModel_Create = False
                                Case "Read"
                                    If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _ModelBOModel.BOModel_Read = False
                                Case "Write"
                                    If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _ModelBOModel.BOModel_Write = False
                                Case "Delete"
                                    If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _ModelBOModel.BOModel_Delete = False
                                Case "Navigate"
                                    If _XmlNode1.Attributes("modifier").Value.ToString = "Deny" Then _ModelBOModel.BOModel_Navigate = False
                            End Select
                        End If
                    Next
                Next
            Next
        Next
        e.ShowViewParameters.CreatedView = Application.CreateListView("ModelBOModel_ListView", _CS, True)
        e.ShowViewParameters.CreatedView.Caption += " (" & _Role.Name & ")"
    End Sub

    Sub RecursiveAddModelNavigation(_uow As UnitOfWork, _MasterKey As Guid, _ModelNavigationParent As ModelNavigation, _IModelNavigationItem As IModelNavigationItem)
        Dim _ModelNavigation As ModelNavigation
        Dim _IModelNavigationItem_Child As IModelNavigationItem

        _ModelNavigation = New ModelNavigation(_uow)

        _ModelNavigation.Parent = _ModelNavigationParent
        _ModelNavigation.Name = _IModelNavigationItem.Id
        _ModelNavigation.Navigation_Caption = _IModelNavigationItem.Caption
        _ModelNavigation.Navigation_ImageName = _IModelNavigationItem.ImageName
        _ModelNavigation.MasterKey = _MasterKey
        'If _IModelNavigationItem.Index IsNot Nothing Then
        '    _ModelNavigation.Index = CLng(_IModelNavigationItem.Index)
        'Else

        'End If


        _uow.CommitChanges()

        If _IModelNavigationItem.Items.Count > 0 Then
            For Each _IModelNavigationItem_Child In _IModelNavigationItem.Items
                RecursiveAddModelNavigation(_uow, _MasterKey, _ModelNavigation, _IModelNavigationItem_Child)
            Next
        End If

    End Sub
    Sub RecursiveDeny(_ModelNavigation As ModelNavigation)
        Dim _ModelNavigation_Children As ModelNavigation
        _ModelNavigation.Navigation_Enabled = False
        If _ModelNavigation.Children.Count > 0 Then
            For Each _ModelNavigation_Children In _ModelNavigation.Children
                RecursiveDeny(_ModelNavigation_Children)
            Next
        End If
    End Sub
    Private Sub aRoleActionDisable_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aRoleActionDisable.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(ModelActions))
        Dim _ModelActions As ModelActions
        Dim _Action As IModelAction
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
        _CS.BeginUpdateCriteria()
        _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("1=2"))
        _CS.EndUpdateCriteria()

        For Each _Action In Application.Model.ActionDesign.Actions
            If _Action.Controller.Name Like "SIS*" Then
                _ModelActions = _onew.CreateObject(Of ModelActions)()
                _ModelActions.Action_ID = _Action.Id
                _ModelActions.Action_Caption = _Action.Caption
                _ModelActions.Action_ImageName = _Action.ImageName
                _ModelActions.Action_Controller = _Action.Controller.Name

                Dim _ModelActionRole As ModelActionRole = _onew.FindObject(Of ModelActionRole)(CriteriaOperator.Parse("Action_ID=? AND Action_Controller=? AND Role=?", _
                                                                                _ModelActions.Action_ID, _ModelActions.Action_Controller, _onew.GetObject(_Role)))
                If _ModelActionRole IsNot Nothing Then
                    _ModelActions.Action_Enabled = False
                Else
                    _ModelActions.Action_Enabled = True
                End If

                _CS.Add(_ModelActions)
            End If
        Next

        e.View = Application.CreateListView("ModelActions_ListView", _CS, False)
    End Sub
    Private Sub aRoleActionDisable_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aRoleActionDisable.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Role As Role = TryCast(View.SelectedObjects(0), Role)
        If _Role IsNot Nothing Then
            For Each _ModelActions As ModelActions In e.PopupWindow.View.SelectedObjects
                Dim _ModelActionRole As ModelActionRole = _ocur.FindObject(Of ModelActionRole)(CriteriaOperator.Parse("Action_ID=? AND Action_Controller=? AND Role=?", _
                                                                                _ModelActions.Action_ID, _ModelActions.Action_Controller, _Role))
                If _ModelActionRole Is Nothing Then
                    _ModelActionRole = New ModelActionRole(_ocur.Session)
                    With _ModelActionRole
                        .Role = _Role
                        .Action_Controller = _ModelActions.Action_Controller
                        .Action_ID = _ModelActions.Action_ID
                    End With
                End If
            Next
        End If
        _ocur.CommitChanges()
    End Sub
End Class
