Skip to content

Commit

Permalink
SqlDatabasePermission: Supports roles and application roles (#1558)
Browse files Browse the repository at this point in the history
- SqlDatabasePermission
  - Now possible to change permissions for database user-defined roles
    (e.g. public) and database application roles (issue #1498).
  • Loading branch information
johlju authored May 28, 2020
1 parent c8a7076 commit 3a449f5
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 163 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ in a future release.
- Added the properties `NpEnabled` and `TcpEnabled` ([issue #1161](https://github.com/dsccommunity/SqlServerDsc/issues/1161)).
- Added the property `UseEnglish` ([issue #1473](https://github.com/dsccommunity/SqlServerDsc/issues/1473)).
- SqlServerReplication
- Add integration tests ([issue #755](https://github.com/dsccommunity/SqlServerDsc/issues/755)
- Add integration tests ([issue #755](https://github.com/dsccommunity/SqlServerDsc/issues/755).
- SqlDatabase
- The property `OwnerName` was added.
- SqlDatabasePermission
- Now possible to change permissions for database user-defined roles
(e.g. public) and database application roles ([issue #1498](https://github.com/dsccommunity/SqlServerDsc/issues/1498).
- SqlServerDsc.Common
- The helper function `Restart-SqlService` was improved to handle Failover
Clusters better. Now the SQL Server service will only be taken offline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,19 @@ function Set-TargetResource

if ($sqlDatabaseObject = $sqlServerObject.Databases[$DatabaseName])
{
if ($sqlDatabaseObject.Users[$Name])
$nameExist = $sqlDatabaseObject.Users[$Name] `
-or (
<#
Skip fixed roles like db_datareader as it is not possible to set
permissions on those.
#>
$sqlDatabaseObject.Roles | Where-Object -FilterScript {
-not $_.IsFixedRole -and $_.Name -eq $Name
}
) `
-or $sqlDatabaseObject.ApplicationRoles[$Name]

if ($nameExist)
{
try
{
Expand Down Expand Up @@ -273,7 +285,7 @@ function Set-TargetResource
}
else
{
$errorMessage = $script:localizedData.LoginIsNotUser -f $Name, $DatabaseName
$errorMessage = $script:localizedData.NameIsMissing -f $Name, $DatabaseName

New-InvalidOperationException -Message $errorMessage
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ConvertFrom-StringData @'
GetDatabasePermission = Get permissions for the user '{0}' in the database '{1}' on the instance '{2}'.
DatabaseNotFound = The database '{0}' does not exist.
ChangePermissionForUser = Changing the permission for the user '{0}' in the database '{1}' on the instance '{2}'.
LoginIsNotUser = The login '{0}' is not a user in the database '{1}'.
NameIsMissing = The name '{0}' is neither a database user, database role (user-defined), or database application role in the database '{1}'.
AddPermission = {0} the permissions '{1}' to the database '{2}'.
DropPermission = Revoking the {0} permissions '{1}' from the database '{2}'.
FailedToSetPermissionDatabase = Failed to set the permissions for the login '{0}' in the database '{1}'.
Expand Down
66 changes: 66 additions & 0 deletions tests/Integration/DSC_SqlDatabasePermission.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,69 @@ Configuration DSC_SqlDatabasePermission_RemoveGrantGuest_Config
}
}
}

<#
.SYNOPSIS
Grant rights in a database for the user-defined role 'public'.
.NOTES
Regression test for issue #1498.
#>
Configuration DSC_SqlDatabasePermission_GrantPublic_Config
{
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
SqlDatabasePermission 'Integration_Test'
{
Ensure = 'Present'
Name = 'public'
DatabaseName = $Node.DatabaseName
PermissionState = 'Grant'
Permissions = @(
'Select'
)

ServerName = $Node.ServerName
InstanceName = $Node.InstanceName

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

<#
.SYNOPSIS
Remove the granted rights in a database for the user-defined role 'public'.
.NOTES
Regression test for issue #1498.
#>
Configuration DSC_SqlDatabasePermission_RemoveGrantPublic_Config
{
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
SqlDatabasePermission 'Integration_Test'
{
Ensure = 'Absent'
Name = 'public'
DatabaseName = $Node.DatabaseName
PermissionState = 'Grant'
Permissions = @(
'Select'
)

ServerName = $Node.ServerName
InstanceName = $Node.InstanceName

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

0 comments on commit 3a449f5

Please sign in to comment.