COMPILE FUNCTION SECURITY_CHECK( Table, RightToCheck )
#pragma precomp SRP_PreCompiler
DECLARE FUNCTION MSG, FIELDCOUNT

$INSERT LOGICAL
$INSERT SECURITY_RIGHTS_EQU
$INSERT LSL_USERS_EQU
$INSERT LSL_COMMON
$INSERT SEC_GROUPS_EQU

DECLARE FUNCTION Memberof

Valid = false$
Rights = 'RWDEP'
ValidRight = Rights[RightToCheck,1]

a = SecTables@
a1 = SecRights@

if MemberOf(@USER4, 'OI_ADMIN') then
   Valid = true$
end else
    if SecTables@ and SecRights@ then
        GOSUB CheckValid
    end else
      *Void = msg( '', 'Building rights' )
        * build it is the first time
		Valid = false$
		UserRec = xlate( 'LSL_USERS', @user4, '', 'X' )
		SecTables@ = UserRec<lsl_users_tables$>
		SecRights@ = UserRec<lsl_users_rights$>
		Groups     = UserRec<lsl_users_groups$>
		Gcnt = fieldcount( Groups, @vm )
		*debug
		for i = 1 to Gcnt
		    ThisGroupRec = xlate( 'SEC_GROUPS', Groups<1,i>, '', 'X' )
		    STables = ThisGroupRec<sec_groups_tables$>
		    SRights = ThisGroupRec<sec_groups_rights$>
		    Tcnt = fieldcount( STables, @vm )
		    for j = 1 to Tcnt
		        ThisGTable   = STables<1,j>
		        TheseGRights = SRights<1,j>
		        locate ThisGTable in SecTables@ using @vm setting Tpos then
		            CurRights = SecRights@<1,Tpos>
		            if CurRights = TheseGRights else
			            RLen = len( TheseGRights )
			            for k = 1 to RLen ;* hard code 5 for num of different rights
			                CurGRight = TheseGRights[k,1]
			                if CurGRight <> ' ' then
			                    CurRights[k,1] = CurGRight
			                end
			            next k
			            SecRights@<1,Tpos> = CurRights
			        end
		        end else
		            SecTables@ = insert( SecTables@, 1, Tpos, 0, ThisGTable )
		            SecRights@ = insert( SecRights@, 1, Tpos, 0, TheseGRights )      
		        end
		    next j
		next i
		gosub CheckValid
    end
end
return Valid
*============================================================================*
CheckValid:
	Locate Table in SecTables@ using @vm setting Fpos then
	   AssignedRights = SecRights@<1,Fpos>
	   if AssignedRights[RightToCheck,1] = ValidRight then
	      Valid = true$
	   end
	end
return

*============================================================================*
 

