aportal.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$ErrorActionPreference = "Stop"
#Requires -Version 5.1
#Requires -Modules PowerShellGet
#Requires -Modules SharePointPnPPowerShellOnline

$moduleName = "SharePointPnPPowerShellOnline"

if (!(Get-command -Module $moduleName).count -gt 0) {
    Install-Module $moduleName -Force -SkipPublisherCheck -RequiredVersion 3.22.2006.2
}
Write-Output "The modules for $moduleVersion have been installed and can now be used."
function Set-APortalWeb {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    Param(
        [Parameter(Mandatory = $true)]
        [string] $Url,
        [string] $ClientId,
        [string] $ClientSecret,
        [switch] $Global
    )
    if ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
        # Critical code
    }
    if ($ClientId) {
        Connect-PnPOnline -Url $Url -ClientId $ClientId -ClientSecret $ClientSecret -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000
    }
    else {
        Connect-PnPOnline -Url $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000
    }

    # Write-Progress -Activity ("Setting the property 'DenyAddAndCustomizePages=false' for the current site.")
    # Set-PnPSite -NoScriptSite $true -Url (Get-PnPSite | Select-Object Url -ExpandProperty Url)

    Install-APortalPnPPackage -Name Web -Global:$Global
    Install-APortalPnPPackage -Name Assets -Global:$Global

    Set-APortalUserGroup

    Write-Host 'APortal installed successfully!'
}

function Get-APortalPnPPackageVersions {
    Param(
        [Parameter(Mandatory = $true)]
        [ValidateSet("Web", "Assets")]
        [string] $Name,
        [switch] $Global
    )
    
    $Address = "https://shuishan-tech.coding.net/p/APortal/d/APortal/git/raw/master/packages/";
    if ($Global) {
        $Address = "https://raw.githubusercontent.com/shuishan-tech/aportal/master/packages/";
    }
    $versionFilePath = "$($Address)$($Name.ToLower())/version.json"

    Invoke-RestMethod -Uri $versionFilePath | ConvertTo-Json
}

function Install-APortalPnPPackage {
    [CmdletBinding(ConfirmImpact = 'Medium')]
    Param(
        [Parameter(Mandatory = $true)]
        [ValidateSet("Web", "Assets")]
        [string] $Name,
        [string] $Url,
        [string] $ClientId,
        [string] $ClientSecret,
        [string] $Version,
        [switch] $Global,
        [switch] $Force
    )
    Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name)
    if ($Url) {    
        if ($ClientId) {
            Connect-PnPOnline -Url $Url -ClientId $ClientId -ClientSecret $ClientSecret -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000
        }
        else {
            Connect-PnPOnline -Url $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000
        }
    }

    $Address = "https://shuishan-tech.coding.net/p/APortal/d/APortal/git/raw/master/packages/";
    if ($Global) {
        $Address = "https://raw.githubusercontent.com/shuishan-tech/aportal/master/packages/";
    }
    # Get current version.
    $versionFilePath = "$($Address)$($Name.ToLower())/version.json"
    $versionFileFolderPathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name.ToLower())"
    $versionFilePathLocal = "$($versionFileFolderPathLocal)\version.json"

    if (!(Test-Path $versionFileFolderPathLocal)) {
        New-Item -Path $versionFileFolderPathLocal -ItemType Directory | Out-Null
    }
    Invoke-RestMethod -Uri $versionFilePath -OutFile $versionFilePathLocal -ErrorAction Stop

    $version = $Version
    if (!$version) {
        $versionObj = (Get-Content $versionFilePathLocal) | ConvertFrom-Json
        $version = $versionObj.currentVersion
    }

    $pnpFilePath = "$($Address)$($Name.ToLower())/$($Version)/aportal.$($Name.ToLower()).pnp"
    $pnpFileFolderPathLocal = "$(SPlit-Path $script:MyInvocation.MyCommand.Path)\packages\$($Name.ToLower())\$($version)"
    $pnpFilePathLocal = "$($pnpFileFolderPathLocal)\aportal.$($Name.ToLower()).pnp"
    $existsLocalFile = Test-Path $pnpFilePathLocal -PathType Leaf

    if ($Force -or !$existsLocalFile) {
        if (!(Test-Path $pnpFileFolderPathLocal)) {
            New-Item -Path $pnpFileFolderPathLocal -ItemType Directory | Out-Null
        }
        # Download current version file and save it.
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status ("Downloading the version ({0})." -f $version)
        Invoke-WebRequest -Uri $pnpFilePath -OutFile $pnpFilePathLocal -ErrorAction Stop
    }

    Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name)
    if (Test-Path $pnpFilePathLocal) {
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "Package name: $($version)\aportal.$($Name.ToLower()).pnp"
        Apply-PnPProvisioningTemplate -Path $pnpFilePathLocal
    }
    else {
        Write-Progress -Activity ("Installing APortal PnP package ({0})." -f $Name) -Status "The current version file is not exist, please run command `Get-APortalPackages` and try again."
    }
}

function Set-APortalUserGroup {
    [CmdletBinding(ConfirmImpact = 'Medium')]
    Param(
        [Parameter(Mandatory = $false)]
        [string] $Url
    )
    
    Write-Progress -Activity ("Setting APortal user group.")
    if ($Url) {  
        Connect-PnPOnline $Url -UseWebLogin -RetryCount 500 -RetryWait 5 -RequestTimeout 360000000
    }

    $SSUser = Get-PnPRoleDefinition -Identity 'APortalUserRole' -ErrorAction SilentlyContinue
    if ($SSUser) {
        Write-Host 'The APortalUserRole RoleDefinition already exists.'
    }
    else {
        Add-PnPRoleDefinition -RoleName 'APortalUserRole' -Description 'For APortal users.' -Include AddListItems, EditListItems, ViewListItems, DeleteListItems, OpenItems, ViewVersions, BrowseDirectories, ViewPages, UseRemoteAPIs, Open
        New-PnPGroup -Title 'APortalUserGroup' -Description 'For APortal users.' -ErrorAction SilentlyContinue | Out-Null
        $site = Get-PnPSite -Includes RootWeb
        Set-PnPGroupPermissions -Identity 'APortalUserGroup' -AddRole 'APortalUserRole' -Web $site.RootWeb
    }
}

Export-ModuleMember -Function Set-APortalWeb, Set-AportalUserGroup, Install-APortalPnPPackage, Get-APortalPnPPackageVersions