Skip to content

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:

yaml
.....
  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.ID

Suppose 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):

yaml
.....
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:

yaml
.....
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:

NodeApplies 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/FilterExpressionthe 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.

Released under Apache License, Version 2.0.