UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.5.0.0'
GO

ALTER TABLE [dbo].[TBDD_MODULES] ADD WMSESSION_STARTSTOP_STARTUP BIT NOT NULL DEFAULT 0
GO
/*Run following command to set the WM-Startup on Appstart to true*/
--UPDATE TBDD_MODULES SET WMSESSION_STARTSTOP_STARTUP = 1 WHERE SHORT_NAME = 'PM'
--GO
CREATE FUNCTION [dbo].[FNDD_GET_USER_FOR_MODULE] (@pMODULE_SHORT_NAME VARCHAR(10),@pCLIENTID INTEGER)
--DECLARE @pMODULE_SHORT_NAME VARCHAR(10),@pCLIENTID INTEGER
--SET @pMODULE_SHORT_NAME = 'PM'
--SET @pCLIENTID = 1
RETURNS
--DECLARE 
@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 = 'GLOBIX'
		BEGIN
			SET @USER_GROUPNAME = 'GLOBIX_USER'
			SET @ADMIN_GROUPNAME = 'GLOBIX_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


