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, source: https://github.com/sasjs/core
24
25 <h4> SAS Macros </h4>
26 @li mp_abort.sas
27 @li mf_getplatform.sas
28 @li mf_getuniquefileref.sas
29 @li mf_getuniquelibref.sas
30
31**/
32
33%macro mv_getusergroups(user
34 ,outds=work.mv_getusergroups
35 ,access_token_var=ACCESS_TOKEN
36 ,grant_type=sas_services
37 );
38%local oauth_bearer;
39%if &grant_type=detect %then %do;
40 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
41 %else %let grant_type=sas_services;
42%end;
43%if &grant_type=sas_services %then %do;
44 %let oauth_bearer=oauth_bearer=sas_services;
45 %let &access_token_var=;
46%end;
47%put &sysmacroname: grant_type=&grant_type;
48%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
49 and &grant_type ne sas_services
50 )
51 ,mac=&sysmacroname
52 ,msg=%str(Invalid value for grant_type: &grant_type)
53)
54options noquotelenmax;
55
56%local base_uri; /* location of rest apis */
57%let base_uri=%mf_getplatform(VIYARESTAPI);
58
59/* fetching folder details for provided path */
60%local fname1;
61%let fname1=%mf_getuniquefileref();
62%let libref1=%mf_getuniquelibref();
63
64proc http method='GET' out=&fname1 &oauth_bearer
65 url="&base_uri/identities/users/&user/memberships?limit=10000";
66 headers
67%if &grant_type=authorization_code %then %do;
68 "Authorization"="Bearer &&&access_token_var"
69%end;
70 "Accept"="application/json";
71run;
72/*data _null_;infile &fname1;input;putlog _infile_;run;*/
73%if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
74 %put NOTE: User &user not found!!;
75%end;
76%else %do;
77 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
78 ,mac=&sysmacroname
79 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
80 )
81%end;
82libname &libref1 JSON fileref=&fname1;
83
84data &outds;
85 set &libref1..items;
86run;
87
88/* clear refs */
89filename &fname1 clear;
90libname &libref1 clear;
91
92%mend mv_getusergroups;