controls/role-assignment-modal.ps1

function New-UDRoleAssignmentModal {
    param (
        [Parameter(Mandatory = $true)]
        $Role
    )
    
    Show-UDModal -Content {
        New-UDPageHeader -Text ("Identity to Role Assignment")
        New-UDParagraph -Text "Select a tab that represents the Assignment type to apply to the $($Role.Name) Role."
        New-UDTabContainer -Tabs {
            New-UDTab -Text 'Members' -Content {
                New-UDCard -Content {

                    New-UDGrid -Id 'identity-grid-assign-modal' -Title "$($Role.Name) Identities" -Headers @("Id", "Name", "Actions") -Properties @("Id", "Name", "Actions") -Endpoint {
                        $Identities = Get-UAIdentity
                        $Identities | Where-Object {$_.Role.Id -eq $Role.Id -and $null -ne $_.Role.Id} | ForEach-Object {
                            $Identity = $_
                            $IdentityRemoveFromRoleModalButton = New-UDButton -Icon user_slash -OnClick {
                                New-UARemoveIdentityFromRoleModal -Identity $Identity -Role $Role
                            }
                
                            
                            $ActionControls = New-UDElement -Tag div -Content {
                                
                                New-UDTooltip -Content {$IdentityRemoveFromRoleModalButton} -TooltipContent { "Remove from Role" }
                                
                            }
                
                            [PSCustomObject]@{
                                Id = $_.Id
                                Name = $_.Name
                                Actions = $ActionControls
                            }
                        } | Out-UDGridData
                    }

                }
            }

            New-UDTab -Text 'Add to Role' -Content {
                New-UDCard -Content {
                    
                    New-UDSelect -Id "RoleAssignmentModalSelect" -Label "Identity" -Option {

                        $Identities = Get-UAIdentity
                        $CurrentUserName = $ClaimsPrincipal.identities.Name
                        $CurrentUserIdentity = Get-UAIdentity -Name $CurrentUserName

                        New-UDSelectOption -Name $CurrentUserIdentity.Name -Value $CurrentUserIdentity.Id

                        $Identities | ForEach-Object{
                            if($_.Name -ne "System" -and $_.Name -ne "admin" -and $_Name -ne $CurrentUserIdentity.Name)
                            {
                                New-UDSelectOption -Name $_.Name -Value $_.Id
                            }
                        }

                    }

                    New-UDElement -Tag div -Attributes @{
                        style = @{
                            textAlign = "right"
                        }
                    } -Content {
                        New-UDButton -Text "Add Identity to Role" -Icon user_plus -OnClick {
                            # Get Input Data
                            $RoleAssignmentUser = ((Get-UDElement -Id 'RoleAssignmentModalSelect').Attributes["value"])
                                                        
                            if($RoleAssignmentUser -eq "null") #UD Bug - this means they selected the default... which is the current user
                            {
                                $CurrentUserName = $ClaimsPrincipal.identities.Name
                                $CurrentUserIdentity = Get-UAIdentity -Name $CurrentUserName
                                $RoleAssignmentUser = $CurrentUserIdentity
                            }

                            # Null Checks
                            if($null -ne $RoleAssignmentUser)
                            {
                                $AssignIdentity = Get-UAIdentity -id $RoleAssignmentUser.Id
                                Set-UAIdentity -Identity $AssignIdentity -Role $Role

                            }
            
                            # Grid Sync
                            Sync-UDElement -Id "role-grid" -Broadcast
                            Sync-UDElement -Id "identity-grid-assign-modal" -Broadcast
                            #Hide-UDModal
                        }
                    }
                   
                }
            }

        }
    }

}