USE [DD_ECM]

UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.7.7';
GO
UPDATE TBDD_MODULES SET NAME = 'fileFLOW', SHORT_NAME = 'fileFLOW' WHERE SHORT_NAME = 'GLOBIX';
GO
--IN FNDD_LOGIN_USER_MODULE
--'GLOBIX' mit 'fileFLOW' ersetzen
--REPLACE 
--SET @USER_GROUPNAME = 'fileFLOW_USER'
--SET @ADMIN_GROUPNAME = 'fileFLOW_ADMINS'

--	20.09.2024	MS	@MODULE_SHORT eingesetzt
CREATE OR ALTER PROCEDURE [dbo].[PRDD_CHECK_REL_DOCTYPE_MODULE] (@MODULE_SHORT VARCHAR(20),@DOCTYPE_ID INTEGER)
AS
DECLARE @MODULEID INTEGER
SELECT @MODULEID = GUID FROM TBDD_MODULES WHERE SHORT_NAME = @MODULE_SHORT

If not exists(select guid from TBDD_DOKUMENTART_MODULE where DOKART_ID = @DOCTYPE_ID and MODULE_ID = @MODULEID)
	INSERT INTO TBDD_DOKUMENTART_MODULE (DOKART_ID,MODULE_ID, ADDED_WHO) VALUES (@DOCTYPE_ID,@MODULEID, 'PRDD_CHECK_REL_DOCTYPE_MODULE')

GO
UPDATE TBDD_GROUPS SET NAME = 'fileFLOW_USER',CHANGED_WHO = 'DD Update 1.9.7.7' WHERE NAME = 'GLOBIX_USER';
UPDATE TBDD_GROUPS SET NAME = 'fileFLOW_ADMINS',CHANGED_WHO = 'DD Update 1.9.7.7' WHERE NAME = 'GLOBIX_ADMINS';

USE [DD_ECM]
GO
--	20.09.2024	MS	fileFLOW as Module
ALTER FUNCTION [dbo].[FNDD_GET_USER_FOR_MODULE] (@pMODULE_SHORT_NAME VARCHAR(10),@pCLIENTID INTEGER)
RETURNS
@Table TABLE 
    (SequentialOrder	INT IDENTITY(1, 1),
	USER_ID				INTEGER, 
	USER_NAME			VARCHAR(50),
	USER_PRENAME		VARCHAR(50), 
	USER_SURNAME		VARCHAR(50), 
	USER_SHORTNAME		VARCHAR(50),
	USER_EMAIL			VARCHAR(100), 
	USER_LANGUAGE		VARCHAR(10),
	USER_RIGHT_FILE_DEL BIT,
	MODULE_ACCESS		BIT,
	IS_ADMIN			BIT, 
	USER_COMMENT		VARCHAR(500),
	COMMENT				VARCHAR(500))
