How to filter data in a view according to connected user###
The feature is implemented by the DefaultFilter node at model or view level.
For example, suppose that in your config.yaml file the login ReadUserCommandText is implemented as in the following:
.....
ReadUserCommandText: |
select
A.ID AS USER_NAME, a.PASSWD AS PASSWORD_HASH,
a.SYSYEM as SYSTEM, E.ID AS EMPLOYEEID, E.DX AS EMPLOYEEDX
from APPUSER A
LEFT JOIN employee E ON E.APPUSERCLASS = 'TISUser' AND E.APPUSERID = A.IDSuppose in a model or view you want to show only records of current user.
just write in the model or in the view the following code (implemented for a view):
.....
MainTable:
.....
DefaultFilter: |
(EMPLOYEEID in (select ID FROM EMPLOYEE where APPUSERID = '%Auth:USER_NAME%'))where %Auth:USER_NAME% is a macro expanded by Kittox
a more complex example: you want to show all records in case current user is a system user. Just use the %Auth:SYSTEM% macro as in the following:
.....
MainTable:
.....
DefaultFilter: |
('%Auth:SYSTEM%' = 1 or
(EMPLOYEEID in (select ID FROM EMPLOYEE where APPUSERID = '%Auth:USER_NAME%')))DefaultFilter vs Controller/FilterExpression
Both restrict the records a view works on, but only one of them is a guarantee:
| Node | Applies to |
|---|---|
DefaultFilter (model or view table) | every load of that table — grid, form, lookup, export, detail table, and the key-based endpoints. It is part of the where clause the SQL builder produces |
Controller/FilterExpression | the record a standalone form loads, plus (since 4.0.15) the key-based endpoints of that view |
So when the point is isolation — this user may only ever see their own rows — put the predicate in DefaultFilter, at model level if it must hold for every view of that model. Use FilterExpression to pick the single record of a standalone form.
Fixed in 4.0.15
Until 4.0.15 FilterExpression was applied only when the standalone form loaded its record: a request that supplied a record key to kx/view/<V>/form, /save, /delete or /blob was answered from the whole table, bypassing the expression. It is now combined with the view filter on those endpoints too. DefaultFilter was never affected.
