Skip to content

Commit

Permalink
[dsccommunity#1474] SqlServerLogin: Added DefaultDatabase parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bozho committed Mar 26, 2020
1 parent 38319b1 commit 2c1b07a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)

## [Unreleased]

### Added

- SqlServerLogin
- Added `DefaultDatabase` parameter ([issue #1474](https://github.com/dsccommunity/SqlServerDsc/issues/1474).)

### Changed

- SqlServerDsc
Expand Down
53 changes: 45 additions & 8 deletions source/DSCResources/MSFT_SqlServerLogin/MSFT_SqlServerLogin.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ function Get-TargetResource
)

$returnValue = @{
Ensure = $ensure
Name = $Name
LoginType = $login.LoginType
ServerName = $ServerName
InstanceName = $InstanceName
Disabled = $login.IsDisabled
Ensure = $ensure
Name = $Name
LoginType = $login.LoginType
ServerName = $ServerName
InstanceName = $InstanceName
Disabled = $login.IsDisabled
DefaultDatabase = $login.DefaultDatabase
}

if ($login.LoginType -eq 'SqlLogin')
Expand Down Expand Up @@ -113,6 +114,9 @@ function Get-TargetResource
.PARAMETER Disabled
Specifies if the login is disabled. Default is $false.
.PARAMETER DefaultDatabase
Specifies the default database for the login.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -167,7 +171,11 @@ function Set-TargetResource

[Parameter()]
[System.Boolean]
$Disabled
$Disabled,

[Parameter()]
[System.String]
$DefaultDatabase
)

$serverObject = Connect-SQL -ServerName $ServerName -InstanceName $InstanceName
Expand Down Expand Up @@ -232,6 +240,12 @@ function Set-TargetResource
$login.Enable()
}
}

if ( $PSBoundParameers.ContainsKey('DefaultDatabase') -and ($login.DefaultDatabase -ne $DefaultDatabase) )
{
$login.DefaultDatabase = $DefaultDatabase
Update-SQLServerLogin -Login $login
}
}
else
{
Expand Down Expand Up @@ -295,6 +309,13 @@ function Set-TargetResource

$login.Disable()
}

# set the default database if specified
if ( $PSBoundParameers.ContainsKey('DefaultDatabase') )
{
$login.DefaultDatabase = $DefaultDatabase
Update-SQLServerLogin -Login $login
}
}
}

Expand Down Expand Up @@ -345,6 +366,9 @@ function Set-TargetResource
.PARAMETER Disabled
Specifies if the login is disabled. Default is $false.
.PARAMETER DefaultDatabase
Specifies the default database for the login.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -399,7 +423,11 @@ function Test-TargetResource

[Parameter()]
[System.Boolean]
$Disabled
$Disabled,

[Parameter()]
[System.String]
$DefaultDatabase
)

Write-Verbose -Message (
Expand Down Expand Up @@ -455,6 +483,15 @@ function Test-TargetResource
$testPassed = $false
}

if ( $PSBoundParameters.ContainsKey('DefaultDatabase') -and ($loginInfo.DefaultDatabase -ne $DefaultDatabase) )
{
Write-Verbose -Message (
$script:localizedData.WrongDefaultDatabase -f $Name, $loginInfo.DefaultDatabase, $DefaultDatabase
)

$testPassed = $false
}

if ( $LoginType -eq 'SqlLogin' )
{
if ( $LoginPasswordExpirationEnabled -ne $loginInfo.LoginPasswordExpirationEnabled )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class MSFT_SqlServerLogin : OMI_BaseResource
[Write, Description("Specifies if the login password is required to expire in accordance to the operating system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordExpirationEnabled;
[Write, Description("Specifies if the login password is required to conform to the password policy specified in the system security policy. Only applies to SQL Logins. Default is $true.")] Boolean LoginPasswordPolicyEnforced;
[Write, Description("Specifies if the login is disabled. Default is $false.")] Boolean Disabled;
[Write, Description("Default database name.")] String DefaultDatabase;
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ConvertFrom-StringData @'
WrongLoginType = The login '{0}' has the login type '{1}', but expected it to have the login type '{2}'.
ExpectedDisabled = Expected the login '{0}' to be disabled, but it is enabled.
ExpectedEnabled = Expected the login '{0}' to be enabled, but it is disabled.
WrongDefaultDatabase = The login '{0}' has the default database '{1}', but expected it to have the default database '{2}'.
ExpectedLoginPasswordExpirationDisabled = The login '{0}' has the password expiration enabled, but expected it to be disabled.
ExpectedLoginPasswordExpirationEnabled = The login '{0}' has the password expiration disabled, but expected it to be enabled.
ExpectedLoginPasswordPolicyEnforcedDisabled = The login '{0}' has the password policy enforced enabled, but expected it to be disabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ try
$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $ConfigurationData.AllNodes.DscUser2Name
$resourceCurrentState.LoginType | Should -Be $ConfigurationData.AllNodes.DscUser2Type
$resourceCurrentState.DefaultDatabase | Should -Be $ConfigurationData.AllNodes.DefaultDbName
$resourceCurrentState.Disabled | Should -Be $false
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/MSFT_SqlServerLogin.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ else
ServerName = $env:COMPUTERNAME
InstanceName = 'DSCSQLTEST'

DefaultDbName = 'DefaultDb'

DscUser1Name = ('{0}\{1}' -f $env:COMPUTERNAME, 'DscUser1')
DscUser1Type = 'WindowsUser'

Expand Down Expand Up @@ -52,6 +54,7 @@ else
Configuration MSFT_SqlServerLogin_CreateDependencies_Config
{
Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0'
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
Expand Down Expand Up @@ -99,6 +102,18 @@ Configuration MSFT_SqlServerLogin_CreateDependencies_Config
'[User]CreateDscUser2'
)
}

SqlDatabase 'DefaultDb_Test'
{
Ensure = 'Present'
ServerName = $Node.ServerName
InstanceName = $Node.InstanceName
Name = $Node.DefaultDbName

PsDscRunAsCredential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force))
}
}
}

Expand Down Expand Up @@ -143,6 +158,7 @@ Configuration MSFT_SqlServerLogin_AddLoginDscUser2_Config
Ensure = 'Present'
Name = $Node.DscUser2Name
LoginType = $Node.DscUser2Type
DefaultDatabase = $Node.DefaultDbName

ServerName = $Node.ServerName
InstanceName = $Node.InstanceName
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Stubs/SMO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public class Login
public bool PasswordPolicyEnforced = false;
public bool PasswordExpirationEnabled = false;
public bool IsDisabled = false;
public string DefaultDatabase;

public string MockName;
public LoginType MockLoginType;
Expand Down

0 comments on commit 2c1b07a

Please sign in to comment.