Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/login.ps1

raw
1 #!powershell
2 # -*- coding: utf-8 -*-
3
4 # (c) 2022, John McCall (@lowlydba)
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 #AnsibleRequires -CSharpUtil Ansible.Basic
8 #AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
9 #Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }
10
11 $ErrorActionPreference = "Stop"
12
13 $spec = @{
14 supports_check_mode = $true
15 options = @{
16 login = @{type = 'str'; required = $true }
17 password = @{type = 'str'; required = $false; no_log = $true }
18 enabled = @{type = 'bool'; required = $false; default = $true }
19 default_database = @{type = 'str'; required = $false }
20 language = @{type = 'str'; required = $false }
21 password_must_change = @{type = 'bool'; required = $false }
22 password_policy_enforced = @{type = 'bool'; required = $false }
23 password_expiration_enabled = @{type = 'bool'; required = $false }
24 sid = @{type = 'str'; required = $false }
25 skip_password_reset = @{type = 'bool'; required = $false; default = $false }
26 state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
27 }
28 }
29
30 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
31 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
32 $login = $module.Params.login
33 if ($null -ne $module.Params.password) {
34 $secPassword = ConvertTo-SecureString -String $module.Params.password -AsPlainText -Force
35 }
36 $enabled = $module.Params.enabled
37 $defaultDatabase = $module.Params.default_database
38 $language = $module.Params.language
39 [nullable[bool]]$passwordMustChange = $module.Params.password_must_change
40 [nullable[bool]]$passwordExpirationEnabled = $module.Params.password_expiration_enabled
41 [nullable[bool]]$passwordPolicyEnforced = $module.Params.password_policy_enforced
42 [nullable[bool]]$skip_password_reset = $module.Params.skip_password_reset
43 $sid = $module.Params.sid
44 $state = $module.Params.state
45 $checkMode = $module.CheckMode
46
47 $module.Result.changed = $false
48
49 try {
50 $getLoginSplat = @{
51 SqlInstance = $sqlInstance
52 SqlCredential = $sqlCredential
53 Login = $login
54 ExcludeSystemLogin = $true
55 EnableException = $true
56 }
57 $existingLogin = Get-DbaLogin @getLoginSplat
58
59 if ($state -eq "absent") {
60 if ($null -ne $existingLogin) {
61 $output = $existingLogin | Remove-DbaLogin -WhatIf:$checkMode -EnableException -Force -Confirm:$false
62 $module.Result.changed = $true
63 }
64 }
65 elseif ($state -eq "present") {
66 $setLoginSplat = @{
67 SqlInstance = $sqlInstance
68 SqlCredential = $sqlCredential
69 Login = $login
70 WhatIf = $checkMode
71 EnableException = $true
72 Confirm = $false
73 }
74 if ($null -ne $defaultDatabase) {
75 $setLoginSplat.add("DefaultDatabase", $defaultDatabase)
76 }
77 if ($null -ne $passwordExpirationEnabled) {
78 if ($existingLogin.PasswordExpirationEnabled -ne $passwordExpirationEnabled) {
79 $changed = $true
80 }
81 if ($passwordExpirationEnabled -eq $true) {
82 $setLoginSplat.add("PasswordExpirationEnabled", $true)
83 }
84 }
85 if ($null -ne $passwordPolicyEnforced) {
86 if ($existingLogin.PasswordPolicyEnforced -ne $passwordPolicyEnforced) {
87 $changed = $true
88 }
89 if ($passwordPolicyEnforced -eq $true) {
90 $setLoginSplat.add("PasswordPolicyEnforced", $true)
91 }
92 }
93 if ($true -eq $passwordMustChange) {
94 if ($existingLogin.PasswordMustChange -ne $passwordMustChange) {
95 $changed = $true
96 }
97 if ($passwordMustChange -eq $true) {
98 $setLoginSplat.add("PasswordMustChange", $true)
99 }
100 }
101
102 # Login already exists
103 if ($null -ne $existingLogin) {
104 if (($null -ne $secPassword) -and ($skip_password_reset -eq $false)) {
105 $setLoginSplat.add("SecurePassword", $secPassword)
106 $changed = $true
107 }
108
109 # Splat login status
110 if ($enabled -eq $false) {
111 $disabled = $true
112 $setLoginSplat.add("Disable", $true)
113 }
114 else {
115 $disabled = $false
116 $setLoginSplat.add("Enable", $true)
117 }
118 # Login needs to be modified
119 if (($changed -eq $true) -or ($disabled -ne $existingLogin.IsDisabled) -or ($setLoginSplat.ContainsKey("SecurePassword"))) {
120 $output = Set-DbaLogin @setLoginSplat
121 $module.result.changed = $true
122 }
123 }
124 # New login
125 else {
126 $setLoginSplat.add("SecurePassword", $secPassword)
127 if ($null -ne $language) {
128 $setLoginSplat.add("Language", $language)
129 }
130 if ($enabled -eq $false) {
131 $setLoginSplat.add("Disabled", $true)
132 }
133 if ($null -ne $sid) {
134 $setLoginSplat.add("Sid", $sid)
135 }
136 $output = New-DbaLogin @setLoginSplat
137 $module.result.changed = $true
138 }
139 # If not in check mode, add extra fields we can change to default display set
140 if ($null -ne $output) {
141 $output.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add("DefaultDatabase")
142 $output.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add("Language")
143 }
144 }
145
146 if ($null -ne $output) {
147 $resultData = ConvertTo-SerializableObject -InputObject $output
148 $module.Result.data = $resultData
149 }
150 $module.ExitJson()
151 }
152 catch {
153 $module.FailJson("Configuring login failed: $($_.Exception.Message) ; $setLoginSplat", $_)
154 }