Portal Administration Guide : Customize Xinet Portal : Code Example: Filtering what users see using Search Engine stored procedures

Code Example: Filtering what users see using Search Engine stored procedures
Goal: Hide the display of all files not relevant to graphic design users.
To achieve this goal, do the following:
1.
2.
3.
Set their Primary Group to design_group.
4.
5.
6.
Add the Audience field to the design user’s metadata Template and Permset.
7.
Add the Audience field to an administrator’s metadata Template and Permset.
8.
9.
For some folders and files, set the Audience keyword value to Design.
10.
11.
12.
Replace every instance of 137 with the correct keyword ID number for your new Audience keyword. (The lines where these appear are set in bold face.)
13.
14.
15.
Verify that this user only sees files that are set to Design or Public.
How it works
The code is comprised of two Xinet Portal stored procedures, that when used together filter Search results directly within the Search Engine, namely sp_RunSearchPostFilter(), which filters Browse and Search results and sp_NavigatorPostFilter(), which filters Navigator results. This method works better with Portal 4 (and later) than the PHP method described beginning on page 758 because it is faster: filtering takes place within the Search Engine, and properly updates Search and Browse paging counts as well as the Navigator’s file- and directory-item counts.
In code example, shown below, the first procedure, sp_RunSearchPostFilter, employs an IF statement that includes g.GroupName='design_group' which tells the Portal Search Engine to apply the filter if the user’s Primary Group is design_group. If the group matches, the DELETE statement filters the results. You can change this specific part of the DELETE statement to suit your needs:
IFNULL(NOT (k.Field137='Design' OR k.Field137='Public'),1);
This code means whenever Field137 does not have a value of Design or Public, or if it is null, that file will be filtered out.
The second procedure, sp_NavigatorPostFilter, provides these three functions:
Like the first procedure, the IF statement checks for design_group, and filters items. It then updates the file and dir counts with two UPDATE statements. You can change this specific part of each UPDATE statement to suit your needs:
(IFNULL(k.Field137='Design' OR k.Field137='Public',0))
Remember to change both UPDATE statements, or your navigator counts will be incorrect.
Code Sample
DROP PROCEDURE IF EXISTS sp_RunSearchPostFilter;
delimiter //
CREATE PROCEDURE sp_RunSearchPostFilter (vResultTable VARCHAR(64))
BEGIN
IF (SELECT COUNT(*) FROM groupuser u, groups g WHERE
u.GroupID=g.GroupID AND g.GroupName='design_group'
AND u.UserID=@vUserID)
THEN
SET @prep=CONCAT("DELETE r FROM ", vResultTable, " r
LEFT JOIN file f ON r.FileID=f.FileID
LEFT OUTER JOIN keyword1 k ON r.FileID=k.FileID WHERE
IFNULL(NOT (k.Field137='Design' OR k.Field137='Public'),1);");
PREPARE sprep FROM @prep;
EXECUTE sprep;
END IF;
END//
delimiter ;
 
DROP PROCEDURE IF EXISTS sp_NavigatorPostFilter;
delimiter //
CREATE PROCEDURE sp_NavigatorPostFilter (vResultTable VARCHAR(64))
BEGIN
IF (SELECT COUNT(*) FROM groupuser u, groups g WHERE
u.GroupID=g.GroupID AND g.GroupName='design_group'
AND u.UserID=@vUserID)
THEN
SET @prep=CONCAT("DELETE r FROM ", vResultTable, " r
LEFT JOIN file f ON r.FileID=f.FileID
LEFT OUTER JOIN keyword1 k ON r.FileID=k.FileID WHERE
IFNULL(NOT (k.Field137='Design' OR k.Field137='Public'),1);");
PREPARE sprep FROM @prep;
EXECUTE sprep;
SET @prep=CONCAT("UPDATE ", vResultTable," t
SET t.FileItems=(SELECT COUNT(*) FROM file f
JOIN path p ON (f.PathID=p.PathID)
LEFT JOIN keyword1 k ON (f.FileID=k.FileID)
WHERE p.FileID=t.FileID AND f.Dir=0 AND (f.Online OR IF
(t.ShowArch,f.Archived,0))AND (!(f.finderflags & 16384))
AND
(IFNULL(k.Field137='Design' OR k.Field137='Public',0)) );");
PREPARE sprep FROM @prep;
EXECUTE sprep;
SET @prep=CONCAT("UPDATE ", vResultTable," t
SET t.DirItems=(SELECT COUNT(*) FROM file f
JOIN path p ON (f.PathID=p.PathID)
LEFT JOIN keyword1 k ON (f.FileID=k.FileID) WHERE
p.FileID=t.FileID AND f.Dir=1 AND (f.Online
OR IF(t.ShowArch,f.Archived,0))
AND (!(f.finderflags & 16384))
AND
(IFNULL(k.Field137='Design' OR k.Field137='Public',0)) );");
PREPARE sprep FROM @prep;
EXECUTE sprep;
END IF;
END//
delimiter ;