-
Notifications
You must be signed in to change notification settings - Fork 21
FM1901 Add delete user capabilities #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,38 @@ | ||
## | ||
# == Define Resource Type: sqlserver::user | ||
# | ||
# === Requirement/Dependencies: | ||
# | ||
# Requires defined type {sqlserver::config} in order to execute against the SQL Server instance | ||
# | ||
# === Examples | ||
# | ||
# sqlserver::user{'myUser': | ||
# database => 'loggingDatabase', | ||
# login => 'myUser', | ||
# } | ||
# | ||
# === Parameters | ||
# [user] | ||
# The username you want to manage, defaults to the title | ||
# | ||
# [database] | ||
# The database you want the user to be created as | ||
# | ||
# [ensure] | ||
# Ensure present or absent | ||
# | ||
# [default_schema] | ||
# SQL schema you would like to default to, typically 'dbo' | ||
# | ||
# [instance] | ||
# The named instance you want to manage against | ||
# | ||
# [login] | ||
# The login to associate the user with, by default SQL Server will assume user and login match if left empty | ||
# | ||
# [password] | ||
# The password for the user, can only be used when the database is a contained database. | ||
# | ||
## | ||
define sqlserver::user ( | ||
|
@@ -11,20 +43,19 @@ | |
$instance = 'MSSQLSERVER', | ||
$login = undef, | ||
$password = undef, | ||
$force_delete = false, | ||
) | ||
{ | ||
sqlserver_validate_instance_name($instance) | ||
|
||
$is_windows_user = sqlserver_is_domain_or_local_user($login) | ||
|
||
if $password { | ||
validate_re($password, '^.{1,128}$', 'Password must be equal or less than 128 characters') | ||
sqlserver_validate_range($password, 1, 128, 'Password must be equal or less than 128 characters') | ||
if $is_windows_user and $login != undef{ | ||
fail('Can not provide password when using a Windows Login') | ||
} | ||
} | ||
validate_re($database, '^.{1,128}$','Database name must be between 1 and 128 characters') | ||
sqlserver_validate_range($database, 1, 128, 'Database name must be between 1 and 128 characters') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
$create_delete = $ensure ? { | ||
present => 'create', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ | |
let(:additional_params) { {:user => 'myMachineName/myUser'} } | ||
let(:sqlserver_tsql_title) { 'user-MSSQLSERVER-myDatabase-myMachineName/myUser' } | ||
let(:should_contain_command) { [ | ||
"USE [myDatabase]", | ||
"USE [myDatabase];", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
'CREATE USER [myMachineName/myUser]' | ||
] } | ||
it_should_behave_like 'sqlserver_tsql command' | ||
|
@@ -93,9 +93,24 @@ | |
] } | ||
it_should_behave_like 'sqlserver_tsql command' | ||
end | ||
|
||
describe 'have dependency on Sqlserver::Config[MSSQLSERVER]' do | ||
it 'should require ::config' do | ||
should contain_sqlserver_tsql(sqlserver_tsql_title).with_require('Sqlserver::Config[MSSQLSERVER]') | ||
end | ||
end | ||
|
||
describe 'when ensure => absent' do | ||
let(:additional_params) { {:ensure => 'absent'} } | ||
let(:sqlserver_contain_command) { [ | ||
'USE [loggingDb];\nDROP [loggingUser]', | ||
"\nIF EXISTS(SELECT name FROM sys.database_principals WHERE name = 'loggingUser')\n THROW", | ||
] } | ||
let(:sqlserver_contain_onlyif) { [ | ||
"\nIF EXISTS(SELECT name FROM sys.database_principals WHERE type in ('U','S','G') AND name = 'loggingUser')\n", | ||
] } | ||
it_should_behave_like 'sqlserver_tsql command' | ||
it_should_behave_like 'sqlserver_tsql onlyif' | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
-- Need to use exec instead of use statement as this will trigger try catch | ||
USE [<%= @database %>]; | ||
<% if @password %> | ||
IF EXISTS(select containment from sys.databases WHERE name = '<%= @database %>' AND containment = 0) | ||
THROW 51000, 'Database must be contained in order to use passwords', 0 | ||
THROW 51000, 'Database must be contained in order to use passwords', 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the 0 versus the 10 signify? |
||
<% end %> | ||
CREATE USER [<%= @user %>] | ||
<% if @login -%> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
USE [<%= @database %>]; | ||
DROP USER [<%= @user %>]; | ||
IF EXISTS(SELECT name FROM sys.database_principals WHERE name = '<%= @user %>') | ||
THROW 51000, 'Failed to drop user <%= @user %>', 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah the level 16 errors... good times.