Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_getusergroups.sas
Go to the documentation of this file.
1 /**
2  @file mv_getusergroups.sas
3  @brief Creates a dataset with a list of groups for a particular user
4  @details If using outside of Viya SPRE, then an access token is needed.
5 
6  Compile the macros here:
7 
8  filename mc url
9  "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
10  %inc mc;
11 
12  Then run the macro!
13 
14  %mv_getusergroups(&sysuserid,outds=users)
15 
16  @param access_token_var= The global macro variable to contain the access token
17  @param grant_type= valid values are "password" or "authorization_code" (unquoted).
18  The default is authorization_code.
19  @param outds= The library.dataset to be created that contains the list of groups
20 
21 
22  @version VIYA V.03.04
23  @author Allan Bowe
24  @source https://github.com/sasjs/core
25 
26  <h4> Dependencies </h4>
27  @li mp_abort.sas
28  @li mf_getplatform.sas
29  @li mf_getuniquefileref.sas
30  @li mf_getuniquelibref.sas
31 
32 **/
33 
34 %macro mv_getusergroups(user
35  ,outds=work.mv_getusergroups
36  ,access_token_var=ACCESS_TOKEN
37  ,grant_type=sas_services
38  );
39 %local oauth_bearer;
40 %if &grant_type=detect %then %do;
41  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
42  %else %let grant_type=sas_services;
43 %end;
44 %if &grant_type=sas_services %then %do;
45  %let oauth_bearer=oauth_bearer=sas_services;
46  %let &access_token_var=;
47 %end;
48 %put &sysmacroname: grant_type=&grant_type;
49 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
50  and &grant_type ne sas_services
51  )
52  ,mac=&sysmacroname
53  ,msg=%str(Invalid value for grant_type: &grant_type)
54 )
55 options noquotelenmax;
56 
57 %local base_uri; /* location of rest apis */
58 %let base_uri=%mf_getplatform(VIYARESTAPI);
59 
60 /* fetching folder details for provided path */
61 %local fname1;
62 %let fname1=%mf_getuniquefileref();
63 %let libref1=%mf_getuniquelibref();
64 
65 proc http method='GET' out=&fname1 &oauth_bearer
66  url="&base_uri/identities/users/&user/memberships?limit=10000";
67  headers
68 %if &grant_type=authorization_code %then %do;
69  "Authorization"="Bearer &&&access_token_var"
70 %end;
71  "Accept"="application/json";
72 run;
73 /*data _null_;infile &fname1;input;putlog _infile_;run;*/
74 %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
75  %put NOTE: User &user not found!!;
76 %end;
77 %else %do;
78  %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
79  ,mac=&sysmacroname
80  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
81  )
82 %end;
83 libname &libref1 JSON fileref=&fname1;
84 
85 data &outds;
86  set &libref1..items;
87 run;
88 
89 /* clear refs */
90 filename &fname1 clear;
91 libname &libref1 clear;
92 
93 %mend;