Loading...
Searching...
No Matches
mpe_columnlevelsecurity.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Filters a table with CLS rules
4 @details Implements CLS as per the rules described here:
5
6 https://docs.datacontroller.io/column-level-security/
7
8 Usage:
9
10 %mpe_columnlevelsecurity(TGTLIB,TGTDS,work.inds
11 ,mode=VIEW
12 ,clsds=dc.mpe_cls
13 ,groupds=work.groups
14 ,outds=work.final
15 ,outmeta=work.cls_rules
16 )
17
18
19 @param [in] tgtlib The libref of the target table
20 @param [in] tgtds The dataset reference of the target table
21 @param [in] inds A two-level (lib.ds) reference to the WORK table to which to
22 apply the column filter rules
23 @param [in] mode= (VIEW) Either VIEW or EDIT to indicate whether the
24 resulting table is intended to be viewable or editable.
25 @param [in] groupds= (work.groups)A two-level (lib.ds) reference to a dataset
26 containing the list of groups of which the current user is a member. The
27 column containing the group name should be called `groupname`.
28 @param [in] clsds= (work.clsds) A two-level (lib.ds) reference to the
29 configuration table containing the CLS rules to apply.
30 @param [out] outds= (WORK.CLSVIEW) A two-level (lib.ds) reference to the WORK
31 dataset to create
32 @param [out] outmeta= (WORK.CLS_RULES)The rule metadata, with the following
33 structure:
34 |CLS_VARIABLE_NM:$32.|CLS_HIDE:best.|
35 |---|---|---|---|---|
36 |`SOME_VARIABLE`|`0 `|
37 |`VAR3`|`1 `|
38
39
40 <h4> SAS Macros </h4>
41 @li mf_nobs.sas
42
43 @author 4GL Apps Ltd
44 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
45 and may not be re-distributed or re-sold without the express permission of
46 4GL Apps Ltd.
47**/
48
49%macro mpe_columnlevelsecurity(tgtlib,tgtds,inds
50 ,mode=VIEW
51 ,groupds=work.groups
52 ,clsds=work.clsview
53 ,outds=CLSVIEW
54 ,outmeta=work.cls_rules
55);
56
57%local col_list is_admin;
58/* filter for the appropriate rules */
59proc sql;
60create table &outmeta as
61 select CLS_VARIABLE_NM,
62 min(case when CLS_HIDE=1 then 1 else 0 end) as CLS_HIDE
63 from &clsds
64 where &dc_dttmtfmt. lt tx_to
65 and CLS_SCOPE in ("&mode",'ALL')
66 and CLS_ACTIVE=1
67 %if &mode=VIEW %then %do;
68 and CLS_HIDE ne 1
69 %end;
70 and upcase(CLS_GROUP) in (select upcase(groupname) from &groupds)
71 and CLS_LIBREF="%upcase(&tgtlib)"
72 and CLS_TABLE="%upcase(&tgtds)"
73 group by CLS_VARIABLE_NM;
74
75%let is_admin=0;
76proc sql;
77select count(*) into: is_admin from &groupds where groupname="&MPEADMINS";
78%put &sysmacroname: &=is_admin;
79%if %mf_nobs(work.cls_rules) = 0 or &is_admin>0 %then %do;
80 %put &sysmacroname: no CLS rules to apply;
81 %put &=is_admin;
82 /* copy using append for speed */
83 data &outds;
84 set &inds;
85 stop;
86 run;
87 proc append base=&outds data=&inds;
88 run;
89 /* ensure CLS_RULES is empty in case of admin */
90 data &outmeta;
91 set &outmeta;
92 stop;
93 run;
94 %return;
95%end;
96%else %if &mode=VIEW %then %do;
97 /* just send back the relevant columns */
98 %let col_list=0;
99 proc sql noprint;
100 select CLS_VARIABLE_NM into: col_list separated by ' ' from &outmeta
101 where CLS_HIDE=0;
102
103 %if &col_list=0 %then %do;
104 /*
105 We have columns that are set to CLS_HIDE=1 but we do not have any to
106 explicitly show. Therefore we assume all columns are to be shown except
107 those that are explicitly hidden.
108 */
109 proc sql noprint;
110 select CLS_VARIABLE_NM into: col_list separated by ' ' from &outmeta
111 where CLS_HIDE=1;
112
113 data &outds;
114 set &inds;
115 drop &col_list;
116 run;
117 %end;
118 %else %do;
119 data &outds;
120 set &inds;
121 keep &col_list;
122 run;
123 %end;
124%end;
125%else %if &mode=EDIT %then %do;
126 /*
127 In this case we pass all columns and the frontend will filter out the
128 ones that are not allowed to be edited.
129 */
130 data &outds;
131 set &inds;
132 stop;
133 run;
134 proc append base=&outds data=&inds;
135 run;
136%end;
137%else %do;
138 %put &sysmacroname: invalid mode - &mode!;
139 %abort;
140%end;
141
142%mend mpe_columnlevelsecurity;