Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 [in] user The username for which to return the list of groups
17 @param [in] access_token_var= (ACCESS_TOKEN)
18 The global macro variable to contain the access token
19 @param [in] grant_type= (sas_services)
20 Valid values are "password" or "authorization_code" (unquoted).
21 @param [out] outds= (work.mv_getusergroups)
22 The library.dataset to be created that contains the list of groups
23
24
25 @version VIYA V.03.04
26 @author Allan Bowe, source: https://github.com/sasjs/core
27
28 <h4> SAS Macros </h4>
29 @li mp_abort.sas
30 @li mf_getplatform.sas
31 @li mf_getuniquefileref.sas
32 @li mf_getuniquelibref.sas
33
34**/
35
36%macro mv_getusergroups(user
37 ,outds=work.mv_getusergroups
38 ,access_token_var=ACCESS_TOKEN
39 ,grant_type=sas_services
40 );
41%local oauth_bearer;
42%if &grant_type=detect %then %do;
43 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
44 %else %let grant_type=sas_services;
45%end;
46%if &grant_type=sas_services %then %do;
47 %let oauth_bearer=oauth_bearer=sas_services;
48 %let &access_token_var=;
49%end;
50%put &sysmacroname: grant_type=&grant_type;
51%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
52 and &grant_type ne sas_services
53 )
54 ,mac=&sysmacroname
55 ,msg=%str(Invalid value for grant_type: &grant_type)
56)
57options noquotelenmax;
58
59%local base_uri; /* location of rest apis */
60%let base_uri=%mf_getplatform(VIYARESTAPI);
61
62/* fetching folder details for provided path */
63%local fname1;
64%let fname1=%mf_getuniquefileref();
65%let libref1=%mf_getuniquelibref();
66
67proc http method='GET' out=&fname1 &oauth_bearer
68 url="&base_uri/identities/users/&user/memberships?limit=10000";
69 headers
70%if &grant_type=authorization_code %then %do;
71 "Authorization"="Bearer &&&access_token_var"
72%end;
73 "Accept"="application/json";
74run;
75/*data _null_;infile &fname1;input;putlog _infile_;run;*/
76%if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
77 %put NOTE: User &user not found!!;
78%end;
79%else %do;
80 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
81 ,mac=&sysmacroname
82 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
83 )
84%end;
85libname &libref1 JSON fileref=&fname1;
86
87data &outds;
88 set &libref1..items;
89run;
90
91/* clear refs */
92filename &fname1 clear;
93libname &libref1 clear;
94
95%mend mv_getusergroups;