-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable-or-re-enable-all-non-admin-logins.sql
85 lines (77 loc) · 3.17 KB
/
disable-or-re-enable-all-non-admin-logins.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Disable non-admin user access to site; users without access prior to this script will be copied into a temporary table so that re-enabling users can filter out previously disabled logins.
*
* NOTE: This script defaults to rolling back changes.
* To commit changes, set @saveChanges = 1.
*/
declare @saveChanges bit; -- set @saveChanges = 1
declare @disabled bit;
-- Set to Disable
set @disabled = 1;
-- Uncomment below to re-enable
-- set @disabled = 0
declare @supportedVersions varchar(1000); select @supportedVersions='16.*'
-- Ensure the correct database version
BEGIN
declare @sep char(2); select @sep=', '
if not exists(select *
from dbo.SystemConfig
join (
select SUBSTRING(@supportedVersions, C.Value+1, CHARINDEX(@sep, @supportedVersions+@sep, C.Value+1)-C.Value-1) as Value
from dbo.Counter C
where C.Value < DataLength(@supportedVersions) and SUBSTRING(@sep+@supportedVersions, C.Value+1, DataLength(@sep)) = @sep
) Version on SystemConfig.Value like REPLACE(Version.Value, '*', '%') and SystemConfig.Name = 'Version'
) begin
raiserror('Only supported on version(s) %s',16,1, @supportedVersions)
goto DONE
end
END
declare @error int, @rowcount int
set nocount on; begin tran; save tran TX
-- Disable
if (@disabled = 1)
begin
-- Create table and store previously disabled logins
if not exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = N'temp_previously_disabled_logins')
begin
create table temp_previously_disabled_logins (
ID int
);
select @rowcount=@@ROWCOUNT, @error=@@ERROR
if @error<>0 goto ERR
raiserror('temp_previously_disabled_logins table created', 0, 1, @rowcount) with nowait
end
if not exists (select ID from temp_previously_disabled_logins)
begin
insert into temp_previously_disabled_logins select ID from Login where IsLoginDisabled = 1;
select @rowcount=@@ROWCOUNT, @error=@@ERROR
if @error<>0 goto ERR
raiserror('%d disabled users backed up to temp_previously_disabled_logins', 0, 1, @rowcount) with nowait
end
-- Set all non-admin logins to be disabled.
update Login set IsLoginDisabled = 1 where exists
(select l.ID from Login l join Member_Now on Member_Now.ID = l.ID where Login.ID = l.ID and Member_Now.DefaultRoleID <> 1);
select @rowcount=@@ROWCOUNT, @error=@@ERROR
if @error<>0 goto ERR
raiserror('%d users disabled', 0, 1, @rowcount) with nowait
end
-- Re-enable
if (@disabled = 0)
begin
update Login set IsLoginDisabled = 0 where exists
(select l.ID from Login l join Member_Now on Member_Now.ID = l.ID where Login.ID = l.ID and Member_Now.DefaultRoleID <> 1)
and not exists
(select ID from temp_previously_disabled_logins t where t.ID = Login.ID);
select @rowcount=@@ROWCOUNT, @error=@@ERROR
if @error<>0 goto ERR
raiserror('%d users re-enabled', 0, 1, @rowcount) with nowait
drop table temp_previously_disabled_logins;
select @rowcount=@@ROWCOUNT, @error=@@ERROR
if @error<>0 goto ERR
raiserror('temp_previously_disabled_logins table dropped', 0, 1, @rowcount) with nowait
end
if (@saveChanges = 1) begin raiserror('Committing changes', 0, 254); goto OK end
raiserror('To commit changes, set @saveChanges=1',16,254)
ERR: raiserror('Rolling back changes', 0, 255); rollback tran TX
OK: commit
DONE: