CEM Revoke User
Overview
Business will make a quarterly request to remove user roles and inactivate users for CEM and/or QUAD.
Script 1: Revoke User
Copy USER_ACCESS (Column D) and EMP_EEID (Column E) to new excel file
- See template file: Excel Template
- Paste into new query window in SQL
- Highlight extra white space and do a
Ctrl + H
and replace all excel tabbed spaces with just an empty value.
Add
App
andUserNo
columns to#Users
temp table- the update script will replace the
UserRole
string with int value App
value will b used to indicate which application's UserRoles needs to be updatedUserNo
will be updated by the join withsam_SetupUser
table onEmployeeID
- NOTE: Could have added to the #Users temp table in step 1, but this seems quicker as you don't have to add extra values to the excel file.
- the update script will replace the
Delete user roles for appropriate UserNo and application type they should not have.
- Get UserNo that still have the role.
- Inactivate users that no longer have the specified role.
USE CORE_CEM
GO
SET QUOTED_IDENTIFIER ON
GO
-- 1
CREATE TABLE #Users (UserRole VARCHAR(60), EmployeeID VARCHAR(8))
INSERT INTO #Users SELECT '[CEM]:Advocate-CEM : .','121212'
INSERT INTO #Users SELECT '[CEM]:BackUp Quality Manager-QUAD : .','256789'
INSERT INTO #Users SELECT '[CEM]:Gate Keeper-QUAD : .','863321'
INSERT INTO #Users SELECT '[CEM]:Advocate-CEM : .','458382'
INSERT INTO #Users SELECT '[CEM]:Business Owner-QUAD : .','111234'
-- 2
ALTER TABLE #Users ADD App VARCHAR(4)
ALTER TABLE #Users ADD UserNo INT
UPDATE #Users SET UserRole = 2, App = 'CEM' WHERE UserRole = '[CEM]:Advocate-CEM : .'
UPDATE #Users SET UserRole = 3, App = 'QUAD' WHERE UserRole = '[CEM]:Gate Keeper-QUAD : .'
UPDATE #Users SET UserRole = 6, App = 'QUAD' WHERE UserRole = '[CEM]:BackUp Quality Manager-QUAD : .'
UPDATE #Users SET UserRole = 9, App = 'QUAD' WHERE UserRole = '[CEM]:Business Owner-QUAD : .'
UPDATE a SET UserNo = b.UserNo FROM #Users a
INNER JOIN smart.sam_SetupUser b on a.EmployeeID = b.EmployeeID
-- 3
UPDATE a SET Deleted = 1 FROM smart.sam_User_JobType a WHERE EXISTS (
SELECT EmployeeID FROM #Users WHERE UserRole = a.JobTypeNo AND UserNo = a.UserNo AND App = 'CEM')
UPDATE a SET Deleted = 1 FROM smart.p11_User_JobType a WHERE EXISTS (
SELECT EmployeeID FROM #Users WHERE UserRole = a.JobTypeNo AND UserNo = a.UserNo AND App = 'QUAD')
-- 4
SELECT DISTINCT UserNo INTO #UsersHavingRole FROM smart.sam_User_JobType WHERE UserNo IN (SELECT UserNo FROM #Users) AND Deleted = 0
UNION
SELECT DISTINCT UserNo FROM smart.p11_User_JobType WHERE UserNo IN (SELECT UserNo FROM #Users) AND Deleted = 0
-- 5
UPDATE smart.sam_SetupUser SET CurrentStatus = 0, LoginAllowed = 0
WHERE UserNo IN (SELECT UserNo FROM #Users) AND UserNo NOT IN (SELECT UserNo FROM #UsersHavingRole)