OldSkoolSkater
MIS
Hi All
Running Sql 2008 R2 Standard.
I have a SQL auth user account that is a member of the sysadmin server role and that allows me to create logins/users manually as you would expect.
The problem I am having is when I try to automate this process and run a stored procedure to do the same this - the SP fails with the following error:
CREATE LOGIN [test1] WITH PASSWORD = N'wibble',DEFAULT_DATABASE=[Erpmap], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;
Msg 15247, Level 16, State 1, Line 1
User does not have permission to perform this action.
CREATE USER [test1] FOR LOGIN [test1];
Msg 15007, Level 16, State 1, Line 1
'test1' is not a valid login or you do not have permission.
EXEC sp_addrolemember N'VFF_Permissions', N'test1';
Msg 15410, Level 11, State 1, Procedure sp_addrolemember, Line 75
User or role 'test1' does not exist in this database.
The SP that my sysadmin user is running is as follows:
USE [SM_Test]
GO
/****** Object: StoredProcedure [dbo].[pr_CallBoostedSecurityProcess] Script Date: 07/11/2013 10:40:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[pr_CallBoostedSecurityProcess]( @name varchar(20), @password varchar(10) )
WITH EXECUTE AS 'SProcNinja'
--this is a sysadmin login and also database user
--(only database users can be used with EXECUTE AS clause in a proc),
--which has been disabled, but is used for elevated permissions.
AS
BEGIN
--CREATE a login, if required.
DECLARE @cmd varchar(max)
IF NOT EXISTS(SELECT 1 FROM master.sys.server_principals WHERE name = @name)
BEGIN
SELECT @cmd = 'CREATE LOGIN ' + QUOTENAME(@name)
+ ' WITH PASSWORD = N'''
+ @password
+ ''',DEFAULT_DATABASE=[Erpmap], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;'
PRINT @cmd
EXECUTE(@cmd)
END --IF
--Create Our role with specific permissions, if required.
IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE type_desc = 'DATABASE_ROLE' and name = 'VFF_Permissions')
BEGIN
SELECT @cmd = 'CREATE ROLE VFF_Permissions;'
PRINT @cmd
EXECUTE(@cmd)
SELECT @cmd ='EXEC sp_addrolemember N''db_datareader'', N''VFF_Permissions'';'
PRINT @cmd
EXECUTE(@cmd)
SELECT @cmd ='EXEC sp_addrolemember N''db_datawriter'', N''VFF_Permissions'';'
PRINT @cmd
EXECUTE(@cmd)
END --IF
--Create our user, if required
IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE type_desc = 'SQL_USER' and name = @name)
BEGIN
SELECT @cmd = 'CREATE USER ' + QUOTENAME(@name) + ' FOR LOGIN ' + QUOTENAME(@name) + ';'
PRINT @cmd
EXECUTE(@cmd)
--assign the role to our user
SELECT @cmd ='EXEC sp_addrolemember N''VFF_Permissions'', N''' + @name + ''';'
PRINT @cmd
EXECUTE(@cmd)
END --IF
END --PROC
Any ideas why this might be happening? I'm well confused!
Cheers
Oldskool - Living the dream in the UK
Running Sql 2008 R2 Standard.
I have a SQL auth user account that is a member of the sysadmin server role and that allows me to create logins/users manually as you would expect.
The problem I am having is when I try to automate this process and run a stored procedure to do the same this - the SP fails with the following error:
CREATE LOGIN [test1] WITH PASSWORD = N'wibble',DEFAULT_DATABASE=[Erpmap], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;
Msg 15247, Level 16, State 1, Line 1
User does not have permission to perform this action.
CREATE USER [test1] FOR LOGIN [test1];
Msg 15007, Level 16, State 1, Line 1
'test1' is not a valid login or you do not have permission.
EXEC sp_addrolemember N'VFF_Permissions', N'test1';
Msg 15410, Level 11, State 1, Procedure sp_addrolemember, Line 75
User or role 'test1' does not exist in this database.
The SP that my sysadmin user is running is as follows:
USE [SM_Test]
GO
/****** Object: StoredProcedure [dbo].[pr_CallBoostedSecurityProcess] Script Date: 07/11/2013 10:40:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[pr_CallBoostedSecurityProcess]( @name varchar(20), @password varchar(10) )
WITH EXECUTE AS 'SProcNinja'
--this is a sysadmin login and also database user
--(only database users can be used with EXECUTE AS clause in a proc),
--which has been disabled, but is used for elevated permissions.
AS
BEGIN
--CREATE a login, if required.
DECLARE @cmd varchar(max)
IF NOT EXISTS(SELECT 1 FROM master.sys.server_principals WHERE name = @name)
BEGIN
SELECT @cmd = 'CREATE LOGIN ' + QUOTENAME(@name)
+ ' WITH PASSWORD = N'''
+ @password
+ ''',DEFAULT_DATABASE=[Erpmap], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;'
PRINT @cmd
EXECUTE(@cmd)
END --IF
--Create Our role with specific permissions, if required.
IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE type_desc = 'DATABASE_ROLE' and name = 'VFF_Permissions')
BEGIN
SELECT @cmd = 'CREATE ROLE VFF_Permissions;'
PRINT @cmd
EXECUTE(@cmd)
SELECT @cmd ='EXEC sp_addrolemember N''db_datareader'', N''VFF_Permissions'';'
PRINT @cmd
EXECUTE(@cmd)
SELECT @cmd ='EXEC sp_addrolemember N''db_datawriter'', N''VFF_Permissions'';'
PRINT @cmd
EXECUTE(@cmd)
END --IF
--Create our user, if required
IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE type_desc = 'SQL_USER' and name = @name)
BEGIN
SELECT @cmd = 'CREATE USER ' + QUOTENAME(@name) + ' FOR LOGIN ' + QUOTENAME(@name) + ';'
PRINT @cmd
EXECUTE(@cmd)
--assign the role to our user
SELECT @cmd ='EXEC sp_addrolemember N''VFF_Permissions'', N''' + @name + ''';'
PRINT @cmd
EXECUTE(@cmd)
END --IF
END --PROC
Any ideas why this might be happening? I'm well confused!
Cheers
Oldskool - Living the dream in the UK