Loading...
Searching...
No Matches
mpe_filtermaster.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief dynamic where clause creation
4 @details Generates a where clause based on the following inputs:
5
6 @li The filter_RK (if provided)
7 @li The mode (if EDIT then filter for current records)
8 @li The user permissions (Row Level Security)
9
10 This macro should be used whenever surfacing data to the user. Note that
11 it is not possible to %include filerefs directly in a proc sql where clause.
12 The workaround is to use a data step view.
13
14 Note - DCLIB should be assigned.
15
16 @param [in] mode The mode. If EDIT, then current rows are returned. Valid
17 Values:
18 @li EDIT
19 @li VIEW
20 @li DLOAD - used in getrawdata
21 @li ULOAD - used by stagedata.sas to prevent restricted rows being submitted
22
23 @param [in] libds The target libref.dataset to which the filter will apply.
24 @param [in] filter_rk= (-1) The filter_rk, if available
25 @param [in] dclib= The libref of the DC control tables
26 @param [out] outref= The output fileref to create (containing the filter)
27 @param [out] outds= (work.query) The query dataset (if filter_rk supplied)
28
29 <h4> SAS Macros </h4>
30 @li mf_fmtdttm.sas
31 @li mf_getuser.sas
32 @li mf_getuniquefileref.sas
33 @li mf_getuniquename.sas
34 @li mp_abort.sas
35 @li mp_filtergenerate.sas
36 @li mpe_getgroups.sas
37
38 @version 9.3
39 @author 4GL Apps Ltd
40 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
41 and may not be re-distributed or re-sold without the express permission of
42 4GL Apps Ltd.
43**/
44
45%macro mpe_filtermaster(mode,libds,
46 dclib=,
47 filter_rk=-1,
48 outref=0,
49 outds=work.query
50);
51
52%put &sysmacroname entry vars:;
53%put _local_;
54
55%let mode=%upcase(&mode);
56%let libds=%upcase(&libds);
57
58
59%mp_abort(iftrue= (
60 &mode ne EDIT and &mode ne VIEW and &mode ne DLOAD and &mode ne ULOAD
61 )
62 ,mac=&sysmacroname
63 ,msg=%str(Invalid MODE: &mode)
64)
65%mp_abort(iftrue= (&outref = 0)
66 ,mac=&sysmacroname
67 ,msg=%str(Please provide a fileref!)
68)
69%mp_abort(iftrue= (&syscc ne 0)
70 ,mac=&sysmacroname
71 ,msg=%str(syscc=&syscc)
72)
73
74filename &outref temp;
75
76/* ensure outputs exist */
77data _null_;
78 file &outref;
79 put ' ';
80run;
81data &outds;
82 set &dclib..mpe_filtersource;
83 stop;
84run;
85
86/**
87 * Deal with FILTER_RK first
88 */
89%if &filter_rk gt 0 %then %do;
90
91 data _null_;
92 file &outref;
93 put '( '@@;
94 set &dclib..mpe_filteranytable(where=(filter_rk=&filter_rk));
95 call symputx('filter_hash',filter_hash,'l');
96 run;
97
98 proc sort data=&dclib..mpe_filtersource(where=(filter_hash="&filter_hash"))
99 out=&outds(drop=filter_hash filter_line processed_dttm);
100 by filter_line;
101 run;
102 %mp_filtergenerate(&outds,outref=&outref)
103
104%end;
105
106/* Now filter for current records if the MODE is EDIT or DLOAD */
107%local varfrom varto;
108%let varfrom=0;
109
110proc sql;
111select coalescec(var_txfrom,'0'), var_txto into: varfrom,:varto
112 from &dclib..MPE_TABLES
113 where &dc_dttmtfmt. lt tx_to
114 and libref="%scan(&libds,1,.)" and dsn="%scan(&libds,2,.)";
115
116%put &=varfrom;
117%put &=varto;
118
119/**
120 * Check if the date variables were mentioned in the query
121 * This is a trigger for serving a historical view instead of current
122 * we skip this part when checking an ULOAD as there are no date vars
123 */
124%if &varfrom ne 0 and (&mode=EDIT or &mode=DLOAD) %then %do;
125 %local validityvars;
126 proc sql;
127 select count(*) into: validityvars
128 from &outds
129 where variable_nm in ("&varfrom","&varto");
130 %if &validityvars=0 %then %do;
131 data _null_;
132 file &outref mod;
133 length filter_text $32767;
134 varfrom=symget('varfrom');
135 varto=symget('varto');
136 filter_text=catx(' ',
137 '("%sysfunc(datetime(),',"%mf_fmtdttm()",')"dt <',varto,')'
138 );
139 if &filter_rk > 0 then put 'AND ' filter_text;
140 else put filter_text;
141 run;
142 %end;
143%end;
144
145/**
146 * Now do Row Level Security based on the MPE_ROW_LEVEL_SECURITY table
147 */
148
149/* first determine users group membership */
150%mpe_getgroups(user=%mf_getuser(),outds=work.groups)
151%local admin_check;
152proc sql;
153select count(*) into: admin_check
154 from work.groups
155 where groupname="&mpeadmins";
156
157%put &sysmacroname: &=admin_check &=mpeadmins;
158%if &admin_check=0 %then %do;
159 %local scopeval;
160 %if &mode=DLOAD %then %let scopeval=VIEW;
161 %if &mode=ULOAD %then %let scopeval=EDIT;
162 %else %let scopeval=&mode;
163 /* extract relevant rows */
164 %local rlsds;
165 %let rlsds=%mf_getuniquename();
166 proc sql;
167 create table work.&rlsds as
168 select rls_group,
169 rls_group_logic as group_logic,
170 rls_subgroup_logic as subgroup_logic,
171 rls_subgroup_id as subgroup_id,
172 rls_variable_nm as variable_nm,
173 rls_operator_nm as operator_nm,
174 rls_raw_value as raw_value
175 from &mpelib..mpe_row_level_security
176 where &dc_dttmtfmt. lt tx_to
177 and rls_scope in ("&scopeval",'ALL')
178 and upcase(rls_group) in (select upcase(groupname) from work.groups)
179 and rls_libref="%scan(&libds,1,.)"
180 and rls_table="%scan(&libds,2,.)"
181 and rls_active=1
182 order by rls_group,rls_subgroup_id;
183 %if &sqlobs>0 %then %do;
184 /* check if we currently have filter or not */
185 data ;
186 infile &outref end=eof;
187 input;
188 if _n_=1 and eof and cats(_infile_)='' then newfilter=1;
189 output;
190 stop;
191 run;
192 data _null_;
193 set &syslast;
194 file &outref mod;
195 if newfilter=1 then put '(';
196 else put 'AND (';
197 run;
198
199 /* loop through and apply filters for each group membership */
200 %local fref ds;
201 %let fref=%mf_getuniquefileref();
202 %let ds=%mf_getuniquename();
203
204 proc sql noprint;
205 select distinct rls_group into : group1 -
206 from work.&rlsds;
207
208 %do i=1 %to &sqlobs;
209 data work.&ds;
210 set work.&rlsds;
211 where rls_group="&&group&i";
212 drop rls_group;
213 run;
214 %mp_filtergenerate(&ds,outref=&fref)
215 data _null_;
216 infile &fref;
217 file &outref mod;
218 input;
219 if &i>1 and _n_=1 then put ' OR ';
220 put _infile_;
221 run;
222 %end;
223 data _null_;
224 file &outref mod;
225 put ')';
226 run;
227 %end; /* &sqlobs>0 */
228 %else %do;
229 %put &sysmacroname: no matching groups;
230 data _null_;
231 set work.groups;
232 putlog (_all_)(=);
233 run;
234 %end;
235 %mp_abort(iftrue= (&syscc>0)
236 ,mac=&sysmacroname
237 ,msg=%str(Row Level Security Generation Error)
238 )
239%end; /* &admin_check=0 */
240
241%put leaving &sysmacroname with the following query:;
242
243%local empty;
244%let empty=0;
245data _null_;
246 infile &outref end=eof;
247 input;
248 putlog _infile_;
249 if _n_=1 and eof and cats(_infile_)='' then do;
250 put '1=1';
251 call symputx('empty',1,'l');
252 end;
253run;
254%if &empty=1 %then %do;
255 data _null_;
256 file &outref;
257 put '1=1';
258 run;
259%end;
260
261%mend mpe_filtermaster;