Public/Users/New-DSUser.ps1

function New-DSUser {
    <#
        .SYNOPSIS

        .DESCRIPTION

        .EXAMPLE
        
    #>

    [CmdletBinding()]
    PARAM (
        #General tab
        [ValidateSet([ServerUserType]::Builtin, [ServerUserType]::Domain, [ServerUserType]::AzureAD)]
        [string]$AuthenticationType = [ServerUserType]::Builtin,
        [UserType]$UserType = [UserType]::User, #TODO Maybe not needed
        [UserLicenceTypeMode]$UserLicenseType = [UserLicenceTypeMode]::Default,

        [ValidateNotNullOrEmpty()]
        [string]$Username = $(throw "Username is null or empty. Please provide a valid username and try again."),
        [ValidateNotNullOrEmpty()]
        [string]$Password,

        [string]$FirstName = "",
        [string]$LastName = "",
        [string]$Email = "",
        [ValidateSet("English", "French", "German", "Spanish", "Hungarian", "Italian", "Dutch", "Polish (Poland)", "Russian", "Swedish", "Ukrainian", "Chinese (Simplified) Legacy", "Chinese (Traditional, Taiwan)", "Czech")]
        [string]$Language = "English",
        [bool]$Enabled = $true,
        [bool]$UserMustChangePasswordAtNextLogin = $false,

        #Information tab
        [string]$CompanyName = "",
        [string]$JobTitle = "",
        [string]$Department = "",
        [string]$GravatarEmail = "",
        [string]$Address = "",
        [string]$State = "",
        [string]$CountryName = "", #TODO Find a way to validateset country name
        [string]$Phone = "",
        [string]$Workphone = "",
        [string]$CellPhone = "",
        [string]$Fax = "",

        #Application access tab
        [bool]$HasAccessRDM = $true,
        [bool]$HasAccessWebLogin = $true,
        [bool]$HasAccessLauncher = $true,
        [bool]$HasAccessWeb = $true,
        [bool]$HasAccessCLI = $true,

        #Privileges tab
        [bool]$AllowDragAndDrop = $true, #CustomSecurityEntity
        [bool]$CanViewInformations = $true, #CustomSecurityEntity
        [bool]$CanViewGlobalLogs = $true, #CustomSecurityEntity
        [bool]$CanImport = $true, #CustomSecurityEntity
        [bool]$CanExport = $true, #CustomSecurityEntity

        [ValidateSet([OfflineMode]::Disabled, [OfflineMode]::ReadOnly, [OfflineMode]::ReadWrite)]
        [string]$OfflineMode = [OfflineMode]::ReadWrite #CustomSecurityEntity
    )
    
    BEGIN {
        Write-Verbose "[New-DSUser] Beginning..."

        $URI = "$Script:DSBaseURI/api/security/user/save"

        if ([string]::IsNullOrWhiteSpace($Global:DSSessionToken)) {
            throw "Session invalid. Please call New-DSSession."
        }
    }
    
    PROCESS {
        if (($AuthenticationType -eq [ServerUserType]::Builtin) -and !(Get-Variable -Name Password)) {
            throw "Password is required when creating new user of type custom. Please provide a valid password and try again."
        }

        if (Get-Variable -Name Password) {
            $Password = $Password
        }

        $Parameters = Get-ParameterValues
        
        $User = @{
            Display      = $Username
            UserAccount  = Get-DSUserAccountSegment $Parameters
            UserProfile  = Get-DSUserProfileSegment $Parameters
            UserSecurity = Get-DSUserSecuritySegment $Parameters
        }

        $RequestParams = @{
            URI    = $URI
            Method = "PUT"
            Body   = $User | ConvertTo-Json
        }

        $res = Invoke-DS @RequestParams -Verbose
        return $res
    }
    
    END {
        if ($res.isSuccess) {
            Write-Verbose "[New-DSUser] Completed successfully!"
        }
        else {
            Write-Verbose "[New-DSUser] Ended with errors..."
            Write-Error $res.ErrorMessage
        }
    }
}