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. Format:
23 |ordinal_root|ordinal_items|version|id|implicit|name|providerId|
24 |---|---|---|---|---|---|---|
25 |1|1|1|viyagroup18|0|SAS Viyagroup18|ldap|
26
27 A user the server does not know returns the columns above with zero rows.
28
29
30 @version VIYA V.03.04
31 @author Allan Bowe, source: https://github.com/sasjs/core
32
33 <h4> SAS Macros </h4>
34 @li mp_abort.sas
35 @li mf_getplatform.sas
36 @li mf_getuniquefileref.sas
37 @li mf_getuniquelibref.sas
38
39**/
40
41%macro mv_getusergroups(user
42 ,outds=work.mv_getusergroups
43 ,access_token_var=ACCESS_TOKEN
44 ,grant_type=sas_services
45 );
46%local oauth_bearer;
47%if &grant_type=detect %then %do;
48 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
49 %else %let grant_type=sas_services;
50%end;
51%if &grant_type=sas_services %then %do;
52 %let oauth_bearer=oauth_bearer=sas_services;
53 %let &access_token_var=;
54%end;
55%put &sysmacroname: grant_type=&grant_type;
56%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
57 and &grant_type ne sas_services
58 )
59 ,mac=&sysmacroname
60 ,msg=%str(Invalid value for grant_type: &grant_type)
61)
62options noquotelenmax;
63
64%local base_uri; /* location of rest apis */
65%let base_uri=%mf_getplatform(VIYARESTAPI);
66
67/* fetching folder details for provided path */
68%local fname1;
69%let fname1=%mf_getuniquefileref();
70%let libref1=%mf_getuniquelibref();
71
72proc http method='GET' out=&fname1 &oauth_bearer
73 url="&base_uri/identities/users/&user/memberships?limit=10000";
74 headers
75%if &grant_type=authorization_code %then %do;
76 "Authorization"="Bearer &&&access_token_var"
77%end;
78 "Accept"="application/json";
79run;
80/*data _null_;infile &fname1;input;putlog _infile_;run;*/
81/* a 404 answers with a document that carries no items dataset, so the output
82 is written here instead, with the documented columns and zero rows, and the
83 JSON libref is never assigned */
84%if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
85 %put NOTE: User &user not found!!;
86 data &outds;
87 length ordinal_root 8 ordinal_items 8 version 8
88 id $256 implicit 8 name $256 providerId $32;
89 call missing(of _all_);
90 stop;
91 run;
92 filename &fname1 clear;
93 %return;
94%end;
95%else %do;
96 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
97 ,mac=&sysmacroname
98 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
99 )
100%end;
101libname &libref1 JSON fileref=&fname1;
102
103/* the documented columns are declared on both paths, so a caller reads the
104 same columns whether the user has memberships or is unknown. The lengths
105 are at least the engine's own, which are sized to the payload */
106data &outds;
107 length ordinal_root 8 ordinal_items 8 version 8
108 id $256 implicit 8 name $256 providerId $32;
109 set &libref1..items;
110run;
111
112/* clear refs */
113filename &fname1 clear;
114libname &libref1 clear;
115
116%mend mv_getusergroups;