AS
BEGIN
/*
This function checks all user-relevant relations an
*/
	DECLARE
		@RESULT					VARCHAR(500),
		@USER_ID				INTEGER,
		@USER_NAME				VARCHAR(50),
		@USER_PRENAME			VARCHAR(50),
		@USER_SURNAME			VARCHAR(50),
		@USER_SHORTNAME			VARCHAR(50),
		@USER_EMAIL				VARCHAR(50),
		@USER_LANGUAGE			VARCHAR(10),
		@IS_IN_MODULE			BIT,
		@IS_ADMIN				BIT,
		@USER_RIGHT_FILE_DEL	BIT,
		@MODULE_NAME			VARCHAR(100),
		@MODULE_GUID			INTEGER,
		@USER_GROUPNAME			VARCHAR(100),
		@ADMIN_GROUPNAME		VARCHAR(100),
		@USER_COMMENT			VARCHAR(500)

	IF @pMODULE_SHORT_NAME = 'PM'
		BEGIN
			SET @USER_GROUPNAME = 'PM_USER'
			SET @ADMIN_GROUPNAME = 'PM_ADMINS'
		END
	ELSE IF @pMODULE_SHORT_NAME = 'CW'
		BEGIN
			SET @USER_GROUPNAME = 'CW_USER'
			SET @ADMIN_GROUPNAME = 'CW_ADMINS'
		END
	ELSE IF @pMODULE_SHORT_NAME = 'ADDI'
		BEGIN
			SET @USER_GROUPNAME = 'ADDI_USER'
			SET @ADMIN_GROUPNAME = 'ADDI_ADMINS'
		END
	ELSE IF @pMODULE_SHORT_NAME = 'fileFLOW'
		BEGIN
			SET @USER_GROUPNAME = 'fileFLOW_USER'
			SET @ADMIN_GROUPNAME = 'fileFLOW_ADMINS'
		END
	ELSE IF @pMODULE_SHORT_NAME = 'UM'
		BEGIN
			SET @USER_GROUPNAME = 'XXXX'
			SET @ADMIN_GROUPNAME = 'UM_ADMINS'
		END
	SELECT @MODULE_GUID = GUID, @MODULE_NAME = NAME FROM TBDD_MODULES WHERE SHORT_NAME = @pMODULE_SHORT_NAME
	
	DECLARE cursor_user CURSOR FOR 
		select GUID, ISNULL(PRENAME,'no prename'), ISNULL(NAME,'no name'), ISNULL(SHORTNAME,'no shortname'), ISNULL(EMAIL,'no email'), LANGUAGE,USERNAME,COMMENT from TBDD_USER
	OPEN cursor_user 
    FETCH NEXT FROM cursor_user INTO @USER_ID,@USER_PRENAME,@USER_SURNAME,@USER_SHORTNAME,@USER_EMAIL,@USER_LANGUAGE,@USER_NAME,@USER_COMMENT
    WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @IS_ADMIN = 0
		SET @IS_IN_MODULE = 0
		SET @RESULT = ''
		--CHECK USER IS IN MODULE_USER_GROUP
		IF EXISTS(select T1.* from TBDD_GROUPS T, TBDD_GROUPS_USER T1 WHERE T.GUID = T1.GROUP_ID AND T1.USER_ID = @USER_ID AND UPPER(T.NAME) = @USER_GROUPNAME)
			BEGIN
				SET @IS_IN_MODULE = 1
				SET @RESULT = 'User is part of Group (' + @USER_GROUPNAME + ')'
				IF EXISTS(select T.* from TBDD_USER_MODULES T WHERE T.USER_ID = @USER_ID AND T.MODULE_ID = @MODULE_GUID)
					BEGIN
						SET @RESULT = @RESULT + '| User is also configured in User-Modules (Unnecessary)'
						select @IS_ADMIN = T.IS_ADMIN,@USER_RIGHT_FILE_DEL = T.RIGHT1 from TBDD_USER_MODULES T WHERE T.USER_ID = @USER_ID AND T.MODULE_ID = @MODULE_GUID
					
					END
			END
		ELSE
			BEGIN
				SET @RESULT = 'User <' + CONVERT(VARCHAR(10),@USER_ID) + ' - ' +  @USER_NAME + '> not part of group (' + @USER_GROUPNAME + ')'
				IF EXISTS(select T.* from TBDD_USER_MODULES T WHERE T.USER_ID = @USER_ID AND T.MODULE_ID = @MODULE_GUID)
					BEGIN
						SET @RESULT = @RESULT + '| BUT User configured in User-Modules'
						select @IS_ADMIN = T.IS_ADMIN,@USER_RIGHT_FILE_DEL = T.RIGHT1 from TBDD_USER_MODULES T WHERE T.USER_ID = @USER_ID AND T.MODULE_ID = @MODULE_GUID
						SET @IS_IN_MODULE = 1
					END
			END

		IF EXISTS(select T1.* from TBDD_GROUPS T, TBDD_GROUPS_USER T1 WHERE T.GUID = T1.GROUP_ID AND T1.USER_ID = @USER_ID AND UPPER(T.NAME) = @ADMIN_GROUPNAME)
			BEGIN
				SET @RESULT = @RESULT + '| User is part of group (' + @ADMIN_GROUPNAME + ')'
				SET @IS_ADMIN = 1
				SET @IS_IN_MODULE = 1
			END
		--PRINT @RESULT
		IF @IS_IN_MODULE = 1
			INSERT INTO @Table (USER_ID,USER_NAME, USER_PRENAME, USER_SURNAME,USER_SHORTNAME,USER_EMAIL,USER_LANGUAGE, USER_RIGHT_FILE_DEL, MODULE_ACCESS,IS_ADMIN,USER_COMMENT,COMMENT)
			VALUES (@USER_ID,@USER_NAME,@USER_PRENAME,@USER_SURNAME,@USER_SHORTNAME,@USER_EMAIL,@USER_LANGUAGE,@USER_RIGHT_FILE_DEL,@IS_IN_MODULE,@IS_ADMIN,@USER_COMMENT,@RESULT)
		FETCH NEXT FROM cursor_user INTO @USER_ID,@USER_PRENAME,@USER_SURNAME,@USER_SHORTNAME,@USER_EMAIL,@USER_LANGUAGE,@USER_NAME,@USER_COMMENT
	END
	CLOSE cursor_user
	DEALLOCATE cursor_user
RETURN
END
GO