1+ /* #info
2+
3+ # Autor
4+ Rodrigo Ribeiro Gomes
5+
6+ # Detalhes
7+ Cria users associado com logins no banco atual e adiciona no role db_owner.
8+
9+
10+ */
11+
12+
13+ SET NOCOUNT ON ;
14+
15+ IF OBJECT_ID (' tempdb..#LoginsAllowed' ) IS NOT NULL
16+ DROP TABLE #LoginsAllowed;
17+ CREATE TABLE #LoginsAllowed(LoginName sysname );
18+ INSERT INTO #LoginsAllowed VALUES (' NomeLogin' );
19+
20+ DECLARE
21+ @col_LoginName varchar (1000 )
22+ ,@userName sysname
23+ ,@LoginSID varbinary (100 )
24+ ,@UserSID varbinary (100 )
25+ ,@tsql nvarchar (4000 )
26+ ;
27+
28+ DECLARE curLoginsAllow CURSOR LOCAL FAST_FORWARD
29+ FOR
30+ SELECT * FROM #LoginsAllowed;
31+
32+
33+ OPEN curLoginsAllow;
34+
35+ FETCH NEXT FROM curLoginsAllow INTO @col_LoginName;
36+
37+ While @@FETCH_STATUS = 0
38+ BEGIN
39+
40+ -- > Check if login exists
41+ IF SUSER_ID (@col_LoginName) IS NULL
42+ BEGIN
43+ RAISERROR (' Login %s não existe. Favor criar e re-executar este script.' ,0 ,1 ,@col_LoginName);
44+ GOTO FETCH_NEXT;
45+ END
46+
47+ SET @LoginSID = SUSER_SID (@col_LoginName);
48+ SET @userName = NULL ;
49+ SET @UserSID = NULL ;
50+
51+ -- > Try get user name and possible mapped login sid...
52+ SELECT
53+ @userName = DP .name
54+ ,@UserSID = ISNULL (SP .sid ,DP .sid )
55+ FROM
56+ sys .database_principals DP
57+ LEFT JOIN
58+ sys .server_principals SP
59+ ON SP .sid = DP .sid
60+ WHERE
61+ DP .name = @col_LoginName
62+
63+ IF @userName IS NULL -- > If user name not found... Try get by login sid...
64+ SELECT
65+ @userName = DP .name
66+ FROM
67+ sys .database_principals DP
68+ WHERE
69+ DP .name = SUSER_SID (@col_LoginName)
70+
71+ -- Possible cenarios
72+ -- User dont exists
73+ -- User exists, different login
74+ -- User exists, no mapping
75+ -- User exists, but mapped with different name
76+ -- User exists
77+
78+ SET @tsql = NULL ;
79+ IF @userName IS NULL -- > User Dont Exists and Login dont have mapped users...
80+
81+ BEGIN
82+ SET @userName = @col_LoginName;
83+ SET @tsql = ' CREATE USER '+ QUOTENAME (@col_LoginName)+ ' FROM LOGIN '+ QUOTENAME (@col_LoginName);
84+ END
85+
86+ ELSE IF @userName IS NOT NULL AND @UserSID IS NULL -- User exists, no mapping
87+ BEGIN
88+ -- Remap user...
89+ SET @tsql = ' ALTER USER '+ QUOTENAME (@userName)+ ' WITH LOGIN = '+ QUOTENAME (@col_LoginName,' '' ' );
90+ END
91+
92+ ELSE IF @userName IS NOT NULL AND @UserSID != SUSER_SID (@col_LoginName) AND @UserSID IS NOT NULL -- > User exists, different login
93+ BEGIN
94+ -- Generate new user name...
95+ SET @userName = @col_LoginName+ CONVERT (varchar (25 ),ABS (CHECKSUM (NEWID ())))
96+ SET @tsql = ' CREATE USER '+ QUOTENAME (@userName)+ ' FROM LOGIN '+ QUOTENAME (@col_LoginName);
97+ END
98+
99+
100+
101+ -- All other cases, the user already exists and @userName must contain correct username
102+ IF @tsql IS NOT NULL
103+ BEGIN
104+ RAISERROR (' Executando (Login: %s): %s' ,0 ,1 ,@col_LoginName,@tsql);
105+ EXEC (@tsql);
106+ END
107+
108+ IF USER_ID (@userName) IS NOT NULL
109+ BEGIN
110+ RAISERROR (' Granting role membership: %s' ,0 ,1 ,@userName);
111+ EXEC sp_addrolemember ' db_owner' ,@userName;
112+ END
113+
114+ FETCH_NEXT:
115+ FETCH NEXT FROM curLoginsAllow INTO @col_LoginName;
116+ END
117+
118+ CLOSE curLoginsAllow;
119+ DEALLOCATE curLoginsAllow;
0 commit comments